-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Lexus Drumgold <[email protected]>
- Loading branch information
1 parent
e0b3571
commit c490266
Showing
7 changed files
with
112 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/** | ||
* @file Unit Tests - includes | ||
* @module tutils/utils/tests/unit/includes | ||
*/ | ||
|
||
import testSubject from '../includes' | ||
|
||
describe('unit:utils/includes', () => { | ||
it('should return false if value does not include target', () => { | ||
// Arrange | ||
const cases: Parameters<typeof testSubject>[] = [ | ||
['abc', 'z'], | ||
['abc', 'a', null, 1], | ||
[['a', 'b', 'c'], 'z'], | ||
[['a', 'b', 'c'], 'a', undefined, 1] | ||
] | ||
|
||
// Act + Expect | ||
cases.forEach(([value, target, identity, position]) => { | ||
expect(testSubject(value, target, identity, position)).to.be.false | ||
}) | ||
}) | ||
|
||
it('should return true if value includes target', () => { | ||
// Arrange | ||
const cases: Parameters<typeof testSubject>[] = [ | ||
['foobar', 'foo'], | ||
['foobar', 'bar', null, 3], | ||
[[faker.number.int(), { x: 0, y: 0 }], { x: 0, y: 0 }, undefined, 1], | ||
[ | ||
[faker.number.int(), { x: 0, y: 0 }], | ||
{ x: 0, y: 0 }, | ||
item => { | ||
const { x, y = Number.NaN } = item as { x: number; y?: number } | ||
return `[${x},${y}]` | ||
}, | ||
1 | ||
] | ||
] | ||
|
||
// Act + Expect | ||
cases.forEach(([value, target, identity, position]) => { | ||
expect(testSubject(value, target, identity, position)).to.be.true | ||
}) | ||
}) | ||
}) |
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,51 @@ | ||
/** | ||
* @file Utilities - includes | ||
* @module tutils/utils/includes | ||
*/ | ||
|
||
import type { | ||
Fn, | ||
IfString, | ||
IndexSignature, | ||
NIL, | ||
Nilable, | ||
NumberString | ||
} from '#src/types' | ||
import equal from './equal' | ||
import isString from './is-string' | ||
|
||
/** | ||
* Checks if an array or string `value` includes `target`. | ||
* | ||
* An array `value` includes `target` if `target` is deeply equal to at least | ||
* one item in the array. A string `value` includes `target` if `target` is | ||
* equal to at least one character in the string. | ||
* | ||
* When searching an array, an `identity` function can be used to convert array | ||
* items to unique keys. If provided, two items with the same identity key will | ||
* be considered equal. | ||
* | ||
* @template T - Value to search | ||
* @template K - Identity key type | ||
* | ||
* @param {T} value - Value to search | ||
* @param {unknown} target - Search target | ||
* @param {Nilable<Fn<[T[0]], K>>} [identity] - Identity key function | ||
* @param {number | undefined} [position=0] - Position to begin search | ||
* @return {boolean} `true` if `data` includes `target` | ||
*/ | ||
function includes< | ||
T extends string | readonly unknown[], | ||
K extends IndexSignature = NumberString | ||
>( | ||
value: T, | ||
target: unknown, | ||
identity?: IfString<T, NIL, Nilable<Fn<[T[0]], K>>>, | ||
position: number | undefined = 0 | ||
): boolean { | ||
return isString(value) | ||
? value.includes(target as string, position) | ||
: value.slice(position).some(item => equal(target, item, identity)) | ||
} | ||
|
||
export default includes |
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
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
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