@ws-ui/formatter 0.1.13 → 0.1.15
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.d.ts +2 -0
- package/dist/date.js +5 -2
- package/dist/date.js.map +1 -1
- package/dist/duration.d.ts +2 -0
- package/dist/duration.js +23 -0
- package/dist/duration.js.map +1 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.js +6 -4
- package/dist/index.js.map +1 -1
- package/dist/waiter.js +27 -0
- package/dist/waiter.js.map +1 -0
- package/out/bundle.min.js +2 -1
- package/out/bundle.min.js.map +4 -4
- package/package.json +1 -1
package/dist/date.d.ts
CHANGED
package/dist/date.js
CHANGED
|
@@ -4,7 +4,7 @@ import(`@ws-ui/intl/locale-data/jsonp/${typeof navigator !== 'undefined' ? navig
|
|
|
4
4
|
function getDateFormat(options) {
|
|
5
5
|
return new Intl.DateTimeFormat(undefined, options).resolvedOptions().resolvedFormat;
|
|
6
6
|
}
|
|
7
|
-
const INVALID_DATE = new Date('').toString();
|
|
7
|
+
export const INVALID_DATE = new Date('').toString();
|
|
8
8
|
export function parseDateFormat(format) {
|
|
9
9
|
switch(format){
|
|
10
10
|
case 'Date short':
|
|
@@ -27,8 +27,11 @@ export function parseDateFormat(format) {
|
|
|
27
27
|
return format;
|
|
28
28
|
}
|
|
29
29
|
}
|
|
30
|
+
export function isValidDate(value) {
|
|
31
|
+
return value instanceof Date && value.toString() !== INVALID_DATE;
|
|
32
|
+
}
|
|
30
33
|
export function formatDate(value, format) {
|
|
31
|
-
if (value
|
|
34
|
+
if (!isValidDate(value)) return INVALID_DATE;
|
|
32
35
|
const parsedFormat = parseDateFormat(format);
|
|
33
36
|
return formatFn(value, parsedFormat);
|
|
34
37
|
}
|
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\
|
|
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,IAAIJ,KAAKK,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,OAAOd,SAASoB,OAAOE;AACzB"}
|
package/dist/duration.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { formatDistance, formatDistanceStrict } from 'date-fns';
|
|
2
|
+
const SECONDS = 1000;
|
|
3
|
+
const MINUTES = SECONDS * 60;
|
|
4
|
+
const HOURS = MINUTES * 60;
|
|
5
|
+
export function formatDuration(value, format = 'Simple') {
|
|
6
|
+
if (format === 'Simple') {
|
|
7
|
+
const v = +value;
|
|
8
|
+
const hours = Math.floor(v / HOURS);
|
|
9
|
+
const minutes = Math.floor(v % HOURS / MINUTES);
|
|
10
|
+
const seconds = Math.floor(v % MINUTES / SECONDS);
|
|
11
|
+
return `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
|
|
12
|
+
}
|
|
13
|
+
const d1 = new Date();
|
|
14
|
+
const d2 = value instanceof Date ? value : new Date(d1.getTime() + +value * SECONDS);
|
|
15
|
+
switch(format){
|
|
16
|
+
case 'Distance':
|
|
17
|
+
return formatDistance(d2, d1);
|
|
18
|
+
case 'Strict Distance':
|
|
19
|
+
return formatDistanceStrict(d2, d1);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
//# sourceMappingURL=duration.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/duration.ts"],"sourcesContent":["import { formatDistance, formatDistanceStrict } from 'date-fns';\n\nexport type Format = 'Simple' | 'Distance' | 'Strict Distance';\n\nconst SECONDS = 1000;\nconst MINUTES = SECONDS * 60;\nconst HOURS = MINUTES * 60;\n\nexport function formatDuration(value: string | number | Date, format: Format = 'Simple') {\n if (format === 'Simple') {\n const v = +value;\n const hours = Math.floor(v / HOURS);\n const minutes = Math.floor((v % HOURS) / MINUTES);\n const seconds = Math.floor((v % MINUTES) / SECONDS);\n return `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${seconds\n .toString()\n .padStart(2, '0')}`;\n }\n\n const d1 = new Date();\n const d2 = value instanceof Date ? value : new Date(d1.getTime() + +value * SECONDS);\n\n switch (format) {\n case 'Distance':\n return formatDistance(d2, d1);\n case 'Strict Distance':\n return formatDistanceStrict(d2, d1);\n }\n}\n"],"names":["formatDistance","formatDistanceStrict","SECONDS","MINUTES","HOURS","formatDuration","value","format","v","hours","Math","floor","minutes","seconds","toString","padStart","d1","Date","d2","getTime"],"mappings":"AAAA,SAASA,cAAc,EAAEC,oBAAoB,QAAQ,WAAW;AAIhE,MAAMC,UAAU;AAChB,MAAMC,UAAUD,UAAU;AAC1B,MAAME,QAAQD,UAAU;AAExB,OAAO,SAASE,eAAeC,KAA6B,EAAEC,SAAiB,QAAQ;IACrF,IAAIA,WAAW,UAAU;QACvB,MAAMC,IAAI,CAACF;QACX,MAAMG,QAAQC,KAAKC,KAAK,CAACH,IAAIJ;QAC7B,MAAMQ,UAAUF,KAAKC,KAAK,CAAC,AAACH,IAAIJ,QAASD;QACzC,MAAMU,UAAUH,KAAKC,KAAK,CAAC,AAACH,IAAIL,UAAWD;QAC3C,OAAO,CAAC,EAAEO,MAAMK,QAAQ,GAAGC,QAAQ,CAAC,GAAG,KAAK,CAAC,EAAEH,QAAQE,QAAQ,GAAGC,QAAQ,CAAC,GAAG,KAAK,CAAC,EAAEF,QACnFC,QAAQ,GACRC,QAAQ,CAAC,GAAG,KAAK,CAAC;IACvB;IAEA,MAAMC,KAAK,IAAIC;IACf,MAAMC,KAAKZ,iBAAiBW,OAAOX,QAAQ,IAAIW,KAAKD,GAAGG,OAAO,KAAK,CAACb,QAAQJ;IAE5E,OAAQK;QACN,KAAK;YACH,OAAOP,eAAekB,IAAIF;QAC5B,KAAK;YACH,OAAOf,qBAAqBiB,IAAIF;IACpC;AACF"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Format } from './string';
|
|
2
|
-
import { parseDateFormat } from './date';
|
|
2
|
+
import { parseDateFormat, isValidDate, INVALID_DATE } from './date';
|
|
3
3
|
export type DataType = 'string' | 'date' | 'number' | 'duration';
|
|
4
4
|
declare function format(rawValue: unknown, dataType: DataType, format?: string | Format): string;
|
|
5
5
|
declare function getStyle(dataType: DataType, format: string | Format, value: unknown): {
|
|
@@ -7,4 +7,4 @@ declare function getStyle(dataType: DataType, format: string | Format, value: un
|
|
|
7
7
|
} | {
|
|
8
8
|
color: any;
|
|
9
9
|
};
|
|
10
|
-
export { format, parseDateFormat, getStyle };
|
|
10
|
+
export { format, parseDateFormat, isValidDate, INVALID_DATE, getStyle };
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import numfmt from 'numfmt';
|
|
2
2
|
import { formatString } from './string';
|
|
3
|
-
import { formatDate, parseDateFormat } from './date';
|
|
3
|
+
import { formatDate, parseDateFormat, isValidDate, INVALID_DATE } from './date';
|
|
4
|
+
import { formatDuration } from './duration';
|
|
4
5
|
function format(rawValue, dataType, format) {
|
|
5
6
|
switch(dataType){
|
|
6
7
|
// string formatting
|
|
@@ -14,9 +15,10 @@ function format(rawValue, dataType, format) {
|
|
|
14
15
|
return numfmt(format)(isNaN(rawValue) ? rawValue : +rawValue);
|
|
15
16
|
// duration formatting
|
|
16
17
|
case 'duration':
|
|
17
|
-
return
|
|
18
|
+
return formatDuration(+rawValue, format);
|
|
19
|
+
default:
|
|
20
|
+
return rawValue;
|
|
18
21
|
}
|
|
19
|
-
return rawValue;
|
|
20
22
|
}
|
|
21
23
|
function getStyle(dataType, format, value) {
|
|
22
24
|
switch(dataType){
|
|
@@ -32,6 +34,6 @@ function getStyle(dataType, format, value) {
|
|
|
32
34
|
return {};
|
|
33
35
|
}
|
|
34
36
|
}
|
|
35
|
-
export { format, parseDateFormat, getStyle };
|
|
37
|
+
export { format, parseDateFormat, isValidDate, INVALID_DATE, getStyle };
|
|
36
38
|
|
|
37
39
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import numfmt from 'numfmt';\nimport { Format, formatString } from './string';\nimport { formatDate, parseDateFormat } from './date';\n\nexport type DataType = 'string' | 'date' | 'number' | 'duration';\n\nfunction format(rawValue: unknown, dataType: DataType, format?: string | Format): string {\n switch (dataType) {\n // string formatting\n case 'string':\n return formatString(rawValue as string, format as Format) as string;\n // date formatting\n case 'date':\n return formatDate(rawValue instanceof Date ? rawValue : new Date(rawValue as string), format);\n // number formatting\n case 'number':\n return numfmt(format)(isNaN(rawValue as number) ? rawValue : +rawValue);\n // duration formatting\n case 'duration':\n return
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import numfmt from 'numfmt';\nimport { Format, formatString } from './string';\nimport { formatDate, parseDateFormat, isValidDate, INVALID_DATE } from './date';\nimport { formatDuration, Format as DurationFormat } from './duration';\n\nexport type DataType = 'string' | 'date' | 'number' | 'duration';\n\nfunction format(rawValue: unknown, dataType: DataType, format?: string | Format): string {\n switch (dataType) {\n // string formatting\n case 'string':\n return formatString(rawValue as string, format as Format) as string;\n // date formatting\n case 'date':\n return formatDate(rawValue instanceof Date ? rawValue : new Date(rawValue as string), format);\n // number formatting\n case 'number':\n return numfmt(format)(isNaN(rawValue as number) ? rawValue : +rawValue);\n // duration formatting\n case 'duration':\n return formatDuration(+rawValue, format as DurationFormat);\n default:\n return rawValue as string;\n }\n}\n\nfunction getStyle(dataType: DataType, format: string | Format, value: unknown) {\n switch (dataType) {\n case 'number': {\n const color = numfmt(format).color(isNaN(value as number) ? value : +value);\n if (color === 'black' && !format.toLowerCase().includes('[black]')) return {};\n return { color };\n }\n default:\n return {};\n }\n}\n\nexport { format, parseDateFormat, isValidDate, INVALID_DATE, getStyle };\n"],"names":["numfmt","formatString","formatDate","parseDateFormat","isValidDate","INVALID_DATE","formatDuration","format","rawValue","dataType","Date","isNaN","getStyle","value","color","toLowerCase","includes"],"mappings":"AAAA,OAAOA,YAAY,SAAS;AAC5B,SAAiBC,YAAY,QAAQ,WAAW;AAChD,SAASC,UAAU,EAAEC,eAAe,EAAEC,WAAW,EAAEC,YAAY,QAAQ,SAAS;AAChF,SAASC,cAAc,QAAkC,aAAa;AAItE,SAASC,OAAOC,QAAiB,EAAEC,QAAkB,EAAEF,MAAwB;IAC7E,OAAQE;QACN,oBAAoB;QACpB,KAAK;YACH,OAAOR,aAAaO,UAAoBD;QAC1C,kBAAkB;QAClB,KAAK;YACH,OAAOL,WAAWM,oBAAoBE,OAAOF,WAAW,IAAIE,KAAKF,WAAqBD;QACxF,oBAAoB;QACpB,KAAK;YACH,OAAOP,OAAOO,QAAQI,MAAMH,YAAsBA,WAAW,CAACA;QAChE,sBAAsB;QACtB,KAAK;YACH,OAAOF,eAAe,CAACE,UAAUD;QACnC;YACE,OAAOC;IACX;AACF;AAEA,SAASI,SAASH,QAAkB,EAAEF,MAAuB,EAAEM,KAAc;IAC3E,OAAQJ;QACN,KAAK;YAAU;gBACb,MAAMK,QAAQd,OAAOO,QAAQO,KAAK,CAACH,MAAME,SAAmBA,QAAQ,CAACA;gBACrE,IAAIC,UAAU,WAAW,CAACP,OAAOQ,WAAW,GAAGC,QAAQ,CAAC,YAAY,OAAO,CAAC;gBAC5E,OAAO;oBAAEF;gBAAM;YACjB;QACA;YACE,OAAO,CAAC;IACZ;AACF;AAEA,SAASP,MAAM,EAAEJ,eAAe,EAAEC,WAAW,EAAEC,YAAY,EAAEO,QAAQ,GAAG"}
|
package/dist/waiter.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export class Mutex {
|
|
2
|
+
current = Promise.resolve();
|
|
3
|
+
lock() {
|
|
4
|
+
let _resolve;
|
|
5
|
+
const p = new Promise((resolve)=>{
|
|
6
|
+
_resolve = ()=>resolve();
|
|
7
|
+
});
|
|
8
|
+
// Caller gets a promise that resolves when the current outstanding
|
|
9
|
+
// lock resolves
|
|
10
|
+
const rv = this.current.then(()=>_resolve);
|
|
11
|
+
// Don't allow the next request until the new promise is done
|
|
12
|
+
this.current = p;
|
|
13
|
+
// Return the new promise
|
|
14
|
+
return rv;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
export function queue() {
|
|
18
|
+
const mutex = new Mutex();
|
|
19
|
+
return async (fn)=>{
|
|
20
|
+
const unlock = await mutex.lock();
|
|
21
|
+
const value = await fn();
|
|
22
|
+
unlock();
|
|
23
|
+
return value;
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
//# sourceMappingURL=waiter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/waiter.ts"],"sourcesContent":["export class Mutex {\n current: Promise<unknown> = Promise.resolve();\n lock() {\n let _resolve;\n const p = new Promise<void>((resolve) => {\n _resolve = () => resolve();\n });\n // Caller gets a promise that resolves when the current outstanding\n // lock resolves\n const rv = this.current.then(() => _resolve);\n // Don't allow the next request until the new promise is done\n this.current = p;\n // Return the new promise\n return rv;\n }\n}\n\nexport function queue() {\n const mutex = new Mutex();\n return async <T>(fn: () => T): Promise<T> => {\n const unlock = await mutex.lock();\n const value = await fn();\n unlock();\n return value;\n };\n}\n"],"names":["Mutex","current","Promise","resolve","lock","_resolve","p","rv","then","queue","mutex","fn","unlock","value"],"mappings":"AAAA,OAAO,MAAMA;IACXC,UAA4BC,QAAQC,OAAO,GAAG;IAC9CC,OAAO;QACL,IAAIC;QACJ,MAAMC,IAAI,IAAIJ,QAAc,CAACC;YAC3BE,WAAW,IAAMF;QACnB;QACA,mEAAmE;QACnE,gBAAgB;QAChB,MAAMI,KAAK,IAAI,CAACN,OAAO,CAACO,IAAI,CAAC,IAAMH;QACnC,6DAA6D;QAC7D,IAAI,CAACJ,OAAO,GAAGK;QACf,yBAAyB;QACzB,OAAOC;IACT;AACF;AAEA,OAAO,SAASE;IACd,MAAMC,QAAQ,IAAIV;IAClB,OAAO,OAAUW;QACf,MAAMC,SAAS,MAAMF,MAAMN,IAAI;QAC/B,MAAMS,QAAQ,MAAMF;QACpBC;QACA,OAAOC;IACT;AACF"}
|