Skip to content

Latest commit

 

History

History
557 lines (399 loc) · 10.2 KB

DOCS.md

File metadata and controls

557 lines (399 loc) · 10.2 KB

EACH

Navigates to every item in Array or [key, value] pair for object.

select([EACH], [1, 2, 3])
// => [1, 2, 3]
transform([EACH], v => v + 1, [1, 2, 3])
// => [2, 3, 4]
select([EACH], {a: 1, b: 2})
// => [['a', 1], ['b', 2]]
transform([EACH], (v => v.slice().reverse()), {a: 1, b: 2})
// => {1: 'a', 2: 'b'}

FIRST

Navigates to first item in Array.

select([FIRST], [1, 2, 3]);
// => [1]
setval([FIRST], 0, [1, 2, 3]);
// => [0, 2, 3]

LAST

Navigates to last item in Array.

select([LAST], [1, 2, 3]);
// => [3]
setval([LAST], 0, [1, 2, 3]);
// => [1, 2, 0]

END

Navigates to the empty array after the end of a array. Useful to add multiple values to array.

setval([END], [4, 5], [1, 2, 3]);
// => [1, 2, 3, 4, 5]

BEGINNING

Navigates to the empty array before the beginning of a array. Useful to add multiple values to array.

setval([BEGINNING], [-1, 0], [1, 2, 3]);
// => [-1, 0, 1, 2, 3]

AFTER_ELEM

Navigates to the void element after the end of a array. Useful to add single value to array.

setval([AFTER_ELEM], 4, [1, 2, 3]);
// => [1, 2, 3, 4]

BEFORE_ELEM

Navigates to the void element before the beginning of a array. Useful to add single value to array.

setval([BEFORE_ELEM], 0, [1, 2, 3]);
// => [0, 1, 2, 3]

OBJECT_VALS

Navigates to each value of an Object.

select([OBJECT_VALS], { a: 1, b: 2 });
// => [1, 2]
transform([OBJECT_VALS], v => v + 2, { a: 1, b: 2 });
// => { a: 3, b: 4 }
select([OBJECT_VALS, OBJECT_VALS], { a: { b: 'c' }, d: { e: 'f' } });
// => ['c', 'f']

OBJECT_KEYS

Navigates to each key of an Object.

select([OBJECT_KEYS], { a: 1, b: 2 });
// => ['a', 'b']
transform([OBJECT_KEYS], v => v + v, { a: 1, b: 2 });
// => { aa: 1, bb: 2 }
select([OBJECT_VALS, OBJECT_KEYS], { a: { b: 'c' }, d: { e: 'f' } });
// => ['b', 'e']

filterer(pred:Function)

Navigates to Array formed from filtering other Array.

select([filterer(v => v % 2 === 0)], [1, 2, 3, 4, 5]);
// => [[2, 4]]
transform([filterer(v => v % 2 === 0)], () => [20, 40], [1, 2, 3, 4, 5]);
// => [1, 20, 3, 40, 5]

prop(key:String|Number)

Navigates to a property value in Object or index value in Array.

select([prop('a')], { a: 1, b: 2 });
// => [1]
setval([prop('a')], 0, { a: 1, b: 2 });
// => { a: 0, b: 2 }

INDEXED_VALS

Navigates to each [elem, index] pair in Array.

select([INDEXED_VALS], [1, 2, 3]);
// => [[1, 0], [2, 1], [3, 2]]
transforms([INDEXED_VALS], ([v, i]) => ([i * 2, v - 1]), [1, 2, 3])
// => [0, 2, 4]

skip(pred:Function)

Navigates to structure only if pred(structure) is false.

select([EACH, skip(v => v % 2 !== 0)], [2, 3, 4]);
// => [2, 4]

keep(pred:Function)

Navigates to structure only if pred(structure) is true.

select([EACH, keep(v => v % 2 !== 0)], [2, 3, 4]);
// => [3]

map(fn:Function)

Navigates to fn(structure).

select([map(() => true)], false);
// => [true]
select([map(Object.values)], { a: 1, b: 2 });
// => [[1, 2]]
transform([EACH, map(v => v + 1)], v => v + 1, [1, 2, 3, 4]);
// => [3, 4, 5, 6]

range(start:Number, end:Number)

Navigates to the sub-array bound by the indexes start (inclusive) and end (exclusive).

select([range(0, 2)], [1, 2, 3, 4]);
// => [[1, 2]]
transform([range(0, 2)], () => [0, 0], [1, 2, 3, 4]);
// => [0, 0, 3, 4]

rangeDynamic(startFn:Function, endFn:Function)

Navigates to the sub-array bound by the indexes created by startFn(structure) (inclusive) and endFn(structure).

select([rangeDynamic(() => 0, () => 2)], [1, 2, 3, 4]);
// => [[1, 2]]
transform([rangeDynamic(() => 0, () => 2)], () => [0, 0], [1, 2, 3, 4]);
// => [0, 0, 3, 4]

propName(pred:String|Number)

Navigates to a key in object (index in array), not the value.

select([propName('a')], { a: 1, b: 2 });
// => ['a']
setval([prop('a')], 'c', { a: 1, b: 2 });
// => { c: 1, b: 2 }

