UNPKG

@rnaga/wp-node

Version:

👉 **[View Full Documentation at rnaga.github.io/wp-node →](https://rnaga.github.io/wp-node/)**

117 lines (116 loc) • 4.12 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.formatDate = void 0; exports.formatDateMySQL = formatDateMySQL; exports.currentUnixTimestamp = currentUnixTimestamp; exports.convertYearToString = convertYearToString; const moment_timezone_1 = __importDefault(require("moment-timezone")); const parseDateArgs = (...args) => { if (0 >= args.length) { return { date: undefined, options: undefined }; } const [date, options] = typeof args[0] === "object" && !(args[0] instanceof Date) && !(args[0] instanceof moment_timezone_1.default) ? [undefined, args[0]] : args.length > 1 ? [args[0], args[1]] : [args[0], undefined]; return { date, options }; }; const formatDate = (...args) => { // Parse the date and options from the provided arguments const { date, options } = parseDateArgs(...args); // Set default values for the format, offsetMinutes, and withGMTOffset if not provided const { format = "MM/DD/YYYY hh:mma", // Default format is "MM/DD/YYYY hh:mma" offsetMinutes = 0, // No offset by default withGMTOffset = false, // Do not include GMT offset by default } = options ?? {}; // Initialize a moment object in UTC with the provided date const dateMoment = moment_timezone_1.default.utc(date); // If the output should include the GMT offset and the provided date is a Date object, // adjust for the local timezone. This adjustment is necessary because JavaScript Date objects, // regardless of the local timezone in which they are created, are internally converted to GMT. if (withGMTOffset && date instanceof Date) { // Subtract the local timezone offset to convert to local time dateMoment.subtract(new Date().getTimezoneOffset(), "minutes"); } // If an offset in minutes is provided, apply it to the date if (typeof offsetMinutes === "number") { dateMoment.add(offsetMinutes, "minutes"); } // Format and return the date as a string according to the specified format return dateMoment.format(format); }; exports.formatDate = formatDate; function formatDateMySQL(...args) { const { date, options } = parseDateArgs(...args); return (0, exports.formatDate)(date, { ...options, format: "YYYY-MM-DD HH:mm:ss", }); } // time() function currentUnixTimestamp() { return Math.floor(new Date().getTime() / 1000); } function convertYearToString(n) { const year = n ?? new Date().getFullYear(); const numberToWords = (n) => { const belowTwenty = [ "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", ]; const tens = [ "", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety", ]; if (n < 20) { return belowTwenty[n]; } else if (n < 100) { return (tens[Math.floor(n / 10)] + (n % 10 !== 0 ? belowTwenty[n % 10] : "")); } else if (n < 1000) { return (belowTwenty[Math.floor(n / 100)] + "hundred" + (n % 100 !== 0 ? numberToWords(n % 100) : "")); } else if (n < 10000) { const firstTwoDigits = Math.floor(n / 100); const lastTwoDigits = n % 100; return numberToWords(firstTwoDigits) + numberToWords(lastTwoDigits); } return "twentytwentyfour"; }; return numberToWords(year).replace(/\s+/g, ""); }