From 377eb09ae0399563dbb439c75938663cba8e46c6 Mon Sep 17 00:00:00 2001 From: Ika Date: Wed, 5 Jul 2017 21:15:46 +0800 Subject: [PATCH 1/5] feat(jsonpath): update to version 0.2.11 --- types/jsonpath/index.d.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/types/jsonpath/index.d.ts b/types/jsonpath/index.d.ts index e691f860e1..90f97be2fc 100644 --- a/types/jsonpath/index.d.ts +++ b/types/jsonpath/index.d.ts @@ -1,17 +1,17 @@ -// 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 +// Definitions by: Hiroki Horiuchi , Ika // 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; }[]; +export declare function query(obj: any, pathExpression: string, count?: number): any[]; +export declare function paths(obj: any, pathExpression: string, count?: number): PathComponent[][]; +export declare function nodes(obj: any, pathExpression: string, count?: number): { path: PathComponent[]; value: any; }[]; export declare function value(obj: any, pathExpression: string): any; -export declare function value(obj: any, pathExpression: string, newValue: any): any; +export declare function value(obj: any, pathExpression: string, newValue: T): T; export declare function parent(obj: any, pathExpression: string): any; export declare function apply(obj: any, pathExpression: string, fn: (x: any) => any): { path: PathComponent[]; value: any; }[]; export declare function parse(pathExpression: string): any[]; From 445f5e856c76a032c4a09b4d2430afce9ff2eb4e Mon Sep 17 00:00:00 2001 From: Ika Date: Wed, 5 Jul 2017 21:30:53 +0800 Subject: [PATCH 2/5] docs(jsonpath): add jsdoc descriptions --- types/jsonpath/index.d.ts | 49 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/types/jsonpath/index.d.ts b/types/jsonpath/index.d.ts index 90f97be2fc..b030605a55 100644 --- a/types/jsonpath/index.d.ts +++ b/types/jsonpath/index.d.ts @@ -3,16 +3,61 @@ // Definitions by: Hiroki Horiuchi , Ika // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped - - type PathComponent = string | number; +/** + * 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: 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; From f70d20ac03f3900258332ddd76321a14a5d87f34 Mon Sep 17 00:00:00 2001 From: Ika Date: Wed, 5 Jul 2017 21:37:16 +0800 Subject: [PATCH 3/5] feat(jsonpath): umd compatible --- types/jsonpath/index.d.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/types/jsonpath/index.d.ts b/types/jsonpath/index.d.ts index b030605a55..36a04ef0de 100644 --- a/types/jsonpath/index.d.ts +++ b/types/jsonpath/index.d.ts @@ -61,3 +61,5 @@ export declare function parse(pathExpression: string): any[]; * by `jp.parse`. */ export declare function stringify(path: PathComponent[]): string; + +export as namespace jsonpath; From 5277a228a0e7fd9e4a9d66edd385a2ad89194822 Mon Sep 17 00:00:00 2001 From: Ika Date: Wed, 5 Jul 2017 21:51:36 +0800 Subject: [PATCH 4/5] test(jsonpath): add tests remove unnecessary comments --- types/jsonpath/jsonpath-tests.ts | 44 ++++++++------------------------ 1 file changed, 11 insertions(+), 33 deletions(-) diff --git a/types/jsonpath/jsonpath-tests.ts b/types/jsonpath/jsonpath-tests.ts index 770258a94f..46b87b3a4c 100644 --- a/types/jsonpath/jsonpath-tests.ts +++ b/types/jsonpath/jsonpath-tests.ts @@ -3,52 +3,30 @@ 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']); - From ce902f704382a0855a6e7cca9ce2c24883a6e7e8 Mon Sep 17 00:00:00 2001 From: ikatyang Date: Thu, 6 Jul 2017 00:29:28 +0800 Subject: [PATCH 5/5] style(jsonpath): remove leading blank line --- types/jsonpath/jsonpath-tests.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/types/jsonpath/jsonpath-tests.ts b/types/jsonpath/jsonpath-tests.ts index 46b87b3a4c..a6e805c5e8 100644 --- a/types/jsonpath/jsonpath-tests.ts +++ b/types/jsonpath/jsonpath-tests.ts @@ -1,4 +1,3 @@ - import jp = require('jsonpath'); var data: any;