Pathist / Pathist
Defined in: pathist.ts:85
A path utility class for parsing, manipulating, and comparing object property paths.
Pathist provides a comprehensive API for working with property paths in JavaScript objects. It supports multiple notation styles (dot, bracket, and mixed), handles numeric indices, and offers powerful comparison and manipulation methods.
| Method | Description |
|---|---|
| from() | Creates a new Pathist instance from various input types |
| fromJSONPointer() | Creates a new Pathist instance from a JSON Pointer string (RFC 6901) |
| toArray() | Returns the path as an array of segments |
| toString() | Converts the path to a string representation using the specified notation |
| toJSONPath() | Converts the path to JSONPath format (RFC 9535) |
| toJSONPointer() | Converts the path to JSON Pointer format (RFC 6901) |
| [[iterator]()](#iterator) | Makes the Pathist instance iterable, allowing use in for...of loops and spread operators |
| reduce() | Convenience wrapper for Array.reduce() on the path segments |
| equals() | Checks if this path is equal to another path |
| startsWith() | Checks if this path starts with the specified path segment sequence |
| endsWith() | Checks if this path ends with the specified path segment sequence |
| includes() | Checks if this path contains the specified path segment sequence anywhere within it |
| relativeTo() | Extracts the relative path from a base path |
| commonStart() | Finds the common prefix path shared between this path and another path |
| commonEnd() | Finds the common suffix path shared between this path and another path |
| positionOf() | Finds the first position where the specified path segment sequence occurs within this path |
| lastPositionOf() | Finds the last position where the specified path segment sequence occurs within this path |
| pathTo() | Returns the path up to and including the first occurrence of the specified path segment sequence |
| pathToLast() | Returns the path up to and including the last occurrence of the specified path segment sequence |
| match() | Returns the first matched subsequence anywhere in this path |
| matchStart() | Returns the matched prefix if this path starts with the pattern |
| matchEnd() | Returns the matched suffix if this path ends with the pattern |
| slice() | Returns a new path containing a subset of this path's segments |
| parentPath() | Returns the parent path by removing segments from the end |
| concat() | Returns a new path that combines this path with one or more other paths |
| merge() | Intelligently merges another path with this path by detecting overlapping segments |
| firstNodePath() | Returns the path to the first node |
| lastNodePath() | Returns the full path to the last node in the contiguous tree structure |
| afterNodePath() | Returns the path segments after the last node in the tree |
| parentNode() | Returns the parent node in the tree structure by removing nodes from the end |
| nodeIndices() | Returns the numeric index values from the contiguous tree structure |
| nodePaths() | Generates paths to each successive node in the tree structure |
| Accessor | Description |
|---|---|
| defaultNotation | Gets or sets the default notation style used when converting paths to strings |
| defaultIndices | Gets or sets the default indices comparison mode |
| indexWildcards | Gets or sets the values that are treated as index wildcards |
| defaultNodeChildrenProperties | No description |
| notation | Gets the notation style for this instance |
| indices | Gets the indices comparison mode for this instance |
| nodeChildrenProperties | Gets the node children properties for this instance |
| array | Gets the path as an array of segments |
| string | Gets the path as a string using the instance's default notation |
| jsonPath | Gets the path as a JSONPath string |
| jsonPointer | Gets the path as a JSON Pointer string |
Basic usage
const path = Pathist.from('foo.bar.baz');
console.log(path.length); // 3
console.log(path.toArray()); // ['foo', 'bar', 'baz']Path comparison
const path1 = Pathist.from('foo.bar');
const path2 = Pathist.from('foo.bar.baz');
console.log(path2.startsWith(path1)); // truenew Pathist(
input,config?):Pathist
Defined in: pathist.ts:810
Creates a new Pathist instance from a string, array, or existing Pathist.
| Parameter | Type | Description |
|---|---|---|
input |
PathistInput |
The path input (string like "foo.bar", array like ['foo', 'bar'], or Pathist instance) |
config? |
PathistConfig |
Optional configuration for notation, indices mode, and node children properties |
Pathist
If the string path contains syntax errors (unclosed brackets, mismatched quotes, etc.)
If array segments contain invalid types (must be string or number)
From string
const path = Pathist.from('foo.bar.baz');From array
const path = Pathist.from(['foo', 'bar', 0, 'baz']);With custom configuration
const path = Pathist.from('foo.bar', {
notation: Pathist.Notation.Bracket,
indices: Pathist.Indices.Ignore
});| Property | Modifier | Type | Description | Defined in |
|---|---|---|---|---|
Notation |
readonly |
object |
Notation styles for converting paths to strings. - Mixed: Combines dot notation for properties and bracket notation for indices (e.g., foo.bar[0].baz) - Dot: Uses dot notation exclusively (e.g., foo.bar.0.baz) - Bracket: Uses bracket notation exclusively (e.g., ["foo"]["bar"][0]["baz"]) |
pathist.ts:97 |
Notation.Mixed |
readonly |
"Mixed" |
- | pathist.ts:98 |
Notation.Dot |
readonly |
"Dot" |
- | pathist.ts:99 |
Notation.Bracket |
readonly |
"Bracket" |
- | pathist.ts:100 |
Indices |
readonly |
object |
Modes for handling numeric indices during path comparisons. - Preserve: Numeric indices must match exactly for paths to be considered equal - Ignore: Any numeric index matches any other numeric index (useful for comparing paths across different array positions) |
pathist.ts:109 |
Indices.Preserve |
readonly |
"Preserve" |
- | pathist.ts:110 |
Indices.Ignore |
readonly |
"Ignore" |
- | pathist.ts:111 |
length |
readonly |
number |
The number of segments in this path. Example const path = Pathist.from('foo.bar.baz'); console.log(path.length); // 3 |
pathist.ts:724 |
hasIndexWildcards |
readonly |
boolean |
Indicates whether this path contains any wildcard index tokens. A path has index wildcards if any of its segments match the configured wildcard values (by default * and -1). Example const path1 = Pathist.from('foo[*].bar'); console.log(path1.hasIndexWildcards); // true const path2 = Pathist.from('foo[-1].bar'); console.log(path2.hasIndexWildcards); // true const path3 = Pathist.from('foo[0].bar'); console.log(path3.hasIndexWildcards); // false const path4 = Pathist.from('foo.bar.baz'); console.log(path4.hasIndexWildcards); // false |
pathist.ts:747 |
get
staticdefaultNotation():Notation
Defined in: pathist.ts:132
Gets or sets the default notation style used when converting paths to strings.
When setting, the notation is validated and applied to all new Pathist instances.
When setting: If the notation value is invalid
Pathist.Notation.Mixed
set
staticdefaultNotation(notation):void
Defined in: pathist.ts:136
| Parameter | Type |
|---|---|
notation |
Notation |
void
get
staticdefaultIndices():Indices
Defined in: pathist.ts:150
Gets or sets the default indices comparison mode.
When setting, the mode is validated and applied to all new Pathist instances.
When setting: If the indices mode is invalid
Pathist.Indices.Preserve
set
staticdefaultIndices(mode):void
Defined in: pathist.ts:154
| Parameter | Type |
|---|---|
mode |
Indices |
void
get
staticindexWildcards():ReadonlySet<string|number>
Defined in: pathist.ts:172
Gets or sets the values that are treated as index wildcards.
Wildcard values match any numeric index during comparisons.
When setting, wildcard values can be:
- Negative numbers or non-finite numbers (Infinity, -Infinity, NaN)
- Strings that don't match the pattern
/^[0-9]+$/
When setting: If any wildcard value is invalid (e.g., positive finite number or numeric string)
Set([-1, '*'])
ReadonlySet<string | number>
set
staticindexWildcards(value):void
Defined in: pathist.ts:176
| Parameter | Type |
|---|---|
value |
string |
void
get
staticdefaultNodeChildrenProperties():ReadonlySet<string>
Defined in: pathist.ts:211
ReadonlySet<string>
set
staticdefaultNodeChildrenProperties(value):void
Defined in: pathist.ts:223
Gets or sets the default property names that contain child nodes in tree structures. These properties are used by node-related methods to identify and traverse tree relationships.
- When setting: If the value is not a Set, Array, or string, or if any value is not a string
Set(['children'])
| Parameter | Type | Description |
|---|---|---|
value |
string |
ReadonlySet<string> |
void
get notation():
Notation
Defined in: pathist.ts:754
Gets the notation style for this instance.
Returns the instance-specific notation if set, otherwise returns the global default.
get indices():
Indices
Defined in: pathist.ts:763
Gets the indices comparison mode for this instance.
Returns the instance-specific mode if set, otherwise returns the global default.
get nodeChildrenProperties():
ReadonlySet<string>
Defined in: pathist.ts:772
Gets the node children properties for this instance.
Returns the instance-specific properties if set, otherwise returns the global default.
ReadonlySet<string>
get array():
PathSegment[]
Defined in: pathist.ts:869
Gets the path as an array of segments.
toArray - Method form of this getter
get string():
string
Defined in: pathist.ts:943
Gets the path as a string using the instance's default notation.
toString - Method form of this getter
string
get jsonPath():
string
Defined in: pathist.ts:1026
Gets the path as a JSONPath string.
toJSONPath - Method form of this getter
string
get jsonPointer():
string
Defined in: pathist.ts:1106
Gets the path as a JSON Pointer string.
toJSONPointer - Method form of this getter
string
staticfrom(input,config?):Pathist
Defined in: pathist.ts:313
Creates a new Pathist instance from various input types.
This is the Temporal-style factory method alternative to using Pathist.from().
| Parameter | Type | Description |
|---|---|---|
input |
PathistInput |
Path string, array of segments, or existing Pathist instance |
config? |
PathistConfig |
Optional configuration for notation, indices mode, etc. |
Pathist
A new Pathist instance
Pathist.from('foo.bar.baz')
Pathist.from(['foo', 'bar', 'baz'])
Pathist.from('foo.bar', { notation: 'bracket' })
staticfromJSONPointer(pointer,config?):Pathist
Defined in: pathist.ts:360
Creates a new Pathist instance from a JSON Pointer string (RFC 6901).
Parses a JSON Pointer formatted string and converts it to a Pathist instance.
JSON Pointer uses / as segment separators and requires unescaping of special
characters (~1 becomes /, ~0 becomes ~).
| Parameter | Type | Description |
|---|---|---|
pointer |
string |
A JSON Pointer string (e.g., '/foo/bar/0') |
config? |
PathistConfig |
Optional configuration for notation, indices mode, etc. |
Pathist
A new Pathist instance
If the pointer contains invalid escape sequences
- toJSONPointer - Convert path to JSON Pointer format
- from - General factory method for creating paths
Basic usage
const path = Pathist.fromJSONPointer('/foo/bar/baz');
console.log(path.toArray()); // ['foo', 'bar', 'baz']With numeric indices
const path = Pathist.fromJSONPointer('/items/0/name');
console.log(path.toString()); // 'items[0].name'With escaped special characters
const path = Pathist.fromJSONPointer('/foo~0bar/baz~1qux');
console.log(path.toArray()); // ['foo~bar', 'baz/qux']Root reference (empty string)
const path = Pathist.fromJSONPointer('');
console.log(path.length); // 0toArray():
PathSegment[]
Defined in: pathist.ts:859
Returns the path as an array of segments.
Returns a copy of the internal segments array to maintain immutability.
A new array containing all path segments
const path = Pathist.from('foo.bar[0].baz');
console.log(path.toArray()); // ['foo', 'bar', 0, 'baz']toString(
notation?):string
Defined in: pathist.ts:906
Converts the path to a string representation using the specified notation.
Results are cached for performance. The notation parameter allows overriding the instance's default notation on a per-call basis.
| Parameter | Type | Description |
|---|---|---|
notation? |
Notation |
Optional notation style to use (overrides instance default) |
string
The path as a string
If the notation value is invalid
- string - Getter alias for this method (uses instance default notation)
- toArray - Convert to array representation
- toJSONPath - Convert to JSONPath format
Default notation (Mixed)
const path = Pathist.from(['foo', 'bar', 0, 'baz']);
console.log(path.toString()); // 'foo.bar[0].baz'Bracket notation
console.log(path.toString(Pathist.Notation.Bracket)); // '["foo"]["bar"][0]["baz"]'Dot notation
console.log(path.toString(Pathist.Notation.Dot)); // 'foo.bar.0.baz'toJSONPath():
string
Defined in: pathist.ts:980
Converts the path to JSONPath format (RFC 9535).
JSONPath is a standardized query language for JSON. This method converts
the path to a JSONPath selector string starting with $ (the root).
string
The path as a JSONPath string
- jsonPath - Getter alias for this method
- toString - Convert to standard notation
- toArray - Convert to array representation
Basic usage
const path = Pathist.from('foo.bar.baz');
console.log(path.toJSONPath()); // '$.foo.bar.baz'With numeric indices
const path = Pathist.from('items[0].name');
console.log(path.toJSONPath()); // '$.items[0].name'With wildcards
const path = Pathist.from('items[*].name');
console.log(path.toJSONPath()); // '$.items[*].name'toJSONPointer():
string
Defined in: pathist.ts:1072
Converts the path to JSON Pointer format (RFC 6901).
JSON Pointer is a standardized string format for identifying a specific value
within a JSON document. Each segment is separated by /, and special characters
are escaped (~ becomes ~0, / becomes ~1).
string
The path as a JSON Pointer string
- jsonPointer - Getter alias for this method
- toJSONPath - Convert to JSONPath format (RFC 9535)
- toString - Convert to standard notation
- toArray - Convert to array representation
Basic usage
const path = Pathist.from('foo.bar.baz');
console.log(path.toJSONPointer()); // '/foo/bar/baz'With numeric indices
const path = Pathist.from('items[0].name');
console.log(path.toJSONPointer()); // '/items/0/name'With special characters requiring escaping
const path = Pathist.from(['foo~bar', 'baz/qux']);
console.log(path.toJSONPointer()); // '/foo~0bar/baz~1qux'Empty path (root)
const path = Pathist.from('');
console.log(path.toJSONPointer()); // ''[iterator]():
Iterator<PathSegment>
Defined in: pathist.ts:1137
Makes the Pathist instance iterable, allowing use in for...of loops and spread operators.
Iterator<PathSegment>
An iterator over the path segments
Using for...of
const path = Pathist.from('foo.bar.baz');
for (const segment of path) {
console.log(segment); // 'foo', 'bar', 'baz'
}Using spread operator
const segments = [...path]; // ['foo', 'bar', 'baz']reduce<
T>(callbackfn,initialValue):T
Defined in: pathist.ts:1186
Convenience wrapper for Array.reduce() on the path segments.
This is equivalent to path.toArray().reduce(...) but more concise.
Allows you to define custom reduction logic for navigating objects.
| Type Parameter |
|---|
T |
| Parameter | Type | Description |
|---|---|---|
callbackfn |
(previousValue, currentValue, currentIndex, array) => T |
Function to execute on each segment |
initialValue |
T |
Value to use as the first argument to the first call of the callback |
T
The accumulated result from the reduction
- toArray - Get the path segments as an array
- array - Getter for path segments
- Symbol.iterator - Iterate over segments
Navigate through an object
const data = {
users: [
{ profile: { name: 'Alice' } },
{ profile: { name: 'Bob' } }
]
};
const path = Pathist.from('users[0].profile.name');
const value = path.reduce((obj, segment) => obj?.[segment], data);
console.log(value); // 'Alice'Custom reduction with default fallback
const path = Pathist.from('users[5].profile.name');
const value = path.reduce((obj, seg) => obj?.[seg] ?? {}, data);
console.log(value); // {} (instead of undefined)Building a path string during reduction
const path = Pathist.from('foo.bar.baz');
const result = path.reduce((acc, seg) => acc + '/' + seg, '');
console.log(result); // '/foo/bar/baz'equals(
other,options?):boolean
Defined in: pathist.ts:1232
Checks if this path is equal to another path.
Two paths are equal if they have the same length and all corresponding segments match. The indices option controls how numeric indices are compared.
| Parameter | Type | Description |
|---|---|---|
other |
PathistInput |
Pathist |
options? |
ComparisonOptions |
Optional comparison options |
boolean
true if the paths are equal, false otherwise
- startsWith for checking if this path starts with another
- endsWith for checking if this path ends with another
- includes for checking if this path contains another
Exact comparison (default)
const path1 = Pathist.from('foo[0].bar');
const path2 = Pathist.from('foo[0].bar');
console.log(path1.equals(path2)); // trueIgnoring indices
const path1 = Pathist.from('foo[0].bar');
const path2 = Pathist.from('foo[5].bar');
console.log(path1.equals(path2, { indices: Pathist.Indices.Ignore })); // truestartsWith(
other,options?):boolean
Defined in: pathist.ts:1274
Checks if this path starts with the specified path segment sequence.
| Parameter | Type | Description |
|---|---|---|
other |
PathistInput |
Pathist |
options? |
ComparisonOptions |
Optional comparison options |
boolean
true if this path starts with the specified sequence, false otherwise
- endsWith for checking if this path ends with a sequence
- equals for exact path comparison
- positionOf for finding the position of a sequence
const path = Pathist.from('foo.bar.baz');
console.log(path.startsWith('foo.bar')); // true
console.log(path.startsWith('bar')); // falseendsWith(
other,options?):boolean
Defined in: pathist.ts:1296
Checks if this path ends with the specified path segment sequence.
| Parameter | Type | Description |
|---|---|---|
other |
PathistInput |
Pathist |
options? |
ComparisonOptions |
Optional comparison options |
boolean
true if this path ends with the specified sequence, false otherwise
- startsWith for checking if this path starts with a sequence
- equals for exact path comparison
- lastPositionOf for finding the last position of a sequence
const path = Pathist.from('foo.bar.baz');
console.log(path.endsWith('bar.baz')); // true
console.log(path.endsWith('bar')); // falseincludes(
other,options?):boolean
Defined in: pathist.ts:1328
Checks if this path contains the specified path segment sequence anywhere within it.
| Parameter | Type | Description |
|---|---|---|
other |
PathistInput |
Pathist |
options? |
ComparisonOptions |
Optional comparison options |
boolean
true if this path contains the specified sequence, false otherwise
- positionOf for finding the exact position of the sequence
- startsWith for checking if the sequence is at the start
- endsWith for checking if the sequence is at the end
const path = Pathist.from('foo.bar.baz.qux');
console.log(path.includes('bar.baz')); // true
console.log(path.includes('baz.foo')); // falserelativeTo(
base,options?):Pathist|null
Defined in: pathist.ts:1376
Extracts the relative path from a base path.
Returns a new path representing the segments that would need to be concatenated to the base
to produce this path. Returns null if this path doesn't start with the base path.
| Parameter | Type | Description |
|---|---|---|
base |
PathistInput |
Pathist |
options? |
ComparisonOptions |
Optional comparison options |
Pathist | null
The relative path, or null if this path doesn't start with the base
- concat for combining paths (the inverse operation)
- startsWith for checking if this path starts with a base
- commonStart for finding the common prefix between paths
Extract relative path
const fullPath = Pathist.from('api.users[0].profile.settings');
const basePath = Pathist.from('api.users[0]');
const relative = fullPath.relativeTo(basePath);
console.log(relative?.string); // 'profile.settings'Inverse of concat
const base = Pathist.from('api.users[0]');
const relative = Pathist.from('profile.settings');
const full = base.concat(relative);
console.log(full.relativeTo(base)?.equals(relative)); // trueReturns null when not relative
const path = Pathist.from('posts.comments');
const base = Pathist.from('users.profile');
console.log(path.relativeTo(base)); // nullcommonStart(
other,options?):Pathist
Defined in: pathist.ts:1446
Finds the common prefix path shared between this path and another path.
Returns a new path containing the longest sequence of segments that both paths start with. Returns an empty path if there is no common prefix.
| Parameter | Type | Description |
|---|---|---|
other |
PathistInput |
Pathist |
options? |
ComparisonOptions |
Optional comparison options |
Pathist
The common prefix path (may be empty if no common prefix exists)
- commonEnd for finding the common suffix
- startsWith for checking if this path starts with another
- relativeTo for extracting the relative path after a common base
Find common prefix
const path1 = Pathist.from('users[0].profile.settings.theme');
const path2 = Pathist.from('users[0].profile.avatar.url');
const common = path1.commonStart(path2);
console.log(common.string); // 'users[0].profile'No common prefix
const path1 = Pathist.from('users.name');
const path2 = Pathist.from('posts.title');
const common = path1.commonStart(path2);
console.log(common.length); // 0 (empty path)Use with relativeTo to decompose paths
const path1 = Pathist.from('api.users[0].profile.settings');
const path2 = Pathist.from('api.users[0].posts.recent');
const common = path1.commonStart(path2); // 'api.users[0]'
const rel1 = path1.relativeTo(common); // 'profile.settings'
const rel2 = path2.relativeTo(common); // 'posts.recent'commonEnd(
other,options?):Pathist
Defined in: pathist.ts:1512
Finds the common suffix path shared between this path and another path.
Returns a new path containing the longest sequence of segments that both paths end with. Returns an empty path if there is no common suffix.
| Parameter | Type | Description |
|---|---|---|
other |
PathistInput |
Pathist |
options? |
ComparisonOptions |
Optional comparison options |
Pathist
The common suffix path (may be empty if no common suffix exists)
- commonStart for finding the common prefix
- endsWith for checking if this path ends with another
Find common suffix
const path1 = Pathist.from('users[0].profile.settings');
const path2 = Pathist.from('config.default.settings');
const common = path1.commonEnd(path2);
console.log(common.string); // 'settings'No common suffix
const path1 = Pathist.from('users.name');
const path2 = Pathist.from('posts.title');
const common = path1.commonEnd(path2);
console.log(common.length); // 0 (empty path)Extract unique prefixes
const path1 = Pathist.from('api.users.profile.settings');
const path2 = Pathist.from('config.profile.settings');
const suffix = path1.commonEnd(path2); // 'profile.settings'
const prefix1 = path1.slice(0, path1.length - suffix.length); // 'api.users'
const prefix2 = path2.slice(0, path2.length - suffix.length); // 'config'positionOf(
other,options?):number
Defined in: pathist.ts:1567
Finds the first position where the specified path segment sequence occurs within this path.
Returns the index of the first segment where the match begins, or -1 if not found.
| Parameter | Type | Description |
|---|---|---|
other |
PathistInput |
Pathist |
options? |
ComparisonOptions |
Optional comparison options |
number
The zero-based position of the first match, or -1 if not found
- lastPositionOf for finding the last occurrence
- includes for checking if a sequence exists without needing the position
- pathTo for extracting the path up to the first occurrence
const path = Pathist.from('foo.bar.baz.bar');
console.log(path.positionOf('bar')); // 1 (first occurrence)
console.log(path.positionOf('qux')); // -1 (not found)lastPositionOf(
other,options?):number
Defined in: pathist.ts:1623
Finds the last position where the specified path segment sequence occurs within this path.
Returns the index of the first segment where the last match begins, or -1 if not found.
| Parameter | Type | Description |
|---|---|---|
other |
PathistInput |
Pathist |
options? |
ComparisonOptions |
Optional comparison options |
number
The zero-based position of the last match, or -1 if not found
- positionOf for finding the first occurrence
- pathToLast for extracting the path up to the last occurrence
const path = Pathist.from('foo.bar.baz.bar');
console.log(path.lastPositionOf('bar')); // 3 (last occurrence)
console.log(path.lastPositionOf('qux')); // -1 (not found)pathTo(
other,options?):Pathist
Defined in: pathist.ts:1683
Returns the path up to and including the first occurrence of the specified path segment sequence.
This method searches for the first match of the provided path within this path and returns a new Pathist instance containing all segments from the start up to and including the match.
| Parameter | Type | Description |
|---|---|---|
other |
PathistInput |
Pathist |
options? |
ComparisonOptions |
Optional comparison options (e.g., indices mode) |
Pathist
A new Pathist instance containing the path up to and including the first match, or an empty path if no match is found
- pathToLast for extracting up to the last occurrence
- positionOf for getting just the position without extraction
- slice for general segment extraction
const p = Pathist.from('foo.bar.baz.bar.qux');
p.pathTo('bar').toString(); // 'foo.bar'
p.pathTo('bar.baz').toString(); // 'foo.bar.baz'
p.pathTo('notfound').toString(); // ''pathToLast(
other,options?):Pathist
Defined in: pathist.ts:1722
Returns the path up to and including the last occurrence of the specified path segment sequence.
This method searches for the last match of the provided path within this path and returns a new Pathist instance containing all segments from the start up to and including the last match.
| Parameter | Type | Description |
|---|---|---|
other |
PathistInput |
Pathist |
options? |
ComparisonOptions |
Optional comparison options (e.g., indices mode) |
Pathist
A new Pathist instance containing the path up to and including the last match, or an empty path if no match is found
- pathTo for extracting up to the first occurrence
- lastPositionOf for getting just the position without extraction
const p = Pathist.from('foo.bar.baz.bar.qux');
p.pathToLast('bar').toString(); // 'foo.bar.baz.bar'
p.pathToLast('bar.baz').toString(); // 'foo.bar.baz'
p.pathToLast('notfound').toString(); // ''match(
pattern,options?):Pathist|null
Defined in: pathist.ts:1784
Returns the first matched subsequence anywhere in this path.
Finds the first occurrence of the pattern within this path and returns a new Pathist containing just the matched segments with their concrete values (not wildcards from the pattern). Returns null if no match is found.
| Parameter | Type | Description |
|---|---|---|
pattern |
PathistInput |
Pathist |
options? |
ComparisonOptions |
Optional comparison options (e.g., indices mode) |
Pathist | null
A new Pathist instance containing the matched subsequence, or null if no match
- matchStart for matching only at the beginning
- matchEnd for matching only at the end
- includes for checking if a match exists without extraction
- positionOf for getting just the position
Basic matching
const path = Pathist.from('foo.bar.baz.qux');
const match = path.match('bar.baz');
console.log(match?.toString()); // 'bar.baz'Wildcard matching with concrete values
const path = Pathist.from(['foo', 0, 'bar', 1, 'baz']);
const match = path.match('[-1].bar');
console.log(match?.toString()); // '[0].bar' - concrete value!No match returns null
const path = Pathist.from('foo.bar.baz');
const match = path.match('qux');
console.log(match); // nullmatchStart(
pattern,options?):Pathist|null
Defined in: pathist.ts:1844
Returns the matched prefix if this path starts with the pattern.
If this path starts with the given pattern, returns a new Pathist containing the matched prefix with concrete values from this path (not wildcards from the pattern). Returns null if the path doesn't start with the pattern.
This method avoids redundant parsing - the pattern is only parsed once, making it more
efficient than calling startsWith() followed by slice().
| Parameter | Type | Description |
|---|---|---|
pattern |
PathistInput |
Pathist |
options? |
ComparisonOptions |
Optional comparison options (e.g., indices mode) |
Pathist | null
A new Pathist instance containing the matched prefix, or null if no match
- startsWith for checking if path starts with pattern without extraction
- matchEnd for matching at the end
- match for matching anywhere
Basic prefix matching
const path = Pathist.from('foo.bar.baz.qux');
const match = path.matchStart('foo.bar');
console.log(match?.toString()); // 'foo.bar'
console.log(match?.length); // 2Wildcard matching preserves concrete values
const errorPath = Pathist.from(['foo', 2, 'bar', 'baz']);
const match = errorPath.matchStart('foo[-1].bar');
if (match) {
console.log(match.toString()); // 'foo[2].bar' - concrete index!
const remaining = errorPath.slice(match.length); // ['baz']
}No match returns null
const path = Pathist.from('foo.bar.baz');
const match = path.matchStart('qux');
console.log(match); // nullmatchEnd(
pattern,options?):Pathist|null
Defined in: pathist.ts:1898
Returns the matched suffix if this path ends with the pattern.
If this path ends with the given pattern, returns a new Pathist containing the matched suffix with concrete values from this path (not wildcards from the pattern). Returns null if the path doesn't end with the pattern.
This method avoids redundant parsing - the pattern is only parsed once, making it more
efficient than calling endsWith() followed by slice().
| Parameter | Type | Description |
|---|---|---|
pattern |
PathistInput |
Pathist |
options? |
ComparisonOptions |
Optional comparison options (e.g., indices mode) |
Pathist | null
A new Pathist instance containing the matched suffix, or null if no match
- endsWith for checking if path ends with pattern without extraction
- matchStart for matching at the start
- match for matching anywhere
Basic suffix matching
const path = Pathist.from('foo.bar.baz.qux');
const match = path.matchEnd('baz.qux');
console.log(match?.toString()); // 'baz.qux'
console.log(match?.length); // 2Wildcard matching preserves concrete values
const errorPath = Pathist.from(['foo', 0, 'bar', 2, 'baz']);
const match = errorPath.matchEnd('[-1].baz');
if (match) {
console.log(match.toString()); // '[2].baz' - concrete index!
}No match returns null
const path = Pathist.from('foo.bar.baz');
const match = path.matchEnd('qux');
console.log(match); // nullslice(
start?,end?):Pathist
Defined in: pathist.ts:1932
Returns a new path containing a subset of this path's segments.
Works like Array.slice(), extracting segments from start to end (end not included). The new path preserves this path's configuration.
| Parameter | Type | Description |
|---|---|---|
start? |
number |
Zero-based index at which to start extraction (default: 0) |
end? |
number |
Zero-based index before which to end extraction (default: path length) |
Pathist
A new Pathist instance containing the extracted segments
- concat - Combine paths sequentially
- merge - Intelligently merge paths with overlap detection
- pathTo - Extract path up to a match
const path = Pathist.from('foo.bar.baz.qux');
console.log(path.slice(1, 3).toString()); // 'bar.baz'
console.log(path.slice(2).toString()); // 'baz.qux'parentPath(
depth):Pathist
Defined in: pathist.ts:1975
Returns the parent path by removing segments from the end.
Removes the specified number of segments from the end of the path, returning a new path representing the parent. If the depth exceeds the path length, returns an empty path rather than throwing an error.
| Parameter | Type | Default value | Description |
|---|---|---|---|
depth |
number |
1 |
Number of levels to go up (default: 1) |
Pathist
A new Pathist instance representing the parent path
If depth is negative
Basic usage
const path = Pathist.from('foo.bar.baz');
console.log(path.parentPath().toString()); // 'foo.bar'
console.log(path.parentPath(2).toString()); // 'foo'
console.log(path.parentPath(3).toString()); // '' (empty path)Exceeding depth returns empty path
const path = Pathist.from('foo.bar');
console.log(path.parentPath(5).toString()); // '' (empty path, not error)With zero depth
const path = Pathist.from('foo.bar.baz');
const clone = path.parentPath(0); // Returns clone of full path
console.log(clone.toString()); // 'foo.bar.baz'concat(...
paths):Pathist
Defined in: pathist.ts:2018
Returns a new path that combines this path with one or more other paths.
Creates a new path by concatenating all segments in order. The new path preserves this path's configuration.
| Parameter | Type | Description |
|---|---|---|
...paths |
(PathistInput |
Pathist)[] |
Pathist
A new Pathist instance containing all concatenated segments
If any path input is invalid
const path1 = Pathist.from('foo.bar');
const path2 = Pathist.from('baz.qux');
console.log(path1.concat(path2).toString()); // 'foo.bar.baz.qux'Multiple paths
const result = path1.concat('baz', ['qux', 'quux']);
console.log(result.toString()); // 'foo.bar.baz.qux.quux'merge(
path):Pathist
Defined in: pathist.ts:2072
Intelligently merges another path with this path by detecting overlapping segments.
Finds the longest suffix of this path that matches a prefix of the other path, then combines them by merging at the overlap point. When overlapping segments include wildcards, concrete values take precedence.
| Parameter | Type | Description |
|---|---|---|
path |
PathistInput |
Pathist |
Pathist
A new Pathist instance containing the merged path
If the path input is invalid
- concat - Simple concatenation without overlap detection
- slice - Extract subset of segments
- positionOf - Find position of subsequence
Basic merge with overlap
const left = Pathist.from('foo.bar.baz');
const right = Pathist.from('baz.qux');
console.log(left.merge(right).toString()); // 'foo.bar.baz.qux'Merge with wildcard replacement
const left = Pathist.from('foo[*].bar');
const right = Pathist.from('foo[5].bar.baz');
console.log(left.merge(right).toString()); // 'foo[5].bar.baz'
// The wildcard is replaced with the concrete indexNo overlap - simple concatenation
const left = Pathist.from('foo.bar');
const right = Pathist.from('qux.quux');
console.log(left.merge(right).toString()); // 'foo.bar.qux.quux'firstNodePath():
Pathist
Defined in: pathist.ts:2277
Returns the path to the first node.
- If the path starts with a numeric index, returns the path up to and including that index
- Otherwise, returns an empty path (representing the root node)
Pathist
A new Pathist representing the path to the first node
- lastNodePath for extracting the full node path
- afterNodePath for extracting the path after all nodes
Path starting with index
const path = new Pathist('[0].children[1].foo');
console.log(path.firstNodePath().toString()); // '[0]'Path starting with property
const path = new Pathist('children[0].children[1].foo');
console.log(path.firstNodePath().toString()); // '' (root)Path with no indices
const path = new Pathist('foo.bar');
console.log(path.firstNodePath().toString()); // '' (root)lastNodePath():
Pathist
Defined in: pathist.ts:2312
Returns the full path to the last node in the contiguous tree structure.
- If the path contains numeric indices, returns the path up to and including the last node
- Otherwise, returns an empty path (representing the root node)
Pathist
A new Pathist representing the full node path
- firstNodePath for extracting the path to the first node
- afterNodePath for extracting the path after all nodes
Tree structure
const path = new Pathist('children[0].children[1].foo');
console.log(path.lastNodePath().toString()); // 'children[0].children[1]'Path with no indices
const path = new Pathist('foo.bar');
console.log(path.lastNodePath().toString()); // '' (root)afterNodePath():
Pathist
Defined in: pathist.ts:2348
Returns the path segments after the last node in the tree.
- If the path contains numeric indices, returns the path after the last node
- Otherwise, returns the full path (all segments are relative to the root node)
Pathist
A new Pathist containing the segments after the tree structure
- lastNodePath for extracting the full node path
- firstNodePath for extracting the path to the first node
Tree with properties after
const path = new Pathist('children[0].children[1].foo');
console.log(path.afterNodePath().toString()); // 'foo'No indices - all relative to root
const path = new Pathist('foo.bar');
console.log(path.afterNodePath().toString()); // 'foo.bar'parentNode(
depth):Pathist
Defined in: pathist.ts:2407
Returns the parent node in the tree structure by removing nodes from the end.
Navigates up the tree hierarchy by the specified depth, removing nodes from the end of the node path. If the depth exceeds the number of nodes, returns the first node (or empty path for root).
| Parameter | Type | Default value | Description |
|---|---|---|---|
depth |
number |
1 |
Number of node levels to go up (default: 1) |
Pathist
A new Pathist instance representing the parent node path
If depth is negative
- parentPath - Remove segments (not node-aware)
- lastNodePath - Get path to last node
- firstNodePath - Get path to first node
- nodePaths - Iterate over all node paths
Basic usage
const path = Pathist.from('children[0].children[1].children[2].value');
console.log(path.lastNodePath().toString()); // 'children[0].children[1].children[2]'
console.log(path.parentNode().toString()); // 'children[0].children[1]'
console.log(path.parentNode(2).toString()); // 'children[0]'
console.log(path.parentNode(3).toString()); // '' (root)Path starting with index
const path = Pathist.from('[0].children[1].name');
console.log(path.parentNode().toString()); // '[0]'
console.log(path.parentNode(2).toString()); // '[0]' (can't go higher)Exceeding depth returns first node
const path = Pathist.from('children[0].children[1].value');
console.log(path.parentNode(10).toString()); // '' (root)No tree structure
const path = Pathist.from('foo.bar.baz');
console.log(path.parentNode().toString()); // '' (root, no nodes in path)nodeIndices():
number[]
Defined in: pathist.ts:2459
Returns the numeric index values from the contiguous tree structure.
Extracts all numeric indices from the tree path, representing the tree path coordinates.
number[]
An array of numeric indices, or an empty array if no tree structure exists
- nodePaths - Generate paths to each node
- firstNodePath - Get path to first node
- lastNodePath - Get path to last node
- afterNodePath - Get path after tree structure
const path = Pathist.from('items[5].children[1].children[3].name');
console.log(path.nodeIndices()); // [5, 1, 3]
const path2 = Pathist.from('foo.bar.baz');
console.log(path2.nodeIndices()); // []nodePaths():
Generator<Pathist,void,undefined>
Defined in: pathist.ts:2525
Generates paths to each successive node in the tree structure.
Yields the path to each node level, starting with the root (empty path) and progressively building up through each node in the tree.
Generator<Pathist, void, undefined>
A generator that yields Pathist instances for each node level
- nodeIndices - Get numeric indices as array
- firstNodePath - Get path to first node
- lastNodePath - Get path to last node
- Symbol.iterator - Iterate over segments
Full tree structure
const path = new Pathist('children[0].children[1].foo');
for (const nodePath of path.nodePaths()) {
console.log(nodePath.string);
}
// Output:
// '' (root)
// 'children[0]' (first node)
// 'children[0].children[1]' (second node)Path starting with index
const path = new Pathist('[0].children[1].children[2]');
const paths = [...path.nodePaths()];
// paths = [
// Pathist('[0]'),
// Pathist('[0].children[1]'),
// Pathist('[0].children[1].children[2]')
// ]No tree structure - just root
const path = new Pathist('foo.bar');
const paths = [...path.nodePaths()];
// paths = [Pathist('')]