Namespace RapidContext.Data

Data

Provides functions for data conversion, filtering, mapping, etc.

Field Summary
(static)
Creates a new array from a collection of elements.
(static)
Converts a value to a boolean.
(static)
Creates a deep copy of an Array or an Object.
(static)
Compares two values to determine relative order.
(static)
Filters a collection based on a predicate function.
(static)
Concatenates nested Array elements into a parent Array.
(static)
get
Retrieves one or more values from a data structure.
(static)
map
Applies a function to each item or property in an input collection and returns the results.
(static)
Creates a new object from a collection of properties.
(static)
Returns a sorted copy of a collection.
(static)
Returns the unique values of a collection.

Members

(static) array

Creates a new array from a collection of elements. This is similar to Array.from(), but also supports iterating over object properties (using Object.values()). When more than a single argument is provided, this function is similar to Array.of().

Parameters:
Name Type Description
coll {Object|Iterable|*} the elements to include
Returns:
{Array} a new Array with all the collection elements
Example:
array(null) //==> []
array({ a: 1, b: 2, c: 3 }) //==> [1, 2, 3]
array(1, 2, 3) //==> [1, 2, 3]
Source:
rapidcontext/data.mjs, line 42

(static) bool

Converts a value to a boolean. This is identical to !!val, but also converts "null", "0", "false", "off" and similar values to false.

Parameters:
Name Type Description
val {*} the value to convert
Returns:
{boolean} true or false depending on the value
Example:
bool(undefined) //==> false
bool('') //==> false
bool('FaLsE') //==> false
Source:
rapidcontext/data.mjs, line 24

(static) clone

Creates a deep copy of an Array or an Object. Nested values will be copied recursively. Only plain objects are be copied, others (including primitive values) are returned as-is.

Parameters:
Name Type Description
value {Array|Object|*} the object or array to copy
Returns:
{Array|Object|*} a new Array or Object with the same content
Source:
rapidcontext/data.mjs, line 134

(static) compare

Compares two values to determine relative order. The return value is a number whose sign indicates the relative order: negative if a is less than b, positive if a is greater than b, or zero if they are equal. If the first argument is a function, it will be used to extract the values to compare from the other arguments. Returns a bound function if only a single argument is specified.

Parameters:
Name Type Attributes Description
valueOf {function} <optional>
a function to extract the value
a {*} the first value
b {*} <optional>
the second value
Returns:
{number|function} the test result, or a bound function
Examples:
compare(1, 1) //==> 0
compare(13, 42) //==> -1
compare('b', 'a') //==> +1
compare((s) => s.toLowerCase(), 'Abc', 'aBC') //==> 0
Source:
rapidcontext/data.mjs, line 350

(static) filter

Filters a collection based on a predicate function. The input collection can be either an Array-like object, or a plain object. The predicate function fn(val, idx/key, arr/obj) is called for each array item or object property. As an alternative to a function, a string query path can be specified instead. Returns a new Array or Object depending on the input collection. Returns a bound function if only a single argument is specified.

Parameters:
Name Type Attributes Description
fn {function|string} the predicate function or query path
coll {object|Array} <optional>
the collection to filter
Returns:
{object|Array|function} the new collection, or a bound function
Example:
filter(Boolean, [null, undefined, true, 0, '']) //==> [true]
filter(Boolean, { a: null, b: true, c: 0, d: 1 }) //==> { b: true, d: 1 }
filter('id', [null, { id: 3 }, {}, { id: false }]) //==> [3]
Source:
rapidcontext/data.mjs, line 194

(static) flatten

Concatenates nested Array elements into a parent Array. As an alternative, the array elements may be specified as arguments. Only the first level of Array elements are flattened by this method. In modern environments, consider using Array.prototype.flat() instead.

Parameters:
Name Type Description
arr {Array|*} the input array or sequence of elements
Returns:
{Array} the new flattened Array
Source:
rapidcontext/data.mjs, line 248

(static) get

Retrieves one or more values from a data structure. The key provides a dot-separated query path to traverse the data structure to any depth. Wildcard property names are specified as *. Wildcard array elements are specified as []. The path may also be provided as an Array if needed. Returns a bound function if only a single argument is specified.

