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

i18next / i18next / #11965

08 Jun 2023 07:01AM UTC coverage: 56.318% (-38.9%) from 95.213%
#11965

push

web-flow
Redesign `t` function types (#1911)

* Redesign t function types

* Add extra tests for t function and fix interpolation types

* Bump typescript version

574 of 1535 branches covered (37.39%)

517 of 918 relevant lines covered (56.32%)

24.67 hits per line

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

56.25
/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) {
×
24
  a.forEach((m) => {
×
25
    if (s[m]) t[m] = s[m];
×
26
  });
27
}
×
28

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

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

8✔
38
  const stack = typeof path !== 'string' ? [].concat(path) : path.split('.');
56✔
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();
359!
44
    // prevent prototype pollution
45
    if (Object.prototype.hasOwnProperty.call(object, key)) {
46
      object = object[key];
377✔
47
    } else {
48
      object = {};
117!
49
    }
117✔
50
  }
260!
51

260✔
52
  if (canNotTraverseDeeper()) return {};
260!
53
  return {
54
    obj: object,
260✔
55
    k: cleanKey(stack.shift()),
196✔
56
  };
57
}
64✔
58

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

99✔
62
  obj[k] = newValue;
63
}
64

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

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

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

×
76
  if (!obj) return undefined;
×
77
  return obj[k];
×
78
}
×
79

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

×
89
export function deepExtend(target, source, overwrite) {
×
90
  /* eslint no-restricted-syntax: 0 */
×
91
  for (const prop in source) {
92
    if (prop !== '__proto__' && prop !== 'constructor') {
93
      if (prop in target) {
×
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 ||
×
98
          typeof source[prop] === 'string' ||
×
99
          source[prop] instanceof String
×
100
        ) {
101
          if (overwrite) target[prop] = source[prop];
×
102
        } else {
×
103
          deepExtend(target[prop], source[prop], overwrite);
104
        }
×
105
      } else {
106
        target[prop] = source[prop];
107
      }
×
108
    }
109
  }
110
  return target;
111
}
×
112

113
export function regexEscape(str) {
114
  /* eslint no-useless-escape: 0 */
115
  return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&');
16✔
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') {
×
131
    return data.replace(/[&<>"'\/]/g, (s) => _entityMap[s]);
×
132
  }
×
133

134
  return data;
135
}
×
136

137
export const isIE10 =
1!
138
  typeof window !== 'undefined' &&
139
  window.navigator && // https://github.com/i18next/i18next/issues/1778
140
  typeof window.navigator.userAgentData === 'undefined' &&
1✔
141
  window.navigator.userAgent &&
1✔
142
  window.navigator.userAgent.indexOf('MSIE') > -1;
143

103!
144
const chars = [' ', ',', '?', '!', ';'];
103✔
145
export function looksLikeObjectPath(key, nsSeparator, keySeparator) {
103✔
146
  nsSeparator = nsSeparator || '';
515✔
147
  keySeparator = keySeparator || '';
148
  const possibleChars = chars.filter(
103!
149
    (c) => nsSeparator.indexOf(c) < 0 && keySeparator.indexOf(c) < 0,
103✔
150
  );
515✔
151
  if (possibleChars.length === 0) return true;
152
  const r = new RegExp(`(${possibleChars.map((c) => (c === '?' ? '\\?' : c)).join('|')})`);
103✔
153
  let matched = !r.test(key);
103!
154
  if (!matched) {
×
155
    const ki = key.indexOf(keySeparator);
×
156
    if (ki > 0 && !r.test(key.substring(0, ki))) {
×
157
      matched = true;
158
    }
159
  }
103✔
160
  return matched;
161
}
162

76!
163
export function deepFind(obj, path, keySeparator = '.') {
76✔
164
  if (!obj) return undefined;
48!
165
  if (obj[path]) return obj[path];
48✔
166
  const paths = path.split(keySeparator);
48✔
167
  let current = obj;
48✔
168
  for (let i = 0; i < paths.length; ++i) {
48!
169
    if (!current) return undefined;
48✔
170
    if (typeof current[paths[i]] === 'string' && i + 1 < paths.length) {
18✔
171
      return undefined;
172
    }
30!
173
    if (current[paths[i]] === undefined) {
30✔
174
      let j = 2;
30✔
175
      let p = paths.slice(i, i + j).join(keySeparator);
30✔
176
      let mix = current[p];
30✔
177
      while (mix === undefined && paths.length > i + j) {
×
178
        j++;
×
179
        p = paths.slice(i, i + j).join(keySeparator);
×
180
        mix = current[p];
181
      }
30!
182
      if (mix === undefined) return undefined;
×
183
      if (mix === null) return null;
×
184
      if (path.endsWith(p)) {
×
185
        if (typeof mix === 'string') return mix;
×
186
        if (p && typeof mix[p] === 'string') return mix[p];
187
      }
×
188
      const joinedPath = paths.slice(i + j).join(keySeparator);
×
189
      if (joinedPath) return deepFind(mix, joinedPath, keySeparator);
×
190
      return undefined;
191
    }
×
192
    current = current[paths[i]];
193
  }
×
194
  return current;
195
}
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