-
Notifications
You must be signed in to change notification settings - Fork 337
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { negate } from './negate'; | ||
import { stubTrue } from '../util/stubTrue'; | ||
import { times } from '../util/times'; | ||
|
||
describe('negate', () => { | ||
function isEven(n: number) { | ||
return n % 2 === 0; | ||
} | ||
it('should create a function that negates the result of `func`', () => { | ||
const negateFn = negate(isEven); | ||
|
||
expect(negateFn(1)).toBe(true); | ||
expect(negateFn(2)).toBe(false); | ||
}); | ||
|
||
it('should create a function that negates the result of `func`', () => { | ||
const negateFn = negate(isEven); | ||
|
||
expect(negateFn(1)).toBe(true); | ||
expect(negateFn(2)).toBe(false); | ||
}); | ||
|
||
it('should create a function that accepts multiple arguments', () => { | ||
let argCount: any; | ||
const count = 5; | ||
// @ts-expect-error - any | ||
const negateFn = negate((...args: any[]) => { | ||
argCount = args.length; | ||
}); | ||
const expected = times(count, stubTrue); | ||
|
||
const actual = times(count, index => { | ||
switch (index) { | ||
case 0: | ||
negateFn(); | ||
break; | ||
case 1: | ||
negateFn(1); | ||
break; | ||
case 2: | ||
negateFn(1, 2); | ||
break; | ||
case 3: | ||
negateFn(1, 2, 3); | ||
break; | ||
case 4: | ||
negateFn(1, 2, 3, 4); | ||
} | ||
return argCount === index; | ||
}); | ||
|
||
expect(actual).toEqual(expected); | ||
}); | ||
|
||
it('should throw an error if `func` is not a function', () => { | ||
expect(() => negate(1 as any)).toThrow(TypeError); | ||
}); | ||
|
||
it('should invoke `func` with the correct `this` binding', () => { | ||
const object = { isEven }; | ||
const negateFn = negate(function (this: any, n: number) { | ||
return this.isEven(n); | ||
}); | ||
|
||
expect(negateFn.call(object, 1)).toBe(true); | ||
expect(negateFn.call(object, 2)).toBe(false); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** | ||
* Creates a function that negates the result of the predicate function. | ||
* | ||
* @template F - The type of the function to negate. | ||
* @param {F} func - The function to negate. | ||
* @returns {F} The new negated function, which negates the boolean result of `func`. | ||
* | ||
* @example | ||
* const array = [1, 2, 3, 4, 5, 6]; | ||
* const isEven = (n: number) => n % 2 === 0; | ||
* const result = array.filter(negate(isEven)); | ||
* // result will be [1, 3, 5] | ||
*/ | ||
export function negate<F extends (...args: any[]) => boolean>(func: F): F { | ||
if (typeof func !== 'function') { | ||
throw new TypeError('Expected a function'); | ||
} | ||
return function (this: any, ...args: any[]) { | ||
return !func.apply(this, args); | ||
} as F; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters