UNPKG

dev-utils-plus

Version:

Type-safe utility functions for JavaScript/TypeScript: string, array, object, date, validation, crypto, format, math

197 lines 4.98 kB
"use strict"; /** * Object utility functions for common operations */ Object.defineProperty(exports, "__esModule", { value: true }); exports.deepClone = deepClone; exports.deepMerge = deepMerge; exports.pick = pick; exports.omit = omit; exports.isEmpty = isEmpty; exports.get = get; exports.set = set; exports.flattenObject = flattenObject; exports.invert = invert; exports.fromEntries = fromEntries; exports.entries = entries; exports.mapValues = mapValues; /** * Deep clones an object */ function deepClone(obj) { if (obj === null || typeof obj !== 'object') { return obj; } if (obj instanceof Date) { return new Date(obj.getTime()); } if (obj instanceof Array) { return obj.map(item => deepClone(item)); } if (typeof obj === 'object') { const cloned = {}; for (const key in obj) { if (obj.hasOwnProperty(key)) { cloned[key] = deepClone(obj[key]); } } return cloned; } return obj; } /** * Merges multiple objects deeply */ function deepMerge(...objects) { if (objects.length === 0) return {}; if (objects.length === 1) return objects[0] || {}; return objects.reduce((result, obj) => { for (const key in obj) { if (obj.hasOwnProperty(key)) { if (obj[key] && typeof obj[key] === 'object' && !Array.isArray(obj[key])) { const existingValue = result[key]; if (existingValue && typeof existingValue === 'object' && !Array.isArray(existingValue)) { result[key] = deepMerge(existingValue, obj[key]); } else { result[key] = deepMerge({}, obj[key]); } } else { result[key] = obj[key]; } } } return result; }, {}); } /** * Picks specific keys from an object */ function pick(obj, keys) { const result = {}; keys.forEach(key => { if (key in obj) { result[key] = obj[key]; } }); return result; } /** * Omits specific keys from an object */ function omit(obj, keys) { const result = { ...obj }; keys.forEach(key => { delete result[key]; }); return result; } /** * Checks if an object is empty */ function isEmpty(obj) { return Object.keys(obj).length === 0; } /** * Gets nested object property value using dot notation */ function get(obj, path, defaultValue) { const keys = path.split('.'); let result = obj; for (const key of keys) { if (result && typeof result === 'object' && key in result) { result = result[key]; } else { return defaultValue; } } return result; } /** * Sets nested object property value using dot notation */ function set(obj, path, value) { const keys = path.split('.'); const result = { ...obj }; let current = result; for (let i = 0; i < keys.length - 1; i++) { const key = keys[i]; if (key && (!(key in current) || typeof current[key] !== 'object')) { current[key] = {}; } if (key) { current = current[key]; } } const lastKey = keys[keys.length - 1]; if (lastKey) { current[lastKey] = value; } return result; } /** * Flattens a nested object into a single level */ function flattenObject(obj, prefix = '', separator = '.') { const result = {}; for (const key in obj) { if (obj.hasOwnProperty(key)) { const newKey = prefix ? `${prefix}${separator}${key}` : key; if (obj[key] && typeof obj[key] === 'object' && !Array.isArray(obj[key])) { Object.assign(result, flattenObject(obj[key], newKey, separator)); } else { result[newKey] = obj[key]; } } } return result; } /** * Inverts object keys and values */ function invert(obj) { const result = {}; for (const key in obj) { if (obj.hasOwnProperty(key)) { result[String(obj[key])] = key; } } return result; } /** * Creates an object from an array of key-value pairs */ function fromEntries(entries) { const result = {}; entries.forEach(([key, value]) => { result[key] = value; }); return result; } /** * Converts an object to an array of key-value pairs */ function entries(obj) { return Object.entries(obj); } /** * Maps object values using a transformation function */ function mapValues(obj, fn) { const result = {}; for (const key in obj) { if (obj.hasOwnProperty(key)) { const value = obj[key]; if (value !== undefined) { result[key] = fn(value, key); } } } return result; } //# sourceMappingURL=index.js.map