Skip to content

Commit

Permalink
Add singleton to interface (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
omrilotan committed Sep 13, 2017
1 parent 7bba72d commit 3ca1def
Show file tree
Hide file tree
Showing 10 changed files with 169 additions and 67 deletions.
12 changes: 12 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
const resolve = require('@fiverr/futile/lib/resolve');
const interpolate = require('@fiverr/futile/lib/interpolate');
const deepassign = require('@fiverr/futile/lib/deepassign');
const _global = require('@fiverr/futile/lib/global');
const freeze = require('deep-freeze');
const getOneOther = require('./utils/get-one-other');
const jsonclone = require('./utils/jsonclone');
Expand Down Expand Up @@ -114,4 +115,15 @@ module.exports = class I18n {

return done ? result : this.find(...alternatives);
}

/**
* Make sure you only have one instance of I18n in your global scope
* @return {I18n} the same instance every time
*
* @example
* const i18n = I18n.singleton;
*/
static get singleton() {
return _global.i18n = _global.i18n || new I18n();
}
};
46 changes: 45 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@fiverr/i18n",
"version": "1.1.0",
"version": "1.2.0",
"description": "Translation helper",
"author": "Fiverr dev team",
"license": "MIT",
Expand All @@ -18,8 +18,8 @@
"main": "index.js",
"scripts": {
"prepublishOnly": "npm t && npm run lint",
"test": "./node_modules/mocha/bin/mocha -- tests/index.js --inspect",
"lint": "./node_modules/eslint/bin/eslint.js -c .eslintrc **/* --quiet"
"test": "./node_modules/mocha/bin/mocha -- tests/*.js --inspect",
"lint": "./node_modules/eslint/bin/eslint.js -c .eslintrc **/*.js --quiet"
},
"dependencies": {
"@fiverr/futile": "^1.10.0",
Expand All @@ -30,6 +30,7 @@
"chai": "^4.1.2",
"eslint": "^4.6.1",
"eslint-plugin-react": "^7.3.0",
"import-fresh": "^2.0.0",
"mocha": "^3.5.1"
}
}
14 changes: 14 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,17 @@ const i18n = new I18n({
// Use:
i18n.translate('title', {username: 'Arthur', $scope: 'users.get'}); // Arthur's page
```

### Singleton
Make sure you only have one instance of I18n in your global scope
```javascript
const i18n = I18n.singleton;

i18n.$scope = 'my.scope'; // Optional
i18n.add({...});
```
Shortcut:
```javascript
const i18n = require('@fiverr/i18n/singleton');
```

7 changes: 7 additions & 0 deletions singleton.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const I18n = require('./');

/**
* Shortcut to I18n's singleton
* @type {I18n}
*/
module.exports = I18n.singleton;
4 changes: 1 addition & 3 deletions tests/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const {assert, expect} = require('chai');

const I18n = require('../');
const translations = require('./translations-stub');
const translations = require('./translations-stub.json');
const $scope = 'controller_name.action_name';

describe('I18n', () => {
Expand Down Expand Up @@ -132,5 +132,3 @@ describe('I18n', () => {
expect(i18n.translate('i.am.in.scope')).to.equal('and now for something completely different');
});
});

require('./missing-key')();
28 changes: 13 additions & 15 deletions tests/missing-key.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@ const {expect} = require('chai');

const I18n = require('../');

module.exports = () => {
describe('missing keys report', () => {
it('reports missing keys', () => {
const i18n = new I18n({
$scope: 'some.scope',
missing: (key, scope, translations) => {
expect(scope).to.equal('some.scope');
expect(key).to.equal('a.missing.key');
expect(translations).to.be.an('object');
expect(translations).to.be.empty;
}
});

i18n.translate('a.missing.key');
describe('missing keys report', () => {
it('reports missing keys', () => {
const i18n = new I18n({
$scope: 'some.scope',
missing: (key, scope, translations) => {
expect(scope).to.equal('some.scope');
expect(key).to.equal('a.missing.key');
expect(translations).to.be.an('object');
expect(translations).to.be.empty;
}
});

i18n.translate('a.missing.key');
});
};
});
28 changes: 28 additions & 0 deletions tests/singleton.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const {expect} = require('chai');
const importFresh = require('import-fresh');


describe('Singleton', () => {
it('sanity on importFresh', () => {
const I18n_a = importFresh('../');
const I18n_b = importFresh('../');

I18n_a.extra_param = 'Extra';
expect(I18n_a.extra_param).to.not.be.undefined;
expect(I18n_a.extra_param).to.equal('Extra');
expect(I18n_b.extra_param).to.be.undefined;
});

it('has only one instance in scope', () => {
const i18n_a = importFresh('../').singleton;
const i18n_b = importFresh('../').singleton;
const i18n_c = importFresh('../').singleton;

i18n_a.add({a: 'one'});
i18n_b.add({b: 'two'});
i18n_c.add({b: 'three'});

expect(i18n_b.t('a')).to.equal('one');
expect(i18n_b.t('b')).to.equal('three');
});
});
45 changes: 0 additions & 45 deletions tests/translations-stub.js

This file was deleted.

45 changes: 45 additions & 0 deletions tests/translations-stub.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"root": {
"user": {
"name": "Martin",
"last": "Prince"
},
"wait": {
"one": "Wait one day",
"other": "Wait %{count} days"
},
"is": {
"a": {
"number": 1,
"object": {"a": 2},
"boolean": false,
"null": null
}
},
"interpolated": {
"phrase": "Please replace %{item} with %{item} and %{another} thing"
}
},
"controller_name": {
"action_name": {
"i": {
"am": {
"in": {
"scope": "I am in scope"
}
}
}
}
},
"another_controller_name": {
"action_name": {
"i": {
"am": {
"in": {
"scope": "I am in a different scope"
}
}
}
}
}
}

0 comments on commit 3ca1def

Please sign in to comment.