UNPKG

@nuxtjs/i18n

Version:

Internationalization for Nuxt

34 lines (33 loc) 833 B
export function pickNested(keys, obj) { const result = {}; for (const key of keys) { const value = getNestedValue(obj, key); if (value !== void 0) { setNestedValue(result, key, value); } } return result; } export function getNestedValue(obj, key) { const parts = key.split("."); let current = obj; for (const part of parts) { if (current && typeof current === "object" && part in current) { current = current[part]; } else { return void 0; } } return current; } export function setNestedValue(obj, key, value) { const parts = key.split("."); let current = obj; for (let i = 0; i < parts.length - 1; i++) { const part = parts[i]; current[part] ||= {}; current = current[part]; } const lastPart = parts[parts.length - 1]; current[lastPart] = value; }