Skip to content
This repository has been archived by the owner on Dec 19, 2023. It is now read-only.

iamdoron/datify

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

41 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

datify

datify your objects.

Build Status

Browser Build Status

how

> npm install datify

const Datify = require('datify');

Datify('2011-09-13T17:09:30.909Z').should.eql(new Date('2011-09-13T17:09:30.909Z'));

Datify("not a date").should.eql("not a date");

Datify({a: {b: '2011-09-13T17:09:30.909Z'}, c:32})
.should.eql({a: {b: new Date('2011-09-13T17:09:30.909Z')}, c:32});

non-conservative

Datify, by default, will parse only simple ISO strings ('2011-09-13T17:09:30.909Z', '2011-09-13T17:09:30Z'), it has a mode where everything that can be parsed to Date is parsed to date. Just:

Datify.options.conservative = false

And then:

Datify("2000").toISOString().should.eql('2000-01-01T00:00:00.000Z');

why

Suppose you are building an API, and you receive a JSON. You want to parse & save that JSON to the DB - but your dates are strings (the JSON way). So what you do is store the datifyed version. You might:

collection.insert(datify(JSON.parse(payload)), function(){ /* do something*/ });

Suppose you are using an API (from the browser, for example), that have dates in it, as strings (it is JSON, after all) - you just datify it.

apiResult = datify(apiResult);

test

> npm install
> make test

test on your favorite browser

> npm install
> make browserify-test

open browserTest.html in your favorite browser, for example:

> firefox ./browserTest.html