Skip to content

Commit 6f4e017

Browse files
committed
Minor code style fixes
1 parent 0bb5daa commit 6f4e017

File tree

2 files changed

+98
-100
lines changed

2 files changed

+98
-100
lines changed

index.js

Lines changed: 57 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -71,70 +71,20 @@ function trueOrMatch(needle, haystack) {
7171
return false;
7272
}
7373

74-
75-
/**
76-
* Loads the dictionaries from the locale directory.
77-
* @param {Object} options
78-
*/
79-
function load(options) {
80-
if (cache[options.locales]) {
81-
options.verbose && gutil.log('Skip loading cached translations from', options.locales);
82-
return dictionaries = cache[options.locales];
83-
}
84-
try {
85-
options.verbose && gutil.log('Loading translations from', options.locales);
86-
var files = fs.readdirSync(options.locales);
87-
var count = 0;
88-
for (var i in files) {
89-
var file = files[i];
90-
switch (path.extname(file)) {
91-
case '.json':
92-
case '.js':
93-
dictionaries[path.basename(file, path.extname(file))] = flat(require(path.join(process.cwd(), options.locales, file)));
94-
options.verbose && gutil.log('Added translations from', file);
95-
count++;
96-
break;
97-
case '.ini':
98-
var iniData = fs.readFileSync(path.join(process.cwd(), options.locales, file));
99-
dictionaries[path.basename(file, path.extname(file))] = flat(ini2json(iniData));
100-
options.verbose && gutil.log('Added translations from', file);
101-
count++;
102-
break;
103-
case '.csv':
104-
var csvData = fs.readFileSync(path.join(process.cwd(), options.locales, file));
105-
dictionaries[path.basename(file, path.extname(file))] = csv2json(csvData);
106-
options.verbose && gutil.log('Added translations from', file);
107-
count++;
108-
break;
109-
default:
110-
options.verbose && gutil.log('Ignored file', file);
111-
}
112-
}
113-
options.verbose && gutil.log('Loaded', count, 'translations from', options.locales);
114-
if (options.cache) {
115-
options.verbose && gutil.log('Cashing translations from', options.locales);
116-
cache[options.locales] = dictionaries;
117-
}
118-
} catch (e) {
119-
e.message = 'No translation dictionaries have been found!';
120-
throw e;
121-
}
122-
}
123-
12474
/**
12575
* Splits a line from an ini file into 2. Any subsequent '=' are ignored.
12676
* @param {String} line
12777
* @returns {String[]}
12878
*/
12979
function splitIniLine(line) {
13080
var separator = line.indexOf('=');
131-
if (separator == -1) {
81+
if (separator === -1) {
13282
return [line];
13383
}
13484
return [
13585
line.substr(0, separator),
13686
line.substr(separator + 1)
137-
]
87+
];
13888
}
13989

