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

i18next / i18next / #81340

05 Jan 2016 06:59PM UTC coverage: 67.771% (-23.4%) from 91.134%
#81340

push

jamuhl
merged

1 of 1 new or added line in 1 file covered. (100.0%)

161 existing lines in 11 files now uncovered.

532 of 785 relevant lines covered (67.77%)

16.75 hits per line

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

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

4
class ResourceStore extends EventEmitter {
1✔
5
  constructor(data = {}, options = {ns: ['translation'], defaultNS: 'translation'}) {
17✔
6
    super();
17✔
7
    this.data = data;
17✔
8
    this.options = options;
17✔
9
  }
1✔
10

11
  addNamespaces(ns) {
12
    if (this.options.ns.indexOf(ns) < 0) {
7✔
13
      this.options.ns.push(ns);
1✔
14
    }
15
  }
16

17
  removeNamespaces(ns) {
18
    var index = this.options.ns.indexOf(ns);
1✔
19
    if (index > -1) {
1✔
20
      this.options.ns.splice(index, 1);
1✔
21
    }
22
  }
23

24
  getResource(lng, ns, key, options = {}) {
89✔
25
    let keySeparator = options.keySeparator || this.options.keySeparator || '.';
89✔
26

27
    let path = [lng, ns];
89✔
28
    if (key && typeof key !== 'string') path = path.concat(key);
89✔
29
    if (key && typeof key === 'string') path = path.concat(key.split(keySeparator));
89✔
30

31
    if (lng.indexOf('.') > -1) {
89✔
32
      path = lng.split('.');
4✔
33
    }
34

35
    return utils.getPath(this.data, path);
89✔
36
  }
37

38
  addResource(lng, ns, key, value, options = { silent: false }) {
4✔
39
    let path = [lng, ns];
4✔
40
    if (key) path = path.concat(key.split(this.options.keySeparator || '.'));
4✔
41

42
    if (lng.indexOf('.') > -1) {
4✔
43
      path = lng.split('.');
2✔
44
      value = ns;
2✔
45
      ns = path[1];
2✔
46
    }
47

48
    this.addNamespaces(ns);
4✔
49

50
    utils.setPath(this.data, path, value);
4✔
51

52
    if (!options.silent) this.emit('added', lng, ns);
4✔
53
  }
54

55
  addResources(lng, ns, resources) {
UNCOV
56
    for (var m in resources) {
×
UNCOV
57
      if (typeof resources[m] === 'string') this.addResource(lng, ns, m, resources[m], { silent: true });
×
58
    }
UNCOV
59
    this.emit('added', lng, ns);
×
60
  }
61

62
  addResourceBundle(lng, ns, resources, deep, overwrite) {
63
    let path = [lng, ns];
3✔
64
    if (lng.indexOf('.') > -1) {
3✔
65
      path = lng.split('.');
1✔
66
      deep = resources;
1✔
67
      resources = ns;
1✔
68
      ns = path[1];
1✔
69
    }
70

71
    this.addNamespaces(ns);
3✔
72

73
    let pack = utils.getPath(this.data, path) || {};
3✔
74

75
    if (deep) {
3✔
UNCOV
76
      utils.deepExtend(pack, resources, overwrite);
×
77
    } else {
78
      pack = {...pack, ...resources};
3✔
79
    }
80

81
    utils.setPath(this.data, path, pack);
3✔
82

83
    this.emit('added', lng, ns);
3✔
84
  }
85

86
  removeResourceBundle(lng, ns) {
87
    if (this.hasResourceBundle(lng, ns)) {
1✔
88
      delete this.data[lng][ns];
1✔
89
    }
90
    this.removeNamespaces(ns);
1✔
91

92
    this.emit('removed', lng, ns);
1✔
93
  }
94

95
  hasResourceBundle(lng, ns) {
96
    return this.getResource(lng, ns) !== undefined;
4✔
97
  }
98

99
  getResourceBundle(lng, ns) {
100
    if (!ns) ns = this.options.defaultNS;
3✔
101

102
    // TODO: COMPATIBILITY remove extend in v2.1.0
103
    if (this.options.compatibilityAPI === 'v1') return {...{}, ...this.getResource(lng, ns)};
3✔
104

105
    return this.getResource(lng, ns);
3✔
106
  }
107

108
  toJSON() {
109
    return this.data;
2✔
110
  }
111
}
112

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