Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@finnair/path-parser

Package Overview
Dependencies
Maintainers
0
Versions
39
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@finnair/path-parser

Simple object path as array of strings and numbers

  • 7.0.0-alpha.3
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
324
increased by63.64%
Maintainers
0
Weekly downloads
 
Created
Source

Path Parser

@finnair/path-parser contains nearley.js based parsers for Path and PathMatcher.

See @finnair/path or instructions on how to use Path and PathMatcher.

Getting Started

Install v-validation using yarn:

yarn add @finnair/path-parser

Or npm:

npm install @finnair/path-parser

Parsing Paths

import { parsePath } from '@finnair/path-parser';

parsePath(`$.array[1]["\\"property\\" with spaces and 'quotes'"]`); // JSON string encoded in brackets!
// Path.of('array', 1, `"property" with spaces and 'quotes'`);

// Single quotes also work, but encoding is still JSON string so the value cannot contain ' character
parsePath(`$['single quotes']`); // Path.of('single quotes')
parsePath(`$['single\'quote']`); // Fail!

// ...without using unicode escape
parsePath(`$['\\u0027']`); // Path.of("'");

Parsing PathMatchers

parsePathMatcher parses simple JsonPath like expressions. Supported expressions are

ExpressionDescription 
$.propertyIdentifiers matching RegExp /^[a-zA-Z_][a-zA-Z0-9_]*$/
$[0]Index match
$.*Any property matcher, wildcard (matches also array indexes)
$[*]Any index matcher, wildcard (matches only array indexes)
$["JSON string encoded property"]Property as JSON encoded string
$['JSON string encoded property']Property as single quoted, but otherwise JSON encoded, string(*)
$[union,"of",4,'components']Union matcher that also supports identifiers and JSON encoded strings

*) This is the official way of JsonPath, but the specification is a bit unclear on the encoding. In this library we prefer proper JSON string encoding with double quotes.

import { parsePathMatcher } from '@finnair/path-parser';

parsePathMatcher(`$.array[0][*].*['union',"of",properties,1]`);
// PathMatcher.of(
//   'array',
//    0,
//   AnyIndex,
//   AnyProperty,
//   UnionMatcher.of('union', 'of', 'properties', 1)
// )

Keywords

FAQs

Package last updated on 11 Nov 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc