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

i18next / i18next / 19837025733

01 Dec 2025 08:47PM UTC coverage: 95.348% (+11.4%) from 83.922%
19837025733

push

github

adrai
TS: remove wrong signature #2372

1072 of 1172 branches covered (91.47%)

2480 of 2601 relevant lines covered (95.35%)

1091.51 hits per line

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

99.24
/src/ResourceStore.js
1
import EventEmitter from './EventEmitter.js';
2✔
2
import { getPath, deepFind, setPath, deepExtend, isString } from './utils.js';
2✔
3

4
class ResourceStore extends EventEmitter {
2✔
5
  constructor(data, options = { ns: ['translation'], defaultNS: 'translation' }) {
2✔
6
    super();
277✔
7

8
    this.data = data || {};
277✔
9
    this.options = options;
277✔
10
    if (this.options.keySeparator === undefined) {
277✔
11
      this.options.keySeparator = '.';
61✔
12
    }
61✔
13
    if (this.options.ignoreJSONStructure === undefined) {
277✔
14
      this.options.ignoreJSONStructure = true;
104✔
15
    }
104✔
16
  }
277✔
17

18
  addNamespaces(ns) {
2✔
19
    if (this.options.ns.indexOf(ns) < 0) {
10,336✔
20
      this.options.ns.push(ns);
10,119✔
21
    }
10,119✔
22
  }
10,336✔
23

24
  removeNamespaces(ns) {
2✔
25
    const index = this.options.ns.indexOf(ns);
3✔
26
    if (index > -1) {
3✔
27
      this.options.ns.splice(index, 1);
3✔
28
    }
3✔
29
  }
3✔
30

31
  getResource(lng, ns, key, options = {}) {
2✔
32
    const keySeparator =
12,090✔
33
      options.keySeparator !== undefined ? options.keySeparator : this.options.keySeparator;
12,090✔
34

35
    const ignoreJSONStructure =
12,090✔
36
      options.ignoreJSONStructure !== undefined
12,090✔
37
        ? options.ignoreJSONStructure
12,090!
38
        : this.options.ignoreJSONStructure;
12,090✔
39

40
    let path;
12,090✔
41
    if (lng.indexOf('.') > -1) {
12,090✔
42
      path = lng.split('.');
10✔
43
    } else {
12,090✔
44
      path = [lng, ns];
12,080✔
45
      if (key) {
12,080✔
46
        if (Array.isArray(key)) {
1,472!
47
          path.push(...key);
×
48
        } else if (isString(key) && keySeparator) {
1,472✔
49
          path.push(...key.split(keySeparator));
1,258✔
50
        } else {
1,448✔
51
          path.push(key);
214✔
52
        }
214✔
53
      }
1,472✔
54
    }
12,080✔
55

56
    const result = getPath(this.data, path);
12,090✔
57
    if (!result && !ns && !key && lng.indexOf('.') > -1) {
12,090✔
58
      lng = path[0];
3✔
59
      ns = path[1];
3✔
60
      key = path.slice(2).join('.');
3✔
61
    }
3✔
62
    if (result || !ignoreJSONStructure || !isString(key)) return result;
12,090✔
63

64
    return deepFind(this.data?.[lng]?.[ns], key, keySeparator);
12,090✔
65
  }
12,090✔
66

67
  addResource(lng, ns, key, value, options = { silent: false }) {
2✔
68
    const keySeparator =
43✔
69
      options.keySeparator !== undefined ? options.keySeparator : this.options.keySeparator;
43!
70

71
    let path = [lng, ns];
43✔
72
    if (key) path = path.concat(keySeparator ? key.split(keySeparator) : key);
43!
73

74
    if (lng.indexOf('.') > -1) {
43✔
75
      path = lng.split('.');
2✔
76
      value = ns;
2✔
77
      ns = path[1];
2✔
78
    }
2✔
79

80
    this.addNamespaces(ns);
43✔
81

82
    setPath(this.data, path, value);
43✔
83

84
    if (!options.silent) this.emit('added', lng, ns, key, value);
43✔
85
  }
43✔
86

87
  addResources(lng, ns, resources, options = { silent: false }) {
2✔
88
    /* eslint no-restricted-syntax: 0 */
6✔
89
    for (const m in resources) {
6✔
90
      if (isString(resources[m]) || Array.isArray(resources[m]))
14!
91
        this.addResource(lng, ns, m, resources[m], { silent: true });
14✔
92
    }
14✔
93
    if (!options.silent) this.emit('added', lng, ns, resources);
6✔
94
  }
6✔
95

96
  addResourceBundle(
2✔
97
    lng,
10,293✔
98
    ns,
10,293✔
99
    resources,
10,293✔
100
    deep,
10,293✔
101
    overwrite,
10,293✔
102
    options = { silent: false, skipCopy: false },
10,293✔
103
  ) {
10,293✔
104
    let path = [lng, ns];
10,293✔
105
    if (lng.indexOf('.') > -1) {
10,293✔
106
      path = lng.split('.');
3✔
107
      deep = resources;
3✔
108
      resources = ns;
3✔
109
      ns = path[1];
3✔
110
    }
3✔
111

112
    this.addNamespaces(ns);
10,293✔
113

114
    let pack = getPath(this.data, path) || {};
10,293✔
115

116
    if (!options.skipCopy) resources = JSON.parse(JSON.stringify(resources)); // make a copy to fix #2081
10,293✔
117

118
    if (deep) {
10,293✔
119
      deepExtend(pack, resources, overwrite);
21✔
120
    } else {
10,293✔
121
      pack = { ...pack, ...resources };
10,272✔
122
    }
10,272✔
123

124
    setPath(this.data, path, pack);
10,293✔
125

126
    if (!options.silent) this.emit('added', lng, ns, resources);
10,293✔
127
  }
10,293✔
128

129
  removeResourceBundle(lng, ns) {
2✔
130
    if (this.hasResourceBundle(lng, ns)) {
3✔
131
      delete this.data[lng][ns];
3✔
132
    }
3✔
133
    this.removeNamespaces(ns);
3✔
134

135
    this.emit('removed', lng, ns);
3✔
136
  }
3✔
137

138
  hasResourceBundle(lng, ns) {
2✔
139
    return this.getResource(lng, ns) !== undefined;
10,584✔
140
  }
10,584✔
141

142
  getResourceBundle(lng, ns) {
2✔
143
    if (!ns) ns = this.options.defaultNS;
17!
144
    return this.getResource(lng, ns);
17✔
145
  }
17✔
146

147
  getDataByLanguage(lng) {
2✔
148
    return this.data[lng];
615✔
149
  }
615✔
150

151
  hasLanguageSomeTranslations(lng) {
2✔
152
    const data = this.getDataByLanguage(lng);
614✔
153
    const n = (data && Object.keys(data)) || [];
614✔
154
    return !!n.find((v) => data[v] && Object.keys(data[v]).length > 0);
614✔
155
  }
614✔
156

157
  toJSON() {
2✔
158
    return this.data;
3✔
159
  }
3✔
160
}
2✔
161

162
export default ResourceStore;
2✔
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