@ws-ui/formatter 0.1.2 → 0.1.4

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 ADDED
@@ -0,0 +1 @@
1
+ export declare function formatDate(value: Date | number, format: string): string;
package/dist/date.js ADDED
@@ -0,0 +1,84 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "formatDate", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return formatDate;
9
+ }
10
+ });
11
+ const _format = /*#__PURE__*/ _interop_require_default(require("date-fns/format"));
12
+ const _intl = /*#__PURE__*/ _interop_require_default(require("@ws-ui/intl"));
13
+ function _interop_require_default(obj) {
14
+ return obj && obj.__esModule ? obj : {
15
+ default: obj
16
+ };
17
+ }
18
+ function _getRequireWildcardCache(nodeInterop) {
19
+ if (typeof WeakMap !== "function") return null;
20
+ var cacheBabelInterop = new WeakMap();
21
+ var cacheNodeInterop = new WeakMap();
22
+ return (_getRequireWildcardCache = function(nodeInterop) {
23
+ return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
24
+ })(nodeInterop);
25
+ }
26
+ function _interop_require_wildcard(obj, nodeInterop) {
27
+ if (!nodeInterop && obj && obj.__esModule) {
28
+ return obj;
29
+ }
30
+ if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
31
+ return {
32
+ default: obj
33
+ };
34
+ }
35
+ var cache = _getRequireWildcardCache(nodeInterop);
36
+ if (cache && cache.has(obj)) {
37
+ return cache.get(obj);
38
+ }
39
+ var newObj = {};
40
+ var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
41
+ for(var key in obj){
42
+ if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
43
+ var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
44
+ if (desc && (desc.get || desc.set)) {
45
+ Object.defineProperty(newObj, key, desc);
46
+ } else {
47
+ newObj[key] = obj[key];
48
+ }
49
+ }
50
+ }
51
+ newObj.default = obj;
52
+ if (cache) {
53
+ cache.set(obj, newObj);
54
+ }
55
+ return newObj;
56
+ }
57
+ Promise.resolve(`@ws-ui/intl/locale-data/jsonp/${typeof navigator !== 'undefined' ? navigator.language : 'en'}`).then((p)=>/*#__PURE__*/ _interop_require_wildcard(require(p)));
58
+ function getFormat(options) {
59
+ return new _intl.default.DateTimeFormat(undefined, options).resolvedOptions().resolvedFormat;
60
+ }
61
+ function formatDate(value, format) {
62
+ switch(format){
63
+ case 'Date short':
64
+ return (0, _format.default)(value, getFormat());
65
+ case 'Date long':
66
+ return (0, _format.default)(value, getFormat({
67
+ weekday: 'long',
68
+ year: 'numeric',
69
+ month: 'long',
70
+ day: 'numeric'
71
+ }));
72
+ case 'Date abbreviated':
73
+ return (0, _format.default)(value, getFormat({
74
+ weekday: 'short',
75
+ year: 'numeric',
76
+ month: 'short',
77
+ day: 'numeric'
78
+ }));
79
+ default:
80
+ return (0, _format.default)(value, format);
81
+ }
82
+ }
83
+
84
+ //# sourceMappingURL=date.js.map
@@ -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":["formatDate","navigator","language","getFormat","options","Intl","DateTimeFormat","undefined","resolvedOptions","resolvedFormat","value","format","formatFn","weekday","year","month","day"],"mappings":";;;;+BAiBgBA;;;eAAAA;;;+DAjBK;6DACJ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AACjB,gBACE,CAAC,8BAA8B,EAAE,OAAOC,cAAc,cAAcA,UAAUC,QAAQ,GAAG,KAAK,CAAC,oDADjG;AASA,SAASC,UAAUC,OAAoC;IACrD,OAAO,AACL,IAAIC,aAAI,CAACC,cAAc,CAACC,WAAWH,SAASI,eAAe,GAC3DC,cAAc;AAClB;AAEO,SAAST,WAAWU,KAAoB,EAAEC,MAAc;IAC7D,OAAQA;QACN,KAAK;YACH,OAAOC,IAAAA,eAAQ,EAACF,OAAOP;QACzB,KAAK;YACH,OAAOS,IAAAA,eAAQ,EACbF,OACAP,UAAU;gBACRU,SAAS;gBACTC,MAAM;gBACNC,OAAO;gBACPC,KAAK;YACP;QAEJ,KAAK;YACH,OAAOJ,IAAAA,eAAQ,EACbF,OACAP,UAAU;gBACRU,SAAS;gBACTC,MAAM;gBACNC,OAAO;gBACPC,KAAK;YACP;QAEJ;YACE,OAAOJ,IAAAA,eAAQ,EAACF,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', format: string | Format): string;
2
+ export declare function format(rawValue: unknown, dataType: 'string' | 'date' | 'number' | 'duration', format: string | Format): string;
package/dist/index.js CHANGED
@@ -9,25 +9,27 @@ Object.defineProperty(exports, "format", {
9
9
  }
10
10
  });
