UNPKG

@rimbu/deep

Version:

Tools to use handle plain JS objects as immutable objects

152 lines 4.07 kB
import { Arr } from '@rimbu/base'; export var Tuple; (function (Tuple) { /** * Convenience method to type Tuple types * @param values - the values of the tuple * @example * ```ts * const t = Tuple.of(1, 'a', true) * // type of t => Tuple<[number, string, boolean]> * ``` */ function of(...values) { return values; } Tuple.of = of; /** * Returns the item at the given `index` in the givn `tuple`. * @param tuple - the tuple to get the item from * @param index - the index in of the tuple element * @example * ```ts * const t = Tuple.of(1, 'a', true) * console.log(Tuple.getIndex(t, 1)) * // => 'a' * ``` */ function getIndex(tuple, index) { return tuple[index]; } Tuple.getIndex = getIndex; /** * Returns the first element of a Tuple. * @param tuple - the source tuple * @example * ```ts * const t = Tuple.of(1, 'a', true) * console.log(Tuple.first(t)) * // => 1 * ``` */ function first(tuple) { return tuple[0]; } Tuple.first = first; /** * Returns the second element of a Tuple. * @param tuple - the source tuple * @example * ```ts * const t = Tuple.of(1, 'a', true) * console.log(Tuple.second(t)) * // => 'a' * ``` */ function second(tuple) { return tuple[1]; } Tuple.second = second; /** * Returns the last element of a Tuple. * @param tuple - the source tuple * @example * ```ts * const t = Tuple.of(1, 'a', true) * console.log(Tuple.last(t)) * // => true * ``` */ function last(tuple) { return tuple[tuple.length - 1]; } Tuple.last = last; /** * Returns a copy of the given `tuple` where the element at given `index` is updated with the * given `updater`. * @param tuple - the source tuple * @param index - the index in the tuple * @param updater - the updater for the value * @example * ```ts * const t = Tuple.of(1, 'a', true) * console.log(Tuple.updateAt(t, 1, 'b')) * // => [1, 'b', true] * ``` */ function updateAt(tuple, index, updater) { return Arr.update(tuple, index, updater); } Tuple.updateAt = updateAt; /** * Returns the given `tuple` with the given `values` appended. * @param tuple - the source tuple * @param values - the values to append * @example * ```ts * const t = Tuple.of(1, 'a') * console.log(Tuple.append(t, true, 5)) * // => [1, 'a', true, 5] * ``` */ function append(tuple, ...values) { return [...tuple, ...values]; } Tuple.append = append; /** * Returns a Tuple containing the elements of given `tuple1` followed by the elements * of given `tuple2`. * @param tuple1 - the first Tuple * @param tuple2 - the second Tuple * @example * ```ts * const t1 = Tuple.of(1, 'a') * const t2 = Tuple.of(true, 5) * console.log(Tuple.concat(t1, t2)) * // => [1, 'a', true, 5] * ``` */ function concat(tuple1, tuple2) { return tuple1.concat(tuple2); } Tuple.concat = concat; /** * Returns a Tuple containing all but the last element of the given `tuple`. * @param tuple - the source tuple * @example * ```ts * const t = Tuple.of(1, 'a', true) * console.log(Tuple.init(t)) * // => [1, 'a'] * ``` */ function init(tuple) { return Arr.init(tuple); } Tuple.init = init; /** * Returns a Tuple containing all but the first element of the given `tuple`. * @param tuple - the source tuple * @example * ```ts * const t = Tuple.of(1, 'a', true) * console.log(Tuple.tail(t)) * // => ['a', true] * ``` */ function tail(tuple) { return Arr.tail(tuple); } Tuple.tail = tail; })(Tuple || (Tuple = {})); //# sourceMappingURL=tuple.mjs.map