seamless-Immutable

  • Seamless -Immutable is a lightweight library of persistent data structures, Rather than implement a full Persistent Data Structure, seamless-immutable extends JavaScript’s Array with object.defineProperty (and therefore only available in IE9 and above) And Object Object to achieve, only support Array and Object two data types, API based on Array and Object operation unchanged. The code base is very small, only 2K downloads compressed. Compared to Immutable. Js compressed download 16K, is much smaller, and the API is relatively simple, easy to understand

Seamless – Immutable API

from(type)
  • Type: array | object
  • Set to Immutable
let code1 = Immutable.from([1, 2, 3]); let code2 = Immutable([1, 2, 3])); / / with the from () to the console. The log (code1); //Immutable([1, 2, 3]) console.log(code2); //Immutable([1, 2, 3])Copy the code
isImmutable(type)
  • Type: array | object | immutable
  • Check whether it is Immutable
let code1=Immutable.from([1, 2, 3])
let code2= {a: 12};
console.log(Immutable.isImmutable(code1)); //true
console.log(Immutable.isImmutable(code2)); //false
Copy the code
  • Note: If the parameter is int string bool,undefined,null returns true
asMutable(obj,type)
  • Obj: immutable
  • Type: default is {deep:false}
  • Return a mutable copy of the array. For mutable copies {deep:true}
code1:
var array = Immutable(['hello', 'world']);
var mutableArray = Immutable.asMutable(array);
console.log(array); //Immutable(['hello', 'world'])
console.log(mutableArray); //['hello', 'world']
code2:
var code1 = Immutable(['hello', 'world', ['immutable', 'react']]);
var code2 = Immutable.asMutable(code1, {deep: true});
console.log(code1); //Immutable(['hello', 'world',['immutable', 'react']])
console.log(code2); //['hello', 'world',['immutable', 'react']]
Copy the code
merge (obj1,obj2,type)
  • obj1 immutable
  • obj2 : object| array
  • Type: object Default is {deep:false}
  • Return merged Immutable, {deep:true} for deep copies
code1: var obj = Immutable({a: 'AA', b: 'BB', c: 'CC'}); let newobj = Immutable.merge(obj, {c: 'CC', d: 'DD'}); console.log(newobj); // Immutable({a: "AA", b: "BB", c: "CC", d: "DD"}) code2: var obj = Immutable({status: 'ok', data: {a: 'AA', b: 'BB'}}); let newsobj = Immutable.merge(obj, [{status: 'error', data: {c: 'CC'}}, {data: {a: 'AAAA'}}], {deep: true}); console.log(newsobj); //Immutable({status: "error", {a: "AAAA", b: "BB", c: "CC"}}) code3: var code1 = Immutable([1, 2, 3]); //Array invalid let code2 = Immutable. Merge (code1, [4, 5, 6]); console.log(code2); / / Immutable ([1, 2, 3])Copy the code
replace(obj1,obj2,type)
  • obj1 immutable
  • obj2 : object
  • Type: object Default is {deep:false}
  • Return the replaced Immutable, {deep:true} for copies of variable depth
var obj1 = Immutable({a: {b: 'test'}, c: 'test'}); var obj2 = Immutable.replace(obj1, {a: {b: 'test'}, d: 'ok'}, {deep: true}); console.log(obj2); //Immutable({a: {b: 'test'}, d: 'ok'}) console.log(obj1 === obj2); // false console.log(obj1.a === obj2.a); // trueCopy the code
set(obj,key,value,type)
  • obj1 immutable
  • key : string
  • value : any
  • Type: object Default is {deep:false}
  • Set the value specified by immutable
var obj = Immutable({a: 'AA', b: 'BB', c: {d: 'DD'}});
let newobj = Immutable.set(obj, 'b', 'BBB');
console.log(obj); // Immutable({a: 'AA', b: 'BB', c: {d: 'DD'}})
console.log(newobj); // Immutable({a: 'AA', b: 'BBB', c: {d: 'DD'}})
Copy the code
setIn (obj,key,value,type)
  • obj1 immutable
  • key : string
  • value : any
  • Type: object Default is {deep:false}
  • Depth set to a value specified by immutable
