country-codes-list 1.6.12 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/publish.yml +27 -0
- package/README.md +109 -20
- package/dist/countriesData.d.ts +20 -0
- package/dist/countriesData.js +4077 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +64 -0
- package/dist/utils/groupBy.d.ts +7 -0
- package/dist/utils/groupBy.js +19 -0
- package/dist/utils/supplant.d.ts +7 -0
- package/dist/utils/supplant.js +17 -0
- package/package.json +29 -15
- package/src/countriesData.ts +4100 -0
- package/src/index.ts +95 -0
- package/src/utils/groupBy.ts +19 -0
- package/src/utils/supplant.ts +17 -0
- package/tests/index.test.ts +62 -0
- package/tsconfig.json +12 -0
- package/countriesData.js +0 -4032
- package/index.d.ts +0 -54
- package/index.js +0 -88
- package/test.js +0 -24
- package/utils/groupBy.js +0 -20
- package/utils/supplant.js +0 -16
package/index.d.ts
DELETED
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
// Types for https://www.npmjs.com/package/country-codes-list
|
|
2
|
-
// by jakeisnt, Feb. 2022
|
|
3
|
-
|
|
4
|
-
declare module 'country-codes-list' {
|
|
5
|
-
|
|
6
|
-
export type CountryData = {
|
|
7
|
-
countryNameEn: string
|
|
8
|
-
countryNameLocal: string
|
|
9
|
-
countryCode: string
|
|
10
|
-
currencyCode: string
|
|
11
|
-
currencyNameEn: string
|
|
12
|
-
tinType: string
|
|
13
|
-
tinName: string
|
|
14
|
-
officialLanguageCode: string
|
|
15
|
-
officialLanguageNameEn: string
|
|
16
|
-
officialLanguageNameLocal: string
|
|
17
|
-
countryCallingCode: string
|
|
18
|
-
region: string
|
|
19
|
-
flag: string
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export type CountryProperty = keyof CountryData
|
|
23
|
-
|
|
24
|
-
type Filter<T> = (element: T, index: number, array: T[]) => T[]
|
|
25
|
-
|
|
26
|
-
type CustomArraySettings = {
|
|
27
|
-
sortDataBy?: CountryProperty
|
|
28
|
-
sortBy?: any
|
|
29
|
-
filter?: Filter<CountryData>
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export function all(): CountryData[]
|
|
33
|
-
|
|
34
|
-
export function filter(
|
|
35
|
-
countryProperty: CountryProperty,
|
|
36
|
-
value: string,
|
|
37
|
-
): CountryData[]
|
|
38
|
-
|
|
39
|
-
export function findOne(
|
|
40
|
-
countryProperty: CountryProperty,
|
|
41
|
-
value: string,
|
|
42
|
-
): CountryData
|
|
43
|
-
|
|
44
|
-
export function customArray(
|
|
45
|
-
fields?: Record<string, string>,
|
|
46
|
-
settings?: CustomArraySettings,
|
|
47
|
-
): Record<string, string>[]
|
|
48
|
-
|
|
49
|
-
export function customList(
|
|
50
|
-
key?: CountryProperty,
|
|
51
|
-
label?: string,
|
|
52
|
-
settings?: CustomArraySettings,
|
|
53
|
-
): { [key in CountryProperty]: string }
|
|
54
|
-
}
|
package/index.js
DELETED
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
const groupBy = require('./utils/groupBy')
|
|
2
|
-
const supplant = require('./utils/supplant')
|
|
3
|
-
const countriesData = require('./countriesData')
|
|
4
|
-
|
|
5
|
-
module.exports = {
|
|
6
|
-
/**
|
|
7
|
-
* Returns some module utils
|
|
8
|
-
*/
|
|
9
|
-
utils: {
|
|
10
|
-
groupBy: groupBy
|
|
11
|
-
},
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Returns the list with all the countries data
|
|
15
|
-
*/
|
|
16
|
-
all: function () {
|
|
17
|
-
return countriesData
|
|
18
|
-
},
|
|
19
|
-
/**
|
|
20
|
-
* Filters the list of countries and returns those matching with the filter criteria
|
|
21
|
-
* @param {String} countryProperty - The property to use in the filter. Must be any of the country properties (countryCode, currencyCode, etc)
|
|
22
|
-
* @param {String} value - The value to use in the filter
|
|
23
|
-
*/
|
|
24
|
-
filter: function (countryProperty, value) {
|
|
25
|
-
return countriesData.filter(countryData => countryData[countryProperty] === value)
|
|
26
|
-
},
|
|
27
|
-
/**
|
|
28
|
-
* Find a country by a property and return the first match
|
|
29
|
-
* @param {String} countryProperty - The property to use in the search. Must be any of the country properties (countryCode, currencyCode, etc)
|
|
30
|
-
* @param {String} value - The value to use in the filter
|
|
31
|
-
*/
|
|
32
|
-
findOne: function (countryProperty, value) {
|
|
33
|
-
return countriesData.find(countryData => countryData[countryProperty] === value)
|
|
34
|
-
},
|
|
35
|
-
/**
|
|
36
|
-
* Returns a collection with fields mapped as requested
|
|
37
|
-
* @param {*} fields - Map of fields and placeholders
|
|
38
|
-
*/
|
|
39
|
-
customArray: function (fields = { name: '{countryNameEn} ({countryCode})', value: '{countryCode}'}, { sortBy, sortDataBy, filter } = {}) {
|
|
40
|
-
const finalCollection = []
|
|
41
|
-
|
|
42
|
-
let data = countriesData
|
|
43
|
-
if (typeof filter === 'function') {
|
|
44
|
-
data = data.filter(filter)
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
if (sortDataBy) {
|
|
48
|
-
// ignore upper and lowercase
|
|
49
|
-
const collator = new Intl.Collator([], { sensitivity:'accent' })
|
|
50
|
-
data.sort((a, b) => collator.compare(a[sortDataBy], b[sortDataBy]))
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
data.forEach(countryData => {
|
|
54
|
-
let collectionObject = {}
|
|
55
|
-
for (const field in fields) {
|
|
56
|
-
collectionObject[field] = supplant(fields[field], countryData)
|
|
57
|
-
}
|
|
58
|
-
finalCollection.push(collectionObject)
|
|
59
|
-
})
|
|
60
|
-
|
|
61
|
-
if (sortBy && fields[sortBy]) {
|
|
62
|
-
// ignore upper and lowercase
|
|
63
|
-
const collator = new Intl.Collator([], {sensitivity:'accent'})
|
|
64
|
-
finalCollection.sort((a, b) => collator.compare(a[sortBy], b[sortBy]))
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
return finalCollection
|
|
68
|
-
},
|
|
69
|
-
/**
|
|
70
|
-
* Returns a custom object with the passed key as object key and a value made up with
|
|
71
|
-
* values set in the placeholders of the label variable
|
|
72
|
-
* @param {*} key - Key used to construct the object to return
|
|
73
|
-
* @param {*} label - Placeholder like string, with all the fields that you want to use
|
|
74
|
-
*/
|
|
75
|
-
customList: function (key = 'countryCode', label = '{countryNameEn} ({countryCode})', { filter } = {}) {
|
|
76
|
-
const finalObject = {}
|
|
77
|
-
let data = countriesData
|
|
78
|
-
if (typeof filter === 'function') {
|
|
79
|
-
data = data.filter(filter)
|
|
80
|
-
}
|
|
81
|
-
data.forEach(countryData => {
|
|
82
|
-
const value = supplant(label, countryData)
|
|
83
|
-
finalObject[countryData[key]] = value
|
|
84
|
-
})
|
|
85
|
-
|
|
86
|
-
return finalObject
|
|
87
|
-
}
|
|
88
|
-
}
|
package/test.js
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
function groupBy (key, array, transformValue) {
|
|
2
|
-
return array.reduce((objectsByKeyValue, obj) => {
|
|
3
|
-
const value = obj[key];
|
|
4
|
-
let val = obj
|
|
5
|
-
if (typeof transformValue === 'function') {
|
|
6
|
-
val = transformValue(val)
|
|
7
|
-
}
|
|
8
|
-
objectsByKeyValue[value] = (objectsByKeyValue[value] || []).concat(val);
|
|
9
|
-
return objectsByKeyValue;
|
|
10
|
-
}, {})
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
const countryCodes = require('./index')
|
|
14
|
-
const tinTypes = countryCodes.customArray(
|
|
15
|
-
{ value: '{tinType}', name: '{countryNameEn}' },
|
|
16
|
-
{
|
|
17
|
-
filter: (countryData) => countryData.tinType !== '',
|
|
18
|
-
},
|
|
19
|
-
)
|
|
20
|
-
|
|
21
|
-
const finalTinTypes = {}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
console.log('tinTypes', JSON.stringify(countryCodes.utils.groupBy('value', tinTypes, tinType => tinType.name), null, 2))
|
package/utils/groupBy.js
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* groupBy() groups an array by the given key. A transformation may be passed to be applied when each value is concatenated
|
|
5
|
-
* to the grouped component.
|
|
6
|
-
* @param {string} key - The key to use to group the array
|
|
7
|
-
* @param {array} array - The array to group
|
|
8
|
-
* @param {function} transform - A transformation to apply to the grouped value
|
|
9
|
-
*/
|
|
10
|
-
module.exports = function groupBy (key, array, transform) {
|
|
11
|
-
return array.reduce((objectsByKeyValue, obj) => {
|
|
12
|
-
const value = obj[key];
|
|
13
|
-
let val = obj
|
|
14
|
-
if (typeof transform === 'function') {
|
|
15
|
-
val = transform(val)
|
|
16
|
-
}
|
|
17
|
-
objectsByKeyValue[value] = (objectsByKeyValue[value] || []).concat(val);
|
|
18
|
-
return objectsByKeyValue;
|
|
19
|
-
}, {})
|
|
20
|
-
};
|
package/utils/supplant.js
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* supplant() does variable substitution on the string. It scans through the string looking for
|
|
5
|
-
* expressions enclosed in { } braces. If an expression is found, use it as a key on the object,
|
|
6
|
-
* and if the key has a string value or number value, it is substituted for the bracket expression
|
|
7
|
-
* and it repeats.
|
|
8
|
-
* @param {string} stringVal - The string that needs supplanting
|
|
9
|
-
* @param {object} replacements - key/value object with the keys to be replaced by the corresponding values
|
|
10
|
-
*/
|
|
11
|
-
module.exports = function (stringVal, replacements) {
|
|
12
|
-
return stringVal.replace(/{([^{}]*)}/g, function (a, b) {
|
|
13
|
-
let r = replacements[b]
|
|
14
|
-
return typeof r === 'string' || typeof r === 'number' ? r : a
|
|
15
|
-
})
|
|
16
|
-
}
|