Skip to content

Commit 3dd134e

Browse files
authored
feat!: remove momentjs (#7)
* chore: fix ci * wip: migrating momentjs * replace: date-fn * docs
1 parent f4fabc3 commit 3dd134e

File tree

6 files changed

+208
-205
lines changed

6 files changed

+208
-205
lines changed

.eslintrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
{"extends": "standard"}
2+
{"extends": "standard"}
File renamed without changes.

README.md

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ const diff = _.differenceValues(editedJson, originalJson[, options]);
3030
| --- | ------ | ------- | ----------- |
3131
| extract | `[only-add, only-remove, only-changed, only-add-change]` | `only-add-change` | Described below
3232
| dateCheck | `true` or `false` | `true` | For performance: deactivate the date object and string evaluation
33-
| dateFormatIn | [momentjs formats](https://momentjs.com/docs/#/parsing/string-format/) | `YYYY-MM-DDTHH:mm:ss.sssZ` | The format to use to evaluate string during date checking
34-
| dateFormatOut | [momentjs formats](https://momentjs.com/docs/#/parsing/string-format/) | `YYYY-MM-DDTHH:mm:ss.sssZ` | The format to use to evaluate date-string comparison
33+
| dateFormatIn | [date-fns formats](https://date-fns.org/v2.29.3/docs/parse) | `yyyy-MM-dd'T'HH:mm:ss.SSS'Z'` | The format to use to evaluate string during date checking
34+
| dateFormatOut | [date-fns formats](https://date-fns.org/v2.29.3/docs/parse) | `yyyy-MM-dd'T'HH:mm:ss.SSS'Z'` | The format to use to evaluate date-string comparison
3535

3636
### Field: extract
3737

@@ -43,9 +43,9 @@ const diff = _.differenceValues(editedJson, originalJson[, options]);
4343
### Field: dateCheck
4444

4545
If `true`: activate deep controls for the date object and strings. The input are evaluated with the `options.dateFormatIn` format and compared using the `options.dateFormatOut` format.
46-
This will enable you to read corectly a date in a format like `options.dateFormatIn: YYYY-MM-DD` but consider the date changed only if the year and month change `options.dateFormatOut: YYYY-MM`.
46+
This will enable you to read corectly a date in a format like `options.dateFormatIn: yyyy-MM-dd` but consider the date changed only if the year and month change `options.dateFormatOut: yyyy-MM`.
4747

48-
If `false`: only Date objects are evaluated as [date.toJSON()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toJSON) for comparison.
48+
If `false`: only Date objects are evaluated as `date.toISOString()` for comparison.
4949

5050
### Field: dateFormat
5151

@@ -125,12 +125,5 @@ const out = _.differenceValues(newObj, oldObj);
125125

126126

127127
## License
128-
(The MIT License)
129128

130-
Copyright (c) 2018 Manuel Spigolon <behemoth89@gmail.com>
131-
132-
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
133-
134-
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
135-
136-
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
129+
Copyright [Manuel Spigolon](https://github.com/Eomm), Licensed under [MIT](./LICENSE).

mixin.js

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

3-
const moment = require('moment')
3+
const { parse, parseISO } = require('date-fns')
4+
const { format, utcToZonedTime } = require('date-fns-tz')
45

56
const _ = {
67
transform: require('lodash.transform'),
@@ -15,20 +16,31 @@ const ignore = Symbol('ignore')
1516
const DEFAULT_OPTIONS = {
1617
extract: 'only-add-change',
1718
dateCheck: true,
18-
dateFormatIn: 'YYYY-MM-DDTHH:mm:ss.sssZ',
19-
dateFormatOut: 'YYYY-MM-DDTHH:mm:ss.sssZ'
19+
dateFormatIn: "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",
20+
dateFormatOut: "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
2021
}
2122

2223
function evaluateDate (value, verifyFormat, formatIn, formatOut) {
2324
// TODO: add date comparison only for some keys?
24-
if (_.isDate(value)) {
25-
value = value.toJSON()
25+
26+
if (value instanceof Date) {
27+
value = value.toISOString()
2628
}
2729

28-
if (verifyFormat === true) {
29-
const analize = moment.utc(value, formatIn, true)
30-
if (analize.isValid()) {
31-
return analize.format(formatOut)
30+
if (verifyFormat === true &&
31+
(typeof value === 'string' || typeof value === 'object')) {
32+
let isValid = false
33+
try {
34+
if (typeof value === 'string' && formatIn === DEFAULT_OPTIONS.dateFormatIn) {
35+
isValid = parseISO(value)
36+
} else {
37+
isValid = parse(value, formatIn, new Date())
38+
}
39+
} catch (error) {
40+
}
41+
42+
if (isValid && !Number.isNaN(isValid.getTime())) {
43+
return format(utcToZonedTime(isValid, 'UTC'), formatOut, { timeZone: 'UTC' })
3244
}
3345
}
3446
return value

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"main": "mixin.js",
66
"scripts": {
77
"lint": "standard --env jest",
8-
"lint:fix": "standard --env jest --fix",
8+
"lint:fix": "standard --fix",
99
"test": "tap test/**/*.test.js"
1010
},
1111
"keywords": [
@@ -22,12 +22,13 @@
2222
"url": "https://github.com/Eomm/lodash-mixin-diff-value"
2323
},
2424
"dependencies": {
25+
"date-fns": "^2.29.3",
26+
"date-fns-tz": "^1.3.7",
2527
"lodash.defaults": "^4.2.0",
2628
"lodash.isarraylike": "^4.2.0",
2729
"lodash.isdate": "^4.0.1",
2830
"lodash.isobjectlike": "^4.0.0",
29-
"lodash.transform": "^4.6.0",
30-
"moment": "^2.22.2"
31+
"lodash.transform": "^4.6.0"
3132
},
3233
"devDependencies": {
3334
"lodash": "^4.17.11",

0 commit comments

Comments
 (0)