@ws-ui/formatter 0.1.11 → 0.1.13

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.js CHANGED
@@ -4,6 +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
8
  export function parseDateFormat(format) {
8
9
  switch(format){
9
10
  case 'Date short':
@@ -27,6 +28,7 @@ export function parseDateFormat(format) {
27
28
  }
28
29
  }
29
30
  export function formatDate(value, format) {
31
+ if (value instanceof Date && value.toString() === INVALID_DATE) return INVALID_DATE;
30
32
  const parsedFormat = parseDateFormat(format);
31
33
  return formatFn(value, parsedFormat);
32
34
  }
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\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"}
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\nconst 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 formatDate(value: Date | number, format: string) {\n if (value instanceof Date && value.toString() === INVALID_DATE) 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","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,MAAMC,eAAe,IAAIC,KAAK,IAAIC,QAAQ;AAE1C,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,WAAWC,KAAoB,EAAEN,MAAc;IAC7D,IAAIM,iBAAiBT,QAAQS,MAAMR,QAAQ,OAAOF,cAAc,OAAOA;IACvE,MAAMW,eAAeR,gBAAgBC;IACrC,OAAOd,SAASoB,OAAOC;AACzB"}
package/dist/index.d.ts CHANGED
@@ -3,8 +3,8 @@ import { parseDateFormat } 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): {
6
- color: any;
7
- } | {
8
6
  color?: undefined;
7
+ } | {
8
+ color: any;
9
9
  };
10
10
  export { format, parseDateFormat, getStyle };
package/dist/index.js CHANGED
@@ -21,9 +21,13 @@ function format(rawValue, dataType, format) {
21
21
  function getStyle(dataType, format, value) {
22
22
  switch(dataType){
23
23
  case 'number':
24
- return {
25
- color: numfmt(format).color(value)
26
- };
24
+ {
25
+ const color = numfmt(format).color(isNaN(value) ? value : +value);
26
+ if (color === 'black' && !format.toLowerCase().includes('[black]')) return {};
27
+ return {
28
+ color
29
+ };
30
+ }
27
31
  default:
28
32
  return {};
29
33
  }
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 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"}
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 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, getStyle };\n"],"names":["numfmt","formatString","formatDate","parseDateFormat","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,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;YAAU;gBACb,MAAMK,QAAQX,OAAOI,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,EAAED,eAAe,EAAEM,QAAQ,GAAG"}