object-pickby
Version:
Creates an object composed of the picked object properties. Written in Typescript for ES2022+ environments.
109 lines • 3.55 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.pick = pick;
exports.pickBy = pickBy;
/**
* Creates an object composed of the picked `object` properties.
*
* @category Object
* @param object The source object.
* @param [props] The property names to pick, specified
* individually or in arrays.
* @returns Returns the new object.
* @example
*
* const object = { 'a': 1, 'b': '2', 'c': 3 };
*
* only(object, ['a', 'c']);
* // => { 'a': 1, 'c': 3 }
*/
function isUnknownArray(value) {
return Array.isArray(value);
}
function pick(objectOrArray, props) {
if (!objectOrArray)
return objectOrArray;
if (!Array.isArray(props))
return objectOrArray;
if (isUnknownArray(objectOrArray)) {
const length = objectOrArray.length;
const result = [];
for (const index of props) {
if (typeof index !== "number" || !Number.isInteger(index)) {
throw new TypeError(`While picking from an array we expect array of integer indexes to pick, but got ${String(index)}`);
}
const normalizedIndex = index < 0 ? length + index : index;
if (normalizedIndex >= 0 && normalizedIndex < length) {
result.push(objectOrArray[normalizedIndex]);
}
}
return result;
}
if (typeof objectOrArray !== "object")
return objectOrArray;
if (props.length === 0)
return {};
if (props.length === 1) {
const key = props[0];
if (typeof key === "symbol") {
if (Object.prototype.propertyIsEnumerable.call(objectOrArray, key)) {
return { [key]: objectOrArray[key] };
}
return {};
}
const stringKey = String(key);
if (Object.prototype.propertyIsEnumerable.call(objectOrArray, stringKey)) {
return { [stringKey]: objectOrArray[stringKey] };
}
return {};
}
const stringKeys = new Set();
const symbolKeys = new Set();
for (const key of props) {
if (typeof key === "symbol")
symbolKeys.add(key);
else
stringKeys.add(String(key));
}
const result = {};
for (const key of stringKeys) {
if (Object.prototype.propertyIsEnumerable.call(objectOrArray, key)) {
result[key] = objectOrArray[key];
}
}
for (const key of symbolKeys) {
if (Object.prototype.propertyIsEnumerable.call(objectOrArray, key)) {
result[key] = objectOrArray[key];
}
}
return result;
}
function pickBy(objectOrArray, predicate) {
if (!objectOrArray || typeof predicate !== "function")
return objectOrArray;
if (isUnknownArray(objectOrArray)) {
const result = [];
for (let i = 0; i < objectOrArray.length; i++) {
const value = objectOrArray[i];
if (predicate(value, i, result))
result.push(value);
}
return result;
}
const result = {};
for (const key of Object.keys(objectOrArray)) {
const value = objectOrArray[key];
if (predicate(value, key))
result[key] = value;
}
const symbols = Object.getOwnPropertySymbols(objectOrArray);
if (symbols.length > 0) {
for (const symbol of symbols) {
const value = objectOrArray[symbol];
if (predicate(value, symbol))
result[symbol] = value;
}
}
return result;
}
//# sourceMappingURL=index.js.map