stabilize.js
Setup
-
Node
npm i stabilize.js
-
CDN
-
This library also supports ES6 Module, AMD and UMD style.
Usage
const stabilize = ; const user = name: 'Bob' age: 17; const immutableUser = ; immutableUsername = 'Alice';console; // => 'Bob'
After stabilizing an object or array, it will become an immutable variable with several useful methods for easy manipulation as bellow.
APIs
This library provides an only function named "stabilize" that you can pass through it an object or array to get back immutable version.
- stabilize(Array | Object val): return immutable version of val
Returned result, depending on which type it's (Array or Object), will have some particular methods as described in the next parts.
For instance:
const stabilize = ; const user = ; // user now is immutableconsole; // access the properties with get() or dotconst name = user; // similar to user.nameconsole; // you can change properties' value with set() method// it will return a copy of user with new property let guest = user;console; // => Tom // the value of user.name can not be changedconsole; // => Bob // because it's immutableusername = 'Jerry';console; // => Bob
Stabilize an object
;
Return an immutable object that has 2 methods:
Because the returned object is standard object, so you can still use the built-in methods in Object.prototype as normal.
.get(key)
Return value of the specified property.
const car = ; const name = car; // or:// const name = car.name console; // => Tesla
.set(key, value)
Return an new immutable object with new property.
Setter also accepts an object to allow to define many properties at the same time:
const car = ; const tesla = car; console;
tesla now is a new object with the following properties:
name: 'Tesla' speed: 1000 color: 'black' price: 40000
Stabilize an array
;
Return an immutable array that has the following methods:
Because the returned array is standard array, so you can still use the built-in methods in Array.prototype as normal.
.unique()
Return a new array with no duplicate elements.
const a = ;const b = a; console; // => [ 1, 2, 3, 4 ]
.min()
Return the smallest value from an array of numbers.
const a = ;const b = a; console; // => 1
.max()
Return the biggest value from an array of numbers.
const a = ;const b = a; console; // => 8
.first()
Return the first element from array.
const a = ;const b = a; console; // => 1
.last()
Return the last element from array.
const a = ;const b = a; console; // => 4
.pick([count])
Extract count elements from array in randomly order.
let a = ;let b = a;console; // output an array of 3 random elements
Without count, this method returns an only random element.
.insert(at, element1, element2, ...elementN)
Return a new array with new elements inserted at the position specified by first parameter.
const a = ;const b = a; console; // => [ 1, 2, 'a', 3, 4 ]
.append(element1, element2, ...elementN)
Return a new array with new elements added at the end.
const a = ;const b = a; console; // => [ 1, 2, 3, 4, 5, 6, 7 ]
.remove(start, count)
Return a new array with count elements deleted beginning at start:
const a = ;const b = a; // remove 2 items from index 3, means 4 and 5 console; // => [ 1, 2, 3, 6 ]
.isort(compareFunction)
Return a new array sorted by compareFunction.
This method does the same thing as Array.sort, but immutable.
const users = ;const sortedUsers = users; console;
Output:
name: 'Kate' age: 17 name: 'Anne' age: 21 name: 'Bob' age: 28 name: 'Jim' age: 33
.msort([Number | String | Object opt])
Advanced version of .isort() that allows to sort an array by some flexible ways.
const points = ;console;console; console;const a1 = points; // without parameterconsole; console;const a2 = points;console; const players = ; console;players; console;const players1 = players;players1; console;const players2 = players;players2;
Results:
Array points, original:
[ 1, 5, 19, 6, 4, 11, 7, 22, 40, 3, 8 ]
Array points, lowest to highest:
[ 1, 3, 4, 5, 6, 7, 8, 11, 19, 22, 40 ]
Array points, descendant:
[ 40, 22, 19, 11, 8, 7, 6, 5, 4, 3, 1 ]
List of players as it is:
Jerome Nash | 24
Jackson Valdez | 21
Benjamin Cole | 23
Manuel Delgado | 33
Caleb McKinney | 28
Sort by age from youngest to oldest:
Jackson Valdez | 21
Benjamin Cole | 23
Jerome Nash | 24
Caleb McKinney | 28
Manuel Delgado | 33
Sort by age from oldest to youngest:
Manuel Delgado | 33
Caleb McKinney | 28
Jerome Nash | 24
Benjamin Cole | 23
Jackson Valdez | 21
.ireverse()
This method does the same thing as Array.reverse, but immutable.
For example now you can reverse the above sortedUsers array:
const reversedUsers = sortedUsers.ireverse();
console.log(reversedUsers);
Output:
name: 'Jim' age: 33 name: 'Bob' age: 28 name: 'Anne' age: 21 name: 'Kate' age: 17
.shuffle()
Return a clone of given array with shuffled elements.
const shuffledUsers = sortedUsers;console;
Test
git clone https://gitlab.com/ndaidong/stabilize-js.gitcd stabilize.jsnpm installnpm test
License
The MIT License (MIT)