Socket
Socket
Sign inDemoInstall

ts-dot-prop

Package Overview
Dependencies
1
Maintainers
1
Versions
28
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    ts-dot-prop

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


Version published
Weekly downloads
14K
increased by25.31%
Maintainers
1
Install size
48.6 kB
Created
Weekly downloads
 

Changelog

Source

2.1.4 (2024-05-24)

Bug Fixes

  • remove should not produce sparse arrays (#75) (cfa7cfa)

Readme

Source

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']

Keywords

FAQs

Last updated on 24 May 2024

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc