From cee87d229898e3906252230449e5f2bbe9b8d74c Mon Sep 17 00:00:00 2001 From: Vladimir Ivakin Date: Thu, 4 Jul 2024 22:54:27 +0200 Subject: [PATCH 1/6] fix: work with falsy values in toggle --- src/array/toggle.ts | 20 +++++--------------- tests/array/toggle.test.ts | 21 +++++++-------------- 2 files changed, 12 insertions(+), 29 deletions(-) diff --git a/src/array/toggle.ts b/src/array/toggle.ts index a46cf41a..67e49d81 100644 --- a/src/array/toggle.ts +++ b/src/array/toggle.ts @@ -30,25 +30,15 @@ export function toggle( strategy?: 'prepend' | 'append' }, ): T[] { - if (!array && !item) { - return [] - } - if (!array) { - return [item] - } - if (!item) { - return [...array] - } const matcher = toKey ? (x: T, idx: number) => toKey(x, idx) === toKey(item, idx) : (x: T) => x === item + const existing = array.find(matcher) - if (existing) { + + if (existing !== undefined) { return array.filter((x, idx) => !matcher(x, idx)) } - const strategy = options?.strategy ?? 'append' - if (strategy === 'append') { - return [...array, item] - } - return [item, ...array] + + return options?.strategy === 'prepend' ? [item, ...array] : [...array, item] } diff --git a/tests/array/toggle.test.ts b/tests/array/toggle.test.ts index 87b83462..188f6306 100644 --- a/tests/array/toggle.test.ts +++ b/tests/array/toggle.test.ts @@ -1,20 +1,6 @@ import * as _ from 'radashi' -const cast = (value: any): T => value - describe('toggle', () => { - test('should handle null input list', () => { - const result = _.toggle(cast(null), 'a') - expect(result).toEqual(['a']) - }) - test('should handle null input list and null item', () => { - const result = _.toggle(cast(null), null) - expect(result).toEqual([]) - }) - test('should handle null item', () => { - const result = _.toggle(['a'], null) - expect(result).toEqual(['a']) - }) test('should add item when it does not exist using default matcher', () => { const result = _.toggle(['a'], 'b') expect(result).toEqual(['a', 'b']) @@ -39,4 +25,11 @@ describe('toggle', () => { const result = _.toggle(['a'], 'b', null, { strategy: 'prepend' }) expect(result).toEqual(['b', 'a']) }) + test('should work with falsy values', () => { + expect(_.toggle([1, 2], 0)).toEqual([1, 2, 0]) + + expect(_.toggle([1, 0, 2], 0)).toEqual([1, 2]) + + expect(_.toggle([1, 2], null)).toEqual([1, 2, null]) + }) }) From c52bf6fa2e02e601e8e7f08c16ead9f1efec4c61 Mon Sep 17 00:00:00 2001 From: Alec Larson <1925840+aleclarson@users.noreply.github.com> Date: Sun, 7 Jul 2024 23:05:44 -0400 Subject: [PATCH 2/6] revert null check removal --- src/array/toggle.ts | 10 ++++++++++ tests/array/toggle.test.ts | 14 ++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/array/toggle.ts b/src/array/toggle.ts index 67e49d81..a4af8256 100644 --- a/src/array/toggle.ts +++ b/src/array/toggle.ts @@ -30,6 +30,16 @@ export function toggle( strategy?: 'prepend' | 'append' }, ): T[] { + if (!array && !item) { + return [] + } + if (!array) { + return [item] + } + if (!item) { + return [...array] + } + const matcher = toKey ? (x: T, idx: number) => toKey(x, idx) === toKey(item, idx) : (x: T) => x === item diff --git a/tests/array/toggle.test.ts b/tests/array/toggle.test.ts index 188f6306..0e6875df 100644 --- a/tests/array/toggle.test.ts +++ b/tests/array/toggle.test.ts @@ -1,6 +1,20 @@ import * as _ from 'radashi' +const cast = (value: any): T => value + describe('toggle', () => { + test('should handle null input list', () => { + const result = _.toggle(cast(null), 'a') + expect(result).toEqual(['a']) + }) + test('should handle null input list and null item', () => { + const result = _.toggle(cast(null), null) + expect(result).toEqual([]) + }) + test('should handle null item', () => { + const result = _.toggle(['a'], null) + expect(result).toEqual(['a']) + }) test('should add item when it does not exist using default matcher', () => { const result = _.toggle(['a'], 'b') expect(result).toEqual(['a', 'b']) From 2367b5a37d1d5206d744518b45a198489cbb46fe Mon Sep 17 00:00:00 2001 From: Alec Larson <1925840+aleclarson@users.noreply.github.com> Date: Sun, 7 Jul 2024 23:07:50 -0400 Subject: [PATCH 3/6] fix null check --- src/array/toggle.ts | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/array/toggle.ts b/src/array/toggle.ts index a4af8256..f7481936 100644 --- a/src/array/toggle.ts +++ b/src/array/toggle.ts @@ -30,14 +30,8 @@ export function toggle( strategy?: 'prepend' | 'append' }, ): T[] { - if (!array && !item) { - return [] - } if (!array) { - return [item] - } - if (!item) { - return [...array] + return item !== undefined ? [item] : [] } const matcher = toKey From be8ef42c85dfd84ec5e749c80d8dd98b37dd056c Mon Sep 17 00:00:00 2001 From: Alec Larson <1925840+aleclarson@users.noreply.github.com> Date: Sun, 7 Jul 2024 23:34:42 -0400 Subject: [PATCH 4/6] fix test --- tests/array/toggle.test.ts | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/tests/array/toggle.test.ts b/tests/array/toggle.test.ts index 0e6875df..6416c38d 100644 --- a/tests/array/toggle.test.ts +++ b/tests/array/toggle.test.ts @@ -7,12 +7,8 @@ describe('toggle', () => { const result = _.toggle(cast(null), 'a') expect(result).toEqual(['a']) }) - test('should handle null input list and null item', () => { - const result = _.toggle(cast(null), null) - expect(result).toEqual([]) - }) - test('should handle null item', () => { - const result = _.toggle(['a'], null) + test('should skip undefined item', () => { + const result = _.toggle(['a'], undefined) expect(result).toEqual(['a']) }) test('should add item when it does not exist using default matcher', () => { From 5fec033c3928122cd99642d9a9d365255a0192f5 Mon Sep 17 00:00:00 2001 From: Alec Larson <1925840+aleclarson@users.noreply.github.com> Date: Sun, 7 Jul 2024 23:53:25 -0400 Subject: [PATCH 5/6] preserve undefined check --- src/array/toggle.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/array/toggle.ts b/src/array/toggle.ts index f7481936..465f405f 100644 --- a/src/array/toggle.ts +++ b/src/array/toggle.ts @@ -33,6 +33,9 @@ export function toggle( if (!array) { return item !== undefined ? [item] : [] } + if (item === undefined) { + return [...array] + } const matcher = toKey ? (x: T, idx: number) => toKey(x, idx) === toKey(item, idx) From 42a85dee027c4f64f2b0faab32f1dd1e49988ae2 Mon Sep 17 00:00:00 2001 From: Alec Larson <1925840+aleclarson@users.noreply.github.com> Date: Tue, 9 Jul 2024 18:48:15 -0400 Subject: [PATCH 6/6] add missing test --- tests/array/toggle.test.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/array/toggle.test.ts b/tests/array/toggle.test.ts index 6416c38d..7769cb3d 100644 --- a/tests/array/toggle.test.ts +++ b/tests/array/toggle.test.ts @@ -4,8 +4,10 @@ const cast = (value: any): T => value describe('toggle', () => { test('should handle null input list', () => { - const result = _.toggle(cast(null), 'a') + let result = _.toggle(cast(null), 'a') expect(result).toEqual(['a']) + result = _.toggle(cast(null), undefined) + expect(result).toEqual([]) }) test('should skip undefined item', () => { const result = _.toggle(['a'], undefined)