From fb2f64c91bd5bf53591373ec34b5cdcc6b2b14c5 Mon Sep 17 00:00:00 2001 From: diego Date: Fri, 9 Jun 2023 15:16:12 -0300 Subject: [PATCH 1/3] fix(sortBy): ignore the arg if a 'null' arg is provided with the others args --- src/plugin/minMax/index.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/plugin/minMax/index.js b/src/plugin/minMax/index.js index f515c60d9..ee321df0e 100644 --- a/src/plugin/minMax/index.js +++ b/src/plugin/minMax/index.js @@ -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)) + } + let result; [result] = dates for (let i = 1; i < dates.length; i += 1) { if (!dates[i].isValid() || dates[i][method](result)) { From 92d99a68ced4fafdce7bf7a61c2b729f13d2c72f Mon Sep 17 00:00:00 2001 From: diego Date: Fri, 9 Jun 2023 15:17:08 -0300 Subject: [PATCH 2/3] feat(test): add tests --- test/plugin/minMax.test.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/plugin/minMax.test.js b/test/plugin/minMax.test.js index eeef92063..f8a508001 100644 --- a/test/plugin/minMax.test.js +++ b/test/plugin/minMax.test.js @@ -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()) +}) From 07e20b0d77df83a4a1a8cbf3a59c5f186d93f923 Mon Sep 17 00:00:00 2001 From: diego Date: Fri, 23 Jun 2023 18:14:39 -0300 Subject: [PATCH 3/3] refactor: change method to filter --- src/plugin/minMax/index.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/plugin/minMax/index.js b/src/plugin/minMax/index.js index ee321df0e..8b3e2d3fe 100644 --- a/src/plugin/minMax/index.js +++ b/src/plugin/minMax/index.js @@ -11,9 +11,7 @@ export default (o, c, d) => { if (dates.length === 1 && dates[0].length > 0) { [dates] = dates } - while (dates.indexOf(null) !== -1) { - dates.forEach((date, index) => (!date ? dates.splice(index, 1) : null)) - } + dates = dates.filter(date => date) let result; [result] = dates for (let i = 1; i < dates.length; i += 1) {