@ws-ui/formatter 0.1.3 → 0.1.5
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 -0
- package/dist/date.js +30 -0
- package/dist/date.js.map +1 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +9 -25
- package/dist/index.js.map +1 -1
- package/dist/string.js +1 -11
- package/dist/string.js.map +1 -1
- package/out/bundle.min.js +15 -1
- package/out/bundle.min.js.map +4 -4
- package/package.json +2 -1
package/dist/date.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function formatDate(value: Date | number, format: string): string;
|
package/dist/date.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import formatFn from 'date-fns/format';
|
|
2
|
+
import Intl from '@ws-ui/intl';
|
|
3
|
+
import(`@ws-ui/intl/locale-data/jsonp/${typeof navigator !== 'undefined' ? navigator.language : 'en'}`);
|
|
4
|
+
function getFormat(options) {
|
|
5
|
+
return new Intl.DateTimeFormat(undefined, options).resolvedOptions().resolvedFormat;
|
|
6
|
+
}
|
|
7
|
+
export function formatDate(value, format) {
|
|
8
|
+
switch(format){
|
|
9
|
+
case 'Date short':
|
|
10
|
+
return formatFn(value, getFormat());
|
|
11
|
+
case 'Date long':
|
|
12
|
+
return formatFn(value, getFormat({
|
|
13
|
+
weekday: 'long',
|
|
14
|
+
year: 'numeric',
|
|
15
|
+
month: 'long',
|
|
16
|
+
day: 'numeric'
|
|
17
|
+
}));
|
|
18
|
+
case 'Date abbreviated':
|
|
19
|
+
return formatFn(value, getFormat({
|
|
20
|
+
weekday: 'short',
|
|
21
|
+
year: 'numeric',
|
|
22
|
+
month: 'short',
|
|
23
|
+
day: 'numeric'
|
|
24
|
+
}));
|
|
25
|
+
default:
|
|
26
|
+
return formatFn(value, format);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
//# sourceMappingURL=date.js.map
|
package/dist/date.js.map
ADDED
|
@@ -0,0 +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 getFormat(options?: Intl.DateTimeFormatOptions) {\n return (\n new Intl.DateTimeFormat(undefined, options).resolvedOptions() as ResolvedDateTimeFormatOptions\n ).resolvedFormat;\n}\n\nexport function formatDate(value: Date | number, format: string) {\n switch (format) {\n case 'Date short':\n return formatFn(value, getFormat());\n case 'Date long':\n return formatFn(\n value,\n getFormat({\n weekday: 'long',\n year: 'numeric',\n month: 'long',\n day: 'numeric',\n }),\n );\n case 'Date abbreviated':\n return formatFn(\n value,\n getFormat({\n weekday: 'short',\n year: 'numeric',\n month: 'short',\n day: 'numeric',\n }),\n );\n default:\n return formatFn(value, format);\n }\n}\n"],"names":["formatFn","Intl","navigator","language","getFormat","options","DateTimeFormat","undefined","resolvedOptions","resolvedFormat","formatDate","value","format","weekday","year","month","day"],"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,UAAUC,OAAoC;IACrD,OAAO,AACL,IAAIJ,KAAKK,cAAc,CAACC,WAAWF,SAASG,eAAe,GAC3DC,cAAc;AAClB;AAEA,OAAO,SAASC,WAAWC,KAAoB,EAAEC,MAAc;IAC7D,OAAQA;QACN,KAAK;YACH,OAAOZ,SAASW,OAAOP;QACzB,KAAK;YACH,OAAOJ,SACLW,OACAP,UAAU;gBACRS,SAAS;gBACTC,MAAM;gBACNC,OAAO;gBACPC,KAAK;YACP;QAEJ,KAAK;YACH,OAAOhB,SACLW,OACAP,UAAU;gBACRS,SAAS;gBACTC,MAAM;gBACNC,OAAO;gBACPC,KAAK;YACP;QAEJ;YACE,OAAOhB,SAASW,OAAOC;IAC3B;AACF"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import { Format } from './string';
|
|
2
|
-
export declare function format(rawValue: unknown, dataType: 'string' | 'date' | 'number' | 'duration', format
|
|
2
|
+
export declare function format(rawValue: unknown, dataType: 'string' | 'date' | 'number' | 'duration', format?: string | Format): string;
|
package/dist/index.js
CHANGED
|
@@ -1,36 +1,20 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
Object.defineProperty(exports, "format", {
|
|
6
|
-
enumerable: true,
|
|
7
|
-
get: function() {
|
|
8
|
-
return format;
|
|
9
|
-
}
|
|
10
|
-
});
|
|
11
|
-
const _format = /*#__PURE__*/ _interop_require_default(require("date-fns/format"));
|
|
12
|
-
const _numfmt = /*#__PURE__*/ _interop_require_default(require("numfmt"));
|
|
13
|
-
const _string = require("./string");
|
|
14
|
-
function _interop_require_default(obj) {
|
|
15
|
-
return obj && obj.__esModule ? obj : {
|
|
16
|
-
default: obj
|
|
17
|
-
};
|
|
18
|
-
}
|
|
19
|
-
function format(rawValue, dataType, format) {
|
|
1
|
+
import numfmt from 'numfmt';
|
|
2
|
+
import { formatString } from './string';
|
|
3
|
+
import { formatDate } from './date';
|
|
4
|
+
export function format(rawValue, dataType, format) {
|
|
20
5
|
switch(dataType){
|
|
21
6
|
// string formatting
|
|
22
7
|
case 'string':
|
|
23
|
-
return
|
|
8
|
+
return formatString(rawValue, format);
|
|
24
9
|
// date formatting
|
|
25
10
|
case 'date':
|
|
26
|
-
return (
|
|
11
|
+
return formatDate(rawValue instanceof Date ? rawValue : new Date(rawValue), format);
|
|
27
12
|
// number formatting
|
|
28
13
|
case 'number':
|
|
29
|
-
return (
|
|
14
|
+
return numfmt(format)(+rawValue);
|
|
15
|
+
// duration formatting
|
|
30
16
|
case 'duration':
|
|
31
|
-
|
|
32
|
-
return (0, _format.default)(+rawValue, format || 'HH:mm:ss');
|
|
33
|
-
}
|
|
17
|
+
return formatDate(+rawValue, format || 'HH:mm:ss');
|
|
34
18
|
}
|
|
35
19
|
}
|
|
36
20
|
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import numfmt from 'numfmt';\nimport { Format, formatString } from './string';\nimport { formatDate } from './date';\n\nexport function format(\n rawValue: unknown,\n dataType: 'string' | 'date' | 'number' | 'duration',\n format?: string | Format,\n): string {\n switch (dataType) {\n // string formatting\n case 'string':\n return formatString(rawValue as string, format as Format);\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)(+rawValue);\n // duration formatting\n case 'duration':\n return formatDate(+rawValue, format || 'HH:mm:ss');\n }\n}\n"],"names":["numfmt","formatString","formatDate","format","rawValue","dataType","Date"],"mappings":"AAAA,OAAOA,YAAY,SAAS;AAC5B,SAAiBC,YAAY,QAAQ,WAAW;AAChD,SAASC,UAAU,QAAQ,SAAS;AAEpC,OAAO,SAASC,OACdC,QAAiB,EACjBC,QAAmD,EACnDF,MAAwB;IAExB,OAAQE;QACN,oBAAoB;QACpB,KAAK;YACH,OAAOJ,aAAaG,UAAoBD;QAC1C,kBAAkB;QAClB,KAAK;YACH,OAAOD,WAAWE,oBAAoBE,OAAOF,WAAW,IAAIE,KAAKF,WAAqBD;QACxF,oBAAoB;QACpB,KAAK;YACH,OAAOH,OAAOG,QAAQ,CAACC;QACzB,sBAAsB;QACtB,KAAK;YACH,OAAOF,WAAW,CAACE,UAAUD,UAAU;IAC3C;AACF"}
|
package/dist/string.js
CHANGED
|
@@ -1,13 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", {
|
|
3
|
-
value: true
|
|
4
|
-
});
|
|
5
|
-
Object.defineProperty(exports, "formatString", {
|
|
6
|
-
enumerable: true,
|
|
7
|
-
get: function() {
|
|
8
|
-
return formatString;
|
|
9
|
-
}
|
|
10
|
-
});
|
|
11
1
|
function _capitalizeEachWord(value) {
|
|
12
2
|
return value.split(' ').map(_toLower).map(_capitalize).join(' ');
|
|
13
3
|
}
|
|
@@ -20,7 +10,7 @@ function _toLower(value) {
|
|
|
20
10
|
function _capitalize(value) {
|
|
21
11
|
return value ? value.charAt(0).toUpperCase() + value.slice(1).toLowerCase() : '';
|
|
22
12
|
}
|
|
23
|
-
function formatString(value, format) {
|
|
13
|
+
export function formatString(value, format) {
|
|
24
14
|
switch(format){
|
|
25
15
|
case 'U':
|
|
26
16
|
return _upperCase(_toLower(value));
|
package/dist/string.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/string.ts"],"sourcesContent":["function _capitalizeEachWord(value: string) {\n return value.split(' ').map(_toLower).map(_capitalize).join(' ');\n}\n\nfunction _upperCase(value: string) {\n return value.toUpperCase();\n}\n\nfunction _toLower(value: string) {\n return value.toLowerCase();\n}\n\nfunction _capitalize(value: string) {\n return value ? value.charAt(0).toUpperCase() + value.slice(1).toLowerCase() : '';\n}\n\nexport type Format = 'U' | 'l' | 'C' | 'c';\n\nexport function formatString(value: string, format: Format) {\n switch (format) {\n case 'U':\n return _upperCase(_toLower(value));\n case 'l':\n return _toLower(value);\n case 'C':\n return _capitalize(_toLower(value));\n case 'c':\n return _capitalizeEachWord(value);\n default:\n return value;\n }\n}\n"],"names":["
|
|
1
|
+
{"version":3,"sources":["../src/string.ts"],"sourcesContent":["function _capitalizeEachWord(value: string) {\n return value.split(' ').map(_toLower).map(_capitalize).join(' ');\n}\n\nfunction _upperCase(value: string) {\n return value.toUpperCase();\n}\n\nfunction _toLower(value: string) {\n return value.toLowerCase();\n}\n\nfunction _capitalize(value: string) {\n return value ? value.charAt(0).toUpperCase() + value.slice(1).toLowerCase() : '';\n}\n\nexport type Format = 'U' | 'l' | 'C' | 'c';\n\nexport function formatString(value: string, format: Format) {\n switch (format) {\n case 'U':\n return _upperCase(_toLower(value));\n case 'l':\n return _toLower(value);\n case 'C':\n return _capitalize(_toLower(value));\n case 'c':\n return _capitalizeEachWord(value);\n default:\n return value;\n }\n}\n"],"names":["_capitalizeEachWord","value","split","map","_toLower","_capitalize","join","_upperCase","toUpperCase","toLowerCase","charAt","slice","formatString","format"],"mappings":"AAAA,SAASA,oBAAoBC,KAAa;IACxC,OAAOA,MAAMC,KAAK,CAAC,KAAKC,GAAG,CAACC,UAAUD,GAAG,CAACE,aAAaC,IAAI,CAAC;AAC9D;AAEA,SAASC,WAAWN,KAAa;IAC/B,OAAOA,MAAMO,WAAW;AAC1B;AAEA,SAASJ,SAASH,KAAa;IAC7B,OAAOA,MAAMQ,WAAW;AAC1B;AAEA,SAASJ,YAAYJ,KAAa;IAChC,OAAOA,QAAQA,MAAMS,MAAM,CAAC,GAAGF,WAAW,KAAKP,MAAMU,KAAK,CAAC,GAAGF,WAAW,KAAK;AAChF;AAIA,OAAO,SAASG,aAAaX,KAAa,EAAEY,MAAc;IACxD,OAAQA;QACN,KAAK;YACH,OAAON,WAAWH,SAASH;QAC7B,KAAK;YACH,OAAOG,SAASH;QAClB,KAAK;YACH,OAAOI,YAAYD,SAASH;QAC9B,KAAK;YACH,OAAOD,oBAAoBC;QAC7B;YACE,OAAOA;IACX;AACF"}
|