31 lines
657 B
JavaScript
31 lines
657 B
JavaScript
|
// @ts-check
|
||
|
|
||
|
/**
|
||
|
* A node path is an array of indices that can traverse a Node tree.
|
||
|
*
|
||
|
* For example, given the following tree:
|
||
|
*
|
||
|
* ```javascript
|
||
|
* {
|
||
|
* name: "top",
|
||
|
* children: [
|
||
|
* { name: "first" },
|
||
|
* { name: "second", children: [{ name: "grandchild" }] },
|
||
|
* ],
|
||
|
* }
|
||
|
* ```
|
||
|
*
|
||
|
* `[]` is "top", `[0]` is "first", `[1]` is "second", and `[1, 0]` is "grandchild".
|
||
|
*
|
||
|
* @typedef {number[]} NodePath
|
||
|
*/
|
||
|
|
||
|
/**
|
||
|
* @param {unknown} value
|
||
|
* @returns {null | NodePath} The parsed node path, or null if invalid.
|
||
|
*/
|
||
|
export const parse = (value) => {
|
||
|
if (typeof value !== "string") return null;
|
||
|
return value.split(".").map(Number);
|
||
|
};
|