forked from annexare/Countries
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
150 lines (127 loc) · 3.73 KB
/
gulpfile.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
'use strict'
const CONTINENTS = 'continents',
COUNTRIES = 'countries',
LANGUAGES = 'languages',
LF = '\n',
DATA = './data/',
DATA_FILE = 'data',
DIST = './dist/',
DO_DATA = 'data',
DO_EMOJI = 'emoji',
DO_MINIMAL = 'minimal',
JSON_EXT = 'json',
JSON_TAB = 2,
fs = require('fs'),
{ series } = require('gulp'),
continents = require(DATA + CONTINENTS + '.json'),
countries = require(DATA + COUNTRIES + '.json'),
DEFAULT_TASKS = [DO_DATA, DO_EMOJI, DO_MINIMAL]
exports[DO_DATA] = function data(callback) {
const fullData = {
continents,
countries: getCountriesWithEmoji(),
languages: languagesInUse,
}
fs.writeFileSync(
`${DIST}${DATA_FILE}.${JSON_EXT}`,
JSON.stringify(fullData, false, JSON_TAB) + LF
)
fs.writeFileSync(`${DIST}${DATA_FILE}.${DO_MIN}.${JSON_EXT}`, JSON.stringify(fullData) + LF)
callback && callback()
}
exports[DO_EMOJI] = function emoji(callback) {
const countriesEmoji = getCountriesWithEmoji()
fs.writeFileSync(
`${DIST}${COUNTRIES}.${DO_EMOJI}.${JSON_EXT}`,
JSON.stringify(countriesEmoji, false, JSON_TAB) + LF
)
fs.writeFileSync(
`${DIST}${COUNTRIES}.${DO_EMOJI}.${DO_MIN}.${JSON_EXT}`,
JSON.stringify(countriesEmoji) + LF
)
callback && callback()
}
exports[DO_MINIMAL] = function minimal(callback) {
// Countries: each item is a String country name in English
const minCountryNames = {}
Object.keys(countries).forEach((code) => {
minCountryNames[code] = countries[code].name
})
fs.writeFileSync(
`${DIST}${DO_MINIMAL}/${COUNTRIES}.en.${DO_MIN}.${JSON_EXT}`,
JSON.stringify(minCountryNames) + LF
)
// Countries: each item is an Array of fields in order
const minCountries = {}
Object.keys(countries).forEach((code) => {
minCountries[code] = getCountryDataValues(countries[code])
})
fs.writeFileSync(
`${DIST}${DO_MINIMAL}/${COUNTRIES}.${DO_MINIMAL}.${DO_MIN}.${JSON_EXT}`,
JSON.stringify(minCountries) + LF
)
// Languages: each item is a String language name in English
const minLanguageNames = {}
Object.keys(languagesInUse).forEach((code) => {
minLanguageNames[code] = languagesInUse[code].name
})
fs.writeFileSync(
`${DIST}${DO_MINIMAL}/${LANGUAGES}.en.${DO_MIN}.${JSON_EXT}`,
JSON.stringify(minLanguageNames) + LF
)
// Languages: each item is an Array of fields in order
const minLanguages = {}
Object.keys(languagesInUse).forEach((code) => {
minLanguages[code] = getLanguageDataValues(languagesInUse[code])
})
fs.writeFileSync(
`${DIST}${DO_MINIMAL}/${LANGUAGES}.${DO_MINIMAL}.${DO_MIN}.${JSON_EXT}`,
JSON.stringify(minLanguages) + LF
)
callback && callback()
}
exports.default = series(DEFAULT_TASKS.map((task) => exports[task]))
function getCountryDataValues(data, key = false) {
const { name, native, phone, continent, capital, currency, languages } = data
const values = [
name,
native,
phone,
continent,
capital,
currency,
key ? getStringFromArray(languages) : languages,
]
if (key) {
values.unshift(key)
}
return values
}
function getLanguageDataValues(data, key = false) {
const { name, native, rtl } = data
const values = [name, native, rtl ? 1 : 0]
if (key) {
values.unshift(key)
}
return values
}
function getCountriesWithEmoji() {
const { getEmojiFlag, getUnicode } = require('./dist/index.es5.min'),
dataWithEmoji = {},
countryCodes = Object.keys(countries)
countryCodes.forEach((code) => {
const emoji = getEmojiFlag(code),
emojiU = getUnicode(emoji)
dataWithEmoji[code] = Object.assign({}, countries[code], {
emoji,
emojiU,
})
})
return dataWithEmoji
}
function getStringFromArray(arr) {
if (!arr || !arr.length) {
return ''
}
return arr.join(',')
}