UNPKG

ts-utls

Version:

Utilities for TypeScript library

70 lines (66 loc) 3.43 kB
"use strict"; /* MIT License Copyright (c) 2021 Cyril Dever Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.toMySQLDateOrEmpty = exports.sleep = exports.currentTimestampMillis = void 0; /** * @returns {number} the current Unix timestamp in milliseconds */ const currentTimestampMillis = () => Date.now(); exports.currentTimestampMillis = currentTimestampMillis; /** * Hold thread for the passed time (in milliseconds) * * @param {number} ms - The numbe of milliseconds to wait * @example * // Sleep for 100 ms * await sleep(100) */ const sleep = (ms) => __awaiter(void 0, void 0, void 0, function* () { return new Promise(resolve => setTimeout(resolve, ms)); }); exports.sleep = sleep; /** * Format a timezone-neutralized date to use in SQL statement for a TIMESTAMP or a DATETIME field, eg. * `db.query('SELECT * FROM mytable WHERE mytimefield < ?', [toMySQLDateOrEmpty('Apr 8 2022')])` * * @param {string} date - The string to use as date * @param {boolean} withTime - [optional] Set to `true` to include trailing time (defaut: `false`) * @returns the MySQL-compatible string (or an empty string if the input is not a valid date) */ const toMySQLDateOrEmpty = (date, withTime = false) => { try { const [str, offset] = date.endsWith('Z') ? [date, 0] : [date, new Date(date).getTimezoneOffset() * 60 * 1000]; const d = offset === 0 ? new Date(str) : new Date(new Date(str).getTime() - offset); return d.toJSON().slice(0, withTime ? 19 : 10).replace('T', ' '); } catch (_) { // eslint-disable-line @typescript-eslint/no-unused-vars return ''; } }; exports.toMySQLDateOrEmpty = toMySQLDateOrEmpty; //# sourceMappingURL=time.js.map