14090
/**
@@ -152,8 +102,8 @@ function ini2json(iniData) {
152102
fields[j] = fields[j].trim();
153103
}
154104
if (fields[0].length) {
155-
if (fields[0].indexOf('[')==0) {
156-
context = fields[0].substring(1, fields[0].length -1)
105+
if (fields[0].indexOf('[')===0) {
106+
context = fields[0].substring(1, fields[0].length - 1);
157107
} else {
158108
if (context) {
159109
if (!result[context]) {
@@ -228,6 +178,55 @@ function csv2json(csvData) {
228178
return result;
229179
}
230180

181+
/**
182+
* Loads the dictionaries from the locale directory.
183+
* @param {Object} options
184+
*/
185+
function load(options) {
186+
if (cache[options.locales]) {
187+
options.verbose && gutil.log('Skip loading cached translations from', options.locales);
188+
return dictionaries = cache[options.locales];
189+
}
190+
try {
191+
options.verbose && gutil.log('Loading translations from', options.locales);
192+
var files = fs.readdirSync(options.locales);
193+
var count = 0;
194+
for (var i in files) {
195+
var file = files[i];
196+
switch (path.extname(file)) {
197+
case '.json':
198+
case '.js':
199+
dictionaries[path.basename(file, path.extname(file))] = flat(require(path.join(process.cwd(), options.locales, file)));
200+
options.verbose && gutil.log('Added translations from', file);
201+
count++;
202+
break;
203+
case '.ini':
204+
var iniData = fs.readFileSync(path.join(process.cwd(), options.locales, file));
205+
dictionaries[path.basename(file, path.extname(file))] = flat(ini2json(iniData));
206+
options.verbose && gutil.log('Added translations from', file);
207+
count++;
208+
break;
209+
case '.csv':
210+
var csvData = fs.readFileSync(path.join(process.cwd(), options.locales, file));
211+
dictionaries[path.basename(file, path.extname(file))] = csv2json(csvData);
212+
options.verbose && gutil.log('Added translations from', file);
213+
count++;
214+
break;
215+
default:
216+
options.verbose && gutil.log('Ignored file', file);
217+
}
218+
}
219+
options.verbose && gutil.log('Loaded', count, 'translations from', options.locales);
220+
if (options.cache) {
221+
options.verbose && gutil.log('Cashing translations from', options.locales);
222+
cache[options.locales] = dictionaries;
223+
}
224+
} catch (e) {
225+
e.message = 'No translation dictionaries have been found!';
226+
throw e;
227+
}
228+
}
229+
231230
/**
232231
* Helper function that detects whether a buffer is binary or utf8.
233232
* @param {Buffer} buffer
@@ -304,10 +303,10 @@ function translate(options, contents, copied, filePath) {
304303
i = next;
305304
}
306305
}
307-
for (var lang in processed) {
308-
if (!processed[lang].length) {
309-
options.verbose && gutil.log('Copying original content to target language', lang, 'because no replacements have happened');
310-
processed[lang] = contents;
306+
for (var procLang in processed) {
307+
if (!processed[procLang].length) {
308+
options.verbose && gutil.log('Copying original content to target language', procLang, 'because no replacements have happened');
309+
processed[procLang] = contents;
311310
}
312311
}
313312
return processed;

test/index.test.js

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,47 @@ var plugin = require('../');
1414

1515

1616
describe('gulp-international', () => {
17+
/**
18+
* Performs common tasks that need to be run for every test. Makes setting up and understanding tests way easier.
19+
*/
20+
function helper() {
21+
var options = {};
22+
var content = '<html><body><h1>R.token1</h1></body></html>';
23+
var validatorCb;
24+
for(let param of arguments) {
25+
if (_.isString(param) || _.isBuffer(param)) {
26+
content = param
27+
} else if (_.isFunction(param)) {
28+
validatorCb = param;
29+
} else if (_.isObject(param)) {
30+
options = param;
31+
}
32+
}
33+
expect(validatorCb).to.be.a('function');
34+
try {
35+
expect(content).to.be.a('string');
36+
} catch (e) {
37+
expect(content).to.be.instanceof(Buffer);
38+
}
39+
40+
var stream = plugin(options);
41+
var files = [];
42+
stream.on('data', file => {
43+
files.push(file);
44+
});
45+
stream.on('finish', () => {
46+
validatorCb(files, plugin.options);
47+
});
48+
stream.write(new File({
49+
path: 'test/helloworld.html',
50+
cwd: 'test/',
51+
base: 'test/',
52+
contents: _.isBuffer(content) || _.isObject(content) && content instanceof stream.Readable
53+
? content
54+
: new Buffer(content, 'utf8')
55+
}));
56+
stream.end();
57+
}
1758

1859
describe('Usage Examples', () => {
1960

@@ -472,45 +513,3 @@ describe('gulp-international', () => {
472513
});
473514
});
474515
});
475-
476-
/**
477-
* Performs common tasks that need to be run for every test. Makes setting up and understanding tests way easier.
478-
*/
479-
function helper() {
480-
var options = {};
481-
var content = '<html><body><h1>R.token1</h1></body></html>';
482-
var validatorCb;
483-
for(let param of arguments) {
484-
if (_.isString(param) || _.isBuffer(param)) {
485-
content = param
486-
} else if (_.isFunction(param)) {
487-
validatorCb = param;
488-
} else if (_.isObject(param)) {
489-
options = param;
490-
}
491-
}
492-
expect(validatorCb).to.be.a('function');
493-
try {
494-
expect(content).to.be.a('string');
495-
} catch (e) {
496-
expect(content).to.be.instanceof(Buffer);
497-
}
498-
499-
var stream = plugin(options);
500-
var files = [];
501-
stream.on('data', file => {
502-
files.push(file);
503-
});
504-
stream.on('finish', () => {
505-
validatorCb(files, plugin.options);
506-
});
507-
stream.write(new File({
508-
path: 'test/helloworld.html',
509-
cwd: 'test/',
510-
base: 'test/',
511-
contents: _.isBuffer(content) || _.isObject(content) && content instanceof stream.Readable
512-
? content
513-
: new Buffer(content, 'utf8')
514-
}));
515-
stream.end();
516-
}

0 commit comments

Comments
 (0)