|
| 1 | +const _stringRegex = /string/; |
| 2 | + |
| 3 | +const _replaceString = (type) => |
| 4 | + _stringRegex.test(type) ? '"__par__"' : "__par__"; |
| 5 | + |
| 6 | +const _isLastRegex = /^("}|})/; |
| 7 | + |
| 8 | +// 3 possibilities after arbitrary property: |
| 9 | +// - ", => non-last string property |
| 10 | +// - , => non-last non-string property |
| 11 | +// - " => last string property |
| 12 | +const _matchStartRe = /^(\"\,|\,|\")/; |
| 13 | + |
1 | 14 | /** |
2 | 15 | * @param {string} str - prepared string already validated. |
3 | 16 | * @param {array} queue - queue containing the property name to match |
4 | 17 | * (used for building dynamic regex) needed for the preparation of |
5 | 18 | * chunks used in different scenarios. |
6 | 19 | */ |
7 | | -export default (str, queue) => str |
8 | | - // Matching prepared properties and replacing with target with or without |
9 | | - // double quotes. |
10 | | - // => Avoiding unnecessary concatenation of doublequotes during serialization. |
11 | | - .replace(/"\w+__sjs"/gm, type => (/string/.test(type) ? '"__par__"' : '__par__')) |
12 | | - .split('__par__') |
13 | | - .map((chunk, index, chunks) => { |
| 20 | +const _makeChunks = (str, queue) => { |
| 21 | + const chunks = str |
| 22 | + // Matching prepared properties and replacing with target with or without |
| 23 | + // double quotes. |
| 24 | + // => Avoiding unnecessary concatenation of doublequotes during serialization. |
| 25 | + .replace(/"\w+__sjs"/gm, _replaceString) |
| 26 | + .split("__par__"), |
| 27 | + result = []; |
| 28 | + |
| 29 | + for (let i = 0; i < chunks.length; ++i) { |
| 30 | + const chunk = chunks[i]; |
| 31 | + |
14 | 32 | // Using dynamic regex to ensure that only the correct property |
15 | 33 | // at the end of the string it's actually selected. |
16 | 34 | // => e.g. ,"a":{"a": => ,"a":{ |
17 | | - const matchProp = `("${(queue[index] || {}).name}":(\"?))$`; |
18 | | - const matchWhenLast = `(\,?)${matchProp}`; |
| 35 | + const matchProp = `("${(queue[i] || {}).name}":(\"?))$`; |
19 | 36 |
|
20 | 37 | // Check if current chunk is the last one inside a nested property |
21 | | - const isLast = /^("}|})/.test(chunks[index + 1] || ''); |
| 38 | + const isLast = _isLastRegex.test(chunks[i + 1] || ""); |
22 | 39 |
|
23 | 40 | // If the chunk is the last one the `isUndef` case should match |
24 | 41 | // the preceding comma too. |
25 | | - const matchPropRe = new RegExp(isLast ? matchWhenLast : matchProp); |
26 | | - |
27 | | - // 3 possibilities after arbitrary property: |
28 | | - // - ", => non-last string property |
29 | | - // - , => non-last non-string property |
30 | | - // - " => last string property |
31 | | - const matchStartRe = /^(\"\,|\,|\")/; |
| 42 | + const matchPropRe = new RegExp(isLast ? `(\,?)${matchProp}` : matchProp); |
32 | 43 |
|
33 | | - return { |
| 44 | + result.push({ |
34 | 45 | // notify that the chunk preceding the current one has not |
35 | 46 | // its corresponding property undefined. |
36 | 47 | // => This is a V8 optimization as it's way faster writing |
37 | 48 | // the value of a property than writing the entire property. |
38 | 49 | flag: false, |
39 | 50 | pure: chunk, |
40 | 51 | // Without initial part |
41 | | - prevUndef: chunk.replace(matchStartRe, ''), |
| 52 | + prevUndef: chunk.replace(_matchStartRe, ""), |
42 | 53 | // Without property chars |
43 | | - isUndef: chunk.replace(matchPropRe, ''), |
| 54 | + isUndef: chunk.replace(matchPropRe, ""), |
44 | 55 | // Only remaining chars (can be zero chars) |
45 | | - bothUndef: chunk |
46 | | - .replace(matchStartRe, '') |
47 | | - .replace(matchPropRe, ''), |
48 | | - }; |
49 | | - }); |
| 56 | + bothUndef: chunk.replace(_matchStartRe, "").replace(matchPropRe, ""), |
| 57 | + }); |
| 58 | + } |
| 59 | + |
| 60 | + return result; |
| 61 | +}; |
| 62 | + |
| 63 | +export { _makeChunks }; |
0 commit comments