-
Notifications
You must be signed in to change notification settings - Fork 28
/
index.js
97 lines (80 loc) · 2.31 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/**
* Npm module for Unicode CLDR JSON data
*
* Copyright 2013 Rafael Xavier de Souza
* Released under the MIT license
* https://github.com/rxaviers/cldr-data-npm/blob/master/LICENSE-MIT
*/
"use strict";
var JSON_EXTENSION = /^(.*)\.json$/;
var assert = require("assert");
var _fs = require("fs");
var _path = require("path");
function argsToArray(arg) {
return [].slice.call(arg, 0);
}
function flatten(obj) {
var arr = [];
function _flatten(obj, arr) {
if(Array.isArray(obj)) {
return obj.forEach(function(obj) {
_flatten(obj, arr);
});
}
arr.push(obj);
}
_flatten(obj, arr);
return arr;
}
function jsonFiles(dirName) {
var fileList = _fs.readdirSync(_path.join(__dirname, dirName));
return fileList.reduce(function(sum, file) {
if (JSON_EXTENSION.test(file)) {
return sum.concat(file.match(JSON_EXTENSION)[1]);
}
}, []);
}
function cldrData(path/*, ...*/) {
assert(typeof path === "string", "must include path (e.g., " +
"\"main/en/numbers\" or \"supplemental/likelySubtags\")");
if (arguments.length > 1) {
return argsToArray(arguments).reduce(function(sum, path) {
sum.push(cldrData(path));
return sum;
}, []);
}
return require("./" + path);
}
function mainPathsFor(locales) {
return locales.reduce(function(sum, locale) {
var mainFiles = jsonFiles(_path.join("main", locale));
mainFiles.forEach(function(mainFile) {
sum.push(_path.join("main", locale, mainFile));
});
return sum;
}, []);
}
function supplementalPaths() {
var supplementalFiles = jsonFiles("supplemental");
return supplementalFiles.map(function(supplementalFile) {
return _path.join("supplemental", supplementalFile);
});
}
Object.defineProperty(cldrData, "availableLocales", {
get: function() {
return cldrData("availableLocales").availableLocales;
}
});
cldrData.all = function() {
var paths = supplementalPaths().concat(mainPathsFor(this.availableLocales));
return cldrData.apply({}, paths);
}
cldrData.entireMainFor = function(locale/*, ...*/) {
assert(typeof locale === "string", "must include locale (e.g., " +
"\"en\")");
return cldrData.apply({}, mainPathsFor(argsToArray(arguments)));
}
cldrData.entireSupplemental = function() {
return cldrData.apply({}, supplementalPaths());
}
module.exports = cldrData;