Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
W

wild-wild-path

> 后端框架
Open source

Object property paths with wildcards and regexps

730 stars0 likes1 views
WebsiteGitHub

About

Object property paths with wildcards and regexps

Object property paths with wildcards and regexps.

Get/set object properties using:

  • ⛏️ Dot-delimited paths: foo.bar.0.baz
  • ⭐ Wildcards: foo.*, **.bar
  • ️ Regexps: foo./ba?/
  • ️ Slices: foo.0:2
  • Unions: foo bar baz

Install

bash
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.

javascript
const target = { settings: { colors: ['red', 'blue'] } }

get(target, 'settings.colors.0') // 'red'
get(target, ['settings', 'colors', 0]) // 'red'

has(target, query, options?)

target: Target
query: Query
options: Options?
Return value: boolean

Return whether the query matches any property.

javascript
const target = { settings: { lastName: undefined, colors: ['red', 'blue'] } }

has(target, 'settings.firstName') // false
has(target, ['settings', 'firstName']) // false
has(target, 'settings.lastName') // true

list(target, query, options?)

target: Target
query: Query
options: Options?
Return value: any[]

Return all properties matching the query, as an array.

…

iterate(target, query, options?)

target: Target
query: Query
options: Options?
Return value: Iterable

Return all properties matching the query, as an iterable. This is slower than list() but uses less memory.

javascript
const target = { settings: { colors: ['red', 'blue'] } }

for (const color of iterate(target, 'settings.colors.*')) {
  console.log(color) // 'red', 'blue'
}

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.

javascript
const target = { colors: ['red', 'blue'] }

set(target, 'colors.0', 'yellow') // ['yellow', 'blue']
set(target, ['colors', 0], 'yellow') // ['yellow', 'blue']
set(target, 'colors.-1', 'yellow') // ['red', 'yellow']
set(target, 'colors.-0', 'yellow') // ['red', 'blue', 'yellow']
set(target, 'colors.*', 'yellow') // ['yellow', 'yellow']
set({}, 'user.0.color', 'red') // { user: [{ 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.

javascript
const target = { user: { firstName: 'John', lastName: 'Doe', age: 72 } }

remove(target, 'user.lastName') // { user: { firstName: 'John', age: 72 } }
remove(target, 'user./Name/') // { user: { age: 72 } }
remove(target, ['user', /Name/]) // { user: { age: 72 } }

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

bash
# Deep properties of objects or arrays.
# Dots are used for array indices, not brackets.
# Symbol properties are always ignored.
user.colors.0

Unions

bash
# Unions ("or") of queries are space-delimited.
# The string must not be empty.
colors name age

⭐ Wildcards

bash
# Shallow wildcards target all properties/items of a single object/array
user.*

# Deep wildcards target all properties/items of 0, 1 or many objects/arrays
user.**
**.colors

️ Regexps

bash
# 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

bash
# Array indices are integers
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

bash
# Array slices. Goes from the start (included) to the end index (excluded).
user.colors.0:2

# The start index defaults to 0, i.e. the beginning
user.colors.:2

# The end index defaults to -0, i.e. the end
user.colors.0:
user.colors.:

Escaping

bash
# Dots, spaces and backslashes in property names must be escaped
name\\ with\\ spaces
name\\.with\\.dots
name\\\\with\\\\backslashes

# Ambiguous property names must be escaped with a backslash at the beginning.
# This includes properties that:
#  - Are integers but are not array elements
#  - Have multiple slashes and start with one
name.\\0
name.\\/not_a_regexp/

Root and empty strings

bash
# A leading dot can optionally be used. It is ignored.
user.colors
.user.colors

# Root value
.

# Empty string properties
user..colors

Query arrays

⛏️ Deep properties

es6
// Deep properties of objects or arrays.
// Symbol properties are always ignored.
['user', 'colors', 0]

Unions

es6
// Unions ("or") of queries are arrays of arrays.
// There must be at least one item.
[['colors'], ['name'], ['age']]

⭐ Wildcards

es6
// 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

es6
// 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

es6
// 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

es6
// 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

es6
// 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

es6
// 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.

bash
# Path string
user.colors.0
es6
// 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.

javascript
const target = { name: undefined }

has(target, 'name') // true
has(target, 'colors') // false

get(target, 'name') // undefined
get(target, 'colors') // undefined
get(target, 'name', { entries: true, missing: true })
// { value: undefined, path: ['name'], missing: false }
get(target, 'colors', { entries: true, missing: true })
// { value: undefined, path: ['colors'], missing: true }

list(target, '*') // [undefined]
list(target, '*', { entries: true })
// [{ value: undefined, path: ['name'], missing: false }]

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.

javascript
const target = {}
console.log(set(target, 'name', 'Alice')) // { name: 'Alice' }
console.log(target) // {}
console.log(set(target, 'name', 'Alice', { mutate: true })) // { name: 'Alice' }
console.log(target) // { name: 'Alice' }

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 value
  • path Path: property's full path
  • missing boolean: whether the property is missing from the target
javascript
const target = { firstName: 'Alice', lastName: 'Smith' }
list(target, '*') // ['Alice', 'Smith']
list(target, '*', { entries: true })
// [
//   { value: 'Alice', path: ['firstName'], missing: false },
//   { value: 'Smith', path: ['lastName'], missing: false },
// ]

missing

Methods: all except has() and remove()
Type: boolean
Default: false with list|iterate(), true with set()

When false, properties [not de

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

JavaScriptalgorithmdata-structuresfilterfunctional-programming

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category后端框架
PricingOpen source

> Related tools

N
Node.js
基于 V8 的 JavaScript 运行时
D
Django
Python 高级 Web 框架
S
Spring Boot
Java 生态主流微服务框架