locize-cli 10.3.2 → 10.4.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.
package/unflatten.js CHANGED
@@ -1,77 +1,77 @@
1
1
  module.exports = (data, testNatural) => {
2
- const result = {};
3
- const shouldConvertArray = {};
2
+ const result = {}
3
+ const shouldConvertArray = {}
4
4
  for (const i in data) {
5
- let keys = [];
5
+ let keys = []
6
6
  if (testNatural && /( |,|\?)/.test(i)) {
7
- keys = [i];
7
+ keys = [i]
8
8
  } else {
9
- keys = i.split('.');
9
+ keys = i.split('.')
10
10
  }
11
11
  keys.reduce((r, e, j) => {
12
- const isNumber = !isNaN(Number(keys[j + 1]));
13
- const hasLeadingZero = isNumber && keys[j + 1].length > 1 && keys[j + 1][0] === '0';
14
- const tooHighNumberToBeAnArrayIndex = isNumber && Number(keys[j + 1]) > 1000;
12
+ const isNumber = !isNaN(Number(keys[j + 1]))
13
+ const hasLeadingZero = isNumber && keys[j + 1].length > 1 && keys[j + 1][0] === '0'
14
+ const tooHighNumberToBeAnArrayIndex = isNumber && Number(keys[j + 1]) > 1000
15
15
  // if assumed to be an array, but now see a key wih non number value => transform to an object
16
16
  if (Array.isArray(r[e]) && (!isNumber || hasLeadingZero || tooHighNumberToBeAnArrayIndex)) {
17
17
  r[e] = r[e].reduce((mem, item, ind) => {
18
- mem[ind] = item;
19
- return mem;
20
- }, {});
18
+ mem[ind] = item
19
+ return mem
20
+ }, {})
21
21
  }
22
22
  if (Array.isArray(r[e]) && r[e].length > 50) {
23
- const base = i.substring(0, i.indexOf(e) + e.length);
23
+ const base = i.substring(0, i.indexOf(e) + e.length)
24
24
  if (Object.values(r[e]).length < (r[e].length / 2)) {
25
- shouldConvertArray[base] = true;
25
+ shouldConvertArray[base] = true
26
26
  } else if (shouldConvertArray[base]) {
27
- delete shouldConvertArray[base];
27
+ delete shouldConvertArray[base]
28
28
  }
29
29
  }
30
30
  if (typeof r === 'string') {
31
31
  if (e === '' && keys.length > 2) {
32
- const lastPart = keys[keys.length - 2] + '.';
33
- const firstParts = keys.slice(0, keys.length - 2);
34
- let obj;
32
+ const lastPart = keys[keys.length - 2] + '.'
33
+ const firstParts = keys.slice(0, keys.length - 2)
34
+ let obj
35
35
  try {
36
- obj = firstParts.reduce((acc, p) => acc[p], result);
36
+ obj = firstParts.reduce((acc, p) => acc[p], result)
37
37
  } catch (err) {
38
- if (firstParts.indexOf('') < 0) throw err;
38
+ if (firstParts.indexOf('') < 0) throw err
39
39
 
40
- let navRes = result;
40
+ let navRes = result
41
41
  for (let ind = 0; ind < firstParts.length; ind++) {
42
- const p = firstParts[ind];
42
+ const p = firstParts[ind]
43
43
  if (typeof navRes[p] === 'object') {
44
- navRes = navRes[p];
44
+ navRes = navRes[p]
45
45
  } else {
46
- const rest = firstParts.slice(ind).map((c) => (c === '' ? '.' : c)).join('.');
47
- navRes[rest] = data[i];
48
- break;
46
+ const rest = firstParts.slice(ind).map((c) => (c === '' ? '.' : c)).join('.')
47
+ navRes[rest] = data[i]
48
+ break
49
49
  }
50
50
  }
51
51
  }
52
52
  if (obj && typeof obj !== 'string' && obj[lastPart] === undefined) {
53
- obj[lastPart] = data[i];
53
+ obj[lastPart] = data[i]
54
54
  }
55
55
  }
56
- return r;
56
+ return r
57
57
  }
58
- return r[e] || (r[e] = (!isNumber || hasLeadingZero || tooHighNumberToBeAnArrayIndex) ? (keys.length - 1 === j ? data[i] : {}) : []);
59
- }, result);
58
+ return r[e] || (r[e] = (!isNumber || hasLeadingZero || tooHighNumberToBeAnArrayIndex) ? (keys.length - 1 === j ? data[i] : {}) : [])
59
+ }, result)
60
60
  }
61
- const arrsToConvert = Object.keys(shouldConvertArray);
61
+ const arrsToConvert = Object.keys(shouldConvertArray)
62
62
  arrsToConvert.forEach((arrToConvert) => {
63
- const parts = arrToConvert.split('.');
64
- let pr = result;
63
+ const parts = arrToConvert.split('.')
64
+ let pr = result
65
65
  parts.forEach((part, ind) => {
66
66
  if (ind === parts.length - 1 && Array.isArray(pr[part])) {
67
67
  pr[part] = pr[part].reduce((mem, item, ind) => {
68
- mem[ind] = item;
69
- return mem;
70
- }, {});
68
+ mem[ind] = item
69
+ return mem
70
+ }, {})
71
71
  } else {
72
- pr = pr[part];
72
+ pr = pr[part]
73
73
  }
74
- });
75
- });
76
- return result;
77
- };
74
+ })
75
+ })
76
+ return result
77
+ }