@ws-ui/formatter 0.1.13 → 0.1.14

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 CHANGED
@@ -1,2 +1,4 @@
1
+ export declare const INVALID_DATE: string;
1
2
  export declare function parseDateFormat(format: string): string;
3
+ export declare function isValidDate(value: Date | number): boolean;
2
4
  export declare function formatDate(value: Date | number, format: string): string;
package/dist/date.js CHANGED
@@ -4,7 +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
+ export const INVALID_DATE = new Date('').toString();
8
8
  export function parseDateFormat(format) {
9
9
  switch(format){
10
10
  case 'Date short':
@@ -27,8 +27,11 @@ export function parseDateFormat(format) {
27
27
  return format;
28
28
  }
29
29
  }
30
+ export function isValidDate(value) {
31
+ return value instanceof Date && value.toString() !== INVALID_DATE;
32
+ }
30
33
  export function formatDate(value, format) {
31
- if (value instanceof Date && value.toString() === INVALID_DATE) return INVALID_DATE;
34
+ if (!isValidDate(value)) return INVALID_DATE;
32
35
  const parsedFormat = parseDateFormat(format);
33
36
  return formatFn(value, parsedFormat);
34
37
  }
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\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"}
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 const 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 isValidDate(value: Date | number) {\n return value instanceof Date && value.toString() !== INVALID_DATE;\n}\n\nexport function formatDate(value: Date | number, format: string) {\n if (!isValidDate(value)) 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","isValidDate","value","formatDate","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,MAAMC,eAAe,IAAIC,KAAK,IAAIC,QAAQ,GAAG;AAEpD,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,YAAYC,KAAoB;IAC9C,OAAOA,iBAAiBT,QAAQS,MAAMR,QAAQ,OAAOF;AACvD;AAEA,OAAO,SAASW,WAAWD,KAAoB,EAAEN,MAAc;IAC7D,IAAI,CAACK,YAAYC,QAAQ,OAAOV;IAChC,MAAMY,eAAeT,gBAAgBC;IACrC,OAAOd,SAASoB,OAAOE;AACzB"}
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { Format } from './string';
2
- import { parseDateFormat } from './date';
2
+ import { parseDateFormat, isValidDate, INVALID_DATE } 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): {
@@ -7,4 +7,4 @@ declare function getStyle(dataType: DataType, format: string | Format, value: un
7
7
  } | {
8
8
  color: any;
9
9
  };
10
- export { format, parseDateFormat, getStyle };
10
+ export { format, parseDateFormat, isValidDate, INVALID_DATE, 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, parseDateFormat } from './date';
3
+ import { formatDate, parseDateFormat, isValidDate, INVALID_DATE } from './date';
4
4
  function format(rawValue, dataType, format) {
5
5
  switch(dataType){
6
6
  // string formatting
@@ -32,6 +32,6 @@ function getStyle(dataType, format, value) {
32
32
  return {};
33
33
  }
34
34
  }
35
- export { format, parseDateFormat, getStyle };
35
+ export { format, parseDateFormat, isValidDate, INVALID_DATE, getStyle };
36
36
 
37
37
  //# 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, 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"}
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 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, isValidDate, INVALID_DATE, getStyle };\n"],"names":["numfmt","formatString","formatDate","parseDateFormat","isValidDate","INVALID_DATE","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;AAIhF,SAASC,OAAOC,QAAiB,EAAEC,QAAkB,EAAEF,MAAwB;IAC7E,OAAQE;QACN,oBAAoB;QACpB,KAAK;YACH,OAAOP,aAAaM,UAAoBD;QAC1C,kBAAkB;QAClB,KAAK;YACH,OAAOJ,WAAWK,oBAAoBE,OAAOF,WAAW,IAAIE,KAAKF,WAAqBD;QACxF,oBAAoB;QACpB,KAAK;YACH,OAAON,OAAOM,QAAQI,MAAMH,YAAsBA,WAAW,CAACA;QAChE,sBAAsB;QACtB,KAAK;YACH,OAAOL,WAAW,CAACK,UAAUD,UAAU;IAC3C;IAEA,OAAOC;AACT;AAEA,SAASI,SAASH,QAAkB,EAAEF,MAAuB,EAAEM,KAAc;IAC3E,OAAQJ;QACN,KAAK;YAAU;gBACb,MAAMK,QAAQb,OAAOM,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,EAAEH,eAAe,EAAEC,WAAW,EAAEC,YAAY,EAAEM,QAAQ,GAAG"}