1 | var read = require('fs').readFileSync
|
2 | var gunzip = require('zlib-browserify').gunzipSync
|
3 | var join = require('path').join
|
4 |
|
5 | var countries = read(join(__dirname, '../data', '/countries.json.gz'))
|
6 | var ISOCodes = JSON.parse(gunzip(countries).toString())
|
7 |
|
8 | module.exports = (function iso3166() {
|
9 | var state = ''
|
10 |
|
11 | |
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | var to2 = function to2(alpha3) {
|
18 | if (alpha3 && alpha3.length > 1) state = alpha3
|
19 | if (state.length !== 3) return state
|
20 | return ISOCodes.filter(function(row) {
|
21 | return row.alpha3 === state
|
22 | })[0].alpha2
|
23 | }
|
24 |
|
25 | |
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | var to3 = function to3(alpha2) {
|
32 | if (alpha2 && alpha2.length > 1) state = alpha2
|
33 | if (state.length !== 2) return state
|
34 | return ISOCodes.filter(function(row) {
|
35 | return row.alpha2 === state
|
36 | })[0].alpha3
|
37 | }
|
38 |
|
39 | |
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | var from = function from(code) {
|
47 | if (typeof code !== 'string') return state
|
48 | state = code.toUpperCase()
|
49 | return this
|
50 | }
|
51 |
|
52 | |
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 | var fromLocale = function fromLocale(locale) {
|
60 | if (typeof locale !== 'string') return state
|
61 | state = locale.split('-').pop().toUpperCase()
|
62 | return this
|
63 | }
|
64 |
|
65 | |
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 | var list = function list() {
|
72 | return ISOCodes
|
73 | }
|
74 |
|
75 | |
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 | var is2 = function is2(alpha2) {
|
83 | return ISOCodes.some(function(row) {
|
84 | return row.alpha2 === alpha2
|
85 | })
|
86 | }
|
87 |
|
88 | |
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 | var is3 = function is3(alpha3) {
|
96 | return ISOCodes.some(function(row) {
|
97 | return row.alpha3 === alpha3
|
98 | })
|
99 | }
|
100 |
|
101 | return {
|
102 | to2: to2,
|
103 | to3: to3,
|
104 | from: from,
|
105 | fromLocale: fromLocale,
|
106 | list: list,
|
107 | is2: is2,
|
108 | is3: is3
|
109 | }
|
110 | })()
|