UNPKG

7.59 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var tslib_1 = require("tslib");
4/**
5 * Provides utilities to transform hydrated and persisted data.
6 */
7var DateUtils = /** @class */ (function () {
8 function DateUtils() {
9 }
10 // -------------------------------------------------------------------------
11 // Public Static Methods
12 // -------------------------------------------------------------------------
13 /**
14 * Normalizes date object hydrated from the database.
15 */
16 DateUtils.normalizeHydratedDate = function (mixedDate) {
17 if (!mixedDate)
18 return mixedDate;
19 return typeof mixedDate === "string" ? new Date(mixedDate) : mixedDate;
20 };
21 /**
22 * Converts given value into date string in a "YYYY-MM-DD" format.
23 */
24 DateUtils.mixedDateToDateString = function (value) {
25 if (value instanceof Date)
26 return this.formatZerolessValue(value.getFullYear()) + "-" + this.formatZerolessValue(value.getMonth() + 1) + "-" + this.formatZerolessValue(value.getDate());
27 return value;
28 };
29 /**
30 * Converts given value into date object.
31 */
32 DateUtils.mixedDateToDate = function (mixedDate, toUtc, useMilliseconds) {
33 if (toUtc === void 0) { toUtc = false; }
34 if (useMilliseconds === void 0) { useMilliseconds = true; }
35 var date = typeof mixedDate === "string" ? new Date(mixedDate) : mixedDate;
36 if (toUtc)
37 date = new Date(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(), date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds(), date.getUTCMilliseconds());
38 if (!useMilliseconds)
39 date.setUTCMilliseconds(0);
40 return date;
41 };
42 /**
43 * Converts given value into time string in a "HH:mm:ss" format.
44 */
45 DateUtils.mixedDateToTimeString = function (value, skipSeconds) {
46 if (skipSeconds === void 0) { skipSeconds = false; }
47 if (value instanceof Date)
48 return this.formatZerolessValue(value.getHours()) +
49 ":" + this.formatZerolessValue(value.getMinutes()) +
50 (!skipSeconds ? ":" + this.formatZerolessValue(value.getSeconds()) : "");
51 return value;
52 };
53 /**
54 * Converts given value into time string in a "HH:mm:ss" format.
55 */
56 DateUtils.mixedTimeToDate = function (value) {
57 if (typeof value === "string") {
58 var _a = tslib_1.__read(value.split(":"), 3), hours = _a[0], minutes = _a[1], seconds = _a[2];
59 var date = new Date();
60 if (hours)
61 date.setHours(parseInt(hours));
62 if (minutes)
63 date.setMinutes(parseInt(minutes));
64 if (seconds)
65 date.setSeconds(parseInt(seconds));
66 return date;
67 }
68 return value;
69 };
70 /**
71 * Converts given string value with "-" separator into a "HH:mm:ss" format.
72 */
73 DateUtils.mixedTimeToString = function (value, skipSeconds) {
74 if (skipSeconds === void 0) { skipSeconds = false; }
75 value = value instanceof Date ? (value.getHours() + ":" + value.getMinutes() + (!skipSeconds ? ":" + value.getSeconds() : "")) : value;
76 if (typeof value === "string") {
77 return value.split(":")
78 .map(function (v) { return v.length === 1 ? "0" + v : v; }) // append zero at beginning if we have a first-zero-less number
79 .join(":");
80 }
81 return value;
82 };
83 /**
84 * Converts given value into datetime string in a "YYYY-MM-DD HH-mm-ss" format.
85 */
86 DateUtils.mixedDateToDatetimeString = function (value) {
87 if (typeof value === "string") {
88 value = new Date(value);
89 }
90 if (value instanceof Date) {
91 return this.formatZerolessValue(value.getFullYear()) + "-" +
92 this.formatZerolessValue(value.getMonth() + 1) + "-" +
93 this.formatZerolessValue(value.getDate()) + " " +
94 this.formatZerolessValue(value.getHours()) + ":" +
95 this.formatZerolessValue(value.getMinutes()) + ":" +
96 this.formatZerolessValue(value.getSeconds()) + "." +
97 this.formatMilliseconds(value.getMilliseconds());
98 }
99 return value;
100 };
101 /**
102 * Converts given value into utc datetime string in a "YYYY-MM-DD HH-mm-ss.sss" format.
103 */
104 DateUtils.mixedDateToUtcDatetimeString = function (value) {
105 if (typeof value === "string") {
106 value = new Date(value);
107 }
108 if (value instanceof Date) {
109 return this.formatZerolessValue(value.getUTCFullYear()) + "-" +
110 this.formatZerolessValue(value.getUTCMonth() + 1) + "-" +
111 this.formatZerolessValue(value.getUTCDate()) + " " +
112 this.formatZerolessValue(value.getUTCHours()) + ":" +
113 this.formatZerolessValue(value.getUTCMinutes()) + ":" +
114 this.formatZerolessValue(value.getUTCSeconds()) + "." +
115 this.formatMilliseconds(value.getUTCMilliseconds());
116 }
117 return value;
118 };
119 /**
120 * Converts each item in the given array to string joined by "," separator.
121 */
122 DateUtils.simpleArrayToString = function (value) {
123 if (value instanceof Array) {
124 return value
125 .map(function (i) { return String(i); })
126 .join(",");
127 }
128 return value;
129 };
130 /**
131 * Converts given string to simple array split by "," separator.
132 */
133 DateUtils.stringToSimpleArray = function (value) {
134 if (value instanceof String || typeof value === "string") {
135 if (value.length > 0) {
136 return value.split(",");
137 }
138 else {
139 return [];
140 }
141 }
142 return value;
143 };
144 DateUtils.simpleJsonToString = function (value) {
145 return JSON.stringify(value);
146 };
147 DateUtils.stringToSimpleJson = function (value) {
148 try {
149 var simpleJSON = JSON.parse(value);
150 return (typeof simpleJSON === "object") ? simpleJSON : {};
151 }
152 catch (err) {
153 return {};
154 }
155 };
156 DateUtils.simpleEnumToString = function (value) {
157 return "" + value;
158 };
159 DateUtils.stringToSimpleEnum = function (value, columnMetadata) {
160 if (columnMetadata.enum
161 && !isNaN(value)
162 && columnMetadata.enum.indexOf(parseInt(value)) >= 0) {
163 // convert to number if that exists in poosible enum options
164 value = parseInt(value);
165 }
166 return value;
167 };
168 // -------------------------------------------------------------------------
169 // Private Static Methods
170 // -------------------------------------------------------------------------
171 /**
172 * Formats given number to "0x" format, e.g. if it is 1 then it will return "01".
173 */
174 DateUtils.formatZerolessValue = function (value) {
175 if (value < 10)
176 return "0" + value;
177 return String(value);
178 };
179 /**
180 * Formats given number to "0x" format, e.g. if it is 1 then it will return "01".
181 */
182 DateUtils.formatMilliseconds = function (value) {
183 if (value < 10) {
184 return "00" + value;
185 }
186 else if (value < 100) {
187 return "0" + value;
188 }
189 else {
190 return String(value);
191 }
192 };
193 return DateUtils;
194}());
195exports.DateUtils = DateUtils;
196
197//# sourceMappingURL=DateUtils.js.map