@l33tc0d3/reshape
Version:
Provides useful methods for reshaping collections
104 lines (101 loc) • 2.95 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __moduleCache = /* @__PURE__ */ new WeakMap;
var __toCommonJS = (from) => {
var entry = __moduleCache.get(from), desc;
if (entry)
return entry;
entry = __defProp({}, "__esModule", { value: true });
if (from && typeof from === "object" || typeof from === "function")
__getOwnPropNames(from).map((key) => !__hasOwnProp.call(entry, key) && __defProp(entry, key, {
get: () => from[key],
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
}));
__moduleCache.set(from, entry);
return entry;
};
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, {
get: all[name],
enumerable: true,
configurable: true,
set: (newValue) => all[name] = () => newValue
});
};
// src/index.ts
var exports_src = {};
__export(exports_src, {
objectValues: () => objectValues,
objectKeys: () => objectKeys,
objectFromEntries: () => objectFromEntries,
objectEntries: () => objectEntries,
arrayToRecord: () => arrayToRecord,
arrayToMap: () => arrayToMap
});
module.exports = __toCommonJS(exports_src);
// src/array.ts
function arrayToMap(array, keyProp, valueProps) {
const getKey = typeof keyProp === "function" ? keyProp : (item) => item[keyProp];
let getValue;
if (valueProps === undefined) {
getValue = (item) => item;
} else if (typeof valueProps === "function") {
getValue = valueProps;
} else if (Array.isArray(valueProps)) {
const props = valueProps;
getValue = (item) => {
const obj = {};
for (const prop of props) {
obj[prop] = item[prop];
}
return obj;
};
} else {
getValue = (item) => item[valueProps];
}
const map = new Map;
for (const item of array) {
map.set(getKey(item), getValue(item));
}
return map;
}
function arrayToRecord(array, keyProp, valueProps) {
const getKey = typeof keyProp === "function" ? keyProp : (item) => item[keyProp];
let getValue;
if (valueProps === undefined) {
getValue = (item) => item;
} else if (typeof valueProps === "function") {
getValue = valueProps;
} else if (Array.isArray(valueProps)) {
const props = valueProps;
getValue = (item) => {
const obj = {};
for (const prop of props) {
obj[prop] = item[prop];
}
return obj;
};
} else {
getValue = (item) => item[valueProps];
}
return array.reduce((acc, item) => {
acc[getKey(item)] = getValue(item);
return acc;
}, {});
}
// src/object.ts
function objectKeys(obj) {
return Object.keys(obj);
}
function objectValues(obj) {
return Object.values(obj);
}
function objectEntries(obj) {
return Object.entries(obj);
}
function objectFromEntries(entries) {
return Object.fromEntries(entries);
}