plac-micro-common 1.3.54 → 1.3.56

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.
@@ -1,3 +1,4 @@
1
+ export declare const SERVER_DATE_FORMAT = "YYYY-MM-DD";
1
2
  export declare const DISPLAY_DATE_FORMAT = "DD MMM YYYY";
2
3
  export declare const DISPLAY_DATE_TIME_FORMAT = "DD MMM YYYY hh:mm A";
3
4
  export declare const APP_ERROR_CODES: {
@@ -14,7 +14,8 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.APP_ERROR_CODES = exports.DISPLAY_DATE_TIME_FORMAT = exports.DISPLAY_DATE_FORMAT = void 0;
17
+ exports.APP_ERROR_CODES = exports.DISPLAY_DATE_TIME_FORMAT = exports.DISPLAY_DATE_FORMAT = exports.SERVER_DATE_FORMAT = void 0;
18
+ exports.SERVER_DATE_FORMAT = "YYYY-MM-DD";
18
19
  exports.DISPLAY_DATE_FORMAT = "DD MMM YYYY";
19
20
  exports.DISPLAY_DATE_TIME_FORMAT = "DD MMM YYYY hh:mm A";
20
21
  exports.APP_ERROR_CODES = {
@@ -4,6 +4,7 @@ export declare function buildUserFullName(first?: string | null, last?: string |
4
4
  export declare function buildFullNameKh(firstKh?: string | null, lastKh?: string | null): string | null;
5
5
  export declare function formatDate(value?: Date | string | number | null, format?: string): string;
6
6
  export declare function formatDateTime(value?: Date | string | number | null, format?: string): string;
7
+ export declare function formatServerDate(value?: Date | string | number | null): string | null;
7
8
  export declare function formatAmount(amount: number | string, digits?: number, options?: {
8
9
  locale?: string;
9
10
  }): string;
@@ -9,10 +9,13 @@ exports.buildUserFullName = buildUserFullName;
9
9
  exports.buildFullNameKh = buildFullNameKh;
10
10
  exports.formatDate = formatDate;
11
11
  exports.formatDateTime = formatDateTime;
12
+ exports.formatServerDate = formatServerDate;
12
13
  exports.formatAmount = formatAmount;
13
14
  exports.roundAmount = roundAmount;
14
15
  exports.calculateAge = calculateAge;
15
16
  const dayjs_1 = __importDefault(require("dayjs"));
17
+ const customParseFormat_1 = __importDefault(require("dayjs/plugin/customParseFormat"));
18
+ dayjs_1.default.extend(customParseFormat_1.default);
16
19
  const constants_1 = require("../../constants");
17
20
  function delay(ms) {
18
21
  return new Promise((resolve) => setTimeout(resolve, ms));
@@ -29,7 +32,7 @@ function buildFullNameKh(firstKh, lastKh) {
29
32
  const v = `${lastKh ?? ""} ${firstKh ?? ""}`.trim();
30
33
  return v.length ? v : null;
31
34
  }
32
- //--- DISPLAY UTILITIES ---//
35
+ //--- FORMAT UTILITIES ---//
33
36
  function formatDate(value, format = constants_1.DISPLAY_DATE_FORMAT) {
34
37
  if (!value)
35
38
  return "-";
@@ -46,6 +49,34 @@ function formatDateTime(value, format = constants_1.DISPLAY_DATE_TIME_FORMAT) {
46
49
  return "-";
47
50
  return date.format(format);
48
51
  }
52
+ function formatServerDate(value) {
53
+ if (!value)
54
+ return null;
55
+ const INPUT_FORMATS = [
56
+ "YYYY-MM-DD",
57
+ "DD/MM/YYYY",
58
+ "MM/DD/YYYY",
59
+ "YYYY/MM/DD",
60
+ "DD-MM-YYYY",
61
+ "YYYY-MM-DDTHH:mm:ss",
62
+ ];
63
+ let date;
64
+ if (value instanceof Date || typeof value === "number") {
65
+ date = (0, dayjs_1.default)(value);
66
+ }
67
+ else if (typeof value === "string") {
68
+ date = (0, dayjs_1.default)(value, INPUT_FORMATS, true);
69
+ if (!date.isValid()) {
70
+ date = (0, dayjs_1.default)(value);
71
+ }
72
+ }
73
+ else {
74
+ return null;
75
+ }
76
+ if (!date.isValid())
77
+ return null;
78
+ return date.format(constants_1.SERVER_DATE_FORMAT);
79
+ }
49
80
  function formatAmount(amount, digits = 0, options) {
50
81
  const value = Number(amount);
51
82
  if (!Number.isFinite(value))
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "plac-micro-common",
3
- "version": "1.3.54",
3
+ "version": "1.3.56",
4
4
  "types": "dist/index.d.ts",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {