Skip to content

Commit

Permalink
feat!: drop moment lib date format (#442)
Browse files Browse the repository at this point in the history
* feat!: drop moment lib date format

* deps: replace moment with luxon
  • Loading branch information
ivan-tymoshenko committed May 31, 2022
1 parent 0f63aec commit 42aabc8
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 109 deletions.
11 changes: 5 additions & 6 deletions README.md
Expand Up @@ -6,8 +6,8 @@
[![NPM downloads](https://img.shields.io/npm/dm/fast-json-stringify.svg?style=flat)](https://www.npmjs.com/package/fast-json-stringify)


__fast-json-stringify__ is significantly faster than `JSON.stringify()` for small payloads.
Its performance advantage shrinks as your payload grows.
__fast-json-stringify__ is significantly faster than `JSON.stringify()` for small payloads.
Its performance advantage shrinks as your payload grows.
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`.


Expand Down Expand Up @@ -159,18 +159,17 @@ And nested ones, too.

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

Example with a MomentJS object:
Example with a Date object:

```javascript
const moment = require('moment')

const stringify = fastJson({
title: 'Example Schema with string date-time field',
type: 'string',
format: 'date-time'
})

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


Expand Down
3 changes: 0 additions & 3 deletions example.js
@@ -1,4 +1,3 @@
const moment = require('moment')
const fastJson = require('.')
const stringify = fastJson({
title: 'Example Schema',
Expand Down Expand Up @@ -67,12 +66,10 @@ console.log(stringify({
lastName: 'Collina',
age: 32,
now: new Date(),
birthdate: moment(),
reg: /"([^"]|\\")*"/,
foo: 'hello',
numfoo: 42,
test: 42,
date: moment(),
strtest: '23',
arr: [{ str: 'stark' }, { str: 'lannister' }],
obj: { bool: true },
Expand Down
15 changes: 3 additions & 12 deletions index.js
Expand Up @@ -127,11 +127,8 @@ class Serializer {
const quotes = skipQuotes === true ? '' : '"'
if (date instanceof Date) {
return quotes + date.toISOString() + quotes
} else if (date && typeof date.toISOString === 'function') {
return quotes + date.toISOString() + quotes
} else {
return this.asString(date, skipQuotes)
}
return this.asString(date, skipQuotes)
}

asDatetimeNullable (date, skipQuotes) {
Expand All @@ -142,11 +139,8 @@ class Serializer {
const quotes = skipQuotes === true ? '' : '"'
if (date instanceof Date) {
return quotes + new Date(date.getTime() - (date.getTimezoneOffset() * 60000)).toISOString().slice(0, 10) + quotes
} else if (date && typeof date.format === 'function') {
return quotes + date.format('YYYY-MM-DD') + quotes
} else {
return this.asString(date, skipQuotes)
}
return this.asString(date, skipQuotes)
}

asDateNullable (date, skipQuotes) {
Expand All @@ -157,11 +151,8 @@ class Serializer {
const quotes = skipQuotes === true ? '' : '"'
if (date instanceof Date) {
return quotes + new Date(date.getTime() - (date.getTimezoneOffset() * 60000)).toISOString().slice(11, 19) + quotes
} else if (date && typeof date.format === 'function') {
return quotes + date.format('HH:mm:ss') + quotes
} else {
return this.asString(date, skipQuotes)
}
return this.asString(date, skipQuotes)
}

asTimeNullable (date, skipQuotes) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -39,7 +39,7 @@
"compile-json-stringify": "^0.1.2",
"inquirer": "^8.2.4",
"is-my-json-valid": "^2.20.0",
"moment": "^2.24.0",
"luxon": "^2.4.0",
"pre-commit": "^1.2.2",
"proxyquire": "^2.1.3",
"semver": "^7.1.0",
Expand Down
22 changes: 0 additions & 22 deletions test/array.test.js
@@ -1,6 +1,5 @@
'use strict'

const moment = require('moment')
const test = require('tap').test
const validator = require('is-my-json-valid')
const build = require('..')
Expand Down Expand Up @@ -177,27 +176,6 @@ test('invalid items throw', (t) => {
t.throws(() => stringify({ args: ['invalid'] }))
})

test('moment array', (t) => {
t.plan(1)
const schema = {
type: 'object',
properties: {
times: {
type: 'array',
items: {
type: 'string',
format: 'date-time'
}
}
}
}
const stringify = build(schema)
const value = stringify({
times: [moment('2018-04-21T07:52:31.017Z')]
})
t.equal(value, '{"times":["2018-04-21T07:52:31.017Z"]}')
})

buildTest({
title: 'item types in array default to any',
type: 'object',
Expand Down
72 changes: 9 additions & 63 deletions test/date.test.js
@@ -1,7 +1,7 @@
'use strict'

const test = require('tap').test
const moment = require('moment')
const { DateTime } = require('luxon')
const validator = require('is-my-json-valid')
const build = require('..')

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

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

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

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

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

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

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

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

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

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

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

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

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

t.equal(output, `"${moment(toStringify).format('HH:mm:ss')}"`)
t.ok(validate(JSON.parse(output)), 'valid schema')
})

test('render a moment.js instance in a string when format is date-time as ISOString', (t) => {
t.plan(2)

const schema = {
title: 'a moment.js object in a string',
type: 'string',
format: 'date-time'
}
const toStringify = moment()

const validate = validator(schema)
const stringify = build(schema)
const output = stringify(toStringify)

t.equal(output, JSON.stringify(toStringify))
t.ok(validate(JSON.parse(output)), 'valid schema')
})

test('render a moment.js instance in a string when format is date as YYYY-MM-DD', (t) => {
t.plan(2)

const schema = {
title: 'a moment.js object in a string',
type: 'string',
format: 'date'
}
const toStringify = moment()

const validate = validator(schema)
const stringify = build(schema)
const output = stringify(toStringify)

t.equal(output, `"${toStringify.format('YYYY-MM-DD')}"`)
t.ok(validate(JSON.parse(output)), 'valid schema')
})

test('render a moment.js instance in a string when format is time as HH:mm:ss', (t) => {
t.plan(2)

const schema = {
title: 'a moment.js object in a string',
type: 'string',
format: 'time'
}
const toStringify = moment()

const validate = validator(schema)
const stringify = build(schema)
const output = stringify(toStringify)

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

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

const validate = validator(schema)
const stringify = build(schema)
Expand Down
3 changes: 1 addition & 2 deletions test/typesArray.test.js
@@ -1,7 +1,6 @@
'use strict'

const test = require('tap').test
const moment = require('moment')
const build = require('..')

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

0 comments on commit 42aabc8

Please sign in to comment.