-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for dot-prop syntax to resolve #23
- Loading branch information
1 parent
0578816
commit 408d447
Showing
5 changed files
with
66 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
|
||
/** | ||
* Splits a path string into an array of parts. | ||
* | ||
* @example splitPath('a.b.c') // ['a', 'b', 'c'] | ||
* @example splitPath('a\\.b.c') // ['a.b', 'c'] | ||
* @example splitPath('a\\\\.b.c') // ['a\\', 'b', 'c'] | ||
* @example splitPath('a\\\\\\.b.c') // ['a\\.b', 'c'] | ||
* @example splitPath('hello') // ['hello'] | ||
* @example splitPath('hello\\') // ['hello\\'] | ||
* @example splitPath('hello\\\\') // ['hello\\'] | ||
* | ||
* @param {string} str | ||
* @param {string} separator | ||
* @returns {string[]} | ||
*/ | ||
export function splitPath (str, separator = '.', escape = '\\') { | ||
const parts = [] | ||
let current = '' | ||
|
||
for (let i = 0; i < str.length; i++) { | ||
const char = str[i] | ||
if (char === escape) { | ||
if (str[i + 1] === separator) { | ||
current += separator | ||
i++ | ||
} else if (str[i + 1] === escape) { | ||
current += escape | ||
i++ | ||
} else current += escape | ||
} else if (char === separator) { | ||
parts.push(current) | ||
current = '' | ||
} else current += char | ||
} | ||
parts.push(current) | ||
|
||
return parts | ||
} |