Socket
Socket
Sign inDemoInstall

s-date

Package Overview
Dependencies
0
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.4.1 to 1.5.0

test/test.js

241

index.js

@@ -1,131 +0,114 @@

'use strict';
// ----- years
// ---------------------------------------
// years
function yyyy(date) {
return date.getFullYear().toString(); // {yyyy}
return date.getFullYear().toString(); // {yyyy}
}
function yy(date) {
return date.getFullYear().toString().slice(-2); // {yy}
return date.getFullYear().toString().slice(-2); // {yy}
}
// ----- months
// ---------------------------------------
// months
function m(date) {
var monthInt = date.getMonth() + 1;
return monthInt.toString(); // {m}
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}
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}
var months = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
];
return months[date.getMonth()]; // {Month}
}
// relies on month
function mo(date) {
return month(date).substr(0, 3);
return month(date).substr(0, 3);
}
// ----- days
// ---------------------------------------
// days
function day(date) {
return date.getDate();
return date.getDate();
}
function dd(date) {
var d = date.getDate();
return d < 10 ? '0' + d : d; // {dd}
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];
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;
}
}
// loop through 'st', 'nd', and 'rd' (suffixes like '1st', '2nd', etc)
// if suffix is not found, it is 'th' (most numbers)
var suffixNotFound;
var suffix;
for (suffix in daySuffixes) {
if (daySuffixes[suffix].indexOf(d) > -1) {
suffixNotFound = false;
break;
}
suffixNotFound = true;
}
if (suffixNotFound) {
suffix = 'th';
}
return d + suffix;
if (suffixNotFound) suffix = 'th';
return d + suffix;
}
function weekday(date) {
var weekdays = [
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday',
'Sunday'
];
return weekdays[date.getDay()];
var weekdays = [
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday',
'Sunday'
];
return weekdays[date.getDay()];
}
// relies on weekday
function weekday3(date) {
return weekday(date).substr(0, 3);
return weekday(date).substr(0, 3);
}
// relies on weekday
function weekday2(date) {
return weekday(date).substr(0, 2);
return weekday(date).substr(0, 2);
}
// relies on weekday
function weekday1(date) {
return weekday(date)[0];
return weekday(date)[0];
}
// ----- hours
// ---------------------------------------
// hours
function hours24(date) {
return date.getHours();
return date.getHours();
}
function hh24(date) {
var h = date.getHours();
return h < 10 ? '0' + h : h;
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 h = date.getHours();
return h % 12 === 0 ? 12 : h % 12; // {h}
}

@@ -135,69 +118,65 @@

function hh(date) {
var h = hours(date);
return h < 10 ? '0' + h : h;
var h = hours(date);
return h < 10 ? '0' + h : h;
}
function ampm(date) {
return date.getHours() < 12 ? 'am' : 'pm'; // {ampm}
return date.getHours() < 12 ? 'am' : 'pm'; // {ampm}
}
function AMPM(date) {
return date.getHours() < 12 ? 'AM' : 'PM'; // {ampm}
return date.getHours() < 12 ? 'AM' : 'PM'; // {ampm}
}
// ----- minutes, seconds
// ---------------------------------------
// minutes, seconds
function minutes(date) {
var min = date.getMinutes();
return min < 10 ? '0' + min : min;
var min = date.getMinutes();
return min < 10 ? '0' + min : min;
}
function seconds(date) {
var sec = date.getSeconds();
return sec < 10 ? '0' + sec : sec;
var sec = date.getSeconds();
return sec < 10 ? '0' + sec : sec;
}
// ----- Date formatter
// -- @param format {String}
// -- @param date {Date} optional date context
// -- @return {String}
// ---------------------------------------
/**
* foo vbar
* @param {String}
* @param {Date?}
* @return {String}
*/
module.exports = function formatDate(format, date) {
// default to today
if (!date) {
date = new Date();
}
var map = {
'{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
};
// default to today
if (!date) date = new 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;
var map = {
'{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
};
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;
};
return format.replace(regex, function(match) {
return map[match](date);
});
};
{
"name": "s-date",
"version": "1.4.1",
"version": "1.5.0",
"description": "Tiny date/time formatter",

@@ -16,8 +16,4 @@ "main": "index.js",

"scripts": {
"test": "mocha",
"posttest": "npm run lint",
"lint": "jscs *.js --config=./etc/.jscs.json && eslint *.js -c etc/.eslint.json",
"postlint": "npm run coverage",
"coverage": "istanbul cover node_modules/mocha/bin/_mocha -dir etc --report lcovonly -- test/ -R spec",
"postcoverage": "rm etc/coverage.json"
"coverage": "nyc --reporter=lcov --reporter=text ava",
"test": "ava"
},

@@ -40,10 +36,6 @@ "repository": {

"devDependencies": {
"@seabass/eslint-config": "^1.0.4",
"chai": "^2.3.0",
"ava": "0.24.0",
"codeclimate-test-reporter": "0.0.4",
"eslint": "^1.3.1",
"istanbul": "^0.3.19",
"jscs": "^2.1.1",
"mocha": "^2.2.5"
"nyc": "11.4.1"
}
}

@@ -30,3 +30,3 @@ # s-date

| `{ds}` | 2nd | date + suffix |
| `{dd}` | 2 | 2-digit date |
| `{dd}` | 02 | 2-digit date |
| `{Weekday}` | Thursday | day of the week |

@@ -52,8 +52,1 @@ | `{Day}` | Thu | day shortname |

```
## License
Copyright (c) 2015, Sebastian Sandqvist <s.github@sparque.me>
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc