This repository has been archived by the owner on Dec 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.js
124 lines (104 loc) · 4.2 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
var Datify = require('./');
var Chai = require('chai');
Chai.should();
describe('conservative datify', function(){
it('should keep a string integer a string (conservative by default)', function(){
Datify('1999').should.eql('1999');
})
it('should convert an ISO Date string to the right date', function(done){
Datify('2011-09-13T17:09:30.909Z').should.eql(new Date('2011-09-13T17:09:30.909Z'));
Datify('2011-09-13T17:09:30.909Z').toISOString().should.eql('2011-09-13T17:09:30.909Z');
Datify('2011-09-13T17:09:30.909Z').constructor.toString().should.contain('function Date');
done();
}),
it('should convert an ISO Date string with no ms to the right date', function(done){
Datify('2011-09-13T17:09:30Z').should.eql(new Date('2011-09-13T17:09:30Z'));
Datify('2011-09-13T17:09:30Z').toISOString().should.eql('2011-09-13T17:09:30.000Z');
Datify('2011-09-13T17:09:30Z').constructor.toString().should.contain('function Date');
done();
}),
it('should keep the string "a message" unchanged', function(done){
Datify('a message').should.eql("a message");
done();
}),
it('should keep a js Date unchanged', function(done){
Datify(new Date('2011-09-13T17:09:30.909Z')).should.eql(new Date('2011-09-13T17:09:30.909Z'));
Datify(new Date('2011-09-13T17:09:30.909Z')).toISOString().should.eql('2011-09-13T17:09:30.909Z');
done();
}),
it('should convert an object with an ISO date string to an object with Date', function(done){
var anObject = {
content: { },
createdAt: '2011-09-13T17:12:30.909Z'
}
Datify(anObject).should.eql({
content: { },
createdAt: new Date('2011-09-13T17:12:30.909Z')
});
done();
}),
it('should return a different object then the one it was converting', function(done){
var anObject = {
content: { },
createdAt: '2011-09-13T17:12:30.909Z'
}
Datify(anObject).should.not.equal(anObject);
done();
}),
it('should convert an object with nested ISO date strings to an object with Dates', function(done){
var anObject = {
number: 3441,
date: new Date(),
content: { createdAt: '2011-10-13T17:12:30.909Z', i: {am: {losing: {it: '2011-10-13T17:12:30.929Z', yep: "i am"}}}},
createdAt: '2011-09-13T17:12:30.909Z'
}
Datify(anObject).should.eql({
number: 3441,
date: anObject.date,
content: { createdAt: new Date('2011-10-13T17:12:30.909Z'), i: {am: {losing: {it: new Date('2011-10-13T17:12:30.929Z'), yep: "i am"}}}},
createdAt: new Date('2011-09-13T17:12:30.909Z')
});
done();
}),
it('should convert an array of Dates', function(done){
var anArrayOfDates = ['2011-10-13T17:12:30.909Z', '2011-10-13T17:12:30.909Z', '2012-10-13T17:12:30.909Z']
Datify(anArrayOfDates).should.eql([new Date('2011-10-13T17:12:30.909Z'),
new Date('2011-10-13T17:12:30.909Z'), new Date('2012-10-13T17:12:30.909Z')]);
done();
});
it('should convert Date(0).toISOString() to Date', function(done){
Datify(new Date(0).toISOString()).should.eql(new Date(0));
done();
});
it('should convert 2000-01-01T00:00:00.000000Z to Date', function(done){
Datify("2000-01-01T00:00:00.000000Z").should.eql(new Date("2000-01-01T00:00:00.000000Z"));
done();
});
});
describe('non-conservative datify', function(){
before(function(){
Datify.options.conservative = false;
});
it('should parse a year as a date', function(){
Datify('1999').should.eql(new Date('1999'));
})
it('should convert an object with nested ISO date strings to an object with Dates', function(){
var anObject = {
number: 3441,
date: new Date(),
content: { createdAt: '2011-10-13T17:12:30.909Z', i: {am: {losing: {it: '2011-10-13T17:12:30.929Z', yep: "i am"}}}},
createdAt: '2011-09-13T17:12:30.909Z'
}
Datify(anObject).should.eql({
number: 3441,
date: anObject.date,
content: { createdAt: new Date('2011-10-13T17:12:30.909Z'), i: {am: {losing: {it: new Date('2011-10-13T17:12:30.929Z'), yep: "i am"}}}},
createdAt: new Date('2011-09-13T17:12:30.909Z')
});
}),
it('should convert an array of Dates', function(){
var anArrayOfDates = ['2011-10-13T17:12:30.909Z', '2011-10-13T17:12:30.909Z', '2012-10-13T17:12:30.909Z']
Datify(anArrayOfDates).should.eql([new Date('2011-10-13T17:12:30.909Z'),
new Date('2011-10-13T17:12:30.909Z'), new Date('2012-10-13T17:12:30.909Z')]);
});
});