Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

lens-o

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

lens-o

Parse object path and get, set value

  • 1.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

lens-o npm

  • Parse object path
  • Get value from parsed path
  • Set value to parsed path

Install

npm install lens-o

Usage

import { parse, get, set } from 'lens-o';

const path = parse('a.b.1.c');
// path = ['a', 'b', 1, 'c']
// path[2] = 1 - index of array
const path = parse.ignore('a.b.-1.c');
// path = ['a', 'b', 'c']
const path = parse.unsafe('a.b.-1.c');
// path = ['a', 'b', 0, 'c'] - replace nagative index to zero
const path = parse.abs('a.b.-1.c');
// path = ['a', 'b', 1, 'c'] - absolute number

const val = get('a.b.1.c', {
	a: {
		b: [
			{
				c: 'hello',
			},
			{
				c: 'world',
			},
		],
	},
});
const val = get(path, {
	a: {
		b: [
			{
				c: 'hello',
			},
			{
				c: 'world',
			},
		],
	},
});
// val = world

const val = set('a.b.1.c', 'x', {
	a: {
		b: [
			{
				c: 'hello',
			},
			{
				c: 'world',
			},
		],
	},
});
const val = set(path, 'x', {
	a: {
		b: [
			{
				c: 'hello',
			},
			{
				c: 'world',
			},
		],
	},
});
// val = {
// 	a: 1,
// 	b: [{
// 		c: 'hello'
// 	}, {
// 		c: 'x'
// 	}]
// }

License

MIT

Keywords

FAQs

Package last updated on 30 Nov 2022

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc