scontainers
Version:
A container/collection/iterator library for JavaScript, comfortable to use, performant and versatile.
137 lines (103 loc) • 3.02 kB
JavaScript
;
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,
toStr,
KVN
} = require('../utils.js');
_testTraitSet(traits.utils);
_testTraitSet(traits.scontainers);
_testTraitSet(traits.semantics);
const _describeScontainer = _getSymbol("describeScontainer", traits.utils, traits.scontainers, traits.semantics);
const _implCoreTraits = _getSymbol("implCoreTraits", traits.utils, traits.scontainers, traits.semantics);
const _hasKey = _getSymbol("hasKey", traits.utils, traits.scontainers, traits.semantics);
const _iterator = _getSymbol("iterator", traits.utils, traits.scontainers, traits.semantics);
module.exports = function (ParentCollection) {
assert(ParentCollection === Object, `EnumerableProperties is only needed by Object...`);
return function () {
class EnumerableProperties {
static get name() {
return `ObjectProperties`;
}
constructor(coll) {
this.wrapped = coll;
}
toString() {
var _context;
return `${(_context = this.wrapped, toStr).call(_context)}::properties()`;
}
}
EnumerableProperties[_describeScontainer]({
argKeys: []
});
EnumerableProperties[_implCoreTraits]({
get(key) {
return this[_hasKey](key) && this.wrapped[key];
},
set(key, value) {
this.wrapped[key] = value;
},
hasKey(key) {
return this.wrapped.propertyIsEnumerable(key);
},
*iterator() {
for (let key in this.wrapped) {
yield [key, this.wrapped[key]];
}
},
kvIterator() {
return {
it: this[_iterator](),
next() {
const next = this.it.next();
if (!next.done) {
return new KVN(next.value[0], next.value[1]);
}
}
};
},
forEach(fn) {
for (let key in this.wrapped) {
fn(this.wrapped[key], key);
}
},
whileEach(fn) {
for (let key in this.wrapped) {
const value = this.wrapped[key];
if (!fn(value, key, this)) {
return [key, value];
}
}
}
});
return EnumerableProperties;
};
};