forked from adlawson/nodejs-langs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
62 lines (52 loc) · 1.27 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
var data = require('./data');
var langs = {
all: allLanguages,
has: hasLanguage,
codes: getCodes,
names: getNames,
where: findBy
};
module.exports = langs;
// allLanguages :: -> Language[]
function allLanguages() {
return data;
}
// hasLanguage :: String, String -> Boolean
function hasLanguage(crit, val) {
return void(0) !== findBy(crit, val);
}
// getCodes :: String -> String[]
function getCodes(type) {
if (isValidType(type)) {
return forAll(data, function getCodesIterator(row) {
return row[type];
});
}
}
// getNames :: Boolean -> String[]
function getNames(local) {
return forAll(data, function getNamesIterator(row) {
return local ? row.local : row.name;
});
}
// findBy :: String, String -> Language
function findBy(crit, val) {
for (var i = 0; i < data.length; i++) {
if (val === data[i][crit]) {
return data[i];
}
}
}
// forAll :: Array, Function -> Array
function forAll(arr, fn) {
var out = [], i;
for (i = 0; i < arr.length; i++) {
out.push(fn(arr[i], i));
}
return out;
}
// isValidType :: String -> Boolean
function isValidType(type) {
var types = [1, 2, 3, '1', '2', '2B', '2T', '3'];
return -1 !== types.indexOf(type);
}