Data processing
Data processing functions helping you write concise programs in batonjs. These are bundled with batonjs.
Most of the functions introduced on this page manipulate collections (objects or arrays), such as adding or removing elements.
As mentioned in baton core, batonjs uses ===
to determine if the page state has changed.
This means that when you update the properties of an object or add/remove elements from an array, you need to make sure that you have a new collection on hand.
The functions presented on this page meet that requirement and are therefore suitable for use withState.
Note that the functions introduced here treat arrays as lists and objects as "maps keyed by strings".
A list is a sequence of elements packed from beginning to end.
typeOf
Examines the type of the value.
This is almost the same as typeof
operator. But the difference is that it distinguishes nulls and arrays from objects.
typeOf(null) // === "null"
typeOf([]) // === "array"
typeOf({}) // === "object"
typeOf(0) // === "number"
typeOf("") // === "string"
typeOf(false) // === "boolean"
typeOf(undefined) // === "undefined"
typeof(() => 0) // === "function"
Parameters
name | type | default | description |
---|---|---|---|
value | any | mandatory | Value whose type is to be examined |
Return Value
name | type | description |
---|---|---|
type | string | String representing the type |
insert
Create a collection (array or object) with an element added.
If the collection is an object, the element is not overwritten.
insert(0, "a", [0, 1, 2]) // --> ["a", 0, 1, 2]
insert(3, "a", [0, 1, 2]) // --> [0, 1, 2, "a"]
insert("a", "a", {x: 1, y: 2}) // --> {x: 1, y: 2, a: "a"}
insert("x", "x", {x: 1, y: 2}) // --> error
Parameters
name | type | default | description |
---|---|---|---|
index | number|string | mandatory | The position at which the element is to be added. If the collection of the third argument is an array, an integer that will be the index of the array; if it is an object, a string that will be the property name. |
value | any | mandatory | An element to be added to the collection |
collection | array|object | mandatory | Collection to add an element |
Return Value
name | type | description |
---|---|---|
collection | array|object | Collection with element added |
remove
Creates a collection (array or object) with an element removed.
If an element to be deleted is not found, an error is thrown.
remove(0, [0, 1, 2]) // --> [1, 2]
remove(3, [0, 1, 2]) // --> error
remove("x", {x: 1, y: 2}) // --> {y: 2}
remove("a", {x: 1, y: 2}) // --> error
Parameters
name | type | default | description |
---|---|---|---|
index | number|string | mandatory | The position of the element to be deleted. If the collection of the second argument is an array, an integer that will be the array index; if it is an object, a string that will be the property name. |
collection | array|object | mandatory | Collection to delete an element |
Return Value
name | type | description |
---|---|---|
collection | array|object | Collection with element deleted |
update
Create a collection (array or object) with elements overwritten.
Multiple elements can be overwritten at once.
update(0, "a", [0, 1, 2]) // --> ["a", 1, 2]
update(0, "a", 2, "b", [0, 1, 2]) // --> ["a", 1, "b"]
update(3, "a", [0, 1, 2]) // --> error
update("x", "a", {x: 0, y: 1, z: 2}) // --> {x: "a", y: 1, z: 2}
update("x", "a", "z", "b", {x: 0, y: 1, z: 2}) // --> {x: "a", y: 1, z: "b"}
update("a", "a", {x: 0, y: 1, z: 2}) // --> error
Parameters
name | type | default | description |
---|---|---|---|
index | number|string | mandatory | The position at which the element is to be overwritten. If the collection of the last argument is an array, the integer that will be the array index; if it is an object, the string that will be the property name. |
value | any | mandatory | An element to be added to the collection. You can specify multiple key/value pairs. |
collection | array|object | mandatory | Collection to be overwritten |
Return Value
name | type | description |
---|---|---|
collection | array|object | Collection whose elements are overwritten |
sort
Create an array with the elements in a different order.
The sorting specification is the same as the javascript standard Array#sort. It is a stable sort.
const less = (a, b) => (a - b)
sort(less, [5, 2, 4, 1, 3]) // --> [1, 2, 3, 4, 5]
Parameters
name | type | default | description |
---|---|---|---|
compare | function | mandatory | A function that compares two elements a and b , and returns a numeric value according to the result.If a precedes b , return a number less than 0If the order of a and b is the same (no reordering), return 0If a is subsequent to b , return a number greater than 0 |
collection | array | mandatory | Array to be sorted |
Return Value
name | type | description |
---|---|---|
collection | array | Array that has been sorted |