object.js
1 import map from 'iter-tools-es/methods/map'; 2 3 export const { hasOwn, getOwnPropertySymbols, getPrototypeOf, fromEntries } = Object; 4 5 export const isObject = (obj) => obj !== null && typeof obj === 'object'; 6 export const { isArray } = Array; 7 export const isFunction = (obj) => typeof obj === 'function'; 8 export const isSymbol = (obj) => typeof obj === 'symbol'; 9 export const isString = (obj) => typeof obj === 'string'; 10 export const isRegex = (obj) => obj instanceof RegExp; 11 export const isPattern = (obj) => isString(obj) || isRegex(obj); 12 export const isType = (obj) => isSymbol(obj) || isString(obj); 13 14 export const objectKeys = (obj) => { 15 return { 16 *[Symbol.iterator]() { 17 for (let key in obj) if (hasOwn(obj, key)) yield key; 18 yield* getOwnPropertySymbols(obj); 19 }, 20 }; 21 }; 22 23 export const objectValues = (obj) => { 24 return { 25 *[Symbol.iterator]() { 26 for (let key in obj) if (hasOwn(obj, key)) yield obj[key]; 27 yield* map((sym) => obj[sym], getOwnPropertySymbols(obj)); 28 }, 29 }; 30 }; 31 32 export const objectEntries = (obj) => { 33 return { 34 *[Symbol.iterator]() { 35 for (let key in obj) if (hasOwn(obj, key)) yield [key, obj[key]]; 36 yield* map((sym) => [sym, obj[sym]], getOwnPropertySymbols(obj)); 37 }, 38 }; 39 }; 40 41 export const mapObject = (fn, obj) => { 42 let result = {}; 43 for (let key in obj) if (hasOwn(obj, key)) result[key] = fn(obj[key]); 44 for (const sym of getOwnPropertySymbols(obj)) result[sym] = fn(obj[sym]); 45 return result; 46 }; 47 48 export const arrayLast = (arr) => arr[arr.length - 1];