var obj = Immutable({a: 'AA', b: 'BB', c: {d: 'DD'}}); let newobj = Immutable.setIn(obj, ['c', 'd'], 'DDDD'); console.log(obj); //Immutable({a: 'AA', b: 'BB', c: {d: 'DD'}) console.log(newobj); //Immutable({a: 'AA', b: 'BB', c: {d: 'DDDD'})Copy the code
getIn(ob,arr,default)
  • Obj: immutable
  • key : array
  • Default: If no value is returned, the default value is returned
  • Gets the value specified by immutable
var obj = Immutable({a: {b: 'BB', c: 'CC'}, d: 'DD'});
let code1 = Immutable.getIn(obj, ['a', 'b']);
let code2 = Immutable.getIn(obj, ['a', 'c'], 'EE');
console.log(code1); //BB
console.log(code2); //CC
Copy the code
update(obj,key,fun,parmas)
  • Obj: immutable
  • key : string
  • fun : function
  • Parmas: any, the argument to the callback function
  • Modify the value of immutable
code1:
let fun = x => x + x;
var obj = Immutable({a: 'AA'});
let newobj = Immutable.update(obj, 'a', fun);
console.log(obj); //Immutable({a: 'AA'})
console.log(newobj); // Immutable({a: 'AAAA'})
code 2 :
let add = (x, y) => x + ' ' + y;
var obj = Immutable({a: 'hello'});
var newobj = Immutable.update(obj, 'a', add, 'world');
console.log(obj); //Immutable({a: 'hello'})
console.log(newobj); //Immutable({a: 'hello world'})
Copy the code
updateIn(obj,key,fun,parmas)
  • Obj: immutable
  • key : array
  • fun : function
  • Parmas: any, the argument to the callback function
  • Deeply modify the value of immutable
let add = (x, y) => x + y; var obj = Immutable({a: {b: 1}}); let newobj = Immutable.updateIn(obj, ['a', 'b'], add, 10); console.log(obj); //Immutable({foo: {bar: 1}}) console.log(newobj); //Immutable({foo: {bar: 11}})Copy the code
without(obj,key)
  • Obj: immutable
  • key : array | string |function
  • Delete the value of immutable
code 1:
var obj = Immutable({a: 'AA', b: 'BB', c: 'CC'});
let obj1 = Immutable.without(obj, 'b');
console.log(obj1); // Immutable({a: 'AA', c: 'CC'})
code 2:
var obj = Immutable({a: 'AA', b: 'BB', c: 'CC'});
let obj2 = Immutable.without(obj, ['a', 'b']);
console.log(obj2); // Immutable({ c: 'CC'})
code 3:
var obj = Immutable({a: 'AA', b: 'BB', c: 'CC'});
let obj3 = Immutable.without(obj, 'a', 'b');
console.log(obj3); // Immutable({ c: 'CC'})
code 4:
var obj = Immutable({a: 'AA', b: 'BB', c: 'CC'});
let obj4 = Immutable.without(obj, (value, key) => key === 'a' || value === 'BB');
console.log(obj4); // Immutable({ c: 'CC'})
Copy the code
flatMap(obj,fun)
  • Obj: immutable
  • fun : function
  • Loop imMUTABLE to return a new IMmutable
var array = Immutable(['AA', 'BB', 'CC']);
let newarr = Immutable.flatMap(array, str => 'hello ' + str);
console.log(newarr); //Immutable(["hello AA", "hello BB", "hello CC"])
Copy the code
asObject(obj,fun)
  • Obj: immutable
  • fun : function
  • The iterator function returns an array of two elements – the first representing the key and the other the value. It then returns an immutable object made up of these keys and values.
var array = Immutable(['aa', 'bb']);
let newobj = Immutable.asObject(array, str => {
    return [str, str.toUpperCase()];
});
console.log(newobj); //Immutable({aa: "AA", bb: "BB"})
Copy the code