@powfix/core-js 0.13.13 → 0.13.15

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,5 +1,6 @@
1
1
  import { MomentInput, RelativeTimeSpec } from "moment";
2
2
  import { DATE } from "../constants/DATE";
3
+ export type DateUtilsFrom = number | bigint | string | Date;
3
4
  export declare class DateUtils {
4
5
  static isPositiveInteger(value: unknown, strict?: boolean): boolean;
5
6
  static isMilliseconds(value: unknown, strict?: boolean): boolean;
@@ -7,6 +8,11 @@ export declare class DateUtils {
7
8
  static isUnix(value: unknown, strict?: boolean): boolean;
8
9
  static isISO8601(value: unknown): boolean;
9
10
  static isValid(value: unknown, format?: DATE.FORMAT, strict?: boolean): boolean;
11
+ static from(value: DateUtilsFrom, format?: DATE.FORMAT, strict?: boolean): Date;
12
+ static fromUnix(value: DateUtilsFrom, strict?: boolean): Date;
13
+ static fromSeconds(value: DateUtilsFrom, strict?: boolean): Date;
14
+ static fromMilliseconds(value: DateUtilsFrom, strict?: boolean): Date;
15
+ static fromISO8601(value: string): Date;
10
16
  static relativeDate: (input: MomentInput, base?: MomentInput) => string;
11
17
  static setLocale: (language: DateUtils.Locale) => void;
12
18
  private static getRelativeTimeSpec;
@@ -65,6 +65,45 @@ class DateUtils {
65
65
  }
66
66
  }
67
67
  }
68
+ static from(value, format, strict) {
69
+ var _a;
70
+ if (value instanceof Date) {
71
+ return value;
72
+ }
73
+ if (!DateUtils.isValid(value, format, strict)) {
74
+ throw new Error('invalid date');
75
+ }
76
+ if (format === undefined) {
77
+ if (DateUtils.isMilliseconds(value, strict)) {
78
+ return DateUtils.from(value, DATE_1.DATE.FORMAT.MILLISECONDS);
79
+ }
80
+ if (DateUtils.isISO8601(value)) {
81
+ return DateUtils.from(value, DATE_1.DATE.FORMAT.ISO_8601);
82
+ }
83
+ throw new Error(`no option to convert value to date`);
84
+ }
85
+ switch (format) {
86
+ case DATE_1.DATE.FORMAT.UNIX: return new Date(value * 1000);
87
+ case DATE_1.DATE.FORMAT.SECONDS: return new Date(value * 1000);
88
+ case DATE_1.DATE.FORMAT.MILLISECONDS: return new Date(value);
89
+ case DATE_1.DATE.FORMAT.ISO_8601: return new Date(value);
90
+ default: {
91
+ throw new Error(`unknown format: ${format}(${(_a = DATE_1.DATE.FORMAT.toString(format)) !== null && _a !== void 0 ? _a : 'unknown'})`);
92
+ }
93
+ }
94
+ }
95
+ static fromUnix(value, strict) {
96
+ return DateUtils.from(value, DATE_1.DATE.FORMAT.UNIX, strict);
97
+ }
98
+ static fromSeconds(value, strict) {
99
+ return DateUtils.from(value, DATE_1.DATE.FORMAT.SECONDS, strict);
100
+ }
101
+ static fromMilliseconds(value, strict) {
102
+ return DateUtils.from(value, DATE_1.DATE.FORMAT.MILLISECONDS, strict);
103
+ }
104
+ static fromISO8601(value) {
105
+ return DateUtils.from(value, DATE_1.DATE.FORMAT.ISO_8601);
106
+ }
68
107
  }
69
108
  exports.DateUtils = DateUtils;
70
109
  DateUtils.relativeDate = (input, base = Date.now()) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@powfix/core-js",
3
- "version": "0.13.13",
3
+ "version": "0.13.15",
4
4
  "description": "core package",
5
5
  "author": "Kwon Kyung-Min <powfix@gmail.com>",
6
6
  "private": false,