object-property-editor
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

Property Editor

Description

This JavaScript library tries to simplify the fact of reading/writing to a nested object property using a path contained in a string. It's created with the purpose of been used on polymorphic interfaces or web-components with dynamic mappings.

Features

  • Handles the initialization on write, the path doesn't need to exist.
  • It's compatible with array paths.
  • It's a light weight package, implementation is short and there are no external dependencies.
  • Includes Typescript definitions (paths are not type checked).
  • It's fast.

Under the hood the library process the path once and creates a function chain for reading and another for writing. This way operations are way faster than if they have to reprocess the path.

The implementation way has also other advantages like avoiding problems with references, the editor will always point to the correct object and will recreate its path on write, there is no problem if you modify it externally without using the PropertyEditor instance.

Examples

Reading from an object property in a nested array:

const model = { test: { nested: [{ property: 16 }, { property: 1 }, { property: 1992 }] } };
const propertyEditor = getPropertyEditor<number>(model, "test.nested[1].property");
propertyEditor.read() === model.test.nested[1].property; // This evaluates to true

Writing to an object property in a nested array:

const model = {} as any;
const value = 1;
const propertyEditor = getPropertyEditor<number>(model, "test.nested[1].property");
propertyEditor.write(value);
model.test.nested[1].property === value; // This will also be true

Package Sidebar

Install

npm i object-property-editor

Weekly Downloads

8

Version

1.0.0

License

MIT

Unpacked Size

7.79 kB

Total Files

4

Last publish

Collaborators

  • givimad