• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

i18next / i18next / #40818

02 Jan 2024 10:08AM UTC coverage: 92.045% (-0.07%) from 92.112%
#40818

push

adrai
try to fix circleci

1558 of 2443 branches covered (63.77%)

1481 of 1609 relevant lines covered (92.04%)

1239.15 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.43
/src/utils.js
1
// http://lea.verou.me/2016/12/resolve-promises-externally-with-this-one-weird-trick/
2
export function defer() {
3
  let res;
1✔
4
  let rej;
5

6
  const promise = new Promise((resolve, reject) => {
1✔
7
    res = resolve;
1✔
8
    rej = reject;
1✔
9
  });
1✔
10

1✔
11
  promise.resolve = res;
1✔
12
  promise.reject = rej;
1✔
13

1✔
14
  return promise;
1✔
15
}
1✔
16

1✔
17
export function makeString(object) {
1✔
18
  if (object == null) return '';
1✔
19
  /* eslint prefer-template: 0 */
20
  return '' + object;
21
}
22

23
export function copy(a, s, t) {
87✔
24
  a.forEach((m) => {
87✔
25
    if (s[m]) t[m] = s[m];
87✔
26
  });
27
}
87✔
28

87✔
29
function getLastOfPath(object, path, Empty) {
87✔
30
  function cleanKey(key) {
31
    return key && key.indexOf('###') > -1 ? key.replace(/###/g, '.') : key;
32
  }
25✔
33

34
  function canNotTraverseDeeper() {
20✔
35
    return !object || typeof object === 'string';
36
  }
37

69✔
38
  const stack = typeof path !== 'string' ? [].concat(path) : path.split('.');
483✔
39
  while (stack.length > 1) {
40
    if (canNotTraverseDeeper()) return {};
41

42
    const key = cleanKey(stack.shift());
43
    if (!object[key] && Empty) object[key] = new Empty();
73,761!
44
    // prevent prototype pollution
45
    if (Object.prototype.hasOwnProperty.call(object, key)) {
46
      object = object[key];
73,784✔
47
    } else {
48
      object = {};
41,634✔
49
    }
41,634✔
50
  }
32,151✔
51

32,150✔
52
  if (canNotTraverseDeeper()) return {};
32,150✔
53
  return {
54
    obj: object,
32,150✔
55
    k: cleanKey(stack.shift()),
21,767✔
56
  };
57
}
10,383✔
58

59
export function setPath(object, path, newValue) {
60
  const { obj, k } = getLastOfPath(object, path, Object);
41,633✔
61

41,611✔
62
  obj[k] = newValue;
63
}
64

65
export function pushPath(object, path, newValue, concat) {
66
  const { obj, k } = getLastOfPath(object, path, Object);
67

10,160✔
68
  obj[k] = obj[k] || [];
10,160✔
69
  if (concat) obj[k] = obj[k].concat(newValue);
10,160✔
70
  if (!concat) obj[k].push(newValue);
10,160✔
71
}
72

73
export function getPath(object, path) {
10,133✔
74
  const { obj, k } = getLastOfPath(object, path);
10,133✔
75

10,133✔
76
  if (!obj) return undefined;
10,133✔
77
  return obj[k];
10,133!
78
}
10,133!
79

80
export function getPathWithDefaults(data, defaultData, key) {
81
  const value = getPath(data, key);
21,341✔
82
  if (value !== undefined) {
21,341✔
83
    return value;
21,341✔
84
  }
21,341✔
85
  // Fallback to default values
21,318✔
86
  return getPath(defaultData, key);
87
}
88

189✔
89
export function deepExtend(target, source, overwrite) {
189✔
90
  /* eslint no-restricted-syntax: 0 */
175✔
91
  for (const prop in source) {
92
    if (prop !== '__proto__' && prop !== 'constructor') {
93
      if (prop in target) {
14✔
94
        // If we reached a leaf string in target or source then replace with source or skip depending on the 'overwrite' switch
95
        if (
96
          typeof target[prop] === 'string' ||
97
          target[prop] instanceof String ||
6✔
98
          typeof source[prop] === 'string' ||
8✔
99
          source[prop] instanceof String
6✔
100
        ) {
101
          if (overwrite) target[prop] = source[prop];
2!
102
        } else {
2✔
103
          deepExtend(target[prop], source[prop], overwrite);
104
        }
×
105
      } else {
106
        target[prop] = source[prop];
107
      }
4✔
108
    }
109
  }
110
  return target;
111
}
6✔
112

113
export function regexEscape(str) {
114
  /* eslint no-useless-escape: 0 */
115
  return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&');
332✔
116
}
117

118
/* eslint-disable */
119
var _entityMap = {
1✔
120
  '&': '&',
121
  '<': '&lt;',
122
  '>': '&gt;',
123
  '"': '&quot;',
124
  "'": '&#39;',
125
  '/': '&#x2F;',
126
};
127
/* eslint-enable */
128

129
export function escape(data) {
130
  if (typeof data === 'string') {
143!
131
    return data.replace(/[&<>"'\/]/g, (s) => _entityMap[s]);
143✔
132
  }
23✔
133

134
  return data;
135
}
×
136

137
const chars = [' ', ',', '?', '!', ';'];
1✔
138
export function looksLikeObjectPath(key, nsSeparator, keySeparator) {
139
  nsSeparator = nsSeparator || '';
811✔
140
  keySeparator = keySeparator || '';
811✔
141
  const possibleChars = chars.filter(
811✔
142
    (c) => nsSeparator.indexOf(c) < 0 && keySeparator.indexOf(c) < 0,
4,055✔
143
  );
144
  if (possibleChars.length === 0) return true;
811!
145
  const r = new RegExp(`(${possibleChars.map((c) => (c === '?' ? '\\?' : c)).join('|')})`);
811✔
146
  let matched = !r.test(key);
4,055✔
147
  if (!matched) {
148
    const ki = key.indexOf(keySeparator);
811✔
149
    if (ki > 0 && !r.test(key.substring(0, ki))) {
811✔
150
      matched = true;
50✔
151
    }
50✔
152
  }
4✔
153
  return matched;
154
}
155

811✔
156
export function deepFind(obj, path, keySeparator = '.') {
157
  if (!obj) return undefined;
158
  if (obj[path]) return obj[path];
280!
159
  const paths = path.split(keySeparator);
280✔
160
  let current = obj;
168✔
161
  for (let i = 0; i < paths.length; ++i) {
159✔
162
    if (!current) return undefined;
159✔
163
    if (typeof current[paths[i]] === 'string' && i + 1 < paths.length) {
159✔
164
      return undefined;
176!
165
    }
176✔
166
    if (current[paths[i]] === undefined) {
18✔
167
      let j = 2;
168
      let p = paths.slice(i, i + j).join(keySeparator);
158✔
169
      let mix = current[p];
122✔
170
      while (mix === undefined && paths.length > i + j) {
122✔
171
        j++;
122✔
172
        p = paths.slice(i, i + j).join(keySeparator);
122✔
173
        mix = current[p];
1✔
174
      }
1✔
175
      if (mix === undefined) return undefined;
1✔
176
      if (mix === null) return null;
177
      if (path.endsWith(p)) {
122✔
178
        if (typeof mix === 'string') return mix;
11!
179
        if (p && typeof mix[p] === 'string') return mix[p];
11✔
180
      }
2!
181
      const joinedPath = paths.slice(i + j).join(keySeparator);
×
182
      if (joinedPath) return deepFind(mix, joinedPath, keySeparator);
183
      return undefined;
9✔
184
    }
9!
185
    current = current[paths[i]];
×
186
  }
187
  return current;
36✔
188
}
189

19✔
190
export function getCleanedCode(code) {
191
  if (code && code.indexOf('_') > 0) return code.replace('_', '-');
192
  return code;
447!
193
}
447✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc