0.9.5 • Published 2 years ago

dot-dotty v0.9.5

Weekly downloads
4
License
MPL-2.0
Repository
github
Last release
2 years ago

DotDotty

(of a person, action, or idea) somewhat mad or eccentric.

DotDotty provides a very simple wrapper around a provided object that allows for lazy dot-syntax access via the [] accessor.

Usage

const DotDotty = require('dot-dotty')

let myData = {a: true, b: false}
let dot = DotDotty(myData)

dot['a'] // true
dot['c'] = 'maybe' // 'maybe'
dot['d.0.first'] = 'expansion!' // 'expansion
// dot now is {a: true: b: false, c: 'maybe', d: [{first: 'expansion!'}]}

A given object can be made immutable or denied expansion by providing an additional options object.

If isExpandable is true(default), new arrays, objects, and keys are created when set access is attempted. Arrays will be created if the value at a given key is not an object or array already and the target key can evaluate to a number. If the target key cannot evaluate as a number, the value at the given key is created as an object. Arrays will attempt to remain as arrays unless a non-number is used as the key, in which case the array will be non-destructively converted to an object.

API

Table of contents

function DotDotty

DotDotty return a Proxy against the given target object that can access properties via dot-syntax.

ParameterTypeDescription
targetObjectThe target object to proxy DotDotty against.
optionsObject
options.isImmutableBoolean? = falseWhether or not the target object should be changeable.
options.isExpandableBoolean? = trueWhether or not new values may be placed into the object. These include new array entries and new object entries.
options.expandOverNullBoolean? = falseWhether or not null values should be treated as nonexistent for the purposes of expansion.
options.throwErrorsBoolean? = trueWhether or not to throw errors when invalid access or expansion occurs. If false, invalid access or expansion will return undefined.
options.throwTrapsBoolean? = trueWhether or not to throw trap errors for set and delete. If false, invalid sets or deletes will be silently ignored.
options.preventPrototypeKeywordsBoolean? = trueWhether or not prototype keywords should be allowed within keys. If true, "proto", "constructor", and "prototype" will be silently truncated. If throwErrors is true, an error will be thrown.
options.removeLeadingDotsBoolean? = trueWhether or not to remove leading dots from the target. The effectively cleans ".prop.a" => "prop.a".
options.prefixString? = ''A prefix to add to the dot-notation string.
options.suffixString? = ''A suffix to add to the dot-notation string.

Returns: Proxy — A proxy to the target object that can use dot-notation to access or create properties.

Examples

const DotDotty = require('dot-dotty')

let myData = {a: 1, b: 2}

let dot = DotDotty(myData)

dot"a" // returns 1

dot"c.cA" = true // returns true

// dot now contains {a: 1, b:2, c: {cA: true}}

dot"d.0.a" = "test" // returns "test"

// dot now contains {a: 1, b:2, c: {cA: true}, d: {a: test}}

0.9.5

2 years ago

0.9.3

3 years ago

0.9.2

3 years ago

0.9.1

3 years ago

0.8.0

3 years ago

0.7.1

3 years ago

0.5.0

3 years ago

0.7.0

3 years ago

0.6.0

3 years ago

0.4.0

3 years ago

0.4.3

3 years ago

0.4.2

3 years ago

0.3.0

3 years ago

0.3.1

3 years ago

0.2.0

4 years ago

0.1.0

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago