@ws-ui/formatter 0.1.14 → 0.1.16
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/duration.d.ts +2 -0
- package/dist/duration.js +23 -0
- package/dist/duration.js.map +1 -0
- package/dist/index.js +4 -2
- 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/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);
|
|
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);\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;IAEpE,OAAQC;QACN,KAAK;YACH,OAAOP,eAAekB,IAAIF;QAC5B,KAAK;YACH,OAAOf,qBAAqBiB,IAAIF;IACpC;AACF"}
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import numfmt from 'numfmt';
|
|
2
2
|
import { formatString } from './string';
|
|
3
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){
|
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, isValidDate, INVALID_DATE } 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"}
|