UNPKG

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