submap(keys:Array)

Navigates to a submap of the original map.

select([submap(['a', 'b'])], { a: 1, b: 2, c: 3 });
// => [{ a: 1, b: 2 }]
transform([submap(['a', 'b'])], () => ({ d: 4 }), { a: 1, b: 2, c: 3 });
// => { c: 3, d: 4 }

keypath()

Navigates to the value in specified keys path or undefined if the path doesn't exist in the structure.

select([keypath('a', 'b')], { a: { b: 1 } });
// => [1]
select([keypath('a', 'b')], {});
// => [undefined]
transform([keypathStrict('a', 'b')], v => v + 1, {});
// => { a: { b: 2 } }

keypathStrict()

Same as keypath, but stops navigation if the path doesn't exist in structure.

transform([keypathStrict('a', 'b')], v => v + 1, {});
// => {}

when(pred:Function, val:Any)

Navigates to the structure if pred(structure) is false, else navigate to the provided argument.

select([EACH, when((v => v.length == 0), [1])], [[2], [], [3], []]);
// => [[2], [1], [3], [1]]

or(val:Any)

Navigates to the value if it is not null or undefined, else navigate to the provided argument.

select([EACH, or([1])], [[2], null, [3], null]);
// => [[2], [1], [3], [1]]

beforeIndex(i:Number)

Navigates to empty sub-array before selected index and previous index. It is useful to insert one or multiple elements before selected index.

setval([beforeIndex(2)], [3], [1, 2, 4, 5]);
// => [1, 2, 3, 4, 5]

STOP

For select, stops navigation and returns empty result. For transform, returns structure unchanged.

SELF

Navigates to the structure unchanged.

transform([SELF], v => v + 1, 1);
// => 2

subselect()

Like filterer but instead of predicate accepts path. Navigates to array of selected values, but this array is view of the original structure and can be transformed.

transform(
  [subselect(OBJECT_VALS, EACH, OBJECT_VALS)],
  v => v.slice().reverse(),
  {:items [{ a: 1}, { b: 2 }, { c: 3 }]}
)
// => {:items [{ a: 3}, { b: 2 }, { c: 1 }]}

transformed(path:Array, fn:Function)

Navigates to transform(path, fn, structure).

select([transformed([OBJECT_VALS], v => v + 1)], { a: 1, b: 2, c: 3 });
// => [{ a: 2, b: 3, c: 4 }]

reduced(path:Array, fn:Function)

Navigates to a view of the current structure by transforming with a reduction over the selected values.

select([reduced([EACH], (p, n) => p + n)], [1, 2, 3, 4]);
// => [10]

multiPath()

Navigates to all the items in all the pats. For transforms, applies updates to the paths in order. It is like calling select/transform multiple times.

select([multi-path([prop('a')], [prop('b')])], {a: 0, b: 1, c: 2});
// => [0, 1]
transform(
  [multi-path([prop('a')], [prop('b')])],
  v => v - 1,
  { a: 0, b: 1, c: 2 }
);
// => { a: -1, b: 0, c: 2 }

ifPath(checkPath:Array, thenPath:Array, elsePath:Array)

Tests if selecting with checkPath on the current structure returns anything. If so, it navigates to the corresponding thenPath, if not - navigates to elsePath.

transform(
  [ifPath([prop('a')], [prop('b')], [prop('c')])],
  v => v + 1,
  { a: 0, b: 1, c: 2 }
);
// => { a: 0, b: 2, c: 2 }
transform(
  [ifPath([prop('a')], [prop('b')], [prop('c')])],
  v => v + 1,
  { b: 1, c: 2 }
);
// => { b: 1, c: 3 }

condPath(checkPath:Array, thenPath:Array)

Tests if selecting with checkPath on the current structure returns anything. If so, it navigates to the corresponding thenPath, otherwise, it tries the next checkPath. If nothing matches, then the structure is not selected.

select(
  [condPath(
    [prop('a')], [prop('b')],
    [prop('c')], [prop('d')],
    [prop('e')]
  )],
  { b: 1, d: 3, e: 4 }
);
// => 4
transform(
   [condPath(
     [prop('a')], [prop('b')],
     [prop('c')], [prop('d')]
   ), EACH],
   v => v + 1,
   { b: 1, c: 2, d: [1, 2, 3] }
 );
 // => { b: 1, c: 2, d: [2, 3, 4] }

collect()

Adds the result of running select with given path to the array of collected values. Note that collect, like select, returns an array containing its results. If transform is called, each collected value will be passed as an argument to the transforming function with the resulting value as the last argument.

select(
  [collect(FIRST), EACH],
  [1, 2, 3]
);
// => [[[1], 1], [[1], 2], [[1], 3]]

transform(

  [collect(SELF), EACH],
  ([all], v) => all.reduce((p, n) => p + n, v),
  [1, 2, 3],
);
// => [7, 8, 9]

collectOne()

Same as collect, but uses selectOne.

See: collect

COLLECT_CURRENT

Same as collectOne(SELF). Collects current value.

See: collect, collectOne

putval(v:Any)

Adds v to collected items.

See: collect