Debian Codegraph @

src/api.js

raw symbols SCIP
1 /**
2 * Functions to get first day of week.
3 *
4 * @module api
5 */
6
7
8 /**
9 * Return first day of week for country/region code.
10 *
11 * @example
12 * getWeekStartByRegion('PNG', {}); // 1
13 * getWeekStartByRegion('png', {BR: 0, PNG: 3, EG: 6}); // 3
14 * getWeekStartByRegion('qa', {QA: 6}); // 6
15 * getWeekStartByRegion(50, {BD: 5, 50: 5, SD: 6}); // 5
16 *
17 * @param {number | string} regionCode
18 * ISO 3166 Alpha-2, Alpha-3 or numeric code.
19 * @param {object} regionDayMap
20 * Mapping of country/region code to first day of week that should be used to get result.
21 * Country codes should be in upper case.
22 * @return {number}
23 * Code of first day of week for the given country/region code:
24 * 0 - Sunday, 1 - Monday, 2 - Tuesday, 3 - Wednesday, 4 - Thursday, 5 - Friday, 6 - Saturday.
25 * @alias module:api.getWeekStartByRegion
26 */
27 export function getWeekStartByRegion(regionCode, regionDayMap) {
28 /* eslint-disable indent */
29 const code = regionDayMap[typeof regionCode === 'string'
30 ? regionCode.toUpperCase()
31 : regionCode];
32 /* eslint-enable indent */
33
34 return typeof code === 'number'
35 ? code
36 : 1;
37 }
38
39 /**
40 * Return first day of week for locale identifier.
41 *
42 * @example
43 * getWeekStartByLocale('no', {}, {}); // 1
44 * getWeekStartByLocale('no', {no: 'abc'}, {ABC: 3}); // 3
45 * getWeekStartByLocale('KK_arab', {kk_arab: 'CN'}, {CN: 0}); // 0
46 * getWeekStartByLocale('fr-DZ', {fr: 'FR'}, {FR: 1, DZ: 6}); // 6
47 *
48 * @param {string} locale
49 * Locale identifier.
50 * @param {object} langRegionMap
51 * Mapping of language code to country/region code that should be used to get result.
52 * Language codes should be in lower case.
53 * @param {object} regionDayMap
54 * Mapping of country/region code to first day of week that should be used to get result.
55 * Country codes should be in upper case.
56 * @return {number}
57 * Code of first day of week for the given locale identifier:
58 * 0 - Sunday, 1 - Monday, 2 - Tuesday, 3 - Wednesday, 4 - Thursday, 5 - Friday, 6 - Saturday.
59 * @alias module:api.getWeekStartByLocale
60 */
61 export function getWeekStartByLocale(locale, langRegionMap, regionDayMap) {
62 if (locale) {
63 // Locale form: http://www.unicode.org/reports/tr35/tr35.html#Unicode_Language_and_Locale_Identifiers
64 const data = locale.toLowerCase().split(/[-_]/);
65 const langTag = data[0];
66 let language = langTag;
67 let country;
68 if (data[1] && data[1].length === 4) {
69 language += `_${data[1]}`;
70 country = data[2];
71 }
72 else {
73 country = data[1];
74 }
75 if (! country) {
76 country = langRegionMap[language] || langRegionMap[langTag];
77 }
78 if (country) {
79 return getWeekStartByRegion(
80 country.match(/^\d+$/)
81 ? Number(country)
82 : country,
83 regionDayMap
84 );
85 }
86 }
87
88 return 1;
89 }