Skip to content

Latest commit

 

History

History
48 lines (31 loc) · 1.13 KB

File metadata and controls

48 lines (31 loc) · 1.13 KB

A Data driven Optional chaining

Similar to JS optional chaining operator (?.) but is data driven and suitable to be use in configuration file.

syntax

pObj.dot(input, path, [value])

parameters

input: object, input can be either array or object

path: array, paths to reading a value from the input object. it supported multiple branching

value optional: any, default value if the path doesn't yeild any value from input

returns

value yeild by the path in input or the default value

example

const input = [{name: 'Darren'}, {name: 'San'}]

pObj.dot(input, [1, 'name']) // return 'Darren'

pObj.dot(input, [2, 'name']) // return undefined

pObj.dot(input, [2, 'name'], 'NA') // return 'NA'

import

with es6 module

import pCommon from 'pico-common'

const pObj = pCommon.export('pico/obj')

with nodejs commonjs

const pObj = require('pico-common').export('pico/obj')

with pico-common amd

const pObj = require('pico/obj')