1.0.1 • Published 3 years ago

@utilityjs/use-immutable-array v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

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.