The object namespace of Bijou.js, for stuff like flattening nested objects, cloning, merging, and even listening to changes to objects!
Methods
# static deepSet(path, value, obj) → {Object}
A function that sets a value at a given path in an object by creating nested objects along the way for any undefined keys in the path, while keeping the original object immutable.
Parameters:
| Name | Type | Description |
|---|---|---|
path |
string
|
Array.<string>
|
The path to set the value at, can be either a string or an array of strings |
value |
any
|
The value to set at the given path |
obj |
Object
|
The object to set the value in |
A new object with the updated value at the given path
Object
Example
const obj = { a: { b: { c: 1 } } };
const newObj = deepSet("a.b.d", 2, obj);
console.log(newObj);
// Output: { a: { b: { c: 1, d: 2 } } } }
# static exports.clone(src) → {Object}
Deep clones an object (or anything else, like an array or string)
Parameters:
| Name | Type | Description |
|---|---|---|
src |
Object
|
Array
|
String
|
The object to clone. |
The output cloned object.
Object
Example
let obj = { hello: { puny: "earthlings" }};
let cloned = _$.clone(obj); // cloned can be operated on without changing obj
# static exports.deepGet(key, object) → {*}
Retrieves a deeply nested value from an object given a key.
Parameters:
| Name | Type | Description |
|---|---|---|
key |
string
|
Array.<string>
|
The key (if string will split by '.') or an array of keys to access the value. |
object |
object
|
The object to retrieve the value from. |
The retrieved value or null if the key does not exist.
*
Example
_$.deepGet("hello.world", {hello: {world: "Hello World!"}}); // "Hello World!"
# static exports.flattenObj(o) → {Object}
Flattens an object recursively into one.
Parameters:
| Name | Type | Description |
|---|---|---|
o |
Object
|
The object to flatten |
The flattened object.
Object
Example
_$.flattenObj({
hello: "world",
another: {
nested: "Value",
anotherNestedValue: {
"something": "A value"
},
"more Values!!": "lol"
}
}); // { hello: "world", nested: "Value", something: "A value", more Values!!: "lol" }
# static exports.formToObject(form) → {Object}
Converts a form to an Object.
Parameters:
| Name | Type | Description |
|---|---|---|
form |
HTMLFormElement
|
The form element. |
The object of form data (The keys are the "name" attributes of the form inputs and the values are the value attributes of the form data.)
Object
Example
html:
```
<form id="form">
<input name"input" />
<input name="input2" />
</form>
```
js:
const form = document.getElementById("form");
console.log(_$.formToObject(form)); // e.g. { input: "hello", input2: "world" }
# static exports.listen(obj, getCallbackopt, setCallbackopt) → {Proxy}
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
obj |
Object
|
The object to listen to. |
||
getCallback |
listenCallback
|
<optional> |
()=>null | The callback function to run when a value is set, with the arguments, key (the key changed) and value (the new value of the key). |
setCallback |
listenCallback
|
<optional> |
()=>null | The callback function to run when a value is gotten, with the arguments, key (the key got) and value (the value of the key). |
A proxy object that behaves like any other object but listens to changes.
Proxy
Example
let obj = {something: "This is part of the object", anotherThing: "This is another!"};
obj = _$.listen(obj, (k, v) => console.log(`set ${k} to ${v}`), () => console.log("Gotten"));
obj.something; // Logs "Gotten" to the console!
obj.anotherThing = "Hello world!"; // Logs "Set abotherThing to Hello world!" to the console!
# static exports.mapObjectKeys(obj, fn) → {Object}
Maps the keys of an object.
Parameters:
| Name | Type | Description |
|---|---|---|
obj |
Object
|
The object to map. |
fn |
mapObjKeysCallback
|
The function to run (passed the current key of the object) which returns the new value from that key. |
The new Object.
Object
Example
_$.mapObjectKeys({something: "A value", anotherThing: "Another value!"}, (key) => key.toUpperCase());
//Returns {SOMETHING: "A value", ANOTHERTHING: "Another value!"}
# static exports.mapObjectValues(obj, fn) → {Object}
Maps an object's values.
Parameters:
| Name | Type | Description |
|---|---|---|
obj |
Object
|
The object to map the values of. |
fn |
mapObjValuesCallback
|
The callback function to use. |
The mapped object.
Object
Example
console.log(_$.mapObjectValues({ hello: "World", bijou: "is GREAT" }, val => val.toLowerCase())); // { hello: "world", bijou: "is great" }
# static exports.merge(obj1, obj2) → {Object}
Merges two objects into one. Note that object 2 properties will overwrite those of object 2.
Parameters:
| Name | Type | Description |
|---|---|---|
obj1 |
Object
|
The 1st object to merge |
obj2 |
Object
|
The 2nd object to merge. |
The merged object.
Object
Example
console.log(_$.merge({hello: "Hello!!"}, {world: " World", world: " Earthlings"})); // {hello: "Hello!!", world: " Earthlings"}
# static exports.sortObj(obj) → {Object}
Sorts an object alphabetically by its keys.
Parameters:
| Name | Type | Description |
|---|---|---|
obj |
Object
|
The object to sort. |
The sorted object.
Object
Example
let object = _$.sortObj({testing: "A value", anotherThing: "Another value!"});
// The object is now {anotherThing: "Another value!", testing: "A value"}