Skip to content

Commit 42aabc8

Browse files
feat!: drop moment lib date format (#442)
* feat!: drop moment lib date format * deps: replace moment with luxon
1 parent 0f63aec commit 42aabc8

File tree

7 files changed

+19
-109
lines changed

7 files changed

+19
-109
lines changed

README.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
[![NPM downloads](https://img.shields.io/npm/dm/fast-json-stringify.svg?style=flat)](https://www.npmjs.com/package/fast-json-stringify)
77

88

9-
__fast-json-stringify__ is significantly faster than `JSON.stringify()` for small payloads.
10-
Its performance advantage shrinks as your payload grows.
9+
__fast-json-stringify__ is significantly faster than `JSON.stringify()` for small payloads.
10+
Its performance advantage shrinks as your payload grows.
1111
It pairs well with [__flatstr__](https://www.npmjs.com/package/flatstr), which triggers a V8 optimization that improves performance when eventually converting the string to a `Buffer`.
1212

1313

@@ -159,18 +159,17 @@ And nested ones, too.
159159

160160
**Note**: In the case of string formatted Date and not Date Object, there will be no manipulation on it. It should be properly formatted.
161161

162-
Example with a MomentJS object:
162+
Example with a Date object:
163163

164164
```javascript
165-
const moment = require('moment')
166-
167165
const stringify = fastJson({
168166
title: 'Example Schema with string date-time field',
169167
type: 'string',
170168
format: 'date-time'
171169
})
172170

173-
console.log(stringify(moment())) // '"YYYY-MM-DDTHH:mm:ss.sssZ"'
171+
const date = new Date()
172+
console.log(stringify(date)) // '"YYYY-MM-DDTHH:mm:ss.sssZ"'
174173
```
175174

176175

example.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
const moment = require('moment')
21
const fastJson = require('.')
32
const stringify = fastJson({
43
title: 'Example Schema',
@@ -67,12 +66,10 @@ console.log(stringify({
6766
lastName: 'Collina',
6867
age: 32,
6968
now: new Date(),
70-
birthdate: moment(),
7169
reg: /"([^"]|\\")*"/,
7270
foo: 'hello',
7371
numfoo: 42,
7472
test: 42,
75-
date: moment(),
7673
strtest: '23',
7774
arr: [{ str: 'stark' }, { str: 'lannister' }],
7875
obj: { bool: true },

index.js

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,8 @@ class Serializer {
127127
const quotes = skipQuotes === true ? '' : '"'
128128
if (date instanceof Date) {
129129
return quotes + date.toISOString() + quotes
130-
} else if (date && typeof date.toISOString === 'function') {
131-
return quotes + date.toISOString() + quotes
132-
} else {
133-
return this.asString(date, skipQuotes)
134130
}
131+
return this.asString(date, skipQuotes)
135132
}
136133

137134
asDatetimeNullable (date, skipQuotes) {
@@ -142,11 +139,8 @@ class Serializer {
142139
const quotes = skipQuotes === true ? '' : '"'
143140
if (date instanceof Date) {
144141
return quotes + new Date(date.getTime() - (date.getTimezoneOffset() * 60000)).toISOString().slice(0, 10) + quotes
145-
} else if (date && typeof date.format === 'function') {
146-
return quotes + date.format('YYYY-MM-DD') + quotes
147-
} else {
148-
return this.asString(date, skipQuotes)
149142
}
143+
return this.asString(date, skipQuotes)
150144
}
151145

152146
asDateNullable (date, skipQuotes) {
@@ -157,11 +151,8 @@ class Serializer {
157151
const quotes = skipQuotes === true ? '' : '"'
158152
if (date instanceof Date) {
159153
return quotes + new Date(date.getTime() - (date.getTimezoneOffset() * 60000)).toISOString().slice(11, 19) + quotes
160-
} else if (date && typeof date.format === 'function') {
161-
return quotes + date.format('HH:mm:ss') + quotes
162-
} else {
163-
return this.asString(date, skipQuotes)
164154
}
155+
return this.asString(date, skipQuotes)
165156
}
166157

167158
asTimeNullable (date, skipQuotes) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"compile-json-stringify": "^0.1.2",
4040
"inquirer": "^8.2.4",
4141
"is-my-json-valid": "^2.20.0",
42-
"moment": "^2.24.0",
42+
"luxon": "^2.4.0",
4343
"pre-commit": "^1.2.2",
4444
"proxyquire": "^2.1.3",
4545
"semver": "^7.1.0",

test/array.test.js

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict'
22

3-
const moment = require('moment')
43
const test = require('tap').test
54
const validator = require('is-my-json-valid')
65
const build = require('..')
@@ -177,27 +176,6 @@ test('invalid items throw', (t) => {
177176
t.throws(() => stringify({ args: ['invalid'] }))
178177
})
179178

180-
test('moment array', (t) => {
181-
t.plan(1)
182-
const schema = {
183-
type: 'object',
184-
properties: {
185-
times: {
186-
type: 'array',
187-
items: {
188-
type: 'string',
189-
format: 'date-time'
190-
}
191-
}
192-
}
193-
}
194-
const stringify = build(schema)
195-
const value = stringify({
196-
times: [moment('2018-04-21T07:52:31.017Z')]
197-
})
198-
t.equal(value, '{"times":["2018-04-21T07:52:31.017Z"]}')
199-
})
200-
201179
buildTest({
202180
title: 'item types in array default to any',
203181
type: 'object',

test/date.test.js

Lines changed: 9 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

33
const test = require('tap').test
4-
const moment = require('moment')
4+
const { DateTime } = require('luxon')
55
const validator = require('is-my-json-valid')
66
const build = require('..')
77

@@ -73,7 +73,7 @@ test('render a date in a string when format is date as YYYY-MM-DD', (t) => {
7373
const stringify = build(schema)
7474
const output = stringify(toStringify)
7575

76-
t.equal(output, `"${moment(toStringify).format('YYYY-MM-DD')}"`)
76+
t.equal(output, `"${DateTime.fromJSDate(toStringify).toISODate()}"`)
7777
t.ok(validate(JSON.parse(output)), 'valid schema')
7878
})
7979

@@ -92,7 +92,7 @@ test('render a nullable date in a string when format is date as YYYY-MM-DD', (t)
9292
const stringify = build(schema)
9393
const output = stringify(toStringify)
9494

95-
t.equal(output, `"${moment(toStringify).format('YYYY-MM-DD')}"`)
95+
t.equal(output, `"${DateTime.fromJSDate(toStringify).toISODate()}"`)
9696
t.ok(validate(JSON.parse(output)), 'valid schema')
9797
})
9898

@@ -110,7 +110,7 @@ test('verify padding for rendered date in a string when format is date', (t) =>
110110
const stringify = build(schema)
111111
const output = stringify(toStringify)
112112

113-
t.equal(output, `"${moment(toStringify).format('YYYY-MM-DD')}"`)
113+
t.equal(output, `"${DateTime.fromJSDate(toStringify).toISODate()}"`)
114114
t.ok(validate(JSON.parse(output)), 'valid schema')
115115
})
116116

@@ -131,7 +131,7 @@ test('render a date in a string when format is time as kk:mm:ss', (t) => {
131131
validate(JSON.parse(output))
132132
t.equal(validate.errors, null)
133133

134-
t.equal(output, `"${moment(toStringify).format('HH:mm:ss')}"`)
134+
t.equal(output, `"${DateTime.fromJSDate(toStringify).toFormat('HH:mm:ss')}"`)
135135
t.ok(validate(JSON.parse(output)), 'valid schema')
136136
})
137137

@@ -153,7 +153,7 @@ test('render a nullable date in a string when format is time as kk:mm:ss', (t) =
153153
validate(JSON.parse(output))
154154
t.equal(validate.errors, null)
155155

156-
t.equal(output, `"${moment(toStringify).format('HH:mm:ss')}"`)
156+
t.equal(output, `"${DateTime.fromJSDate(toStringify).toFormat('HH:mm:ss')}"`)
157157
t.ok(validate(JSON.parse(output)), 'valid schema')
158158
})
159159

@@ -174,7 +174,7 @@ test('render a midnight time', (t) => {
174174
validate(JSON.parse(output))
175175
t.equal(validate.errors, null)
176176

177-
t.equal(output, `"${moment(midnight).format('HH:mm:ss')}"`)
177+
t.equal(output, `"${DateTime.fromJSDate(midnight).toFormat('HH:mm:ss')}"`)
178178
t.ok(validate(JSON.parse(output)), 'valid schema')
179179
})
180180

@@ -195,61 +195,7 @@ test('verify padding for rendered date in a string when format is time', (t) =>
195195
validate(JSON.parse(output))
196196
t.equal(validate.errors, null)
197197

198-
t.equal(output, `"${moment(toStringify).format('HH:mm:ss')}"`)
199-
t.ok(validate(JSON.parse(output)), 'valid schema')
200-
})
201-
202-
test('render a moment.js instance in a string when format is date-time as ISOString', (t) => {
203-
t.plan(2)
204-
205-
const schema = {
206-
title: 'a moment.js object in a string',
207-
type: 'string',
208-
format: 'date-time'
209-
}
210-
const toStringify = moment()
211-
212-
const validate = validator(schema)
213-
const stringify = build(schema)
214-
const output = stringify(toStringify)
215-
216-
t.equal(output, JSON.stringify(toStringify))
217-
t.ok(validate(JSON.parse(output)), 'valid schema')
218-
})
219-
220-
test('render a moment.js instance in a string when format is date as YYYY-MM-DD', (t) => {
221-
t.plan(2)
222-
223-
const schema = {
224-
title: 'a moment.js object in a string',
225-
type: 'string',
226-
format: 'date'
227-
}
228-
const toStringify = moment()
229-
230-
const validate = validator(schema)
231-
const stringify = build(schema)
232-
const output = stringify(toStringify)
233-
234-
t.equal(output, `"${toStringify.format('YYYY-MM-DD')}"`)
235-
t.ok(validate(JSON.parse(output)), 'valid schema')
236-
})
237-
238-
test('render a moment.js instance in a string when format is time as HH:mm:ss', (t) => {
239-
t.plan(2)
240-
241-
const schema = {
242-
title: 'a moment.js object in a string',
243-
type: 'string',
244-
format: 'time'
245-
}
246-
const toStringify = moment()
247-
248-
const validate = validator(schema)
249-
const stringify = build(schema)
250-
const output = stringify(toStringify)
251-
252-
t.equal(output, `"${toStringify.format('HH:mm:ss')}"`)
198+
t.equal(output, `"${DateTime.fromJSDate(toStringify).toFormat('HH:mm:ss')}"`)
253199
t.ok(validate(JSON.parse(output)), 'valid schema')
254200
})
255201

@@ -266,7 +212,7 @@ test('render a nested object in a string when type is date-format as ISOString',
266212
}
267213
}
268214
}
269-
const toStringify = { date: moment() }
215+
const toStringify = { date: new Date() }
270216

271217
const validate = validator(schema)
272218
const stringify = build(schema)

test/typesArray.test.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict'
22

33
const test = require('tap').test
4-
const moment = require('moment')
54
const build = require('..')
65

76
test('possibly nullable integer primitive alternative', (t) => {
@@ -361,7 +360,7 @@ test('string type array can handle dates', (t) => {
361360
const stringify = build(schema)
362361
const value = stringify({
363362
date: new Date('2018-04-20T07:52:31.017Z'),
364-
dateObject: moment('2018-04-21T07:52:31.017Z')
363+
dateObject: new Date('2018-04-21T07:52:31.017Z')
365364
})
366365
t.equal(value, '{"date":"2018-04-20T07:52:31.017Z","dateObject":"2018-04-21T07:52:31.017Z"}')
367366
})

0 commit comments

Comments
 (0)