Repository URL to install this package:
|
Version:
1.2.13 ▾
|
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.indexBy = exports.indexByIdentity = exports.indexByKeyVals = exports.indexByKeyVal = void 0;
var _chainAbleBoost = require("chain-able-boost");
var _ramda = require("ramda");
// @NOTE ramda's curry errors - enforces strict arity
// @NOTE this was a could be this way
// const key = Object.keys(keyVal)[0]
// const property = keyVal[key]
// {label: 'value'}
const indexByKeyVal = (0, _chainAbleBoost.curry)(3, (key, property, list) => {
const indexed = {};
list.forEach(item => {
const atKey = item[key];
indexed[atKey] = item[property];
});
return indexed;
});
exports.indexByKeyVal = indexByKeyVal;
const indexByKeyVals = (0, _chainAbleBoost.curry)(3, (key, property, list) => {
const indexed = {};
list.forEach(item => {
const atKey = item[key];
indexed[atKey] = indexed[atKey] || [];
indexed[atKey].push(item[property]);
});
return indexed;
});
exports.indexByKeyVals = indexByKeyVals;
const indexByIdentity = (0, _ramda.indexBy)((0, _ramda.prop)('identifier'));
exports.indexByIdentity = indexByIdentity;
const indexByWithArray = (key, property, list) => {
const indexed = {};
list.forEach((item, index) => {
const atKey = item[key];
indexed[atKey] = item[property];
Object.defineProperty(indexed, index, {
enumerable: false,
value: item
});
}); // add the original list as a hidden property to the data
Object.defineProperty(indexed, 'array', {
enumerable: false,
value: list
}); // array-like
Object.defineProperty(indexed, 'length', {
enumerable: false,
value: list.length
});
Object.defineProperty(indexed, 'forEach', {
enumerable: false,
value(fn) {
return Object.keys(indexed).forEach(name => fn(indexed[name], name));
}
});
Object.defineProperty(indexed, 'map', {
enumerable: false,
value(fn) {
return Object.keys(indexed).map(name => fn(indexed[name], name));
}
});
return indexed;
}; // --- export default exports
exports.indexBy = indexByWithArray;