Skip to content

Commit

Permalink
fix: update MinMax plugin 1. ignore the 'null' in args 2. return the …
Browse files Browse the repository at this point in the history
…only one arg (iamkun#2330)
  • Loading branch information
dchueri authored Jun 24, 2023
1 parent 013968f commit 3c2c6ee
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/plugin/minMax/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
export default (o, c, d) => {
const sortBy = (method, dates) => {
if (!dates || !dates.length || !dates[0] || (dates.length === 1 && !dates[0].length)) {
if (
!dates ||
!dates.length ||
(dates.length === 1 && !dates[0]) ||
(dates.length === 1 && Array.isArray(dates[0]) && !dates[0].length)
) {
return null
}
if (dates.length === 1 && dates[0].length > 0) {
[dates] = dates
}
let result
dates = dates.filter(date => date)
let result;
[result] = dates
for (let i = 1; i < dates.length; i += 1) {
if (!dates[i].isValid() || dates[i][method](result)) {
Expand Down
14 changes: 14 additions & 0 deletions test/plugin/minMax.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,17 @@ it('If Invalid Date return Invalid Date', () => {
expect(dayjs.min([arg1, arg2, arg3, arg4]).format())
.toBe(arg4.format())
})

it('Ignore if exists an "null" argument', () => {
expect(dayjs.max(null, null, arg1, arg2, null, arg3).format())
.toBe(arg1.format())
expect(dayjs.min([null, null, arg1, arg2, null, arg3]).format())
.toBe(arg3.format())
})

it('Return the only date if just provided one argument', () => {
expect(dayjs.max(arg1).format())
.toBe(arg1.format())
expect(dayjs.min([arg1]).format())
.toBe(arg1.format())
})

0 comments on commit 3c2c6ee

Please sign in to comment.