UNPKG

3.51 kBTypeScriptView Raw
1// tslint:disable:variable-name Describing an API that's defined elsewhere.
2// tslint:disable:no-any describes the API as best we are able today
3
4export {isPath};
5
6
7/**
8 * Returns true if the given string is a structured data path (has dots).
9 *
10 * Example:
11 *
12 * ```
13 * isPath('foo.bar.baz') // true
14 * isPath('foo') // false
15 * ```
16 *
17 * @returns True if the string contained one or more dots
18 */
19declare function isPath(path: string): boolean;
20
21export {root};
22
23
24/**
25 * Returns the root property name for the given path.
26 *
27 * Example:
28 *
29 * ```
30 * root('foo.bar.baz') // 'foo'
31 * root('foo') // 'foo'
32 * ```
33 *
34 * @returns Root property name
35 */
36declare function root(path: string): string;
37
38export {isAncestor};
39
40
41/**
42 * Given `base` is `foo.bar`, `foo` is an ancestor, `foo.bar` is not
43 * Returns true if the given path is an ancestor of the base path.
44 *
45 * Example:
46 *
47 * ```
48 * isAncestor('foo.bar', 'foo') // true
49 * isAncestor('foo.bar', 'foo.bar') // false
50 * isAncestor('foo.bar', 'foo.bar.baz') // false
51 * ```
52 *
53 * @returns True if `path` is an ancestor of `base`.
54 */
55declare function isAncestor(base: string, path: string): boolean;
56
57export {isDescendant};
58
59
60/**
61 * Given `base` is `foo.bar`, `foo.bar.baz` is an descendant
62 *
63 * Example:
64 *
65 * ```
66 * isDescendant('foo.bar', 'foo.bar.baz') // true
67 * isDescendant('foo.bar', 'foo.bar') // false
68 * isDescendant('foo.bar', 'foo') // false
69 * ```
70 *
71 * @returns True if `path` is a descendant of `base`.
72 */
73declare function isDescendant(base: string, path: string): boolean;
74
75export {translate};
76
77
78/**
79 * Replaces a previous base path with a new base path, preserving the
80 * remainder of the path.
81 *
82 * User must ensure `path` has a prefix of `base`.
83 *
84 * Example:
85 *
86 * ```
87 * translate('foo.bar', 'zot', 'foo.bar.baz') // 'zot.baz'
88 * ```
89 *
90 * @returns Translated string
91 */
92declare function translate(base: string, newBase: string, path: string): string;
93
94export {matches};
95
96
97/**
98 * @returns True if `path` is equal to `base`
99 */
100declare function matches(base: string, path: string): boolean;
101
102export {normalize};
103
104
105/**
106 * Converts array-based paths to flattened path. String-based paths
107 * are returned as-is.
108 *
109 * Example:
110 *
111 * ```
112 * normalize(['foo.bar', 0, 'baz']) // 'foo.bar.0.baz'
113 * normalize('foo.bar.0.baz') // 'foo.bar.0.baz'
114 * ```
115 *
116 * @returns Flattened path
117 */
118declare function normalize(path: string|Array<string|number>): string;
119
120export {split};
121
122
123/**
124 * Splits a path into an array of property names. Accepts either arrays
125 * of path parts or strings.
126 *
127 * Example:
128 *
129 * ```
130 * split(['foo.bar', 0, 'baz']) // ['foo', 'bar', '0', 'baz']
131 * split('foo.bar.0.baz') // ['foo', 'bar', '0', 'baz']
132 * ```
133 *
134 * @returns Array of path parts
135 */
136declare function split(path: string|Array<string|number>): string[];
137
138export {get};
139
140
141/**
142 * Reads a value from a path. If any sub-property in the path is `undefined`,
143 * this method returns `undefined` (will never throw.
144 *
145 * @returns Value at path, or `undefined` if the path could not be
146 * fully dereferenced.
147 */
148declare function get(root: object|null, path: string|Array<string|number>, info?: object|null): any;
149
150export {set};
151
152
153/**
154 * Sets a value to a path. If any sub-property in the path is `undefined`,
155 * this method will no-op.
156 *
157 * @returns The normalized version of the input path
158 */
159declare function set(root: object|null, path: string|Array<string|number>, value: any): string|undefined;