@jaisocx/jpath
Version:
JPath accepts queries of datatype (string|number)[] and performs lookup in js objects and arrays
154 lines (101 loc) β’ 4.06 kB
Markdown
# JPath
> π‘The JPath class gets or sets values of an object or array, without eval js func invoked.
| π **οΈLibrary** | `@jaisocx/jpath` | π· **οΈver.**: `2.0.5` |
| π **Updated** | π· Spring 2026 | `07. Mai AD 2026 07:23:20 UTC` |
| π **Size** | π Folder: 105 KB | π¦ Tarball: 10 KB | π .js: 14 KB |
| β‘ **JS Engine** | π Browser: β
yes | π§ Express: β
yes | π₯οΈ Console: β
yes |
## The aim of the setup
> π‘ The JPath class gets or sets values of an object or array.
## Examples
**JPath Expression**
```javascript
let jpathExpression_js: string = "data.records[0].id";
let jpathExpression_points_joined: string = "data.records.0.id";
let jpathExpression_jsonkey_ext_symbols: string = "[data][records][0][field 2]";
```
**In HTML**
```html
<!-- π‘ In `.html` for the 'js framework like' Reactivity by central json data in a SPA. -->
<html>
...
<div data-jpath="data.records.0.id"></div>
...
<script src="node_modules/@jaisocx/jpath/transpiled/Simple/JPathData.js"></script>
<script src="node_modules/@jaisocx/jpath/transpiled/Simple/JPath.js"></script>
<script>
// DOMContentLoaded ...
</script>
</html>
```
**Typescript**
```typescript
let obj: any = {
"data": {
"records": [
{ "id": 1, "field 2": "text line of field 2" }
]
}
};
let jPath: JPath = new JPath();
let jpathExpression: string = "data.records[0][field 2]";
let field2: any = jPath.getByJPathExpression( obj, jpathExpression );
console.log ( field2 );
// $ _ text line of field 2
```
## Structure
```ls
π JPath
π src/
π index.ts
π JPath.ts
π JPathData.ts
π JPathDataInterface.ts
π JPathInterface.ts
π test/
π JPath_Test.js
π Split_Test.js
π transpiled/
π CommonJS/
π ESNext/
π Simple/
π JPath.js
π JPathData.js
π¦ jaisocx-jpath-2.0.4.tgz
π package.json
π package-lock.json
π README.md
```
## Interfaces
**JPathInterface**
```typescript
// $ _ ${package}/src/JPathInterface.ts
// @class JPath. π‘ To lookup in js objects and arrays, without eval js func invoked.
export interface JPathInterface {
// jpath string exression as "subtree[1].opened" => [ "subtree", 1, "opened" ]
parse ( jpathExpression: string ): (string|number)[];
// @invokedBy method parse(), splits with symbol point, adds to jpathArray, numeric array elem id or textual field name.
parseDotted ( jpathExpressionDotted: string, jpathArray: ( string|number )[] ): number;
serialize ( jpath: (string|number)[], delimiter: string, start: string, finish: string ): string;
getByJPathExpression ( obj: any, jpathExpression: string ): any;
// faster than JPath.getByJPathExpression( jpathExpression: string, value: any );
getByJPath ( obj: any, jpath: (string|number)[] ): any;
setByJPathExpression ( obj: any, jpathExpression: string, value: any ): JPathInterface;
setByJPath ( obj: any, jpath: (string|number)[], value: any ): JPathInterface;
setByJPathWalkFlatRebuild ( obj: any, jpath: (string|number)[], value: any, nameHolderId: string, nameId: string, branchName: string ): JPathInterface;
}
```
**JPathDataInterface**
```typescript
// $ _ ${package}/src/JPathDataInterface.ts
// @class JPathData. π‘ The data record for 2 fields: jpathExpression and jpathArray.
export interface JPathDataInterface {
getJPathExpression(): string;
getJPath(): ( string | number )[];
setJPathExpression( jpathExpression: string ): JPathDataInterface;
setJPath( jpath: ( string | number )[] ): JPathDataInterface;
}
```
Have a nice day,
Jaisocx Software Architect Elias