@utilityjs/use-immutable-array
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

useImmutableArray

A React hook that creates an array with immutable operations.

license npm latest package npm downloads types

npm i @utilityjs/use-immutable-array | yarn add @utilityjs/use-immutable-array

Usage

const array = useImmutableArray([1, 2, 3, 4, 5]);

array.push(6) // [1, 2, 3, 4, 5, 6]
array.pop() // [1, 2, 3, 4, 5]
array.shift() // [2, 3, 4, 5]
array.shift(10) // [10, 2, 3, 4, 5]
array.reverse() // [5, 4, 3, 2, 10]
array.removeByIndex(1) // [5, 3, 2, 10]
array.removeByValue(10) // [5, 3, 2]
array.filter(item => item % 2 !== 0) // [5, 3]
array.insertItem(1, 7) // [5, 7, 3]
array.moveItem(0, 1) // [7, 5, 3]
array.values // [7, 5, 3]
array.setValues([1, 2, 3, 4]) // [1, 2, 3, 4]

API

useImmutableArray(array)

interface Return<T> {
  pop: () => void;
  push: (value: T) => void;
  shift: () => void;
  unshift: (value: T) => void;
  reverse: () => void;
  removeByIndex: (index: number) => void;
  removeByValue: (value: T) => void;
  filter: (
    predicate: (value: T, index: number, array: T[]) => value is T,
    thisArg?: any
  ) => void;
  insertItem: (index: number, value: T) => void;
  moveItem: (fromIndex: number, toIndex: number) => void;
  values: T[];
  setValues: SetArrayValues<T>;
}

declare const useImmutableArray: <T>(array: T[]) => Return<T>;

array

The initial value of the immutable-array.

Package Sidebar

Install

npm i @utilityjs/use-immutable-array

Weekly Downloads

0

Version

1.0.1

License

MIT

Unpacked Size

11.9 kB

Total Files

8

Last publish

Collaborators

  • mimshins