π€ Object property paths with wildcards and regexps. π΅
Get/set object properties using:
Hire me
Please
reach out
if you're looking for a Node.js API or CLI engineer (11 years of experience).
Most recently I have been Netlify Build's
and Netlify Plugins'
technical lead for 2.5 years. I am available for full-time remote positions.
Install
npm install wild-wild-path
This package works in both Node.js >=18.18.0 and
browsers.
This is an ES module. It must be loaded using
an import
or import()
statement,
not require()
. If TypeScript is used, it must be configured to
output ES modules,
not CommonJS.
API
Methods
get(target, query, options?)
target
: Target
query
: Query
options
: Options?
Return value: any | undefined
Return the first property matching the query
.
const target = { settings: { colors: ['red', 'blue'] } }
get(target, 'settings.colors.0')
get(target, ['settings', 'colors', 0])
has(target, query, options?)
target
: Target
query
: Query
options
: Options?
Return value: boolean
Return whether the query
matches any property.
const target = { settings: { lastName: undefined, colors: ['red', 'blue'] } }
has(target, 'settings.firstName')
has(target, ['settings', 'firstName'])
has(target, 'settings.lastName')
list(target, query, options?)
target
: Target
query
: Query
options
: Options?
Return value: any[]
Return all properties matching the query
, as an array.
const target = {
userOne: { firstName: 'John', lastName: 'Doe', age: 72 },
userTwo: { firstName: 'Alice', colors: ['red', 'blue', 'yellow'] },
}
list(target, 'userOne.firstName userTwo.colors.0')
list(target, [
['userOne', 'firstName'],
['userTwo', 'colors', 0],
])
list(target, 'userOne./Name/')
list(target, ['userOne', /Name/])
list(target, 'userTwo.colors.*')
list(target, 'userTwo.colors.0:2')
list(target, '**.firstName')
list(target, 'userOne.*', { entries: true })
iterate(target, query, options?)
target
: Target
query
: Query
options
: Options?
Return value: Iterable<any>
Return all properties matching the query
, as an
iterable.
This is slower than list()
but uses less memory.
const target = { settings: { colors: ['red', 'blue'] } }
for (const color of iterate(target, 'settings.colors.*')) {
console.log(color)
}
set(target, query, value, options?)
target
: Target
query
: Query
value
: any
options
: Options?
Return value: Target
Sets all properties matching the query
. The return value is a deep clone
unless the mutate
option is true
.
const target = { colors: ['red', 'blue'] }
set(target, 'colors.0', 'yellow')
set(target, ['colors', 0], 'yellow')
set(target, 'colors.-1', 'yellow')
set(target, 'colors.-0', 'yellow')
set(target, 'colors.*', 'yellow')
set({}, 'user.0.color', 'red')
set({}, 'user.0.color', 'red', { missing: false })
remove(target, query, options?)
target
: Target
query
: Query
options
: Options?
Return value: Target
Delete all properties matching the query
. The return value is a deep clone
unless the mutate
option is true
.
const target = { user: { firstName: 'John', lastName: 'Doe', age: 72 } }
remove(target, 'user.lastName')
remove(target, 'user./Name/')
remove(target, ['user', /Name/])
Functional utilities
wild-wild-utils
is a separate
library which provides with additional, higher-level methods:
map()
,
merge()
,
push()
,
unshift()
,
find()
,
pick()
,
include()
,
exclude()
,
flatten()
.
Target
The target value must be an object or an array.
Queries
There are two equivalent formats for queries: strings and arrays.
- Query strings are friendlier to CLI usage, more expressive,
and easier to serialize.
- Query arrays are friendlier to programmatic usage, and
faster. Also, they do not require escaping, so they should be used when the
input is dynamic or user-provided to prevent injection attacks.
Query strings
βοΈ Deep properties
user.colors.0
π Unions
colors name age
β Wildcards
user.*
user.**
**.colors
πΊοΈ Regexps
user./name/
user./name/i
user./^name$/i
π΅ Arrays indices
user.colors.0
user.colors.-1
ποΈ Array slices
user.colors.0:2
user.colors.:2
user.colors.0:
user.colors.:
πͺ¨ Escaping
name\\ with\\ spaces
name\\.with\\.dots
name\\\\with\\\\backslashes
name.\\0
name.\\/not_a_regexp/
π¨ Root and empty strings
user.colors
.user.colors
.
user..colors
Query arrays
βοΈ Deep properties
// Deep properties of objects or arrays.
// Symbol properties are always ignored.
['user', 'colors', 0]
π Unions
// Unions ("or") of queries are arrays of arrays.
// There must be at least one item.
[['colors'], ['name'], ['age']]
β Wildcards
// Shallow wildcards target all properties/items of a single object/array
['user', { type: 'any' }]
// Deep wildcards target all properties/items of 0, 1 or many objects/arrays
['user', { type: 'anyDeep' }]
[{ type: 'anyDeep' }, 'colors']
π€ Regexps
// Regexps match property names
['user', /name/]
// Flags can be used, e.g. to make it case-insensitive
['user', /name/i]
// ^ $ must be used to match from the beginning or until the end
['user', /^name$/i]
π΅ Arrays indices
// Array indices are integers, not strings
['user', 'colors', 0]
// Array indices can be negative.
// -1 is the last item.
// -0 is the item after it, which can be used to append.
['user', 'colors', -1]
ποΈ Array slices
// Array slices. Goes from the start (included) to the end index (excluded).
['user', 'colors', { type: 'slice', from: 0, to: 2 }]
// The start index defaults to 0, i.e. the beginning
['user', 'colors', { type: 'slice', to: 2 }]
// The end index defaults to -0, i.e. the end
['user', 'colors', { type: 'slice', from: 0 }]
['user', 'colors', { type: 'slice' }]
πͺ¨ Escaping
// Escaping is not necessary with query arrays
['name with spaces']
['name.with.dots']
['name\\with\\backslashes']
['name', '0']
['name', '/not_a_regexp/']
π¨ Root and empty strings
// Root value
[]
// Empty string properties
['user', '', 'colors']
Paths
A "path" is any query using only
property names and positive
array indices. This excludes
negative indices, slices,
wildcards, regexps and unions.
Paths are returned by the entries
option.
user.colors.0
// Path array
['user', 'colors', 0]
Conversions and comparisons
wild-wild-parser
can be used to
convert between both formats, or to compare queries.
Undefined values
Object properties with a defined key but an undefined
value are not ignored.
However, object properties without any defined key are ignored. The
has()
method, missing
option and
entries
option can be used to distinguish those.
const target = { name: undefined }
has(target, 'name')
has(target, 'colors')
get(target, 'name')
get(target, 'colors')
get(target, 'name', { entries: true, missing: true })
get(target, 'colors', { entries: true, missing: true })
list(target, '*')
list(target, '*', { entries: true })
Options
Options are optional plain objects.
mutate
Methods: set()
,
remove()
Type: boolean
Default: false
By default, the target is deeply cloned.
When true
, it is directly mutated instead, which is faster but has side effects.
const target = {}
console.log(set(target, 'name', 'Alice'))
console.log(target)
console.log(set(target, 'name', 'Alice', { mutate: true }))
console.log(target)
entries
Methods: get()
,
list()
,
iterate()
Type: boolean
Default: false
By default, properties' values are returned.
When true
, objects with the following shape are returned instead:
value
any
: property's valuepath
Path
: property's full pathmissing
boolean
: whether the property is missing from the
target
const target = { firstName: 'Alice', lastName: 'Smith' }
list(target, '*')
list(target, '*', { entries: true })
missing
Methods: all except has()
and
remove()
Type: boolean
Default: false
with list|iterate()
, true
with set()
When false
, properties not defined in the target are
ignored.
const target = {}
set(target, 'name', 'Alice')
set(target, 'name', 'Alice', { missing: false })
list(target, 'name')
list(target, 'name', { missing: true, entries: true })
sort
Methods: get()
,
list()
,
iterate()
Type: boolean
Default: false
When returning sibling object properties, sort them by the lexigographic order
of their names (not values).
const target = { lastName: 'Doe', firstName: 'John' }
list(target, '*')
list(target, '*', { sort: true })
childFirst
Methods: get()
,
list()
,
iterate()
Type: boolean
Default: false
When using unions or deep wildcards, a query might
match both a property and some of its children.
This option decides whether the returned properties should be sorted from
children to parents, or the reverse.
const target = { user: { name: 'Alice' } }
list(target, 'user.**')
list(target, 'user.**', { childFirst: true })
leaves
Methods: all except has()
Type: boolean
Default: false
When using unions or deep wildcards, a query might
match both a property and some of its children.
When true
, only leaves are matched. In other words, a matching property is
ignored if one of its children also matches.
const target = { user: { name: 'Alice' } }
list(target, 'user.**')
list(target, 'user.**', { leaves: true })
roots
Methods: get()
,
list()
,
iterate()
Type: boolean
Default: false
When using unions or deep wildcards, a query might
match both a property and some of its children.
When true
, only roots are matched. In other words, a matching property is
ignored if one of its parents also matches.
const target = { user: { name: 'Alice' } }
list(target, 'user.**')
list(target, 'user.**', { roots: true })
shallowArrays
Methods: all
Type: boolean
Default: false
If true
, wildcards do not recurse on arrays. Array items can
still be matched by using indices or
slices.
const target = [{ name: 'Alice' }, { name: 'Bob' }]
list(target, '**')
list(target, '**', { shallowArrays: true })
classes
Methods: all
Type: boolean
Default: false
Unless true
, wildcards and regexps ignore
properties of objects that are not plain objects (like class instances, errors
or functions). Those can still be matched by using their
property name.
const target = { user: new User({ name: 'Alice' }) }
list(target, 'user.*')
list(target, 'user.*', { classes: true })
inherited
Methods: all
Type: boolean
Default: false
By default, wildcards and regexps ignore
properties that are either
inherited
or
not enumerable.
Those can still be matched by using their
property name.
When true
, inherited properties are not ignored, but not enumerable ones still
are.
Related projects
Support
For any question, don't hesitate to submit an issue on GitHub.
Everyone is welcome regardless of personal background. We enforce a
Code of conduct in order to promote a positive and
inclusive environment.
Contributing
This project was made with β€οΈ. The simplest way to give back is by starring and
sharing it online.
If the documentation is unclear or has a typo, please click on the page's Edit
button (pencil icon) and suggest a correction.
If you would like to help us fix a bug or add a new feature, please check our
guidelines. Pull requests are welcome!