Shared utility package for intlify project providing performance tools, string formatting, event emitters, and type checking utilities.
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Object creation, property checking, and deep copying utilities for safe object manipulation.
Create objects with specified prototypes or null prototype.
/**
* Create an object with specified prototype (Object.create wrapper)
* @param obj - Prototype object or null (default: null)
* @returns New object with specified prototype
*/
function create(obj?: object | null): object;
/**
* Reference to Object.assign for object property copying
*/
const assign: typeof Object.assign;Usage Examples:
import { create, assign } from "@intlify/shared";
// Create object with null prototype (no inherited properties)
const cleanObject = create();
console.log(Object.getPrototypeOf(cleanObject)); // null
// Create object with specific prototype
const prototype = { method() { return "inherited"; } };
const derived = create(prototype);
console.log(derived.method()); // "inherited"
// Object assignment
const target = { a: 1 };
const source = { b: 2, c: 3 };
const result = assign(target, source);
console.log(result); // { a: 1, b: 2, c: 3 }Check if an object has its own property (not inherited).
/**
* Check if object has own property (not inherited)
* @param obj - Object or array to check
* @param key - Property key to check for
* @returns True if object has own property with the key
*/
function hasOwn(obj: object | Array<any>, key: string): boolean;Usage Example:
import { hasOwn } from "@intlify/shared";
const obj = { name: "Alice", age: 30 };
const arr = ["a", "b", "c"];
console.log(hasOwn(obj, "name")); // true
console.log(hasOwn(obj, "toString")); // false (inherited)
console.log(hasOwn(arr, "0")); // true (array index)
console.log(hasOwn(arr, "length")); // true (own property)
console.log(hasOwn(arr, "push")); // false (inherited method)
// Safe property access
function getProperty(obj: any, key: string) {
if (hasOwn(obj, key)) {
return obj[key];
}
return undefined;
}Deep copy properties from source object to destination object.
/**
* Deep copy properties from source to destination object
* @param src - Source object to copy from
* @param des - Destination object to copy to
* @throws Error if src or des are not objects or are arrays
*/
function deepCopy(src: any, des: any): void;Usage Examples:
import { deepCopy } from "@intlify/shared";
// Basic deep copying
const source = {
name: "Alice",
details: {
age: 30,
address: {
city: "New York",
country: "USA"
}
},
hobbies: ["reading", "swimming"]
};
const destination = {};
deepCopy(source, destination);
console.log(destination);
// Deep copy complete - nested objects and arrays are copied
// Modifying source doesn't affect destination
source.details.age = 31;
source.hobbies.push("cooking");
console.log(destination.details.age); // Still 30
console.log(destination.hobbies); // Still ["reading", "swimming"]Advanced Usage:
import { deepCopy, create } from "@intlify/shared";
// Merge configuration objects
function mergeConfig(defaultConfig: any, userConfig: any) {
const result = create();
deepCopy(defaultConfig, result);
deepCopy(userConfig, result);
return result;
}
const defaultConfig = {
theme: "light",
features: {
notifications: true,
analytics: false
}
};
const userConfig = {
theme: "dark",
features: {
analytics: true
}
};
const finalConfig = mergeConfig(defaultConfig, userConfig);
console.log(finalConfig);
// {
// theme: "dark",
// features: {
// notifications: true,
// analytics: true
// }
// }Additional utilities for object type handling.
/**
* Reference to Object.prototype.toString for type checking
*/
const objectToString: () => string;
/**
* Get type string representation of a value
* @param value - Value to get type string for
* @returns Type string like '[object Object]', '[object Array]'
*/
function toTypeString(value: unknown): string;Usage Example:
import { toTypeString, objectToString } from "@intlify/shared";
console.log(toTypeString({})); // "[object Object]"
console.log(toTypeString([])); // "[object Array]"
console.log(toTypeString(new Date())); // "[object Date]"
console.log(toTypeString(/regex/)); // "[object RegExp]"
// Use objectToString directly
console.log(objectToString.call(null)); // "[object Null]"
console.log(objectToString.call(undefined)); // "[object Undefined]"The deepCopy function handles:
__proto__ propertyimport { deepCopy } from "@intlify/shared";
// ❌ These will throw errors
try {
deepCopy(null, {}); // Error: src is not an object
deepCopy({}, []); // Error: des is an array
deepCopy([], {}); // Error: src is an array
} catch (error) {
console.error("deepCopy error:", error.message);
}
// ✅ Correct usage
const src = { a: 1, b: { c: 2 } };
const dest = {};
deepCopy(src, dest); // Works correctlyThe function uses an iterative approach with a stack to handle deep nesting without recursion limits:
// Conceptual implementation
function deepCopy(src: any, des: any): void {
const stack = [{ src, des }];
while (stack.length) {
const { src, des } = stack.pop()!;
Object.keys(src).forEach(key => {
if (key === '__proto__') return; // Security: skip prototype pollution
// Handle nested objects and arrays
if (isObject(src[key])) {
if (!isObject(des[key])) {
des[key] = Array.isArray(src[key]) ? [] : create();
}
stack.push({ src: src[key], des: des[key] });
} else {
des[key] = src[key]; // Copy primitive values
}
});
}
}