UNPKG

@threlte/core

Version:

A 3D framework for the web, built on top of Svelte and Three.js

34 lines (33 loc) 810 B
/** * ### `resolvePropertyPath` * * This function resolves a property path to a target object and the property * key. The property path can include dots to indicate nested objects. * * ```ts * const object = { position: { x: 0 } } * const { target, key } = resolvePropertyPath(object, 'position.x') * * // target is { x: 0 } * // key is 'x' * ``` */ export const resolvePropertyPath = (target, propertyPath) => { if (propertyPath.includes('.')) { const path = propertyPath.split('.'); const key = path.pop(); for (let i = 0; i < path.length; i += 1) { target = target[path[i]]; } return { target, key }; } else { return { target, key: propertyPath }; } };