@esmate/utils 1.0.0 → 1.0.2
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/README.md +1 -23
- package/dist/locale.d.ts +17 -1
- package/dist/locale.js +50 -3
- package/dist/pkgs/iso-3166-1.d.ts +2 -0
- package/dist/pkgs/iso-3166-1.js +4 -0
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -1,23 +1 @@
|
|
|
1
|
-
# @esmate/
|
|
2
|
-
|
|
3
|
-
## React Hooks
|
|
4
|
-
|
|
5
|
-
### useSearchParam
|
|
6
|
-
|
|
7
|
-
React sensor hook that tracks browser's location search param.
|
|
8
|
-
|
|
9
|
-
```tsx
|
|
10
|
-
import { useSearchParam } from "@esmate/react/hooks/use-search-param";
|
|
11
|
-
|
|
12
|
-
export function Search() {
|
|
13
|
-
const searchParam = useSearchParam();
|
|
14
|
-
|
|
15
|
-
return <div>{searchParam.get("q")}</div>;
|
|
16
|
-
}
|
|
17
|
-
```
|
|
18
|
-
|
|
19
|
-
#### Caveats/Gotchas
|
|
20
|
-
|
|
21
|
-
When using a hash router, like `react-router`'s
|
|
22
|
-
[`<HashRouter>`](https://github.com/ReactTraining/react-router/blob/master/packages/react-router-dom/docs/api/HashRouter.md),
|
|
23
|
-
this hook won't be able to read the search parameters as they are considered part of the hash of the URL by browsers.
|
|
1
|
+
# @esmate/utils
|
package/dist/locale.d.ts
CHANGED
|
@@ -3,4 +3,20 @@ export interface TimeZone {
|
|
|
3
3
|
value: string;
|
|
4
4
|
offset: string;
|
|
5
5
|
}
|
|
6
|
-
export declare function getTimeZoneList(): TimeZone[];
|
|
6
|
+
export declare function getTimeZoneList(locale?: string): TimeZone[];
|
|
7
|
+
interface Language {
|
|
8
|
+
name: string;
|
|
9
|
+
value: string;
|
|
10
|
+
}
|
|
11
|
+
export declare function getLanguageList(locale?: string): Language[];
|
|
12
|
+
interface Country {
|
|
13
|
+
name: string;
|
|
14
|
+
value: string;
|
|
15
|
+
}
|
|
16
|
+
export declare function getCountryList(locale?: string): Country[];
|
|
17
|
+
interface Currency {
|
|
18
|
+
name: string;
|
|
19
|
+
value: string;
|
|
20
|
+
}
|
|
21
|
+
export declare function getCurrencyList(locale?: string): Currency[];
|
|
22
|
+
export {};
|
package/dist/locale.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
|
|
1
|
+
import * as __WEBPACK_EXTERNAL_MODULE_iso_3166_1_0ebcf719__ from "iso-3166-1";
|
|
2
|
+
import * as __WEBPACK_EXTERNAL_MODULE_iso_639_1_39eb3c8b__ from "iso-639-1";
|
|
3
|
+
function getTimeZoneList(locale = "en-US") {
|
|
2
4
|
const timeZones = Intl.supportedValuesOf("timeZone");
|
|
3
5
|
const getOffset = (timeZone)=>{
|
|
4
|
-
const offset = new Intl.DateTimeFormat(
|
|
6
|
+
const offset = new Intl.DateTimeFormat(locale, {
|
|
5
7
|
timeZone,
|
|
6
8
|
timeZoneName: "longOffset"
|
|
7
9
|
}).formatToParts(new Date()).find((part)=>"timeZoneName" === part.type)?.value || "GMT";
|
|
@@ -17,4 +19,49 @@ function getTimeZoneList() {
|
|
|
17
19
|
};
|
|
18
20
|
}).sort((a, b)=>a.offset.localeCompare(b.offset) || a.name.localeCompare(b.name));
|
|
19
21
|
}
|
|
20
|
-
|
|
22
|
+
function getLanguageList(locale = "en-US") {
|
|
23
|
+
const codes = __WEBPACK_EXTERNAL_MODULE_iso_639_1_39eb3c8b__["default"].getAllCodes().sort((a, b)=>a.localeCompare(b));
|
|
24
|
+
return codes.map((code)=>{
|
|
25
|
+
const name = new Intl.DisplayNames([
|
|
26
|
+
locale
|
|
27
|
+
], {
|
|
28
|
+
type: "language"
|
|
29
|
+
}).of(code);
|
|
30
|
+
if (void 0 === name) throw new Error(`Language name not found for code: ${code}`);
|
|
31
|
+
return {
|
|
32
|
+
name,
|
|
33
|
+
value: code
|
|
34
|
+
};
|
|
35
|
+
}).sort((a, b)=>a.name.localeCompare(b.name));
|
|
36
|
+
}
|
|
37
|
+
function getCountryList(locale = "en-US") {
|
|
38
|
+
const codes = __WEBPACK_EXTERNAL_MODULE_iso_3166_1_0ebcf719__["default"].all().map((country)=>country.alpha2).sort((a, b)=>a.localeCompare(b));
|
|
39
|
+
return codes.map((code)=>{
|
|
40
|
+
const name = new Intl.DisplayNames([
|
|
41
|
+
locale
|
|
42
|
+
], {
|
|
43
|
+
type: "region"
|
|
44
|
+
}).of(code);
|
|
45
|
+
if (void 0 === name) throw new Error(`Country name not found for code: ${code}`);
|
|
46
|
+
return {
|
|
47
|
+
name,
|
|
48
|
+
value: code
|
|
49
|
+
};
|
|
50
|
+
}).sort((a, b)=>a.name.localeCompare(b.name));
|
|
51
|
+
}
|
|
52
|
+
function getCurrencyList(locale = "en-US") {
|
|
53
|
+
const codes = Intl.supportedValuesOf("currency");
|
|
54
|
+
return codes.map((code)=>{
|
|
55
|
+
const name = new Intl.DisplayNames([
|
|
56
|
+
locale
|
|
57
|
+
], {
|
|
58
|
+
type: "currency"
|
|
59
|
+
}).of(code);
|
|
60
|
+
if (void 0 === name) throw new Error(`Currency name not found for code: ${code}`);
|
|
61
|
+
return {
|
|
62
|
+
name,
|
|
63
|
+
value: code
|
|
64
|
+
};
|
|
65
|
+
}).sort((a, b)=>a.name.localeCompare(b.name));
|
|
66
|
+
}
|
|
67
|
+
export { getCountryList, getCurrencyList, getLanguageList, getTimeZoneList };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@esmate/utils",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.0.
|
|
4
|
+
"version": "1.0.2",
|
|
5
5
|
"description": "JavaScript/TypeScript utils, functions, and classes in one package.",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"repository": {
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"typescript"
|
|
19
19
|
],
|
|
20
20
|
"exports": {
|
|
21
|
-
"
|
|
21
|
+
"./*": {
|
|
22
22
|
"types": "./dist/lib/*.d.ts",
|
|
23
23
|
"import": "./dist/lib/*.js"
|
|
24
24
|
},
|
|
@@ -32,6 +32,7 @@
|
|
|
32
32
|
],
|
|
33
33
|
"dependencies": {
|
|
34
34
|
"@types/typopo": "^2.5.4",
|
|
35
|
+
"iso-3166-1": "^2.1.1",
|
|
35
36
|
"iso-639-1": "^3.1.5",
|
|
36
37
|
"tiny-invariant": "^1.3.3",
|
|
37
38
|
"title": "^4.0.1",
|