Comparing version 2.0.2 to 3.0.0
import type { ParsedJson } from './parse.js'; | ||
import { getAstByObject, getAstByString } from './parse.js'; | ||
import type { Position, LocationPath, Location, LocationOptions } from './location.js'; | ||
import type { LocationOptionsPath, LocationPath } from './path.js'; | ||
import { parsePath } from './path.js'; | ||
import type { Position, Location, LocationOptions } from './location.js'; | ||
import { getLocation } from './location.js'; | ||
export type { ParsedJson }; | ||
export { getAstByObject, getAstByString }; | ||
export type { Position, LocationPath, Location, LocationOptions, }; | ||
export type { LocationOptionsPath, LocationPath }; | ||
export { parsePath }; | ||
export type { Position, Location, LocationOptions }; | ||
export { getLocation }; | ||
export declare function jsonpos(json: string, options: LocationOptions): Location; |
import { getAstByObject, getAstByString } from './parse.js'; | ||
import { parsePath } from './path.js'; | ||
import { getLocation } from './location.js'; | ||
export { getAstByObject, getAstByString }; | ||
export { parsePath }; | ||
export { getLocation }; | ||
@@ -5,0 +7,0 @@ export function jsonpos(json, options) { |
import type { ParsedJson } from './parse.js'; | ||
export declare type LocationPath = Array<number | string>; | ||
export interface LocationOptions { | ||
dataPath: string | LocationPath; | ||
import { LocationOptionsPath } from './path.js'; | ||
export declare type LocationOptions = LocationOptionsPath & { | ||
markIdentifier?: boolean; | ||
} | ||
}; | ||
export interface Position { | ||
@@ -16,2 +15,2 @@ line: number; | ||
} | ||
export declare function getLocation(parsedJson: ParsedJson, { dataPath, markIdentifier }: LocationOptions): Location; | ||
export declare function getLocation(parsedJson: ParsedJson, options: LocationOptions): Location; |
@@ -1,11 +0,6 @@ | ||
export function getLocation(parsedJson, { dataPath, markIdentifier = false }) { | ||
import { parsePath } from './path.js'; | ||
export function getLocation(parsedJson, options) { | ||
const { jsonAST } = parsedJson; | ||
const path = Array.isArray(dataPath) | ||
? dataPath | ||
: | ||
(dataPath.startsWith('.') | ||
? dataPath.slice(1) | ||
: dataPath) | ||
.split('.') | ||
.filter(val => val); | ||
const { markIdentifier = false } = options; | ||
const path = parsePath(options); | ||
const pathAsString = () => path.join('.'); | ||
@@ -12,0 +7,0 @@ const getParentPath = (index) => '.' + path.slice(0, index).join('.'); |
{ | ||
"name": "jsonpos", | ||
"version": "2.0.2", | ||
"version": "3.0.0", | ||
"description": "Get the textual position of a property in a JSON text", | ||
@@ -5,0 +5,0 @@ "author": "Gustaf Räntilä", |
@@ -23,3 +23,3 @@ [![npm version][npm-image]][npm-url] | ||
The position of `foo.bar` (or `["foo", "bar"]` if provided as an array), is: | ||
The position of `/foo/bar` (or `["foo", "bar"]` if provided as an array), is: | ||
```js | ||
@@ -43,4 +43,9 @@ { | ||
* Since v2 this is a [pure ESM][pure-esm] package, and requires Node.js >=12.20 | ||
* Since v3 the API has changed. The `dataPath` option has been renamed with changed semantics. | ||
* Dot-based (string) `dataPath` is now `dotPath`. **It's not recommended to use as it's not safe for certain characters**. | ||
* Also, it now requires an initial `.`. Only the path `.` represents the root object. | ||
* Array-based `dataPath` is now simply `path`. | ||
* An empty object represents the root object, like in v2. | ||
* New slash-based (string) `pointerPath` is allowed, following JSON Pointer encoding. | ||
# Simple usage | ||
@@ -56,4 +61,8 @@ | ||
interface LocationOptions | ||
dataPath: string | Array< string | number >; | ||
markIdentifier?: boolean; | ||
// Only one of the following | ||
dotPath: string; | ||
path: Array< string | number >; | ||
pointerPath: string; | ||
} | ||
@@ -82,3 +91,3 @@ ``` | ||
### As textual path: | ||
### As dot-separated textual path: | ||
@@ -90,6 +99,21 @@ ```ts | ||
'{ "foo": { "bar": "baz" } }', | ||
{ dataPath: 'foo.bar' } | ||
{ dotPath: 'foo.bar' } | ||
); | ||
``` | ||
*Note that this method is strongly advised against.* | ||
### As /-separated textual path: | ||
```ts | ||
import { jsonpos } from 'jsonpos' | ||
const loc = jsonpos( | ||
'{ "foo": { "bar": "baz" } }', | ||
{ pointerPath: 'foo/bar' } | ||
); | ||
``` | ||
### As array path: | ||
@@ -102,3 +126,3 @@ | ||
'{ "foo": { "bar": "baz" } }', | ||
{ dataPath: [ 'foo', 'bar' ] } | ||
{ path: [ 'foo', 'bar' ] } | ||
); | ||
@@ -163,3 +187,3 @@ ``` | ||
const ast = getAstByString( '{ "foo": "bar" }' ); | ||
const loc = getLocation( ast, { dataPath: 'foo' } ); | ||
const loc = getLocation( ast, { pointerPath: '/foo' } ); | ||
``` | ||
@@ -166,0 +190,0 @@ |
14661
11
185
194