Methods
(static) _switch(value, cases) → {*}
Pattern matching utility that returns a value based on matching a value to cases. Supports nested array paths for partial matching and a 'default' case.
Parameters:
| Name | Type | Description |
|---|---|---|
value |
* | The value to match (can be a string, array path, etc.) |
cases |
Object | Object with cases to match against (supports 'default' key) |
Returns:
The matched case value, or false if no match
- Type
- *
Example
_switch('a', { a: 1, b: 2, default: 0 }) // => 1
_switch('c', { a: 1, b: 2, default: 0 }) // => 0
_switch(['a', 'b'], { a: { b: 1 } }) // => 1
(static) chainCall(o, chain) → {*}
Chains multiple method calls on an object.
Parameters:
| Name | Type | Description |
|---|---|---|
o |
Object | The object to call methods on |
chain |
Array.<Array> | Array of [methodName, ...args] tuples |
Returns:
The result of the final method call
- Type
- *
Example
chainCall([1, 2, 3], [['map', x => x * 2], ['filter', x => x > 2]]) // => [4, 6]
(static) clone(o) → {Object}
Creates a shallow clone of an object, preserving its prototype.
Parameters:
| Name | Type | Description |
|---|---|---|
o |
Object | The object to clone |
Returns:
A shallow clone of the object
- Type
- Object
Example
const obj = { a: 1, b: 2 };
const cloned = clone(obj);
cloned.a = 3;
console.log(obj.a) // => 1 (original unchanged)
(static) filter(o, filterFn) → {Object}
Immutably filters an object's properties based on a predicate.
Parameters:
| Name | Type | Description |
|---|---|---|
o |
Object | The source object |
filterFn |
function | The filter predicate (key, value, index, originalObject) => boolean |
Returns:
A new object with only properties that pass the predicate
- Type
- Object
Example
filter({ a: 1, b: 2, c: 3 }, (k, v) => v > 1) // => { b: 2, c: 3 }
(static) isLiteral(o) → {boolean}
Checks if a value is a plain object literal (not an array, null, or other object type).
Parameters:
| Name | Type | Description |
|---|---|---|
o |
* | The value to check |
Returns:
True if the value is a plain object literal
- Type
- boolean
Example
isLiteral({}) // => true
isLiteral([]) // => false
isLiteral(null) // => false
(static) keyValue(k, v) → {Object}
Creates an object with a single key-value pair.
Parameters:
| Name | Type | Description |
|---|---|---|
k |
string | The key |
v |
* | The value |
Returns:
An object with the single key-value pair
- Type
- Object
Example
keyValue('name', 'John') // => { name: 'John' }
(static) map(o, mapFn) → {Object}
Immutably maps over an object's values.
Parameters:
| Name | Type | Description |
|---|---|---|
o |
Object | The source object |
mapFn |
function | The mapping function (key, value, index, originalObject) => newValue |
Returns:
A new object with mapped values
- Type
- Object
Example
map({ a: 1, b: 2 }, (k, v) => v * 2) // => { a: 2, b: 4 }
(static) patch(o, k, v) → {Object}
Immutably updates a value in an object at a given path. For nested paths, creates intermediate objects as needed. If the value at the path is a literal object and v is also a literal object, they are merged.
Parameters:
| Name | Type | Description |
|---|---|---|
o |
Object | The source object |
k |
string | Array.<string> | The path (string key or array of keys for nested updates) |
v |
* | The value to set |
Returns:
A new object with the value updated at the path
- Type
- Object
Example
patch({ a: 1 }, 'b', 2) // => { a: 1, b: 2 }
patch({ a: { b: 1 } }, ['a', 'c'], 2) // => { a: { b: 1, c: 2 } }
patch({ a: { b: 1 } }, 'a', { c: 2 }) // => { a: { b: 1, c: 2 } } (merged)
(static) reduce(o, reduceFn, initialopt) → {*}
Reduces an object to a single value by iterating over its keys. Similar to Array.reduce, but for objects.
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
o |
Object | The object to reduce |
|
reduceFn |
function | The reducer function (accumulator, key, value, index, originalObject) => newAccumulator |
|
initial |
* |
<optional> |
The initial accumulator value (defaults to first property value) |
Returns:
The final accumulated value
- Type
- *
Example
reduce({ a: 1, b: 2, c: 3 }, (sum, key, val) => sum + val, 0) // => 6
(static) sub(o, p) → {*}
Gets a nested value from an object using a path.
Parameters:
| Name | Type | Description |
|---|---|---|
o |
Object | The source object |
p |
string | Array.<string> | The path (string key or array of keys for nested access) |
Returns:
The value at the path, or undefined if not found
- Type
- *
Example
sub({ a: { b: { c: 1 } } }, ['a', 'b', 'c']) // => 1
sub({ a: 1 }, 'a') // => 1
sub({ a: 1 }, 'b') // => undefined
(static) traverse(tree, fn) → {Object}
Recursively traverses an object tree and applies a function to leaf values. Non-literal values (primitives, arrays, etc.) are transformed by the function.
Parameters:
| Name | Type | Description |
|---|---|---|
tree |
Object | The object tree to traverse |
fn |
function | The function to apply to leaf values (key, value, index, parent) => newValue |
Returns:
A new object tree with transformed leaf values
- Type
- Object
Example
traverse({ a: { b: 1 }, c: 2 }, (k, v) => v * 2) // => { a: { b: 2 }, c: 4 }