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

i18next / i18next / #34067

18 Nov 2017 09:51AM UTC coverage: 84.058% (-8.0%) from 92.045%
#34067

push

web-flow
Update README.md

696 of 828 relevant lines covered (84.06%)

39.11 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.js';
1✔
2
import * as utils from './utils.js';
1✔
3

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

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

17
  removeNamespaces(ns) {
18
    const 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 = {}) {
179✔
25
    let keySeparator = options.keySeparator || this.options.keySeparator;
179✔
26
    if (keySeparator === undefined) keySeparator = '.';
179✔
27

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

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

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

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

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

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

52
    this.addNamespaces(ns);
4✔
53

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

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

59
  addResources(lng, ns, resources) {
60
    /* eslint no-restricted-syntax: 0 */
61
    for (const m in resources) {
×
62
      if (typeof resources[m] === 'string') this.addResource(lng, ns, m, resources[m], { silent: true });
×
63
    }
64
    this.emit('added', lng, ns, resources);
×
65
  }
66

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

76
    this.addNamespaces(ns);
7✔
77

78
    let pack = utils.getPath(this.data, path) || {};
7✔
79

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

86
    utils.setPath(this.data, path, pack);
7✔
87

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

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

97
    this.emit('removed', lng, ns);
1✔
98
  }
99

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

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

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

110
    return this.getResource(lng, ns);
7✔
111
  }
112

113
  toJSON() {
114
    return this.data;
2✔
115
  }
116
}
117

118
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