Skip to content

Commit

Permalink
Refactor: es2016nify (#6)
Browse files Browse the repository at this point in the history
* Use Default Parametor

* let to const

* Use Rest Parametor

* Use Destructuring assignment

* Use arrow function
  • Loading branch information
segayuu authored and tomap committed Nov 18, 2018
1 parent 0f0cf43 commit ad6bfbe
Showing 1 changed file with 16 additions and 40 deletions.
56 changes: 16 additions & 40 deletions lib/i18n.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
'use strict';

const vsprintf = require('sprintf-js').vsprintf;

function i18n(options) {
options = options || {};
const { vsprintf } = require('sprintf-js');

function i18n(options = {}) {
this.data = {};
this.languages = options.languages || ['default'];

Expand All @@ -14,9 +12,8 @@ function i18n(options) {
}

i18n.prototype.get = function(languages) {
const data = this.data;
const { data } = this;
const result = {};
let lang, langData, keys, key, i, leni, j, lenj;

if (languages) {
if (!Array.isArray(languages)) {
Expand All @@ -26,15 +23,15 @@ i18n.prototype.get = function(languages) {
languages = this.languages;
}

for (i = 0, leni = languages.length; i < leni; i++) {
lang = languages[i];
langData = data[lang];
for (let i = 0, leni = languages.length; i < leni; i++) {
const lang = languages[i];
const langData = data[lang];
if (!langData) continue;

keys = Object.keys(langData);
const keys = Object.keys(langData);

for (j = 0, lenj = keys.length; j < lenj; j++) {
key = keys[j];
for (let j = 0, lenj = keys.length; j < lenj; j++) {
const key = keys[j];
if (!result.hasOwnProperty(key)) result[key] = langData[key];
}
}
Expand Down Expand Up @@ -63,17 +60,12 @@ i18n.prototype.list = function() {
return Object.keys(this.data);
};

function flattenObject(data, obj, parent) {
obj = obj || {};
parent = parent || '';

function flattenObject(data, obj = {}, parent = '') {
const keys = Object.keys(data);
let key = '';
let item;

for (let i = 0, len = keys.length; i < len; i++) {
key = keys[i];
item = data[key];
const key = keys[i];
const item = data[key];

if (typeof item === 'object') {
flattenObject(item, obj, parent + key + '.');
Expand All @@ -88,17 +80,9 @@ function flattenObject(data, obj, parent) {
i18n.prototype.__ = function(lang) {
const data = this.get(lang);

return function() {
const len = arguments.length;
if (!len) return '';

const args = new Array(len);

for (let i = 0; i < len; i++) {
args[i] = arguments[i];
}
return (key, ...args) => {
if (!key) return '';

const key = args.shift();
const str = data[key] || key;

return vsprintf(str, args);
Expand All @@ -108,17 +92,9 @@ i18n.prototype.__ = function(lang) {
i18n.prototype._p = function(lang) {
const data = this.get(lang);

return function() {
const len = arguments.length;
if (!len) return '';

const args = new Array(len);

for (let i = 0; i < len; i++) {
args[i] = arguments[i];
}
return (key, ...args) => {
if (!key) return '';

const key = args.shift();
const number = args.length ? +args[0] : 0;
let str = key;

Expand Down

0 comments on commit ad6bfbe

Please sign in to comment.