• 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

98.46
/src/ResourceStore.js
1
import EventEmitter from './EventEmitter.js';
1✔
2
import * as utils from './utils.js';
1✔
3

4
class ResourceStore extends EventEmitter {
5
  constructor(data, options = { ns: ['translation'], defaultNS: 'translation' }) {
50✔
6
    super();
50✔
7
    if (utils.isIE10) {
50✔
8
      EventEmitter.call(this); // <=IE10 fix (unable to call parent constructor)
×
9
    }
10

11
    this.data = data || {};
50✔
12
    this.options = options;
50✔
13
    if (this.options.keySeparator === undefined) {
50✔
14
      this.options.keySeparator = '.';
36✔
15
    }
16
  }
17

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

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

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

35
    let path = [lng, ns];
444✔
36
    if (key && typeof key !== 'string') path = path.concat(key);
444✔
37
    if (key && typeof key === 'string')
444✔
38
      path = path.concat(keySeparator ? key.split(keySeparator) : key);
343✔
39

40
    if (lng.indexOf('.') > -1) {
444✔
41
      path = lng.split('.');
5✔
42
    }
43

44
    return utils.getPath(this.data, path);
444✔
45
  }
46

47
  addResource(lng, ns, key, value, options = { silent: false }) {
16✔
48
    let keySeparator = this.options.keySeparator;
16✔
49
    if (keySeparator === undefined) keySeparator = '.';
16✔
50

51
    let path = [lng, ns];
16✔
52
    if (key) path = path.concat(keySeparator ? key.split(keySeparator) : key);
16✔
53

54
    if (lng.indexOf('.') > -1) {
16✔
55
      path = lng.split('.');
2✔
56
      value = ns;
2✔
57
      ns = path[1];
2✔
58
    }
59

60
    this.addNamespaces(ns);
16✔
61

62
    utils.setPath(this.data, path, value);
16✔
63

64
    if (!options.silent) this.emit('added', lng, ns, key, value);
16✔
65
  }
66

67
  addResources(lng, ns, resources, options = { silent: false }) {
4✔
68
    /* eslint no-restricted-syntax: 0 */
69
    for (const m in resources) {
4✔
70
      if (
7✔
71
        typeof resources[m] === 'string' ||
72
        Object.prototype.toString.apply(resources[m]) === '[object Array]'
73
      )
74
        this.addResource(lng, ns, m, resources[m], { silent: true });
7✔
75
    }
76
    if (!options.silent) this.emit('added', lng, ns, resources);
4✔
77
  }
78

79
  addResourceBundle(lng, ns, resources, deep, overwrite, options = { silent: false }) {
14✔
80
    let path = [lng, ns];
14✔
81
    if (lng.indexOf('.') > -1) {
14✔
82
      path = lng.split('.');
3✔
83
      deep = resources;
3✔
84
      resources = ns;
3✔
85
      ns = path[1];
3✔
86
    }
87

88
    this.addNamespaces(ns);
14✔
89

90
    let pack = utils.getPath(this.data, path) || {};
14✔
91

92
    if (deep) {
14✔
93
      utils.deepExtend(pack, resources, overwrite);
4✔
94
    } else {
95
      pack = { ...pack, ...resources };
10✔
96
    }
97

98
    utils.setPath(this.data, path, pack);
14✔
99

100
    if (!options.silent) this.emit('added', lng, ns, resources);
14✔
101
  }
102

103
  removeResourceBundle(lng, ns) {
104
    if (this.hasResourceBundle(lng, ns)) {
2✔
105
      delete this.data[lng][ns];
2✔
106
    }
107
    this.removeNamespaces(ns);
2✔
108

109
    this.emit('removed', lng, ns);
2✔
110
  }
111

112
  hasResourceBundle(lng, ns) {
113
    return this.getResource(lng, ns) !== undefined;
90✔
114
  }
115

116
  getResourceBundle(lng, ns) {
117
    if (!ns) ns = this.options.defaultNS;
5✔
118

119
    // COMPATIBILITY: remove extend in v2.1.0
120
    if (this.options.compatibilityAPI === 'v1') return { ...{}, ...this.getResource(lng, ns) };
5✔
121

122
    return this.getResource(lng, ns);
5✔
123
  }
124

125
  getDataByLanguage(lng) {
126
    return this.data[lng];
1✔
127
  }
128

129
  toJSON() {
130
    return this.data;
2✔
131
  }
132
}
133

134
export default ResourceStore;
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