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

i18next / i18next / #51488

09 May 2016 07:09AM UTC coverage: 66.793% (-25.2%) from 91.945%
#51488

push

jamuhl
Merge pull request #691 from i18next/greenkeeper-i18next-xhr-backend-0.5.4

Update i18next-xhr-backend to version 0.5.4 🚀

529 of 792 relevant lines covered (66.79%)

20.01 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';
1✔
3

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

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

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

36
    return utils.getPath(this.data, path);
111✔
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
    for (var m in resources) {
×
61
      if (typeof resources[m] === 'string') this.addResource(lng, ns, m, resources[m], { silent: true });
×
62
    }
63
    this.emit('added', lng, ns, resources);
×
64
  }
65

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

75
    this.addNamespaces(ns);
3✔
76

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

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

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

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

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

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

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

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

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

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

112
  toJSON() {
113
    return this.data;
2✔
114
  }
115
}
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