source: node_modules/es-toolkit/dist/compat/util/toPath.js

Last change on this file was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago

Added visualizations

  • Property mode set to 100644
File size: 1.9 KB
Line 
1'use strict';
2
3Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
5const toString = require('./toString.js');
6const toKey = require('../_internal/toKey.js');
7
8function toPath(deepKey) {
9 if (Array.isArray(deepKey)) {
10 return deepKey.map(toKey.toKey);
11 }
12 if (typeof deepKey === 'symbol') {
13 return [deepKey];
14 }
15 deepKey = toString.toString(deepKey);
16 const result = [];
17 const length = deepKey.length;
18 if (length === 0) {
19 return result;
20 }
21 let index = 0;
22 let key = '';
23 let quoteChar = '';
24 let bracket = false;
25 if (deepKey.charCodeAt(0) === 46) {
26 result.push('');
27 index++;
28 }
29 while (index < length) {
30 const char = deepKey[index];
31 if (quoteChar) {
32 if (char === '\\' && index + 1 < length) {
33 index++;
34 key += deepKey[index];
35 }
36 else if (char === quoteChar) {
37 quoteChar = '';
38 }
39 else {
40 key += char;
41 }
42 }
43 else if (bracket) {
44 if (char === '"' || char === "'") {
45 quoteChar = char;
46 }
47 else if (char === ']') {
48 bracket = false;
49 result.push(key);
50 key = '';
51 }
52 else {
53 key += char;
54 }
55 }
56 else {
57 if (char === '[') {
58 bracket = true;
59 if (key) {
60 result.push(key);
61 key = '';
62 }
63 }
64 else if (char === '.') {
65 if (key) {
66 result.push(key);
67 key = '';
68 }
69 }
70 else {
71 key += char;
72 }
73 }
74 index++;
75 }
76 if (key) {
77 result.push(key);
78 }
79 return result;
80}
81
82exports.toPath = toPath;
Note: See TracBrowser for help on using the repository browser.