UNPKG

scontainers

Version:

A container/collection/iterator library for JavaScript, comfortable to use, performant and versatile.

136 lines (102 loc) 3.15 kB
"use strict"; function _implSymbol(target, sym, value) { Object.defineProperty(target, sym, { value, configurable: true }); return target[sym]; } function _getSymbol(targetSymName, ...traitSets) { let symbol; traitSets.forEach(traitSet => { const sym = traitSet[targetSymName]; if (typeof sym === 'symbol') { if (!!symbol && symbol !== sym) { throw new Error(`Symbol ${targetSymName} offered by multiple trait sets.`); } symbol = sym; } }); if (!symbol) { throw new Error(`No trait set is providing symbol ${targetSymName}.`); } return symbol; } function _testTraitSet(traitSet) { if (!traitSet || typeof traitSet === 'boolean' || typeof traitSet === 'number' || typeof traitSet === 'string') { throw new Error(`${traitSet} cannot be used as a trait set.`); } } const { assert, traits, id } = require('../utils.js'); _testTraitSet(traits.utils); _testTraitSet(traits.scontainers); _testTraitSet(traits.semantics); const _nth = _getSymbol("nth", traits.utils, traits.scontainers, traits.semantics); const _len = _getSymbol("len", traits.utils, traits.scontainers, traits.semantics); const _describeScontainer = _getSymbol("describeScontainer", traits.utils, traits.scontainers, traits.semantics); const _implCoreGenerators = _getSymbol("implCoreGenerators", traits.utils, traits.scontainers, traits.semantics); const _plus = _getSymbol("plus", traits.utils, traits.scontainers, traits.semantics); const _minus = _getSymbol("minus", traits.utils, traits.scontainers, traits.semantics); const _implCoreTraits = _getSymbol("implCoreTraits", traits.utils, traits.scontainers, traits.semantics); module.exports = function (ParentCollection) { if (!ParentCollection.prototype[_nth]) { return; } return function () { class Slice { static get name() { return `${ParentCollection.name}::Slice`; } constructor(coll, begin, end = coll[_len]()) { this.wrapped = coll; this.begin = begin; this.end = end; assert(this.begin >= 0 && this.end <= coll[_len]() && this.end >= this.begin); } toString() { return `${this.wrapped}[${this.begin}:${this.end}]`; } } const parentProto = ParentCollection.prototype; Slice[_describeScontainer]({ InnerCollection: ParentCollection, innerCollectionKey: id`wrapped`, argKeys: [id`begin`, id`end`] }); Slice[_implCoreGenerators]({ stage(kvn) { return kvn; }, indexToParentIndex(index) { return index[_plus](this.args.begin); }, len() { if (parentProto[_len]) { return function () { return this.args.end[_minus](this.args.begin); }; } } }); Slice[_implCoreTraits]({ stage(kvn) { return kvn; }, indexToParentIndex(index) { return index + this.begin; }, len() { if (parentProto[_len]) { return function () { return this.end - this.begin; }; } } }); return Slice; }; };