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

i18next / i18next / #51577

25 Oct 2020 10:57AM UTC coverage: 92.982% (+4.6%) from 88.413%
#51577

push

adrai
19.8.3

954 of 1026 relevant lines covered (92.98%)

79.45 hits per line

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

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

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

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

14
  return promise;
38✔
15
}
16

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

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

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

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

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

42
    const key = cleanKey(stack.shift());
891✔
43
    if (!object[key] && Empty) object[key] = new Empty();
891✔
44
    object = object[key];
891✔
45
  }
46

47
  if (canNotTraverseDeeper()) return {};
550✔
48
  return {
515✔
49
    obj: object,
50
    k: cleanKey(stack.shift()),
51
  };
52
}
53

54
export function setPath(object, path, newValue) {
30✔
55
  const { obj, k } = getLastOfPath(object, path, Object);
56

57
  obj[k] = newValue;
30✔
58
}
59

60
export function pushPath(object, path, newValue, concat) {
9✔
61
  const { obj, k } = getLastOfPath(object, path, Object);
62

63
  obj[k] = obj[k] || [];
9✔
64
  if (concat) obj[k] = obj[k].concat(newValue);
9✔
65
  if (!concat) obj[k].push(newValue);
9✔
66
}
67

68
export function getPath(object, path) {
577✔
69
  const { obj, k } = getLastOfPath(object, path);
70

71
  if (!obj) return undefined;
577✔
72
  return obj[k];
476✔
73
}
74

75
export function getPathWithDefaults(data, defaultData, key) {
1✔
76
  const value = getPath(data, key);
104✔
77
  if (value !== undefined) {
104✔
78
    return value;
89✔
79
  }
80
  // Fallback to default values
81
  return getPath(defaultData, key);
15✔
82
}
83

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

108
export function regexEscape(str) {
1✔
109
  /* eslint no-useless-escape: 0 */
110
  return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&');
142✔
111
}
112

113
/* eslint-disable */
114
var _entityMap = {
1✔
115
  '&': '&',
116
  '<': '&lt;',
117
  '>': '&gt;',
118
  '"': '&quot;',
119
  "'": '&#39;',
120
  '/': '&#x2F;',
121
};
122
/* eslint-enable */
123

124
export function escape(data) {
1✔
125
  if (typeof data === 'string') {
74✔
126
    return data.replace(/[&<>"'\/]/g, s => _entityMap[s]);
74✔
127
  }
128

129
  return data;
×
130
}
131

132
export const isIE10 =
1✔
133
  typeof window !== 'undefined' &&
134
  window.navigator &&
135
  window.navigator.userAgent &&
136
  window.navigator.userAgent.indexOf('MSIE') > -1;
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc