perpetual-js

1.2.1 • Public • Published

perpetual-js

A light weight version of Immutable.js. This library comes in at 8kb minified and 3kb minified + gzipped.

Because of efforts to keep bundle size small not all methods are included in Perpetual. Just the essential ones.

Installation

Install perpetual-js using npm or yarn.

npm install perpetual-js
yarn add perpetual-js

Requirements

Object.assign()
rest/spread
{ ...object }

Usage

Perpetual comes with two different immutable collections: Map and List. Map: is a collection of key value pairs. List: is a collection of values.

Example

  • Can import multiple different ways.
import { Map, List } from 'perpetual-js';
 
const map = Map({ value: 'value' });
const list = List([1, 2, 3, 4]);

fromJS()

Deeply converts plain JS objects and arrays to Immutable Maps and Lists.

fromJS(jsValueany)any

is()

Value equality check with semantics similar to Object.is, but treats Immutable Collections as values, equal if the second Collection includes equivalent values.

is(firstany, secondany)boolean

Map

*get()

Returns the value associated with the provided key, or notSetValue if the Collection does not contain this key.

get<NSV>(keyK, notSetValueNSV)V | NSV
get(keyK)V | undefined

*getIn()

Returns the value found by following a path of keys or indices through nested Collections.

getIn(searchKeyPathIterable<array>, notSetValue?: any)any

*hashCode()

The hashCode() function is an important part of how Perpetual determines if two values are equivalent and is used to determine how to store those values.

hashCode()number

*has()

True if a key exists within this Collection.

has(keyK)boolean

*hasIn()

True if the result of following a path of keys or indices through nested Collections results in a set value.

hasIn(searchKeyPathIterable<array>)boolean

*map()

Returns a new Map with values passed through a mapper function.

map<M>(mapper: (value: V, key: K, iter: this) => M, context?: any)Map<K, M>

*merge()

Returns a new Map resulting from merging the provided Collections (or JS objects) into this Map. In other words, this takes each entry of each collection and sets it on this Map.

merge<KC, VC>(...collectionsArray<Iterable<[KC, VC]>>)Map<K | KC, V | VC>
merge<C>(...collectionsArray<{[keystring]: C}>)Map<K | string, V | C>

*mergeDeep()

Like merge(), but when two Collections conflict, it merges them as well, recursing deeply through the nested data.

mergeDeep(...collectionsArray<Iterable<[K, V]> | {[keystring]: V}>)this

*mergeDeepIn()

A combination of updateIn and mergeDeep, returning a new Map, but performing the deep merge at a point arrived at by following the keyPath.

mergeDeepIn(keyPathIterable<array>, ...collectionsArray<any>)this

*mergeIn()

A combination of updateIn and merge, returning a new Map, but performing the merge at a point arrived at by following the keyPath.

mergeIn(keyPathIterable<array>, ...collectionsArray<any>)this

*set()

Returns a new Map also containing the new key, value pair. If an equivalent key already exists in this Map, it will be replaced.

set(keyK, valueV)this

*setIn()

Returns a new Map having set value at this keyPath. If any keys in keyPath do not exist, a new immutable Map will be created at that key.

setIn(keyPathIterable<array>, valueany)this

*spread()

Returns an Object with the values provided to the spread method.

spread(...keyskeyPath<array> | key<string>) : Object

*delete()

Returns a new Map which exludes this key.

delete(key: K): this

alias: remove()

*deleteIn()

Returns a new Map having removed the value at this keyPath. If any keys in keyPath do not exist, no change will occur.

deleteIn(keyPathIterable<array>)this

*reduce()

Returns a new Map with values passed through a reduce function.

reduce<R>(
reducer: (reduction: R, value: [K, V], key: number, iter: this) => R,
initialReductionR,
context?: any
)R
reduce<R>(
reducer: (reduction: T | R, value: [K, V], key: number, iter: this) => R
)R

*toJS()

Deeply converts this Keyed collection to equivalent native JavaScript Object.

toJS()Object

*update()

Returns a new Map having updated the value at this key with the return value of calling updater with the existing value.

update(keyK, notSetValueV, updater: (value: V) => V)this
update(keyK, updater: (value: V) => V)this
update<R>(updater: (value: this) => R)R

*updateIn()

Returns a new Map having applied the updater to the entry found at the keyPath.

updateIn(
keyPathIterable<array>,
notSetValueany,
updater: (value: any) => any
)this
updateIn(keyPathIterable<array>, updater: (value: any) => any)this

*withMutations()

Every time you call one of the above functions, a new immutable Map is created. If a pure function calls a number of these to produce a final return value, then a penalty on performance and memory has been paid by creating all of the intermediate immutable Maps.

withMutations(mutator: (mutable: this) => any)this

List

*clear()

Returns a new List with 0 size and no values in constant time.

clear()List<T>

*concat()

Returns a new List with other values or collections concatenated to this one.

concat<C>(...valuesOrCollectionsArray<Iterable<C> | C>)List<T | C>

alias: merge

*delete()

Returns a new List which excludes this index and with a size 1 less than this List. Values at indices above index are shifted down by 1 to fill the position.

delete(index: number): List<T>

*deleteIn()

Returns a new List having removed the value at this keyPath. If any keys in keyPath do not exist, no change will occur.

deleteIn(keyPathIterable<any>)this

*get()

Returns the value associated with the provided index, or notSetValue if the index is beyond the bounds of the Collection.

get<NSV>(indexnumber, notSetValueNSV)T | NSV
get(indexnumber)T | undefined

*getIn()

Returns the value found by following a path of keys or indices through nested Collections.

getIn(searchKeyPathIterable<any>, notSetValue?: any)any

*has()

True if a key exists within this Collection.

has(keynumber)boolean

*hashCode()

The hashCode() function is an important part of how Perpetual determines if two values are equivalent and is used to determine how to store those values.

hashCode()number

*includes()

True if a value exists within this Collection, using Immutable.is to determine equality

includes(valueT)boolean

*insert()

Returns a new List with value at index with a size 1 more than this List. Values at indices above index are shifted over by 1.

insert(indexnumber, valueT)List<T>

*map()

Returns a new List with values passed through a mapper function.

map<M>(mapper: (value: T, key: number, iter: this) => M, context?: any)List<M>

*mergeDeepIn()

A combination of updateIn and mergeDeep, returning a new Map, but performing the deep merge at a point arrived at by following the keyPath.

mergeDeepIn(keyPathIterable<array>, ...collectionsArray<any>)this

*mergeIn()

A combination of updateIn and merge, returning a new Map, but performing the merge at a point arrived at by following the keyPath.

mergeIn(keyPathIterable<array>, ...collectionsArray<any>)this

*pop()

Returns a new List with a size ones less than this List, excluding the last index in this List.

pop()List<T>

*push()

Returns a new List with the provided values appended, starting at this List's size.

push(...valuesArray<T>)List<T>

*set()

Returns a new List which includes value at index. If index already exists in this List, it will be replaced.

set(indexnumber, valueT)List<T>

*setIn()

Returns a new List having set value at this keyPath. If any keys in keyPath do not exist, a new immutable Map will be created at that key.

setIn(keyPathIterable<any>, valueany)this

*shift()

Returns a new List with a size ones less than this List, excluding the first index in this List, shifting all other values to a lower index.

shift()List<T>

*splice()

Splice returns a new indexed Collection by replacing a region of this Collection with new values. If values are not provided, it only skips the region to be removed.

splice(indexnumber, removeNumnumber, ...valuesArray<T>)this

*spread()

Returns an Array with the values provided to the spread method.

spread(...keyskeyPath<array> | key<string>) : Array

*reduce()

Reduces the Collection to a value by calling the reducer for every entry in the Collection and passing along the reduced value.

reduce<R>(
reducer: (reduction: R, value: T, key: number, iter: this) => R,
initialReductionR,
context?: any
)R
reduce<R>(
reducer: (reduction: T | R, value: T, key: number, iter: this) => R
)R

*toJS()

Deeply converts this Indexed collection to equivalent native JavaScript Array.

toJS()Array<any>

*unshift()

Returns a new List with the provided values prepended, shifting other values ahead to higher indices.

unshift(...valuesArray<T>)List<T>

*update()

Returns a new List with an updated value at index with the return value of calling updater with the existing value, or notSetValue if index was not set. If called with a single argument, updater is called with the List itself.

update(indexnumber, notSetValueT, updater: (value: T) => T)this
update(indexnumber, updater: (value: T) => T)this
update<R>(updater: (value: this) => R)R

*updateIn()

Returns a new Map having applied the updater to the entry found at the keyPath.

updateIn(
keyPathIterable<array>,
notSetValueany,
updater: (value: any) => any
)this
updateIn(keyPathIterable<array>, updater: (value: any) => any)this

*withMutations()

Every time you call one of the above functions, a new immutable List is created. If a pure function calls a number of these to produce a final return value, then a penalty on performance and memory has been paid by creating all of the intermediate immutable Lists.

withMutations(mutator: (mutable: this) => any)this

Package Sidebar

Install

npm i perpetual-js

Weekly Downloads

1

Version

1.2.1

License

ISC

Unpacked Size

350 kB

Total Files

72

Last publish

Collaborators

  • kjjenson