Merge pull request #17771 from ikatyang/jsonpath-patch

feat(jsonpath): update to version 0.2.11
This commit is contained in:
Hiroki Horiuchi
2017-07-06 17:30:48 +09:00
committed by GitHub
2 changed files with 66 additions and 42 deletions
+55 -8
View File
@@ -1,18 +1,65 @@
// Type definitions for jsonpath 0.1.3
// Type definitions for jsonpath 0.2.11
// Project: https://www.npmjs.org/package/jsonpath
// Definitions by: Hiroki Horiuchi <https://github.com/horiuchi>
// Definitions by: Hiroki Horiuchi <https://github.com/horiuchi>, Ika <https://github.com/ikatyang>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
type PathComponent = string | number;
export declare function query(obj: any, pathExpression: string): any[];
export declare function paths(obj: any, pathExpression: string): PathComponent[][];
export declare function nodes(obj: any, pathExpression: string): { path: PathComponent[]; value: any; }[];
/**
* Find elements in `obj` matching `pathExpression`. Returns an array of elements that
* satisfy the provided JSONPath expression,or an empty array if none were matched.
* Returns only first `count` elements if specified.
*/
export declare function query(obj: any, pathExpression: string, count?: number): any[];
/**
* Find paths to elements in `obj` matching `pathExpression`. Returns an array of
* element paths that satisfy the provided JSONPath expression. Each path is itself an
* array of keys representing the location within `obj` of the matching element. Returns
* only first `count` paths if specified.
*/
export declare function paths(obj: any, pathExpression: string, count?: number): PathComponent[][];
/**
* Find elements and their corresponding paths in `obj` matching `pathExpression`.
* Returns an array of node objects where each node has a `path` containing an array of
* keys representing the location within `obj`, and a `value` pointing to the matched
* element. Returns only first `count` nodes if specified.
*/
export declare function nodes(obj: any, pathExpression: string, count?: number): { path: PathComponent[]; value: any; }[];
/**
* Returns the value of the first element matching `pathExpression`. If `newValue` is
* provided, sets the value of the first matching element and returns the new value.
*/
export declare function value(obj: any, pathExpression: string): any;
export declare function value(obj: any, pathExpression: string, newValue: any): any;
export declare function value<T>(obj: any, pathExpression: string, newValue: T): T;
/**
* Returns the parent of the first matching element.
*/
export declare function parent(obj: any, pathExpression: string): any;
/**
* Runs the supplied function `fn` on each matching element, and replaces each
* matching element with the return value from the function. The function accepts the
* value of the matching element as its only parameter. Returns matching nodes with
* their updated values.
*/
export declare function apply(obj: any, pathExpression: string, fn: (x: any) => any): { path: PathComponent[]; value: any; }[];
/**
* Parse the provided JSONPath expression into path components and their associated
* operations.
*/
export declare function parse(pathExpression: string): any[];
/**
* Returns a path expression in string form, given a path. The supplied path may either
* be a flat array of keys, as returned by `jp.nodes` for example, or may alternatively be a
* fully parsed path expression in the form of an array of path components as returned
* by `jp.parse`.
*/
export declare function stringify(path: PathComponent[]): string;
export as namespace jsonpath;
+11 -34
View File
@@ -1,54 +1,31 @@
import jp = require('jsonpath');
var data: any;
/**
* jp.query(obj, pathExpression)
* Find elements in obj matching pathExpression. Returns an array of elements that satisfy the provided JSONPath expression, or an empty array if none were matched.
*/
// jp.query()
var authors = jp.query(data, '$..author');
var authors = jp.query(data, '$..author', 2);
/**
* jp.paths(obj, pathExpression)
* Find elements in obj matching pathExpression. Returns an array of element paths that satisfy the provided JSONPath expression. Each path is itself an array of keys representing the location within obj of the matching element.
*/
// jp.paths()
var paths = jp.paths(data, '$..author');
var paths = jp.paths(data, '$..author', 2);
/**
* jp.nodes(obj, pathExpression)
* Find elements and their corresponding paths in obj matching pathExpression. Returns an array of node objects where each node has a path containing an array of keys representing the location within obj, and a value pointing to the matched element.
*/
// jp.nodes()
var nodes = jp.nodes(data, '$..author');
var nodes = jp.nodes(data, '$..author', 2);
/**
* jp.value(obj, pathExpression, [newValue])
* Returns the value of the first element matching pathExpression. If newValue is provided, sets the value of the first matching element and returns the new value.
*/
// jp.value()
var value = jp.value(data, '$.store..price');
jp.value(data, '$.store..price', 12.5);
/**
* jp.parent(obj, pathExpression)
* Returns the parent of the first matching element.
*/
// jp.parent()
var parent = jp.parent(data, '$.store..price');
/**
* jp.apply(obj, pathExpression, fn)
* Runs the supplied function fn on each matching element, and replaces each matching element with the return value from the function. The function accepts the value of the matching element as its only parameter. Returns matching nodes with their updated values.
*/
// jp.apply()
var nodes = jp.apply(data, '$..author', (value: string) => { return value.toUpperCase() });
/**
* jp.parse(pathExpression)
* Parse the provided JSONPath expression into path components and their associated operations.
*/
// jp.parse()
var path = jp.parse('$..author');
/**
* jp.stringify(path)
* Returns a path expression in string form, given a path. The supplied path may either be a flat array of keys, as returned by jp.nodes for example, or may alternatively be a fully parsed path expression in the form of an array of path components as returned by jp.parse.
*/
// jp.stringify()
var pathExpression = jp.stringify(['$', 'store', 'book', 0, 'author']);