|
Last change
on this file since ba17441 was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago |
|
Added visualizations
|
-
Property mode
set to
100644
|
|
File size:
1.8 KB
|
| Line | |
|---|
| 1 | import { toString } from './toString.mjs';
|
|---|
| 2 | import { toKey } from '../_internal/toKey.mjs';
|
|---|
| 3 |
|
|---|
| 4 | function toPath(deepKey) {
|
|---|
| 5 | if (Array.isArray(deepKey)) {
|
|---|
| 6 | return deepKey.map(toKey);
|
|---|
| 7 | }
|
|---|
| 8 | if (typeof deepKey === 'symbol') {
|
|---|
| 9 | return [deepKey];
|
|---|
| 10 | }
|
|---|
| 11 | deepKey = toString(deepKey);
|
|---|
| 12 | const result = [];
|
|---|
| 13 | const length = deepKey.length;
|
|---|
| 14 | if (length === 0) {
|
|---|
| 15 | return result;
|
|---|
| 16 | }
|
|---|
| 17 | let index = 0;
|
|---|
| 18 | let key = '';
|
|---|
| 19 | let quoteChar = '';
|
|---|
| 20 | let bracket = false;
|
|---|
| 21 | if (deepKey.charCodeAt(0) === 46) {
|
|---|
| 22 | result.push('');
|
|---|
| 23 | index++;
|
|---|
| 24 | }
|
|---|
| 25 | while (index < length) {
|
|---|
| 26 | const char = deepKey[index];
|
|---|
| 27 | if (quoteChar) {
|
|---|
| 28 | if (char === '\\' && index + 1 < length) {
|
|---|
| 29 | index++;
|
|---|
| 30 | key += deepKey[index];
|
|---|
| 31 | }
|
|---|
| 32 | else if (char === quoteChar) {
|
|---|
| 33 | quoteChar = '';
|
|---|
| 34 | }
|
|---|
| 35 | else {
|
|---|
| 36 | key += char;
|
|---|
| 37 | }
|
|---|
| 38 | }
|
|---|
| 39 | else if (bracket) {
|
|---|
| 40 | if (char === '"' || char === "'") {
|
|---|
| 41 | quoteChar = char;
|
|---|
| 42 | }
|
|---|
| 43 | else if (char === ']') {
|
|---|
| 44 | bracket = false;
|
|---|
| 45 | result.push(key);
|
|---|
| 46 | key = '';
|
|---|
| 47 | }
|
|---|
| 48 | else {
|
|---|
| 49 | key += char;
|
|---|
| 50 | }
|
|---|
| 51 | }
|
|---|
| 52 | else {
|
|---|
| 53 | if (char === '[') {
|
|---|
| 54 | bracket = true;
|
|---|
| 55 | if (key) {
|
|---|
| 56 | result.push(key);
|
|---|
| 57 | key = '';
|
|---|
| 58 | }
|
|---|
| 59 | }
|
|---|
| 60 | else if (char === '.') {
|
|---|
| 61 | if (key) {
|
|---|
| 62 | result.push(key);
|
|---|
| 63 | key = '';
|
|---|
| 64 | }
|
|---|
| 65 | }
|
|---|
| 66 | else {
|
|---|
| 67 | key += char;
|
|---|
| 68 | }
|
|---|
| 69 | }
|
|---|
| 70 | index++;
|
|---|
| 71 | }
|
|---|
| 72 | if (key) {
|
|---|
| 73 | result.push(key);
|
|---|
| 74 | }
|
|---|
| 75 | return result;
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | export { toPath };
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.