util

(defined in /src/util/index.ts)

e.g.) (obj, 'a.b.c') -> [ obj.a.b, 'c' ]

Parameters

(None)

Returns

(Nothing)
(defined in /src/util/index.ts:13)
export function getLeafOfPath(obj: object, path: string | string[], create_object = true): [object | undefined, string | undefined] {
  const parts = Array.isArray(path) ? path.slice(0) : path.split('.');
  const last = parts.pop();
  if (parts.length > 0) {
    if (create_object !== false) {
      for (const part of parts) {
        obj = (obj as any)[part] || ((obj as any)[part] = {});
      }
    } else {
      for (const part of parts) {
        obj = (obj as any)[part];
        if (!obj) {
          return [undefined, undefined];
        }
      }
    }
  }
  return [obj, last];
}

Parameters

(None)

Returns

(Nothing)
(defined in /src/util/index.ts:38)
export function getPropertyOfPath(obj: object, path: string | string[]): any {
  const [child, last] = getLeafOfPath(obj, path, false);
  return child && last ? (child as any)[last] : undefined;
}

Parameters

(None)

Returns

(Nothing)
(defined in /src/util/index.ts:47)
export function setPropertyOfPath(obj: object, path: string | string[], value: any) {
  const [child, last] = getLeafOfPath(obj, path);
  if (child && last) {
    (child as any)[last] = value;
  }
}
Fork me on GitHub