@ws-ui/formatter 0.1.9 → 0.1.11
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 +1 -2
- package/dist/date.js +12 -8
- package/dist/date.js.map +1 -1
- package/dist/index.d.ts +9 -3
- package/dist/index.js +12 -2
- package/dist/index.js.map +1 -1
- package/out/bundle.min.js +1 -1
- package/out/bundle.min.js.map +3 -3
- package/package.json +1 -1
package/dist/date.d.ts
CHANGED
package/dist/date.js
CHANGED
|
@@ -1,30 +1,34 @@
|
|
|
1
1
|
import formatFn from 'date-fns/format';
|
|
2
2
|
import Intl from '@ws-ui/intl';
|
|
3
3
|
import(`@ws-ui/intl/locale-data/jsonp/${typeof navigator !== 'undefined' ? navigator.language : 'en'}`);
|
|
4
|
-
|
|
4
|
+
function getDateFormat(options) {
|
|
5
5
|
return new Intl.DateTimeFormat(undefined, options).resolvedOptions().resolvedFormat;
|
|
6
6
|
}
|
|
7
|
-
export function
|
|
7
|
+
export function parseDateFormat(format) {
|
|
8
8
|
switch(format){
|
|
9
9
|
case 'Date short':
|
|
10
|
-
return
|
|
10
|
+
return getDateFormat();
|
|
11
11
|
case 'Date long':
|
|
12
|
-
return
|
|
12
|
+
return getDateFormat({
|
|
13
13
|
weekday: 'long',
|
|
14
14
|
year: 'numeric',
|
|
15
15
|
month: 'long',
|
|
16
16
|
day: 'numeric'
|
|
17
|
-
})
|
|
17
|
+
});
|
|
18
18
|
case 'Date abbreviated':
|
|
19
|
-
return
|
|
19
|
+
return getDateFormat({
|
|
20
20
|
weekday: 'short',
|
|
21
21
|
year: 'numeric',
|
|
22
22
|
month: 'short',
|
|
23
23
|
day: 'numeric'
|
|
24
|
-
})
|
|
24
|
+
});
|
|
25
25
|
default:
|
|
26
|
-
return
|
|
26
|
+
return format;
|
|
27
27
|
}
|
|
28
28
|
}
|
|
29
|
+
export function formatDate(value, format) {
|
|
30
|
+
const parsedFormat = parseDateFormat(format);
|
|
31
|
+
return formatFn(value, parsedFormat);
|
|
32
|
+
}
|
|
29
33
|
|
|
30
34
|
//# 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\
|
|
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 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 formatDate(value: Date | number, format: string) {\n const parsedFormat = parseDateFormat(format);\n return formatFn(value, parsedFormat);\n}\n"],"names":["formatFn","Intl","navigator","language","getDateFormat","options","DateTimeFormat","undefined","resolvedOptions","resolvedFormat","parseDateFormat","format","weekday","year","month","day","formatDate","value","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,SAASC,gBAAgBC,MAAc;IAC5C,OAAQA;QACN,KAAK;YACH,OAAOP;QACT,KAAK;YACH,OAAOA,cAAc;gBACnBQ,SAAS;gBACTC,MAAM;gBACNC,OAAO;gBACPC,KAAK;YACP;QACF,KAAK;YACH,OAAOX,cAAc;gBACnBQ,SAAS;gBACTC,MAAM;gBACNC,OAAO;gBACPC,KAAK;YACP;QACF;YACE,OAAOJ;IACX;AACF;AAEA,OAAO,SAASK,WAAWC,KAAoB,EAAEN,MAAc;IAC7D,MAAMO,eAAeR,gBAAgBC;IACrC,OAAOX,SAASiB,OAAOC;AACzB"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
import { Format } from './string';
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
import { parseDateFormat } from './date';
|
|
3
|
+
export type DataType = 'string' | 'date' | 'number' | 'duration';
|
|
4
|
+
declare function format(rawValue: unknown, dataType: DataType, format?: string | Format): string;
|
|
5
|
+
declare function getStyle(dataType: DataType, format: string | Format, value: unknown): {
|
|
6
|
+
color: any;
|
|
7
|
+
} | {
|
|
8
|
+
color?: undefined;
|
|
9
|
+
};
|
|
10
|
+
export { format, parseDateFormat, getStyle };
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import numfmt from 'numfmt';
|
|
2
2
|
import { formatString } from './string';
|
|
3
|
-
import { formatDate,
|
|
3
|
+
import { formatDate, parseDateFormat } from './date';
|
|
4
4
|
function format(rawValue, dataType, format) {
|
|
5
5
|
switch(dataType){
|
|
6
6
|
// string formatting
|
|
@@ -18,6 +18,16 @@ function format(rawValue, dataType, format) {
|
|
|
18
18
|
}
|
|
19
19
|
return rawValue;
|
|
20
20
|
}
|
|
21
|
-
|
|
21
|
+
function getStyle(dataType, format, value) {
|
|
22
|
+
switch(dataType){
|
|
23
|
+
case 'number':
|
|
24
|
+
return {
|
|
25
|
+
color: numfmt(format).color(value)
|
|
26
|
+
};
|
|
27
|
+
default:
|
|
28
|
+
return {};
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
export { format, parseDateFormat, getStyle };
|
|
22
32
|
|
|
23
33
|
//# 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,
|
|
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 formatDate(+rawValue, format || 'HH:mm:ss');\n }\n\n return rawValue as string;\n}\n\nfunction getStyle(dataType: DataType, format: string | Format, value: unknown) {\n switch (dataType) {\n case 'number':\n return { color: numfmt(format).color(value) };\n default:\n return {};\n }\n}\n\nexport { format, parseDateFormat, getStyle };\n"],"names":["numfmt","formatString","formatDate","parseDateFormat","format","rawValue","dataType","Date","isNaN","getStyle","value","color"],"mappings":"AAAA,OAAOA,YAAY,SAAS;AAC5B,SAAiBC,YAAY,QAAQ,WAAW;AAChD,SAASC,UAAU,EAAEC,eAAe,QAAQ,SAAS;AAIrD,SAASC,OAAOC,QAAiB,EAAEC,QAAkB,EAAEF,MAAwB;IAC7E,OAAQE;QACN,oBAAoB;QACpB,KAAK;YACH,OAAOL,aAAaI,UAAoBD;QAC1C,kBAAkB;QAClB,KAAK;YACH,OAAOF,WAAWG,oBAAoBE,OAAOF,WAAW,IAAIE,KAAKF,WAAqBD;QACxF,oBAAoB;QACpB,KAAK;YACH,OAAOJ,OAAOI,QAAQI,MAAMH,YAAsBA,WAAW,CAACA;QAChE,sBAAsB;QACtB,KAAK;YACH,OAAOH,WAAW,CAACG,UAAUD,UAAU;IAC3C;IAEA,OAAOC;AACT;AAEA,SAASI,SAASH,QAAkB,EAAEF,MAAuB,EAAEM,KAAc;IAC3E,OAAQJ;QACN,KAAK;YACH,OAAO;gBAAEK,OAAOX,OAAOI,QAAQO,KAAK,CAACD;YAAO;QAC9C;YACE,OAAO,CAAC;IACZ;AACF;AAEA,SAASN,MAAM,EAAED,eAAe,EAAEM,QAAQ,GAAG"}
|