-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
73 lines (62 loc) · 2.33 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
const got = require('got');
const YAML = require('yamljs');
const oPick = require('object.pick');
const backupList = require('./data/icons.json');
const getIcons = () => got('fontawesome.com/v4.7.0/icons.yml')
.then(response => YAML.parse(response.body.icons))
.catch(() => backupList.icons);
const printVersion = data => `${data[data.length - 1].created}.x`;
const cleanUpObjectsArray = (data, fields) => data.map(icon => oPick(icon, fields));
const getCategoriesObject = (data, fields = []) => {
const categories = {};
data.forEach((icon) => {
icon.categories.forEach((category) => {
if (!{}.hasOwnProperty.call(categories, category)) {
categories[category] = [];
}
categories[category].push(oPick(icon, fields));
});
});
return categories;
};
const getCategoriesArray = (data, fields) => {
const categoriesObject = getCategoriesObject(data, fields);
const categoriesArray = [];
Object.keys(categoriesObject).forEach((key) => {
categoriesArray.push({
name: key,
icons: categoriesObject[key],
});
});
return categoriesArray;
};
const getIconsByCategoryName = (data, name) =>
getCategoriesObject(data)[name] || [];
module.exports.getList = getIcons;
module.exports.version = () =>
new Promise((resolve) => {
getIcons()
.then(data => resolve(printVersion(data)))
.catch(() => resolve(printVersion(backupList.icons)));
});
module.exports.getCategories = fields =>
new Promise((resolve) => {
getIcons()
.then(data => resolve(getCategoriesArray(data, fields)))
.catch(() => resolve(getCategoriesArray(backupList.icons, fields)));
});
module.exports.getIconsByCategory = categoryName =>
new Promise((resolve) => {
if (!categoryName) {
return resolve([]);
}
return getIcons()
.then(data => resolve(getIconsByCategoryName(data, categoryName)))
.catch(() => resolve(getIconsByCategoryName(backupList.icons, categoryName)));
});
module.exports.getListByKeys = fields =>
new Promise((resolve) => {
getIcons()
.then(data => resolve(cleanUpObjectsArray(data, fields)))
.catch(() => resolve(cleanUpObjectsArray(backupList.icons, fields)));
});