• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In
Build has been canceled!

i18next / i18next / #16043

11 Apr 2017 02:06PM UTC coverage: 66.069% (+0.3%) from 65.798%
#16043

push

jamuhl
update build

590 of 893 relevant lines covered (66.07%)

24.95 hits per line

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

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

4
class ResourceStore extends EventEmitter {
5
  constructor(data = {}, options = {ns: ['translation'], defaultNS: 'translation'}) {
20✔
6
    super();
7
    this.data = data;
20✔
8
    this.options = options;
20✔
9
  }
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 = {}) {
125✔
25
    let keySeparator = options.keySeparator || this.options.keySeparator;
125✔
26
    if (keySeparator === undefined) keySeparator = '.';
125✔
27

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

125✔
32
    if (lng.indexOf('.') > -1) {
113✔
33
      path = lng.split('.');
113✔
34
    }
113✔
35

36
    return utils.getPath(this.data, path);
37
  }
38

68✔
39
  addResource(lng, ns, key, value, options = { silent: false }) {
68✔
40
    let keySeparator = this.options.keySeparator;
56✔
41
    if (keySeparator === undefined) keySeparator = '.';
53✔
42

43
    let path = [lng, ns];
3✔
44
    if (key) path = path.concat(keySeparator ? key.split(keySeparator) : key);
45

46
    if (lng.indexOf('.') > -1) {
47
      path = lng.split('.');
68✔
48
      value = ns;
4✔
49
      ns = path[1];
50
    }
51

68✔
52
    this.addNamespaces(ns);
53

54
    utils.setPath(this.data, path, value);
4✔
55

4✔
56
    if (!options.silent) this.emit('added', lng, ns, key, value);
4✔
57
  }
58

4✔
59
  addResources(lng, ns, resources) {
4✔
60
    for (var m in resources) {
61
      if (typeof resources[m] === 'string') this.addResource(lng, ns, m, resources[m], { silent: true });
4✔
62
    }
2✔
63
    this.emit('added', lng, ns, resources);
2✔
64
  }
2✔
65

66
  addResourceBundle(lng, ns, resources, deep, overwrite) {
67
    let path = [lng, ns];
4✔
68
    if (lng.indexOf('.') > -1) {
69
      path = lng.split('.');
4✔
70
      deep = resources;
71
      resources = ns;
4✔
72
      ns = path[1];
73
    }
74

75
    this.addNamespaces(ns);
×
76

×
77
    let pack = utils.getPath(this.data, path) || {};
78

×
79
    if (deep) {
80
      utils.deepExtend(pack, resources, overwrite);
81
    } else {
82
      pack = {...pack, ...resources};
3✔
83
    }
3✔
84

1✔
85
    utils.setPath(this.data, path, pack);
1✔
86

1✔
87
    this.emit('added', lng, ns, resources);
1✔
88
  }
89

90
  removeResourceBundle(lng, ns) {
3✔
91
    if (this.hasResourceBundle(lng, ns)) {
92
      delete this.data[lng][ns];
3✔
93
    }
94
    this.removeNamespaces(ns);
3✔
95

×
96
    this.emit('removed', lng, ns);
97
  }
3✔
98

99
  hasResourceBundle(lng, ns) {
100
    return this.getResource(lng, ns) !== undefined;
3✔
101
  }
102

3✔
103
  getResourceBundle(lng, ns) {
104
    if (!ns) ns = this.options.defaultNS;
105

106
    // TODO: COMPATIBILITY remove extend in v2.1.0
1✔
107
    if (this.options.compatibilityAPI === 'v1') return {...{}, ...this.getResource(lng, ns)};
1✔
108

109
    return this.getResource(lng, ns);
1✔
110
  }
111

1✔
112
  toJSON() {
113
    return this.data;
114
  }
115
}
4✔
116

117
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