| declare const _default: (format: string, date?: Date) => string; | ||
| export = _default; |
+149
| // years | ||
| const yyyy = (date: Date) => date.getFullYear().toString(); // {yyyy} | ||
| const yy = (date: Date) => date.getFullYear().toString().slice(-2); // {yy} | ||
| // months | ||
| const m = (date: Date) => (date.getMonth() + 1).toString(); // {m} | ||
| const mm = (date: Date) => { | ||
| const monthInt = date.getMonth() + 1; | ||
| return monthInt < 10 ? '0' + monthInt : monthInt.toString(); // {mm} | ||
| } | ||
| const month = (date: Date) => { | ||
| const months = [ | ||
| 'January', | ||
| 'February', | ||
| 'March', | ||
| 'April', | ||
| 'May', | ||
| 'June', | ||
| 'July', | ||
| 'August', | ||
| 'September', | ||
| 'October', | ||
| 'November', | ||
| 'December' | ||
| ]; | ||
| return months[date.getMonth()]; // {Month} | ||
| } | ||
| const mo = (date: Date) => month(date).substr(0, 3); | ||
| // days | ||
| const day = (date: Date) => date.getDate().toString(); | ||
| const dd = (date: Date) => { | ||
| const d = date.getDate(); | ||
| return d < 10 ? '0' + d : d.toString(); // {dd} | ||
| } | ||
| const dayWithSuffix = (date: Date) => { | ||
| const d = date.getDate(); | ||
| const daySuffixes = Object.create(null); | ||
| daySuffixes.st = [1, 21, 31]; | ||
| daySuffixes.nd = [2, 22]; | ||
| daySuffixes.rd = [3, 23]; | ||
| // loop through 'st', 'nd', and 'rd' (suffixes like '1st', '2nd', etc) | ||
| // if suffix is not found, it is 'th' (most numbers) | ||
| let suffixNotFound = true; | ||
| let suffix = ''; | ||
| for (suffix in daySuffixes) { | ||
| if (daySuffixes[suffix].indexOf(d) > -1) { | ||
| suffixNotFound = false; | ||
| break; | ||
| } | ||
| } | ||
| if (suffixNotFound) suffix = 'th'; | ||
| return d + suffix; | ||
| } | ||
| const weekday = (date: Date) => { | ||
| const weekdays = [ | ||
| 'Monday', | ||
| 'Tuesday', | ||
| 'Wednesday', | ||
| 'Thursday', | ||
| 'Friday', | ||
| 'Saturday', | ||
| 'Sunday' | ||
| ]; | ||
| return weekdays[date.getDay()]; | ||
| } | ||
| const weekday3 = (date: Date) => weekday(date).substr(0, 3); | ||
| const weekday2 = (date: Date) => weekday(date).substr(0, 2); | ||
| const weekday1 = (date: Date) => weekday(date)[0]; | ||
| // hours | ||
| const hours24 = (date: Date) => date.getHours().toString(); | ||
| const hh24 = (date: Date) => { | ||
| const h = date.getHours(); | ||
| return h < 10 ? '0' + h : h.toString(); | ||
| } | ||
| const hours = (date: Date) => { | ||
| const h = date.getHours(); | ||
| return h % 12 === 0 ? '12' : (h % 12).toString(); // {h} | ||
| } | ||
| // relies on `hours` function | ||
| const hh = (date: Date) => { | ||
| const h = date.getHours(); | ||
| const n = h % 12 === 0 ? 12 : h % 12; | ||
| return n < 10 ? '0' + n : n.toString(); | ||
| } | ||
| const ampm = (date: Date) => date.getHours() < 12 ? 'am' : 'pm'; // {ampm} | ||
| const AMPM = (date: Date) => date.getHours() < 12 ? 'AM' : 'PM'; // {ampm} | ||
| // minutes, seconds | ||
| const minutes = (date: Date) => { | ||
| const min = date.getMinutes(); | ||
| return min < 10 ? '0' + min : min.toString(); | ||
| } | ||
| const seconds = (date: Date) => { | ||
| const sec = date.getSeconds(); | ||
| return sec < 10 ? '0' + sec : sec.toString(); | ||
| } | ||
| interface DateMap { | ||
| [key: string]: (date: Date) => string; | ||
| } | ||
| const map: DateMap = { | ||
| '{yyyy}': yyyy, | ||
| '{yy}': yy, | ||
| '{m}': m, | ||
| '{mm}': mm, | ||
| '{Month}': month, | ||
| '{Mo}': mo, | ||
| '{d}': day, | ||
| '{dd}': dd, | ||
| '{ds}': dayWithSuffix, | ||
| '{Weekday}': weekday, | ||
| '{Day}': weekday3, | ||
| '{Dy}': weekday2, | ||
| '{D}': weekday1, | ||
| '{h24}': hours24, | ||
| '{hh24}': hh24, | ||
| '{h}': hours, | ||
| '{hh}': hh, | ||
| '{ampm}': ampm, | ||
| '{AMPM}': AMPM, | ||
| '{Minutes}': minutes, | ||
| '{Seconds}': seconds | ||
| }; | ||
| const regex = /\{yyyy\}|\{yy\}|\{m\}|\{mm\}|\{Month\}|\{Mo\}|\{d\}|\{dd\}|\{ds\}|\{Weekday\}|\{Day\}|\{Dy\}|\{D\}|\{h24\}|\{hh24\}|\{h\}|\{hh\}|\{ampm\}|\{AMPM\}|\{Minutes\}|\{Seconds\}/g; | ||
| /* | ||
| formatDate('{mm}/{dd}/{yyyy}', new Date(2001, 0, 1)) => '01/01/2001' | ||
| */ | ||
| export = function formatDate(format: string, date: Date = new Date()): string { | ||
| return format.replace(regex, (match) => map[match](date)); | ||
| } |
| { | ||
| "compilerOptions": { | ||
| "lib": ["es2015"], | ||
| "noUnusedLocals": true, | ||
| "strict": true | ||
| } | ||
| } |
+95
-149
@@ -0,150 +1,98 @@ | ||
| "use strict"; | ||
| // years | ||
| function yyyy(date) { | ||
| return date.getFullYear().toString(); // {yyyy} | ||
| } | ||
| function yy(date) { | ||
| return date.getFullYear().toString().slice(-2); // {yy} | ||
| } | ||
| var yyyy = function (date) { return date.getFullYear().toString(); }; // {yyyy} | ||
| var yy = function (date) { return date.getFullYear().toString().slice(-2); }; // {yy} | ||
| // months | ||
| function m(date) { | ||
| var monthInt = date.getMonth() + 1; | ||
| return monthInt.toString(); // {m} | ||
| } | ||
| function mm(date) { | ||
| var monthInt = date.getMonth() + 1; | ||
| var mth = monthInt.toString(); | ||
| return monthInt < 10 ? '0' + mth : mth; // {mm} | ||
| } | ||
| function month(date) { | ||
| var months = [ | ||
| 'January', | ||
| 'February', | ||
| 'March', | ||
| 'April', | ||
| 'May', | ||
| 'June', | ||
| 'July', | ||
| 'August', | ||
| 'September', | ||
| 'October', | ||
| 'November', | ||
| 'December' | ||
| ]; | ||
| return months[date.getMonth()]; // {Month} | ||
| } | ||
| function mo(date) { | ||
| return month(date).substr(0, 3); | ||
| } | ||
| var m = function (date) { return (date.getMonth() + 1).toString(); }; // {m} | ||
| var mm = function (date) { | ||
| var monthInt = date.getMonth() + 1; | ||
| return monthInt < 10 ? '0' + monthInt : monthInt.toString(); // {mm} | ||
| }; | ||
| var month = function (date) { | ||
| var months = [ | ||
| 'January', | ||
| 'February', | ||
| 'March', | ||
| 'April', | ||
| 'May', | ||
| 'June', | ||
| 'July', | ||
| 'August', | ||
| 'September', | ||
| 'October', | ||
| 'November', | ||
| 'December' | ||
| ]; | ||
| return months[date.getMonth()]; // {Month} | ||
| }; | ||
| var mo = function (date) { return month(date).substr(0, 3); }; | ||
| // days | ||
| function day(date) { | ||
| return date.getDate(); | ||
| } | ||
| function dd(date) { | ||
| var d = date.getDate(); | ||
| return d < 10 ? '0' + d : d; // {dd} | ||
| } | ||
| function dayWithSuffix(date) { | ||
| var d = date.getDate(); | ||
| var daySuffixes = Object.create(null); | ||
| daySuffixes.st = [1, 21, 31]; | ||
| daySuffixes.nd = [2, 22]; | ||
| daySuffixes.rd = [3, 23]; | ||
| // loop through 'st', 'nd', and 'rd' (suffixes like '1st', '2nd', etc) | ||
| // if suffix is not found, it is 'th' (most numbers) | ||
| var suffixNotFound = true; | ||
| var suffix; | ||
| for (suffix in daySuffixes) { | ||
| if (daySuffixes[suffix].indexOf(d) > -1) { | ||
| suffixNotFound = false; | ||
| break; | ||
| var day = function (date) { return date.getDate().toString(); }; | ||
| var dd = function (date) { | ||
| var d = date.getDate(); | ||
| return d < 10 ? '0' + d : d.toString(); // {dd} | ||
| }; | ||
| var dayWithSuffix = function (date) { | ||
| var d = date.getDate(); | ||
| var daySuffixes = Object.create(null); | ||
| daySuffixes.st = [1, 21, 31]; | ||
| daySuffixes.nd = [2, 22]; | ||
| daySuffixes.rd = [3, 23]; | ||
| // loop through 'st', 'nd', and 'rd' (suffixes like '1st', '2nd', etc) | ||
| // if suffix is not found, it is 'th' (most numbers) | ||
| var suffixNotFound = true; | ||
| var suffix = ''; | ||
| for (suffix in daySuffixes) { | ||
| if (daySuffixes[suffix].indexOf(d) > -1) { | ||
| suffixNotFound = false; | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| if (suffixNotFound) suffix = 'th'; | ||
| return d + suffix; | ||
| } | ||
| function weekday(date) { | ||
| var weekdays = [ | ||
| 'Monday', | ||
| 'Tuesday', | ||
| 'Wednesday', | ||
| 'Thursday', | ||
| 'Friday', | ||
| 'Saturday', | ||
| 'Sunday' | ||
| ]; | ||
| return weekdays[date.getDay()]; | ||
| } | ||
| function weekday3(date) { | ||
| return weekday(date).substr(0, 3); | ||
| } | ||
| function weekday2(date) { | ||
| return weekday(date).substr(0, 2); | ||
| } | ||
| function weekday1(date) { | ||
| return weekday(date)[0]; | ||
| } | ||
| if (suffixNotFound) | ||
| suffix = 'th'; | ||
| return d + suffix; | ||
| }; | ||
| var weekday = function (date) { | ||
| var weekdays = [ | ||
| 'Monday', | ||
| 'Tuesday', | ||
| 'Wednesday', | ||
| 'Thursday', | ||
| 'Friday', | ||
| 'Saturday', | ||
| 'Sunday' | ||
| ]; | ||
| return weekdays[date.getDay()]; | ||
| }; | ||
| var weekday3 = function (date) { return weekday(date).substr(0, 3); }; | ||
| var weekday2 = function (date) { return weekday(date).substr(0, 2); }; | ||
| var weekday1 = function (date) { return weekday(date)[0]; }; | ||
| // hours | ||
| function hours24(date) { | ||
| return date.getHours(); | ||
| } | ||
| function hh24(date) { | ||
| var h = date.getHours(); | ||
| return h < 10 ? '0' + h : h; | ||
| } | ||
| function hours(date) { | ||
| var h = date.getHours(); | ||
| return h % 12 === 0 ? 12 : h % 12; // {h} | ||
| } | ||
| var hours24 = function (date) { return date.getHours().toString(); }; | ||
| var hh24 = function (date) { | ||
| var h = date.getHours(); | ||
| return h < 10 ? '0' + h : h.toString(); | ||
| }; | ||
| var hours = function (date) { | ||
| var h = date.getHours(); | ||
| return h % 12 === 0 ? '12' : (h % 12).toString(); // {h} | ||
| }; | ||
| // relies on `hours` function | ||
| function hh(date) { | ||
| var h = hours(date); | ||
| return h < 10 ? '0' + h : h; | ||
| } | ||
| function ampm(date) { | ||
| return date.getHours() < 12 ? 'am' : 'pm'; // {ampm} | ||
| } | ||
| function AMPM(date) { | ||
| return date.getHours() < 12 ? 'AM' : 'PM'; // {ampm} | ||
| } | ||
| var hh = function (date) { | ||
| var h = date.getHours(); | ||
| var n = h % 12 === 0 ? 12 : h % 12; | ||
| return n < 10 ? '0' + n : n.toString(); | ||
| }; | ||
| var ampm = function (date) { return date.getHours() < 12 ? 'am' : 'pm'; }; // {ampm} | ||
| var AMPM = function (date) { return date.getHours() < 12 ? 'AM' : 'PM'; }; // {ampm} | ||
| // minutes, seconds | ||
| function minutes(date) { | ||
| var min = date.getMinutes(); | ||
| return min < 10 ? '0' + min : min; | ||
| } | ||
| function seconds(date) { | ||
| var sec = date.getSeconds(); | ||
| return sec < 10 ? '0' + sec : sec; | ||
| } | ||
| /* | ||
| formatDate('{mm}/{dd}/{yyyy}', new Date(2001, 0, 1)) => '01/01/2001' | ||
| */ | ||
| module.exports = function formatDate(format, date) { | ||
| // default to today | ||
| if (!date) date = new Date(); | ||
| var map = { | ||
| var minutes = function (date) { | ||
| var min = date.getMinutes(); | ||
| return min < 10 ? '0' + min : min.toString(); | ||
| }; | ||
| var seconds = function (date) { | ||
| var sec = date.getSeconds(); | ||
| return sec < 10 ? '0' + sec : sec.toString(); | ||
| }; | ||
| var map = { | ||
| '{yyyy}': yyyy, | ||
@@ -171,9 +119,7 @@ '{yy}': yy, | ||
| '{Seconds}': seconds | ||
| }; | ||
| var regex = /\{yyyy\}|\{yy\}|\{m\}|\{mm\}|\{Month\}|\{Mo\}|\{d\}|\{dd\}|\{ds\}|\{Weekday\}|\{Day\}|\{Dy\}|\{D\}|\{h24\}|\{hh24\}|\{h\}|\{hh\}|\{ampm\}|\{AMPM\}|\{Minutes\}|\{Seconds\}/g; | ||
| return format.replace(regex, function(match) { | ||
| return map[match](date); | ||
| }); | ||
| }; | ||
| var regex = /\{yyyy\}|\{yy\}|\{m\}|\{mm\}|\{Month\}|\{Mo\}|\{d\}|\{dd\}|\{ds\}|\{Weekday\}|\{Day\}|\{Dy\}|\{D\}|\{h24\}|\{hh24\}|\{h\}|\{hh\}|\{ampm\}|\{AMPM\}|\{Minutes\}|\{Seconds\}/g; | ||
| module.exports = function formatDate(format, date) { | ||
| if (date === void 0) { date = new Date(); } | ||
| return format.replace(regex, function (match) { return map[match](date); }); | ||
| }; |
+7
-4
| { | ||
| "name": "s-date", | ||
| "version": "1.5.2", | ||
| "version": "1.6.1", | ||
| "description": "Tiny date/time formatter", | ||
| "main": "index.js", | ||
| "types": "index.d.ts", | ||
| "scripts": { | ||
| "coverage": "nyc --reporter=lcov --reporter=text ava", | ||
| "test": "ava" | ||
| "build": "tsc --declaration index.ts", | ||
| "coverage": "npm run build && nyc --reporter=lcov --reporter=text ava", | ||
| "test": "npm run build && ava" | ||
| }, | ||
@@ -27,4 +29,5 @@ "repository": { | ||
| "ava": "0.24.0", | ||
| "nyc": "11.4.1" | ||
| "nyc": "11.4.1", | ||
| "typescript": "3.0.3" | ||
| } | ||
| } |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
86540
5.12%9
50%294
58.92%3
50%1
Infinity%