Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/minMax #2330

Merged
merged 3 commits into from
Jun 24, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/plugin/minMax/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
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
while (dates.indexOf(null) !== -1) {
dates.forEach((date, index) => (!date ? dates.splice(index, 1) : null))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cloud we use Array.prototype.filter here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

really, I will

}
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())
})