Modules

arr

Array utilities for immutable operations

fn

Function composition utilities

obj

Object utilities for immutable operations

str

String manipulation utilities

arr

Array utilities for immutable operations

arr.add(arr, item) ⇒ Array

Immutably adds an item to an array (returns a new array).

Kind: static method of arr
Returns: Array - A new array with the item added at the end

Param Type Description
arr Array The source array
item * The item to add

Example

add([1, 2, 3], 4) // => [1, 2, 3, 4]

arr.remove(arr, item) ⇒ Array

Immutably removes the first occurrence of an item from an array. If the item is not found, returns the original array.

Kind: static method of arr
Returns: Array - A new array with the item removed, or the original array if not found

Param Type Description
arr Array The source array
item * The item to remove

Example

remove([1, 2, 3, 2], 2) // => [1, 3, 2]
remove([1, 2, 3], 4) // => [1, 2, 3]

arr.toggle(arr, item) ⇒ Array

Immutably toggles an item in an array (adds if not present, removes if present).

Kind: static method of arr
Returns: Array - A new array with the item toggled

Param Type Description
arr Array The source array
item * The item to toggle

Example

toggle([1, 2, 3], 2) // => [1, 3]
toggle([1, 2, 3], 4) // => [1, 2, 3, 4]

arr.contains(a, el) ⇒ boolean

Checks if an array or comma-separated string contains an element.

Kind: static method of arr
Returns: boolean - True if the element is found, false otherwise

Param Type Description
a Array | string The array or comma-separated string to check
el * The element to look for

Example

contains([1, 2, 3], 2) // => true
contains('a,b,c', 'b') // => true
contains([1, 2, 3], 4) // => false

arr.reorder(arr, from, to) ⇒ Array

Immutably reorders an array by moving an item from one index to another.

Kind: static method of arr
Returns: Array - A new array with the item moved

Param Type Description
arr Array The source array
from number The index of the item to move
to number The index to move the item to

Example

reorder(['a', 'b', 'c', 'd', 'e'], 2, 1) // => ['a', 'c', 'b', 'd', 'e']
reorder(['a', 'b', 'c', 'd', 'e'], 3, 1) // => ['a', 'd', 'b', 'c', 'e']
reorder(['a', 'b', 'c', 'd', 'e'], 1, 3) // => ['a', 'c', 'd', 'b', 'e']
reorder(['a', 'b', 'c', 'd', 'e'], 3, 3) // => ['a', 'b', 'c', 'd', 'e'] // no change

arr.patch(arr, index, v) ⇒ Array

Immutably patches an array at a given index.

Kind: static method of arr
Returns: Array - A new array with the patch applied

Param Type Description
arr Array The source array
index number The index to patch
v Object | function The patch value to apply

Example

// Patch with object (merges with existing object)
patch([{a: 1}, {b: 2}], 0, {c: 3}) // => [{a: 1, c: 3}, {b: 2}]

// Patch with function updater
patch([{count: 1}, {count: 2}], 0, (item) => ({...item, count: item.count + 1}))
// => [{count: 2}, {count: 2}]

// Patch at index beyond array length (extends array with undefined)
patch([1, 2], 5, 3) // => [1, 2, undefined, undefined, undefined, 3]

fn

Function composition utilities

fn.pipe(a, ...fns) ⇒ function

Creates a left-to-right function composition pipeline. The first function can accept multiple arguments; the remaining functions must be unary.

Kind: static method of fn
Returns: function - A function that passes the result through each function from left to right

Param Type Description
a function The first function in the pipeline (can accept multiple arguments)
...fns function The remaining functions to pipe through (each takes one argument)

Example

const addOne = x => x + 1;
const double = x => x * 2;
const addOneThenDouble = pipe(addOne, double);
addOneThenDouble(3) // => 8 (3 + 1 = 4, 4 * 2 = 8)

fn.compose(...fns) ⇒ function

Creates a right-to-left function composition. The rightmost function can accept multiple arguments; the remaining functions must be unary.

Kind: static method of fn
Returns: function - A function that passes the result through each function from right to left

Param Type Description
...fns function Functions to compose (executed right to left)

Example

const addOne = x => x + 1;
const double = x => x * 2;
const doubleThenAddOne = compose(addOne, double);
doubleThenAddOne(3) // => 7 (3 * 2 = 6, 6 + 1 = 7)

obj

Object utilities for immutable operations

obj.keyValue(k, v) ⇒ Object

Creates an object with a single key-value pair.

Kind: static method of obj
Returns: Object - An object with the single key-value pair

Param Type Description
k string The key
v * The value

Example

keyValue('name', 'John') // => { name: 'John' }

obj.isLiteral(o) ⇒ boolean

Checks if a value is a plain object literal (not an array, null, or other object type).

Kind: static method of obj
Returns: boolean - True if the value is a plain object literal

Param Type Description
o * The value to check

Example

isLiteral({}) // => true
isLiteral([]) // => false
isLiteral(null) // => false

obj.clone(o) ⇒ Object

Creates a shallow clone of an object, preserving its prototype.

Kind: static method of obj
Returns: Object - A shallow clone of the object

Param Type Description
o Object The object to clone

Example

const obj = { a: 1, b: 2 };
const cloned = clone(obj);
cloned.a = 3;
console.log(obj.a) // => 1 (original unchanged)

obj.sub(o, p) ⇒ *

Gets a nested value from an object using a path.

Kind: static method of obj
Returns: * - The value at the path, or undefined if not found

Param Type Description
o Object The source object
p string | Array.<string> The path (string key or array of keys for nested access)

Example

sub({ a: { b: { c: 1 } } }, ['a', 'b', 'c']) // => 1
sub({ a: 1 }, 'a') // => 1
sub({ a: 1 }, 'b') // => undefined

obj.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.

Kind: static method of obj
Returns: Object - A new object with the value updated at the path

Param 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

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)

obj.reduce(o, reduceFn, [initial]) ⇒ *

Reduces an object to a single value by iterating over its keys. Similar to Array.reduce, but for objects.

Kind: static method of obj
Returns: * - The final accumulated value

Param Type Description
o Object The object to reduce
reduceFn function The reducer function (accumulator, key, value, index, originalObject) => newAccumulator
[initial] * The initial accumulator value (defaults to first property value)

Example

reduce({ a: 1, b: 2, c: 3 }, (sum, key, val) => sum + val, 0) // => 6

obj.map(o, mapFn) ⇒ Object

Immutably maps over an object's values.

Kind: static method of obj
Returns: Object - A new object with mapped values

Param Type Description
o Object The source object
mapFn function The mapping function (key, value, index, originalObject) => newValue

Example

map({ a: 1, b: 2 }, (k, v) => v * 2) // => { a: 2, b: 4 }

obj.filter(o, filterFn) ⇒ Object

Immutably filters an object's properties based on a predicate.

Kind: static method of obj
Returns: Object - A new object with only properties that pass the predicate

Param Type Description
o Object The source object
filterFn function The filter predicate (key, value, index, originalObject) => boolean

Example

filter({ a: 1, b: 2, c: 3 }, (k, v) => v > 1) // => { b: 2, c: 3 }

obj.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.

Kind: static method of obj
Returns: Object - A new object tree with transformed leaf values

Param Type Description
tree Object The object tree to traverse
fn function The function to apply to leaf values (key, value, index, parent) => newValue

Example

traverse({ a: { b: 1 }, c: 2 }, (k, v) => v * 2) // => { a: { b: 2 }, c: 4 }

obj.chainCall(o, chain) ⇒ *

Chains multiple method calls on an object.

Kind: static method of obj
Returns: * - The result of the final method call

Param Type Description
o Object The object to call methods on
chain Array.<Array> Array of [methodName, ...args] tuples

Example

chainCall([1, 2, 3], [['map', x => x * 2], ['filter', x => x > 2]]) // => [4, 6]

obj._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.

Kind: static method of obj
Returns: * - The matched case value, or false if no match

Param 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)

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

str

String manipulation utilities

str.capitalize(chunk) ⇒ string

Capitalizes the first character of a string.

Kind: static method of str
Returns: string - The capitalized string

Param Type Description
chunk string The string to capitalize

Example

capitalize('hello') // => 'Hello'

str.toCamelCase(str, [glue]) ⇒ string

Converts a string to camelCase from a delimited format.

Kind: static method of str
Returns: string - The camelCase string

Param Type Default Description
str string The string to convert
[glue] string "'_'" The delimiter to split on

Example

toCamelCase('hello_world') // => 'helloWorld'
toCamelCase('hello-world', '-') // => 'helloWorld'

str.fromCamelCase(str, [glue]) ⇒ string

Converts a camelCase string to a delimited format.

Kind: static method of str
Returns: string - The delimited string in lowercase

Param Type Default Description
str string The camelCase string to convert
[glue] string "'_'" The delimiter to use

Example

fromCamelCase('helloWorld') // => 'hello_world'
fromCamelCase('helloWorld', '-') // => 'hello-world'

str.singularToPlural(str) ⇒ string

Converts a singular word to its plural form (basic English rules).

Kind: static method of str
Returns: string - The plural form

Param Type Description
str string The singular word

Example

singularToPlural('category') // => 'categories'
singularToPlural('user') // => 'users'

str.pluralToSingular(str) ⇒ string

Converts a plural word to its singular form (basic English rules).

Kind: static method of str
Returns: string - The singular form

Param Type Description
str string The plural word

Example

pluralToSingular('categories') // => 'category'
pluralToSingular('users') // => 'user'

str.toDocumentId(str, [glue], [suffix], [prefix]) ⇒ string

Converts a string to a document ID format (typically used for database foreign keys).

Kind: static method of str
Returns: string - The document ID string

Param Type Default Description
str string The string to convert
[glue] string "'_'" The delimiter in the input string
[suffix] string "'Id'" The suffix to append
[prefix] string "''" The prefix to prepend

Example

toDocumentId('user_roles') // => 'userRoleId'
toDocumentId('categories', '_', 'ID', 'ref') // => 'refCategoryID'