@ws-ui/formatter 0.1.24 → 0.1.25
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/dist/date.js +5 -1
- package/dist/date.js.map +1 -1
- package/dist/duration.d.ts +1 -0
- package/dist/duration.js +10 -4
- package/dist/duration.js.map +1 -1
- package/out/bundle.min.js +2 -2
- package/out/bundle.min.js.map +4 -4
- package/package.json +10 -9
package/dist/date.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import formatFn from 'date-fns/format';
|
|
2
2
|
import Intl from '@ws-ui/intl';
|
|
3
|
+
import * as locales from 'date-fns/locale';
|
|
4
|
+
import { sanitizeLocale } from './duration';
|
|
3
5
|
import(`@ws-ui/intl/locale-data/jsonp/${typeof navigator !== 'undefined' ? navigator.language : 'en'}`);
|
|
4
6
|
function getDateFormat(options) {
|
|
5
7
|
return new Intl.DateTimeFormat(undefined, options).resolvedOptions().resolvedFormat;
|
|
@@ -33,7 +35,9 @@ export function isValidDate(value) {
|
|
|
33
35
|
export function formatDate(value, format) {
|
|
34
36
|
if (!isValidDate(value)) return INVALID_DATE;
|
|
35
37
|
const parsedFormat = parseDateFormat(format);
|
|
36
|
-
return formatFn(value, parsedFormat
|
|
38
|
+
return formatFn(value, parsedFormat, {
|
|
39
|
+
locale: locales[sanitizeLocale(navigator.language)]
|
|
40
|
+
});
|
|
37
41
|
}
|
|
38
42
|
|
|
39
43
|
//# sourceMappingURL=date.js.map
|
package/dist/date.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/date.ts"],"sourcesContent":["import formatFn from 'date-fns/format';\nimport Intl from '@ws-ui/intl';\nimport(\n `@ws-ui/intl/locale-data/jsonp/${typeof navigator !== 'undefined' ? navigator.language : 'en'}`\n);\n\ntype ResolvedDateTimeFormatOptions = Intl.ResolvedDateTimeFormatOptions & {\n pattern?: string;\n resolvedFormat: string;\n};\n\nfunction getDateFormat(options?: Intl.DateTimeFormatOptions) {\n return (\n new Intl.DateTimeFormat(undefined, options).resolvedOptions() as ResolvedDateTimeFormatOptions\n ).resolvedFormat;\n}\n\nexport const INVALID_DATE = new Date('').toString();\n\nexport function parseDateFormat(format: string) {\n switch (format) {\n case 'Date short':\n return getDateFormat();\n case 'Date long':\n return getDateFormat({\n weekday: 'long',\n year: 'numeric',\n month: 'long',\n day: 'numeric',\n });\n case 'Date abbreviated':\n return getDateFormat({\n weekday: 'short',\n year: 'numeric',\n month: 'short',\n day: 'numeric',\n });\n default:\n return format;\n }\n}\n\nexport function isValidDate(value: Date | number) {\n return value instanceof Date && value.toString() !== INVALID_DATE;\n}\n\nexport function formatDate(value: Date | number, format: string) {\n if (!isValidDate(value)) return INVALID_DATE;\n const parsedFormat = parseDateFormat(format);\n return formatFn(value, parsedFormat);\n}\n"],"names":["formatFn","Intl","navigator","language","getDateFormat","options","DateTimeFormat","undefined","resolvedOptions","resolvedFormat","INVALID_DATE","Date","toString","parseDateFormat","format","weekday","year","month","day","isValidDate","value","formatDate","parsedFormat"],"mappings":"AAAA,OAAOA,cAAc,kBAAkB;AACvC,OAAOC,UAAU,cAAc;AAC/B,MAAM,CACJ,CAAC,8BAA8B,EAAE,OAAOC,cAAc,cAAcA,UAAUC,QAAQ,GAAG,KAAK,CAAC;AAQjG,SAASC,cAAcC,OAAoC;IACzD,OAAO,AACL,
|
|
1
|
+
{"version":3,"sources":["../src/date.ts"],"sourcesContent":["import formatFn from 'date-fns/format';\nimport Intl from '@ws-ui/intl';\nimport * as locales from 'date-fns/locale';\nimport { sanitizeLocale } from './duration';\nimport(\n `@ws-ui/intl/locale-data/jsonp/${typeof navigator !== 'undefined' ? navigator.language : 'en'}`\n);\n\ntype ResolvedDateTimeFormatOptions = Intl.ResolvedDateTimeFormatOptions & {\n pattern?: string;\n resolvedFormat: string;\n};\n\nfunction getDateFormat(options?: Intl.DateTimeFormatOptions) {\n return (\n new Intl.DateTimeFormat(undefined, options).resolvedOptions() as ResolvedDateTimeFormatOptions\n ).resolvedFormat;\n}\n\nexport const INVALID_DATE = new Date('').toString();\n\nexport function parseDateFormat(format: string) {\n switch (format) {\n case 'Date short':\n return getDateFormat();\n case 'Date long':\n return getDateFormat({\n weekday: 'long',\n year: 'numeric',\n month: 'long',\n day: 'numeric',\n });\n case 'Date abbreviated':\n return getDateFormat({\n weekday: 'short',\n year: 'numeric',\n month: 'short',\n day: 'numeric',\n });\n default:\n return format;\n }\n}\n\nexport function isValidDate(value: Date | number) {\n return value instanceof Date && value.toString() !== INVALID_DATE;\n}\n\nexport function formatDate(value: Date | number, format: string) {\n if (!isValidDate(value)) return INVALID_DATE;\n const parsedFormat = parseDateFormat(format);\n return formatFn(value, parsedFormat, { locale: locales[sanitizeLocale(navigator.language)] });\n}\n"],"names":["formatFn","Intl","locales","sanitizeLocale","navigator","language","getDateFormat","options","DateTimeFormat","undefined","resolvedOptions","resolvedFormat","INVALID_DATE","Date","toString","parseDateFormat","format","weekday","year","month","day","isValidDate","value","formatDate","parsedFormat","locale"],"mappings":"AAAA,OAAOA,cAAc,kBAAkB;AACvC,OAAOC,UAAU,cAAc;AAC/B,YAAYC,aAAa,kBAAkB;AAC3C,SAASC,cAAc,QAAQ,aAAa;AAC5C,MAAM,CACJ,CAAC,8BAA8B,EAAE,OAAOC,cAAc,cAAcA,UAAUC,QAAQ,GAAG,KAAK,CAAC;AAQjG,SAASC,cAAcC,OAAoC;IACzD,OAAO,AACL,IAAIN,KAAKO,cAAc,CAACC,WAAWF,SAASG,eAAe,GAC3DC,cAAc;AAClB;AAEA,OAAO,MAAMC,eAAe,IAAIC,KAAK,IAAIC,QAAQ,GAAG;AAEpD,OAAO,SAASC,gBAAgBC,MAAc;IAC5C,OAAQA;QACN,KAAK;YACH,OAAOV;QACT,KAAK;YACH,OAAOA,cAAc;gBACnBW,SAAS;gBACTC,MAAM;gBACNC,OAAO;gBACPC,KAAK;YACP;QACF,KAAK;YACH,OAAOd,cAAc;gBACnBW,SAAS;gBACTC,MAAM;gBACNC,OAAO;gBACPC,KAAK;YACP;QACF;YACE,OAAOJ;IACX;AACF;AAEA,OAAO,SAASK,YAAYC,KAAoB;IAC9C,OAAOA,iBAAiBT,QAAQS,MAAMR,QAAQ,OAAOF;AACvD;AAEA,OAAO,SAASW,WAAWD,KAAoB,EAAEN,MAAc;IAC7D,IAAI,CAACK,YAAYC,QAAQ,OAAOV;IAChC,MAAMY,eAAeT,gBAAgBC;IACrC,OAAOhB,SAASsB,OAAOE,cAAc;QAAEC,QAAQvB,OAAO,CAACC,eAAeC,UAAUC,QAAQ,EAAE;IAAC;AAC7F"}
|
package/dist/duration.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
export type Format = 'Simple' | 'Distance' | 'Without seconds' | 'Distance With Suffix' | 'Strict Distance' | 'Strict Distance With Suffix';
|
|
2
|
+
export declare function sanitizeLocale(locale: string): string;
|
|
2
3
|
export declare function formatDuration(value: string | number | Date, format?: Format): string;
|
|
3
4
|
export declare function parseDuration(value: string): number;
|
package/dist/duration.js
CHANGED
|
@@ -1,14 +1,20 @@
|
|
|
1
1
|
import { formatDistance, formatDistanceStrict } from 'date-fns';
|
|
2
|
-
import * as
|
|
2
|
+
import * as locales from 'date-fns/locale';
|
|
3
3
|
const SECONDS = 1000;
|
|
4
4
|
const MINUTES = SECONDS * 60;
|
|
5
5
|
const HOURS = MINUTES * 60;
|
|
6
6
|
function padNumber(value, digits = 2) {
|
|
7
7
|
return `${String(value).padStart(digits, '0')}`;
|
|
8
8
|
}
|
|
9
|
-
function sanitizeLocale(locale) {
|
|
9
|
+
export function sanitizeLocale(locale) {
|
|
10
10
|
const [language, region = ''] = locale.split('-');
|
|
11
|
-
|
|
11
|
+
const lang = language.toLowerCase();
|
|
12
|
+
const key = lang + region.toUpperCase();
|
|
13
|
+
if (locales[key]) {
|
|
14
|
+
return key;
|
|
15
|
+
} else {
|
|
16
|
+
return lang;
|
|
17
|
+
}
|
|
12
18
|
}
|
|
13
19
|
export function formatDuration(value, format = 'Simple') {
|
|
14
20
|
if ([
|
|
@@ -33,7 +39,7 @@ export function formatDuration(value, format = 'Simple') {
|
|
|
33
39
|
}
|
|
34
40
|
const d1 = new Date();
|
|
35
41
|
const d2 = value instanceof Date ? value : new Date(d1.getTime() + +value);
|
|
36
|
-
const l = typeof navigator !== 'undefined' &&
|
|
42
|
+
const l = typeof navigator !== 'undefined' && locales[sanitizeLocale(navigator.language)] ? locales[sanitizeLocale(navigator.language)] : locales.enUS;
|
|
37
43
|
switch(format){
|
|
38
44
|
case 'Distance':
|
|
39
45
|
return formatDistance(d2, d1, {
|
package/dist/duration.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/duration.ts"],"sourcesContent":["import { formatDistance, formatDistanceStrict } from 'date-fns';\nimport * as
|
|
1
|
+
{"version":3,"sources":["../src/duration.ts"],"sourcesContent":["import { formatDistance, formatDistanceStrict } from 'date-fns';\nimport * as locales from 'date-fns/locale';\n\nexport type Format =\n | 'Simple'\n | 'Distance'\n | 'Without seconds'\n | 'Distance With Suffix'\n | 'Strict Distance'\n | 'Strict Distance With Suffix';\n\nconst SECONDS = 1000;\nconst MINUTES = SECONDS * 60;\nconst HOURS = MINUTES * 60;\n\nfunction padNumber(value: number, digits: number = 2) {\n return `${String(value).padStart(digits, '0')}`;\n}\n\nexport function sanitizeLocale(locale: string) {\n const [language, region = ''] = locale.split('-');\n const lang = language.toLowerCase();\n const key = lang + region.toUpperCase();\n if (locales[key]) {\n return key;\n } else {\n return lang;\n }\n}\n\nexport function formatDuration(value: string | number | Date, format: Format = 'Simple') {\n if (['Simple', 'Without seconds'].includes(format)) {\n let v = +value;\n let isNegative = false;\n\n if (v < 0) {\n v = -v;\n isNegative = true;\n }\n\n const hours = Math.floor(v / HOURS);\n const minutes = Math.floor((v % HOURS) / MINUTES);\n const seconds = Math.floor((v % MINUTES) / SECONDS);\n\n switch (format) {\n case 'Simple':\n return `${isNegative ? '-' : ''}${padNumber(hours)}:${padNumber(minutes)}:${padNumber(\n seconds,\n )}`;\n case 'Without seconds':\n return `${isNegative ? '-' : ''}${padNumber(hours)}:${padNumber(minutes)}`;\n }\n }\n\n const d1 = new Date();\n const d2 = value instanceof Date ? value : new Date(d1.getTime() + +value);\n const l =\n typeof navigator !== 'undefined' && locales[sanitizeLocale(navigator.language)]\n ? locales[sanitizeLocale(navigator.language)]\n : locales.enUS;\n\n switch (format) {\n case 'Distance':\n return formatDistance(d2, d1, {\n locale: l,\n });\n case 'Distance With Suffix':\n return formatDistance(d2, d1, {\n addSuffix: true,\n locale: l,\n });\n case 'Strict Distance':\n return formatDistanceStrict(d2, d1, {\n locale: l,\n });\n case 'Strict Distance With Suffix':\n return formatDistanceStrict(d2, d1, {\n addSuffix: true,\n locale: l,\n });\n }\n}\n\nexport function parseDuration(value: string) {\n const [hours = 0, minutes = 0, seconds = 0] = value.split(':').map(Number);\n\n const duration = Math.abs(hours) * HOURS + minutes * MINUTES + seconds * SECONDS;\n\n return hours < 0 ? -duration : duration;\n}\n"],"names":["formatDistance","formatDistanceStrict","locales","SECONDS","MINUTES","HOURS","padNumber","value","digits","String","padStart","sanitizeLocale","locale","language","region","split","lang","toLowerCase","key","toUpperCase","formatDuration","format","includes","v","isNegative","hours","Math","floor","minutes","seconds","d1","Date","d2","getTime","l","navigator","enUS","addSuffix","parseDuration","map","Number","duration","abs"],"mappings":"AAAA,SAASA,cAAc,EAAEC,oBAAoB,QAAQ,WAAW;AAChE,YAAYC,aAAa,kBAAkB;AAU3C,MAAMC,UAAU;AAChB,MAAMC,UAAUD,UAAU;AAC1B,MAAME,QAAQD,UAAU;AAExB,SAASE,UAAUC,KAAa,EAAEC,SAAiB,CAAC;IAClD,OAAO,CAAC,EAAEC,OAAOF,OAAOG,QAAQ,CAACF,QAAQ,KAAK,CAAC;AACjD;AAEA,OAAO,SAASG,eAAeC,MAAc;IAC3C,MAAM,CAACC,UAAUC,SAAS,EAAE,CAAC,GAAGF,OA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