2.1.3 • Published 10 months ago

ts-dot-prop v2.1.3

Weekly downloads
1,642
License
MIT
Repository
github
Last release
10 months ago

NPM Version CI codecov

ts-dot-prop

TypeScript utility to transform nested objects using a dot notation path.

Installation

npm install ts-dot-prop

Usage

import * as dot from 'ts-dot-prop';

const obj = {
  foo: 'bar',
  state: {
    name: 'New York',
  },
  fruit: [
    {
      type: 'Apple',
      color: 'red',
      variety: [{ name: 'cox' }, { name: 'gala' }, { name: 'honeycrips' }],
    },
    {
      type: 'Mango',
      color: 'orange',
      variety: [{ name: 'alice' }, { name: 'alphonso' }],
    },
  ],
};

/**
 * Get
 */
dot.get(obj, 'state.name');
// => 'New York'

dot.get(obj, 'fruit[0].type');
// => 'Apple'

dot.get(obj, 'fruit[*].color');
// => ['red', 'orange']

dot.get(obj, 'fruit[*].variety[*].name');
// => [['cox', 'gala', 'honeycrips'], ['alice', 'alphonso']]

dot.get(obj, 'state.capital');
// => undefined

dot.get(obj, 'state.population.total', 'not found');
// => 'not found'

/**
 * Set
 */
dot.set(obj, 'state.name', 'Paris');
// => state.name === 'Paris'

dot.set(obj, 'state.capital', 'Albany');
// => state.capital === 'Albany'

dot.set(obj, 'fruit[0].color', 'Green');
// => fruit[0].color === 'Green'

dot.set(obj, 'fruit[*].color', 'Yellow');
// => fruit[0].color === 'Yellow'
// => fruit[1].color === 'Yellow'

/**
 * Has
 */
dot.has(obj, 'state.name');
// => true

dot.has(obj, 'fruit[0].type');
// => true

/**
 * Remove
 */
dot.remove(obj, 'state.name');
// => state.name === undefined

dot.remove(obj, 'fruit[0].color');
// => fruit[0].color === undefined

/**
 * Paths
 */
dot.paths(obj);
// => ['foo', 'state.name', 'fruit']
2.1.3

10 months ago

2.1.2

2 years ago

2.1.1

2 years ago

2.1.0

2 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.5.1

3 years ago

1.5.0

3 years ago

1.4.3

3 years ago

1.4.2

3 years ago

1.4.1

4 years ago

1.4.0

7 years ago

1.3.3

7 years ago

1.3.2

7 years ago

1.3.1

7 years ago

1.3.0

7 years ago

1.2.2

7 years ago

1.2.1

7 years ago

1.2.0

7 years ago

1.1.0

7 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago