Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

dateable

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

dateable - npm Package Compare versions

Comparing version 0.1.3 to 0.2.0

lang/index.js

214

index.js
var fs = require('fs')
, path = require('path')
, files = fs.readdirSync(path.join(__dirname, 'lang'))
, langs = {}
, lang
, format = require('util').format
, langs = require('./lang')
files.forEach(function (filename) {
var language = filename.replace(/\.json$/, '');
langs[language] = require('./lang/' + filename);
});
var units = exports.units = {
years : 31536000000
, months : 2592000000
, weeks : 604800000
, days : 86400000
, hours : 3600000
, minutes : 60000
, seconds : 1000
};
var formats = exports.formats = {};
// Set the default language
var lang = langs['en-us'];
exports.setLang = function (language) {
if (typeof language == 'object')
lang = language;
else if (langs[language])
lang = langs[language];
};
// RegExp to tokenize a format string
var TOKENS = /Y{2,4}|[Md]{1,4}|[DHhms]{1,2}|[Aa]|"[^"]*"|'[^']*'/g;
exports.format = function (date, format) {
var tokens = /Y{2,4}|[Md]{1,4}|[DHhms]{1,2}|[Aa]|"[^"]*"|'[^']*'/g
, date = toObject(date);
/**
* Format date
*
* @param {Date} date
* @param {String} format
* @return {String}
* @api public
*/
function dateable (date, format) {
var date = toObject(date);
format = formats[format] || format;
return format.replace(tokens, function (part) {
return format.replace(TOKENS, function (part) {
switch (part) {

@@ -87,9 +70,55 @@ case 'YYYY':

exports.parse = function (string, format) {
var tokenizer = /Y{2,4}|[Md]{1,4}|[DHhms]{1,2}|[Aa]/g
, offset = 0
// Backwards compatibility
dateable.format = dateable;
// Available units
var units = dateable.units = {
years : 31536000000
, months : 2592000000
, weeks : 604800000
, days : 86400000
, hours : 3600000
, minutes : 60000
, seconds : 1000
};
// Stored formats
var formats = dateable.formats = {};
/**
* Set language
*
* @param {String|Object} language
* @api public
*/
dateable.language = function (language) {
if (typeof language !== 'string') {
lang = language;
} else if (langs[language]) {
lang = langs[language];
}
return this;
};
// Backwards compatibility
dateable.setLang = dateable.language;
// Tokenizer for a formatted date
var TOKENIZER = /Y{2,4}|[Md]{1,4}|[DHhms]{1,2}|[Aa]/g ;
/**
* Parse string from format to date
*
* @param {String} string
* @param {String} format
* @return {Date}
* @api public
*/
dateable.parse = function (string, format) {
var offset = 0
, parts = {}
, token
, index
, part
, token;

@@ -105,7 +134,7 @@ format = formats[format] || format;

while (token = tokenizer.exec(format)) {
index = token.index + offset;
while (token = TOKENIZER.exec(format)) {
var index = token.index + offset
, tokenLength = token[0].length
, part = string.substr(index, tokenLength);
var tokenLength = token[0].length;
part = string.substr(index, tokenLength);
index += tokenLength - 1;

@@ -142,3 +171,12 @@

exports.when = function (date, unit) {
/**
* Relative time
*
* @param {Date} date
* @param {String} [unit]
* @return {String}
* @api public
*/
dateable.when = function (date, unit) {
var diff = date.valueOf() - Date.now()

@@ -155,6 +193,16 @@ , time = 'present'

return printify(lang.time[time], pluralize(diff, unit));
return format(lang.time[time], pluralize(diff, unit));
};
exports.diff = function (start, end, unit) {
/**
* Return difference between two dates
*
* @param {Date} start
* @param {Date} end
* @param {String} [unit]
* @return {String}
* @api public
*/
dateable.diff = function (start, end, unit) {
var diff = start.valueOf() - end.valueOf()

@@ -168,16 +216,29 @@ , unit = unit || determineUnit(diff)

/**
* Pluralize unit
*
* @param {Number} value
* @param {String} unit
* @return {String}
* @api private
*/
function pluralize (value, unit) {
var form = lang.units[unit][value > 1 ? 1 : 0];
return printify(form, value);
return format(form, value);
};
function determineUnit (ms) {
var unit;
/**
* Find the best matching unit for an amount of time (ms)
*
* @param {Number} ms
* @return {String}
* @api private
*/
function determineUnit (ms) {
ms = Math.abs(ms);
for (unit in units) {
if (ms > units[unit])
break;
for (var unit in units) {
if (ms > units[unit]) break;
}

@@ -188,20 +249,13 @@

function printify (string) {
var args = [].slice.call(arguments, 1)
, offset = 0;
return string.replace(/%s([0-9])*/g, function (s, n) {
n = n || offset;
if (args[n] && Array.isArray(args[n]))
args[n] = printify.apply(null, args[n]);
/**
* Pad a number with leading zeros
*
* @param {Number} number
* @param {Number} length
* @return {String}
* @api private
*/
offset++;
return args[n];
});
}
function pad (number, zeros) {
return number < Math.pow(10, zeros || 1)
function pad (number, length) {
return number < Math.pow(10, length || 1)
? '0' + number

@@ -211,2 +265,10 @@ : '' + number;

/**
* Convert an object to a date
*
* @param {Object} object
* @return {Number} length
* @api private
*/
function toDate (obj) {

@@ -242,2 +304,10 @@ var date = new Date(0)

/**
* Convert a date to an object
*
* @param {Object} date
* @return {Object}
* @api private
*/
function toObject (date) {

@@ -261,1 +331,3 @@ var obj = {

}
module.exports = dateable;
{
"name": "dateable",
"description": "A date formatter and parser for node",
"version": "0.1.3",
"version": "0.2.0",
"author": "Eivind Fjeldstad",

@@ -6,0 +6,0 @@ "repository": {

@@ -8,20 +8,20 @@ [![build status](https://secure.travis-ci.org/eivindfjeldstad/dateable.png)](http://travis-ci.org/eivindfjeldstad/dateable)

## How?
## Use
```javascript
var dateable = require('dateable');
var str = dateable.format(new Date(), 'MM/DD-YYYY, hh:mm'); // e.g., 03/23-2012, 22:10
dateable.parse(str, 'MM/DD-YYYY, hh:mm') // Returns the original date
var str = dateable(new Date(), 'MM/DD-YYYY, hh:mm'); // => 03/23-2012, 22:10
dateable.parse(str, 'MM/DD-YYYY, hh:mm') // Returns the original date
```
If you want to include text in the formatting, just escape it with either ' or ".
If you want to include text in the formatting, you need to escape it with either ' or ".
```javascript
var date = new Date(2009, 4, 23)
, format = '"I went to the moon in" YYYY. "I think it was a" dddd "in" MMMM';
var date = new Date(2009, 4, 23);
var format = '"I went to the moon a" dddd "in" MMMM, YYYY';
dateable.format(date, format); // I went to the moon in 2009. I think it was a Saturday in May
dateable(date, format);
// => I went to the moon a Saturday in May, 2009
```
You can also get the answers to simple questions, such as:
You can also get the relative date, and the difference between dates.

@@ -31,12 +31,21 @@ ```javascript

dateable.when(date); // 4 years ago
dateable.when(date); // => 4 years ago
dateable.when(new Date(2020, 4, 30)); // => in 8 years
dateable.diff(new Date(2015), new Date()); // => 3 years
```
// The same question can be asked for future days
dateable.when(new Date(2020, 4, 30)); // in 8 years
## API
### dateable(date, format)
Return formatted date
// And...
dateable.diff(new Date(2015), new Date()); // 3 years
```
### dateable#parse(date, format)
Return original date from format
### dateable#when(date, [unit])
Returns relative date in specified or closest matching unit
### dateable#language(language)
Set language
## Why?
Because dealing with dates in javascript is a fucking pain in the ass!

@@ -11,3 +11,3 @@ var dateable = require('./')

assert.equal(dateable.format(date, 'test'), expected);
assert.equal(dateable(date, 'test'), expected);
},

@@ -19,3 +19,3 @@

date = dateable.format(date, 'test');
date = dateable(date, 'test');
assert.equal(dateable.parse(date, 'test').valueOf(), expected);

@@ -38,5 +38,4 @@ },

for (var t in tests)
tests[t]();
for (var t in tests) tests[t]();
console.log('All tests completed successfully');
SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc