PHI

PHI

Source:

Methods

(static) assocPathWith(fnon-null, kopt, non-null, objnon-null) → (non-null) {Object}

Source:
Since:
See:

Calls function on an object and assigns the result to a key. Delegates to assocPath.

Example
const assocPathWithKeys = PHI.assocPathWith(keys)
assocPathWithKeys(['z', 'ks'], { a: 1, b: 2 }) //=> { a: 1, b: 2, { z: { ks: ['a', 'b'] } } }
Parameters:
Name Type Attributes Description
f function

The function to class on obj to get the value

k String <optional>

The path to assign the value

obj Object

The object to pass to assocPath

Returns:

The object with the new association

Type
Object

(static) assocWith(fnon-null, knon-null, objnon-null) → (non-null) {Object}

Source:
Since:
See:

Calls function on an object and assigns the result to a key. Delegates to assoc.

Example
const assocWithKeys = PHI.assocWith(keys)
assocWithKeys('ks', { a: 1, b: 2 }) //=> { a: 1, b: 2, ks: ['a', b] }
Parameters:
Name Type Description
f function

The function to class on obj to get the value

k String

The key to assign the value

obj Object

The object to pass to assoc

Returns:

The object with the new association

Type
Object

(static) defaultWhenEquals(checkVal, defaultVal, val) → {*}

Source:
Since:
See:

Returns the second argument if the third argument is equal (in R.equals terms) to the first argument, otherwise returns the third argument.

Example
const defaultWhenEqualsZero = PHI.defaultWhenEquals('zero', 0)
defaultWhenEqualsZero('zero') //=> 0
defaultWhenEqualsZero(0) //=> 0
defaultWhenEqualsZero('one') //=> 'one'
defaultWhenEqualsZero(1) //=> 1
Parameters:
Name Type Description
checkVal *

The value to check equality against

defaultVal *

The value to return if the other arguments are equal

val *

The value to return if it is not equal to checkVal

Returns:

Either val or defaultVal

Type
*

(static) entries(arrnon-null) → (non-null) {Array}

Source:
Since:
See:

Shortcut for Object.entries.

Returns an array whose elements are arrays corresponding to the enumerable property [key, value] pairs found directly upon an object.

Example
PHI.entries({foo: 'bar', baz: 42})
//=> [['foo', 'bar'], ['baz', 42]]
Parameters:
Name Type Description
arr Object

The object for which to return entries

Returns:

The array of entries

Type
Array

(static) findMax(arrnon-null) → (non-null) {number}

Source:
Since:
See:

Finds the maximum value in a list.

Returns -Infinity for empty lists.

Example
PHI.findMax([5, 4, -7, -2, 10, 5]) //=> 10
Parameters:
Name Type Description
arr Array.<number>

The list to search

Returns:

The maximum value

Type
number

(static) findMin(arrnon-null) → (non-null) {number}

Source:
Since:
See:

Finds the minimum value in a list.

Returns Infinity for empty lists.

Example
PHI.findMin([5, 4, -7, -2, 10, 5]) //=> -7
Parameters:
Name Type Description
arr Array.<number>

The list to search

Returns:

The minimum value

Type
number

(static) fromJson(jsonnon-null) → (non-null) {Object}

Source:
Since:
See:

Parses a JSON string to an object.

Example
PHI.fromJson('{"a":2}')
//=> { a: 2 }
Parameters:
Name Type Description
json String

String to parse as JSON

Returns:

The new object

Type
Object

(static) fromNdjson(ndjsonnon-null) → (non-null) {Array}

Source:
Since:
See:

Parses an NDJSON string to an array of objects.

Example
PHI.fromNdjson('{"a":2}\n{"b":3}')
//=> [{ a: 2 }, { b: "3" }]
Parameters:
Name Type Description
ndjson String

String to parse as NDJSON

Returns:

The new array of objects

Type
Array

(static) identityObj(keysnon-null) → (non-null) {Object}

Source:
Since:

Returns an object containing the provided keys with each key equal to its value.

Example
PHI.identityObj(['a', 'b', 4])
//=> { a: 'a', b: 'b', 4: 4 }
Parameters:
Name Type Description
keys Array

Keys to include in object

Returns:

The new object

Type
Object

(static) mapPath(pathnon-null, fnnon-null, objnon-null) → (non-null) {Object}

Source:
Since:
See:

Maps over the value at the end of a path.

Example
PHI.mapPath(['a', 'b', 'c'], R.inc, {a: {b: {c: 3}}}) //=> {a: {b: {c: 4}}}
PHI.mapPath(['a', 'z'], R.always(10), {a: {b: {c: 3}}}) //=> {a: {z: 10, b: {c: 3}}}
Parameters:
Name Type Description
path Array.<(string|number)>

The path to map

fn function

The function that transforms the value

obj Object

The object with the path to map

Returns:

The object with the mapped path

Type
Object

(static) mapRecursive(fnnon-null, objnon-null) → (non-null) {Object}

Source:
Since:
See:
  • R.map

Like map, but acts recursively on nested objects.

Example
PHI.mapRecursive(R.toUpper, {a: 'b', c: {d: 'e'}}) //=> {a: 'B', c: {d: 'E'}}
Parameters:
Name Type Description
fn function

Function to map key to value

obj Object

The object to map

Returns:

The mapped object

Type
Object

(static) nOf(val, nnon-null) → (non-null) {Array}

Source:
Since:
See:

Returns an array of length n filled with the value provided.

Example
const nOfFoo = PHI.nOf('foo')
nOfFoo(5)
//=> ['foo', 'foo', 'foo', 'foo', 'foo']
Parameters:
Name Type Description
val *

Value to assign to each element

n number

The length of the array

Returns:

The new array

Type
Array

(static) objFromKeys(fnnon-null, keysnon-null) → (non-null) {Object}

Source:
Since:
See:

Returns an object created from a list of keys with values derived from them.

Example
PHI.objFromKeys(R.toUpper, ['a', 'b', 'c'])
//=> {a: 'A', b: 'B', c: 'C'}
Parameters:
Name Type Description
fn function

Function to map key to value

keys Array

Keys to include in object

Returns:

The new object

Type
Object

(static) objFromListWith(fnnon-null, listnon-null) → (non-null) {Object}

Source:
Since:
See:

Returns an object created from a list with keys derived from each element.

Example
PHI.objFromListWith(
  R.prop('id'),
  [{ id: 'foo', name: 'John' }, { id: 'bar', name: 'Jane' }]
)
//=> { foo: { id: 'foo', name: 'John' }, bar: { id: 'bar', name: 'Jane' } }
Parameters:
Name Type Description
fn function

Function to map element to key

list Array

List of elements to include in object

Returns:

The new object

Type
Object

(static) objOfKeys(val, keysnon-null) → (non-null) {Object}

Source:
Since:
See:

Returns an object containing the provided keys with all keys assigned the same provided value.

Example
const objOfFoo = PHI.objOfKeys('foo')
objOfFoo(['a', 'b'])
//=> {a: 'foo', b: 'foo'}
Parameters:
Name Type Description
val *

Value to assign to each property

keys Array

Keys to include in object

Returns:

The new object

Type
Object

(static) sleeP(ms) → (non-null) {Promise}

Source:
Since:

Returns a promise which resolves after the specified number of milliseconds. This should not be relied on for ms accurate timing: the promise may resolve later then requested, but should not resolve before.

Example
PHI.sleeP(100) //=> Promise(null)
Parameters:
Name Type Description
ms Number

Number of milliseconds to sleep (default 0)

Returns:

Promise which resolves to null after specified delay

Type
Promise

(static) sortByPath(pathnon-null, arrnon-null) → (non-null) {Array}

Source:
Since:
See:

Sorts the list by the supplied property path.

Example
const sortByABC = PHI.sortByPath(['a', 'b', 'c'])
sortByABC([{a: {b: {c: 'joe'}}}, {a: {b: {c: 'alice'}}}])
//=> [{a: {b: {c: 'alice'}}}, {a: {b: {c: 'joe'}}}]
Parameters:
Name Type Description
path Array.<(string|number)>

The property path to sort by

arr Array

The list to sort

Returns:

The sorted list

Type
Array

(static) sortByProp(propnon-null, arrnon-null) → (non-null) {Array}

Source:
Since:
See:

Sorts the list by the supplied property.

Example
const sortByFirstItem = PHI.sortByProp(0)
const pairs = [[-1, 1], [-2, 2], [-3, 3]]
sortByFirstItem(pairs) //=> [[-3, 3], [-2, 2], [-1, 1]]

const sortByName = PHI.sortByProp('name')
const people = [{name: 'joe'}, {name: 'alice'}]
sortByName(people) //=> [{name: 'alice'}, {name: 'joe'}]
Parameters:
Name Type Description
prop string | number

The property to sort by

arr Array

The list to sort

Returns:

The sorted list

Type
Array

(static) toJson(jsonnon-null) → (non-null) {Array}

Source:
Since:
See:

Serializes an object to a JSON string.

Example
PHI.toJson({ a: 2 })
//=> '{"a":2}'
Parameters:
Name Type Description
json String

Array to serialize to json

Returns:

The new json string

Type
Array

(static) toNdjson(ndjsonnon-null) → (non-null) {Array}

Source:
Since:
See:

Serializes an array of objects to an NDJSON string.

Example
PHI.toNdjson([{ a: 2 }, { b: "3" }])
//=> '{"a":2}\n{"b":3}'
Parameters:
Name Type Description
ndjson String

Array to serialize to NDJSON

Returns:

The new ndjson string

Type
Array