UNPKG

sequency

Version:

Functional sequences for processing iterable data in JavaScript

55 lines 2.44 kB
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Merge = void 0; var Sequence_1 = require("./Sequence"); var Merge = /** @class */ (function () { function Merge() { } /** * Merges the elements of both sequences into a new sequence. Each element of this sequence is eventually replaced with * an element of the other sequence by comparing results of the given `selector` function. If no value is found in the other * sequence the element is retained. New elements of the other sequence are appended to the end of the new sequence or * prepended to the start of the new sequence, if `prependNewValues` is set to `true`. This operation is not lazy evaluated. * * @param {Sequence<T>} other * @param {(value: T) => S} selector * @param prependNewValues * @returns {Sequence<T>} */ Merge.prototype.merge = function (other, selector, prependNewValues) { if (prependNewValues === void 0) { prependNewValues = false; } var mergeValues = (0, Sequence_1.isSequence)(other) ? other.toArray() : (0, Sequence_1.asSequence)(other).toArray(); var leftValues = this.toArray(); var result = leftValues.map(function (left) { var selected = selector(left); var right = (0, Sequence_1.asSequence)(mergeValues) .find(function (it) { return selector(it) === selected; }); if (right != null) { mergeValues = mergeValues.filter(function (it) { return it !== right; }); return right; } else { return left; } }); if (prependNewValues) { return (0, Sequence_1.asSequence)(__spreadArray(__spreadArray([], mergeValues, true), result, true)); } else { return (0, Sequence_1.asSequence)(__spreadArray(__spreadArray([], result, true), mergeValues, true)); } }; return Merge; }()); exports.Merge = Merge; //# sourceMappingURL=merge.js.map