Skip to content

Commit

Permalink
✨ Add includes function
Browse files Browse the repository at this point in the history
Closes #91
  • Loading branch information
TomokiMiyauci committed Jun 8, 2021
1 parent 58c9e54 commit d79d510
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 8 deletions.
7 changes: 1 addition & 6 deletions api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,7 @@ const api: Api = {
ramda: 'ifElse',
rambda: 'ifElse'
},
includes: {
ramda: 'includes',
rambda: 'includes',
lodash: 'includes',
fonction: undefined
},
includes: ['rambda', 'ramda', 'lodash'],
indexOf: {
ramda: 'indexOf',
rambda: 'indexOf',
Expand Down
1 change: 1 addition & 0 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export { identity } from './src/identity.ts'
export { ifElse } from './src/ifElse.ts'
export { ifElseFn } from './src/ifElseFn.ts'
export { inc } from './src/inc.ts'
export { includes } from './src/includes.ts'
export { isArray } from './src/isArray.ts'
export { isBigint } from './src/isBigint.ts'
export { isBoolean } from './src/isBoolean.ts'
Expand Down
31 changes: 29 additions & 2 deletions src/includes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,31 @@
// Copyright 2021-present the Fonction authors. All rights reserved. MIT license.
const includes = <T>(val: any, list: T[]): boolean => list.indexOf(val, 0) >= 0;
import { curry2 } from './_/curry2.ts'

export { includes };
/**
* @internal
*/
const _includes: {
<T extends unknown[]>(arr: T, val: T[number]): boolean
(str: string, val: string): boolean
} = (collection: any, val: any): boolean => collection.includes(val)

/**
* Checks if value is in collection.
*
* @param collection - The collection to inspect
* @param val - The value to search for
* @returns The result of `collection.includes(val)`
*
* @example
* ```ts
* includes('hello', 'lo') // true
* includes([1, 2, 3], 3) // true
* ```
*
* @category `String` `Array`
*
* @beta
*/
const includes = curry2(_includes)

export { _includes, includes }
17 changes: 17 additions & 0 deletions test/includes.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2021-present the Fonction authors. All rights reserved. MIT license.
import { assertEquals } from '../dev_deps.ts'
import { _includes } from '../src/includes.ts'
Deno.test('includes', () => {
const table: [unknown[], unknown, boolean][] = [
[[], '', false],
[[1, 2, 3], '', false],
[[1, 2, 3], 1, true]
]
table.forEach(([collection, val, expected]) => {
assertEquals(
_includes(collection, val),
expected,
`includes(${collection}, ${val}) -> ${expected}`
)
})
})

0 comments on commit d79d510

Please sign in to comment.