seamless-Immutable API(中文文檔)

seamless-Immutable

  • seamless-Immutable是一套輕量級的持久化數據結構庫 , seamless-immutable 並無實現完整的 Persistent Data Structure 而是使用 Object.defineProperty(所以只能在 IE9 及以上使用)擴展了 JavaScript 的 Array 和 Object 對象來實現,只支持 Array 和 Object 兩種數據類型,API 基於與 Array 和 Object 操持不變。代碼庫很是小,壓縮後下載只有 2K。相比 Immutable.js 壓縮後下載有 16K而言,小巧了不少, 並且API比較簡潔,易懂

seamless-Immutable API彙總

from(type)
  • type:array | object
  • 設置成 Immutable 對象
let code1 = Immutable.from([1, 2, 3]);
let code2 = Immutable([1, 2, 3])); //同 from()
console.log(code1);//Immutable([1, 2, 3])
console.log(code2);//Immutable([1, 2, 3])
複製代碼
isImmutable(type)
  • type:array | object | immutable
  • 判斷是不是 Immutable 對象
let code1=Immutable.from([1, 2, 3])
let code2= {a: 12};
console.log(Immutable.isImmutable(code1)); //true
console.log(Immutable.isImmutable(code2)); //false
複製代碼
  • 注:參數若是是 int string bool,undefined,null 返回 true
asMutable(obj,type)
  • obj: immutable
  • type:默認是{deep:false}
  • 返回數組的可變副本,對於深度可變的副本需設置 {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']]
複製代碼
merge (obj1,obj2,type)
  • obj1 immutable
  • obj2 : object| array
  • type: object 默認是{deep:false}
  • 返回合併後的 Immutable,對於深度可變的副本需設置 {deep:true}
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無效
let code2 = Immutable.merge(code1, [4, 5, 6]);
console.log(code2); //Immutable([1,2,3])
複製代碼
replace(obj1,obj2,type)
  • obj1 immutable
  • obj2 : object
  • type: object 默認是{deep:false}
  • 返回替換後的 Immutable,對於深度可變的副本需設置 {deep:true}
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); //  true
複製代碼
set(obj,key,value,type)
  • obj1 immutable
  • key : string
  • value : any
  • type: object 默認是{deep:false}
  • 設置 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'}})
複製代碼
setIn (obj,key,value,type)
  • obj1 immutable
  • key : string
  • value : any
  • type: object 默認是{deep:false}
  • 深度設置 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'})
複製代碼
getIn(ob,arr,default)
  • obj: immutable
  • key : array
  • default : 若是返回值爲空則返回默認值
  • 獲取 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
複製代碼
update(obj,key,fun,parmas)
  • obj: immutable
  • key : string
  • fun : function
  • parmas:any,回調函數的參數
  • 修改 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'})
複製代碼
updateIn(obj,key,fun,parmas)
  • obj: immutable
  • key : array
  • fun : function
  • parmas:any,回調函數的參數
  • 深度修改 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}})
複製代碼
without(obj,key)
  • obj: immutable
  • key : array | string |function
  • 刪除 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'})
複製代碼
flatMap(obj,fun)
  • obj: immutable
  • fun : function
  • 循環 immutable 對象, 返回 一個新的 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"])
複製代碼
asObject(obj,fun)
  • obj: immutable
  • fun : function
  • 迭代器函數將返回兩個元素的數組 - 第一個表示鍵,另外一個表示值。而後返回由這些鍵和值構成的不可變對象。
var array = Immutable(['aa', 'bb']);
let newobj = Immutable.asObject(array, str => {
    return [str, str.toUpperCase()];
});
console.log(newobj); //Immutable({aa: "AA", bb: "BB"})
複製代碼
相關文章
相關標籤/搜索