scontainers
Version:
A container/collection/iterator library for JavaScript, comfortable to use, performant and versatile.
102 lines (73 loc) • 3.03 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 straits = require('straits');
const utilTraits = require('./utils.js');
const {
assert
} = require('../utils_light.js');
_testTraitSet(straits.utils);
const _asMethod = _getSymbol("asMethod", straits.utils);
const toStr = straits.common.toString[_asMethod](); // metadata traits for Scontainers
const descriptorTraits = straits.utils.TraitSet.fromKeys({
InnerCollection: Object,
// the container this container wraps
innerCollectionKey: '',
// the key of the inner scontainers
argKeys: [''],
// the names of the data-fields of this container
mappingOnly: false,
// the decorator is just a map-like function
standardIteration: false,
// iterating every element in order from `0` to `len()-1`
transformStream: false // the decorator can transform the inner collection as a stream
});
_testTraitSet(utilTraits);
_testTraitSet(descriptorTraits);
const _implTraits = _getSymbol("implTraits", straits.utils, utilTraits, descriptorTraits);
const _addTraits = _getSymbol("addTraits", straits.utils, utilTraits, descriptorTraits);
const _mappingOnly = _getSymbol("mappingOnly", straits.utils, utilTraits, descriptorTraits);
const _InnerCollection = _getSymbol("InnerCollection", straits.utils, utilTraits, descriptorTraits);
const _describeScontainer = _getSymbol("describeScontainer", straits.utils, utilTraits, descriptorTraits);
_implSymbol(Object.prototype, _describeScontainer, function (props) {
var _context;
assert(props.argKeys, `${this}'s descriptor is missing argKeys`);
assert(!!props.innerCollectionKey === !!props.InnerCollection, `${this}'s descriptor needs to either have both innerCollectionKey(=${(_context = props.innerCollectionKey, toStr).call(_context, {
verbose: 0
})}) and InnerCollection(=${(_context = props.InnerCollection, toStr).call(_context, {
verbose: 0
})}) or none.`); // setting properties
descriptorTraits[_implTraits](this, props); // setting default property values
descriptorTraits[_addTraits](this, {
mappingOnly: false,
standardIteration: this[_mappingOnly] || !this[_InnerCollection],
transformStream: this[_mappingOnly]
});
});
module.exports = descriptorTraits;