11
11
  const _numfmt = /*#__PURE__*/ _interop_require_default(require("numfmt"));
12
- const _datefns = require("date-fns");
13
12
  const _string = require("./string");
13
+ const _date = require("./date");
14
14
  function _interop_require_default(obj) {
15
15
  return obj && obj.__esModule ? obj : {
16
16
  default: obj
17
17
  };
18
18
  }
19
19
  function format(rawValue, dataType, format) {
20
- // string formatting
21
- if (dataType === 'string') {
22
- return (0, _string.formatString)(rawValue, format);
23
- }
24
- // number formatting
25
- if (dataType === 'number') {
26
- return (0, _numfmt.default)(format)(+rawValue);
27
- }
28
- // date formatting
29
- if (dataType === 'date') {
30
- return (0, _datefns.format)(rawValue instanceof Date ? rawValue : new Date(rawValue), format);
20
+ switch(dataType){
21
+ // string formatting
22
+ case 'string':
23
+ return (0, _string.formatString)(rawValue, format);
24
+ // date formatting
25
+ case 'date':
26
+ return (0, _date.formatDate)(rawValue instanceof Date ? rawValue : new Date(rawValue), format);
27
+ // number formatting
28
+ case 'number':
29
+ return (0, _numfmt.default)(format)(+rawValue);
30
+ // duration formatting
31
+ case 'duration':
32
+ return (0, _date.formatDate)(+rawValue, format || 'HH:mm:ss');
31
33
  }
32
34
  }
33
35
 
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import numfmt from 'numfmt';\nimport { format as formatDate } from 'date-fns';\n\nimport { formatString, Format } from './string';\n\nexport function format(\n rawValue: unknown,\n dataType: 'string' | 'date' | 'number',\n format: string | Format,\n): string {\n // string formatting\n if (dataType === 'string') {\n return formatString(rawValue as string, format as Format);\n }\n\n // number formatting\n if (dataType === 'number') {\n return numfmt(format)(+rawValue);\n }\n\n // date formatting\n if (dataType === 'date') {\n return formatDate(rawValue instanceof Date ? rawValue : new Date(rawValue as string), format);\n }\n}\n"],"names":["format","rawValue","dataType","formatString","numfmt","formatDate","Date"],"mappings":";;;;+BAKgBA;;;eAAAA;;;+DALG;yBACkB;wBAEA;;;;;;AAE9B,SAASA,OACdC,QAAiB,EACjBC,QAAsC,EACtCF,MAAuB;IAEvB,oBAAoB;IACpB,IAAIE,aAAa,UAAU;QACzB,OAAOC,IAAAA,oBAAY,EAACF,UAAoBD;IAC1C;IAEA,oBAAoB;IACpB,IAAIE,aAAa,UAAU;QACzB,OAAOE,IAAAA,eAAM,EAACJ,QAAQ,CAACC;IACzB;IAEA,kBAAkB;IAClB,IAAIC,aAAa,QAAQ;QACvB,OAAOG,IAAAA,eAAU,EAACJ,oBAAoBK,OAAOL,WAAW,IAAIK,KAAKL,WAAqBD;IACxF;AACF"}
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":["format","rawValue","dataType","formatString","formatDate","Date","numfmt"],"mappings":";;;;+BAIgBA;;;eAAAA;;;+DAJG;wBACkB;sBACV;;;;;;AAEpB,SAASA,OACdC,QAAiB,EACjBC,QAAmD,EACnDF,MAAuB;IAEvB,OAAQE;QACN,oBAAoB;QACpB,KAAK;YACH,OAAOC,IAAAA,oBAAY,EAACF,UAAoBD;QAC1C,kBAAkB;QAClB,KAAK;YACH,OAAOI,IAAAA,gBAAU,EAACH,oBAAoBI,OAAOJ,WAAW,IAAII,KAAKJ,WAAqBD;QACxF,oBAAoB;QACpB,KAAK;YACH,OAAOM,IAAAA,eAAM,EAACN,QAAQ,CAACC;QACzB,sBAAsB;QACtB,KAAK;YACH,OAAOG,IAAAA,gBAAU,EAAC,CAACH,UAAUD,UAAU;IAC3C;AACF"}