Socket
Book a DemoInstallSign in
Socket

dotpath-strict

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

dotpath-strict

Get or set property value by dotpath string, throws on failure.

latest
Source
npmnpm
Version
1.0.5
Version published
Maintainers
1
Created
Source

node-dotpath-strict

NPM Package Build Status Coverage Status Dependency Status

Aren't there a million of these already?

I couldn't find one that distinguishes between a property that is unset and one that is set to undefined so I made this. It throws an error when attempting to access a nonexisting property. Supports basic dot notation and numeric brackets.

Example

var dotPath = require("dotpath-strict");
var post = { title: "Readme", author: { firstName: "Hugh", lastName: "Mungus" } };
dotPath(post, "author.firstName");
//"Hugh"

Installation

npm i dotpath-strict

API Reference

dotPath(object, path, [value]) ⇒ *

Kind: Exported function
Throws:

  • ReferenceError
  • TypeError

Params

  • object Object
  • path string
  • [value] string - Set property to this value

Example

var obj = { a: { aa: 1, ab: [ "1", "2" ] }, b: undefined, c: null };
dotPath(obj, "a.aa");
//1
dotPath(obj, "a.ab[1]");
//"2"
dotPath(obj, "b");
//undefined
dotPath(obj, "c");
//null
dotPath(obj, "d");
//ReferenceError: d is not defined
dotPath(obj, "d", "2");
//"2"
dotPath(obj, "d.e", 3);
//TypeError: d is not an object
dotPath(obj, "d", { e: 3 });
//{ e: 3 }
dotPath(obj, "d.e");
//3

License

MPL 2.0

Keywords

object

FAQs

Package last updated on 13 Sep 2016

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

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.