Parameters:
Name Type Attributes Description
key {string|Array} the value query path
val {object|Array} <optional>
the data structure to traverse
Returns:
{number|function} the value found, or a bound function
Example:
get('a.b', { a: { b: 42 } }) //==> 42
get('*.b', { a: { b: 42 }, c: { b: 13 } }) //==> [42, 13]
get('a.*', { a: { b: 42 }, c: { b: 13 } }) //==> [42]
get('[].b', [ { a: 42 }, { b: 13 }, { b: 1} }) //==> [13, 1]
get('1.b', [ { a: 42 }, { b: 13 }, { b: 1} }) //==> 13
Source:
rapidcontext/data.mjs, line 152

(static) map

Applies a function to each item or property in an input collection and returns the results. The input collection can be either an Array-like object, or a plain object. The mapping function fn(val, idx/key, arr/obj) is called for each array item or object property. As an alternative to a function, a string query path can be specified instead. Returns a new Array or Object depending on the input collection. Returns a bound function if only a single argument is specified.

Parameters:
Name Type Attributes Description
fn {function|string} the mapping function or query path
coll {object|Array} <optional>
the collection to process
Returns:
{object|Array|function} the new collection, or a bound function
Example:
map(Boolean, [null, true, 0, 1, '']) //==> [false, true, false, true, false]
map(Boolean, { a: null, b: true, c: 0 }) //==> { a: false, b: true, c: false }
map('id', [{ id: 1 }, { id: 3 }) //==> [1, 3]
Source:
rapidcontext/data.mjs, line 267

(static) object

Creates a new object from a collection of properties. The properties can be specified in a number of ways, either as two separate arrays of keys and values, as a single array of key-value pairs, as a object with properties to copy, or as a lookup function (or query path). The values can be specified as an array, a lookup object, a generator function, or a constant value.

Parameters:
Name Type Attributes Description
keys {Array|Object|function|string} the object keys or key-pairs
values {Array|Object|function|*} <optional>
the values or generator function
Returns:
{Object} a new Object with the specified properties
Example:
object(['a', 'b'], true) //==> { a: true, b: true }
object(['a', 'b'], [1, 2]) //==> { a: 1, b: 2 }
object(['a', 'b'], { a: 1, b: 2, c: 3, d: 4 }) //==> { a: 1, b: 2 }
object({ a: 1, b: 2 }, true) //==> { a: true, b: true }
object('id', [{ id: 'a', val: 1 }, ...]) //==> { a: { id: 'a', val: 1 }, ... }
Source:
rapidcontext/data.mjs, line 70

(static) sort

Returns a sorted copy of a collection. An optional function may be provided to extract the value to compare with the other elements. If no function is specified, the comparison is made on the elements themselves. Returns a bound function if only a single function argument is specified.

Parameters:
Name Type Attributes Description
fn {function} <optional>
the value extract function
coll {Array|Object|Iterable} the input collection
Returns:
{Array|function} the new sorted Array, or a bound function
Examples:
sort([3, 2, 1]) //==> [1, 2, 3]
sort(get('pos'), [{ pos: 3 }, { pos: 1 }]) //==> [{ pos: 1 }, { pos: 3 }]
const toLower = (s) => s.toLowerCase();
const sortInsensitive = sort(toLower);
sortInsensitive(['b', 'B', 'a', 'aa', 'A']) //==> ['a', 'A', 'aa', 'b', 'B']
Source:
rapidcontext/data.mjs, line 386

(static) uniq

Returns the unique values of a collection. An optional function may be provided to extract the key to compare with the other elements. If no function is specified, JSON.stringify is used to determine uniqueness. Returns a bound function if only a single function argument is specified.

Parameters:
Name Type Attributes Description
fn {function} <optional>
the comparison key extract function
coll {Array|Object|Iterable} the input collection
Returns:
{Array|function} the new Array, or a bound function
Example:
uniq([1, 2, 3, 3, 2, 1]) //==> [1, 2, 3]
uniq([{ a: 1, b: 2 }, { b: 2, a: 1 }]) //==> [{ a: 1, b: 2 }, { b: 2, a: 1 }]
Source:
rapidcontext/data.mjs, line 320