i18ntk 3.1.2 → 3.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -54,7 +54,7 @@ function collectLeaves(obj, prefix = '') {
54
54
  return entries;
55
55
  }
56
56
 
57
- function setLeaf(obj, keyPath, value) {
57
+ function parseKeyPath(keyPath) {
58
58
  const parts = [];
59
59
  let current = '';
60
60
  for (let i = 0; i < keyPath.length; i++) {
@@ -77,13 +77,18 @@ function setLeaf(obj, keyPath, value) {
77
77
  }
78
78
  }
79
79
  if (current) parts.push(current);
80
+ return parts;
81
+ }
82
+
83
+ function setLeaf(obj, keyPath, value) {
84
+ const parts = parseKeyPath(keyPath);
80
85
 
81
86
  let target = obj;
82
87
  for (let i = 0; i < parts.length - 1; i++) {
83
88
  const part = parts[i];
84
89
  if (part.startsWith('[') && part.endsWith(']')) {
85
90
  const idx = parseInt(part.slice(1, -1), 10);
86
- if (!(idx in target)) target[idx] = {};
91
+ if (!(idx in target)) target[idx] = [];
87
92
  target = target[idx];
88
93
  } else {
89
94
  if (!(part in target)) target[part] = {};
@@ -100,28 +105,7 @@ function setLeaf(obj, keyPath, value) {
100
105
  }
101
106
 
102
107
  function getLeaf(obj, keyPath) {
103
- const parts = [];
104
- let current = '';
105
- for (let i = 0; i < keyPath.length; i++) {
106
- const ch = keyPath[i];
107
- if (ch === '.' && keyPath[i - 1] !== '\\') {
108
- if (current) parts.push(current);
109
- current = '';
110
- } else if (ch === '[') {
111
- if (current) parts.push(current);
112
- current = '';
113
- i++;
114
- while (i < keyPath.length && keyPath[i] !== ']') {
115
- current += keyPath[i];
116
- i++;
117
- }
118
- parts.push(`[${current}]`);
119
- current = '';
120
- } else {
121
- current += ch;
122
- }
123
- }
124
- if (current) parts.push(current);
108
+ const parts = parseKeyPath(keyPath);
125
109
 
126
110
  let target = obj;
127
111
  for (const part of parts) {
@@ -136,7 +120,12 @@ function getLeaf(obj, keyPath) {
136
120
  }
137
121
 
138
122
  function deepClone(obj) {
139
- return JSON.parse(JSON.stringify(obj));
123
+ if (obj === null || obj === undefined) return obj;
124
+ try {
125
+ return JSON.parse(JSON.stringify(obj));
126
+ } catch (e) {
127
+ return obj;
128
+ }
140
129
  }
141
130
 
142
131
  module.exports = {