Skip to content

Commit

Permalink
feat@important deprecate contains in favour of includes
Browse files Browse the repository at this point in the history
  • Loading branch information
selfrefactor committed Aug 19, 2019
1 parent 5b77179 commit d916411
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 156 deletions.
30 changes: 7 additions & 23 deletions files/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ https://unpkg.com/[email protected]/dist/rambda.umd.js

- Rambda's **partialCurry** is not part of Ramda API.

- Rambda's **includes** acts as curried Javascript `includes`, while **Ramda** version uses `R.equals` to check if a list contains certain value. Also **Ramda** version will throw an error if input is neither `string` nor `array`, while **Rambda** version will return `false`.
- Ramda's **includes** will throw an error if input is neither `string` nor `array`, while **Rambda** version will return `false`.

> If you need more **Ramda** methods in **Rambda**, you may either submit a `PR` or check the extended version of **Rambda** - [Rambdax](https://github.com/selfrefactor/rambdax). In case of the former, you may want to consult with [Rambda contribution guidelines.](CONTRIBUTING.md)
Expand Down Expand Up @@ -322,21 +322,6 @@ R.concat('foo')('bar') // => 'foobar'

[Source](https://github.com/selfrefactor/rambda/tree/master/src/concat.js)

#### contains

> contains(valueToFind: T, arr: T[]): boolean
It returns `true`, if `valueToFind` is part of `arr`.

Note that while new versions of `Ramda` depricate this method, `contains` will remain in this library.

```
R.contains(2, [1, 2]) // => true
R.contains(3, [1, 2]) // => false
```

[Source](https://github.com/selfrefactor/rambda/tree/master/src/contains.js)

#### curry

> curry(fn: Function): Function
Expand Down Expand Up @@ -736,19 +721,16 @@ R.inc(1) // => 2

#### includes

If `input` is neither `string` nor `array`, then this method will return `false`.
> includes(valueToFind: T|string, input: T[]|string): boolean
> includes(target: any, input: any): boolean
If `input` is string, then this method work as native `includes`.
If `input` is array, then `R.equals` is used to define if `valueToFind` belongs to the list.

```
R.includes(1, [1, 2]) // => true
R.includes('oo', 'foo') // => true
R.includes('z', 'foo') // => false
R.includes('z', null) // => false
R.includes({a: 1}, [{a: 1}]) // => true
```

!! Note that this method is not part of `Ramda` API.

[Source](https://github.com/selfrefactor/rambda/tree/master/src/includes.js)

#### indexBy
Expand Down Expand Up @@ -1709,6 +1691,8 @@ import omit from 'rambda/lib/omit'
## Changelog

- 3.0.0 Deprecate `R.contains`, while `R.includes` is now following Ramda API(it uses `R.equals` for comparision)

- 2.14.5 `R.without` needs currying

- 2.14.4 Close [issue #227](https://github.com/selfrefactor/rambda/issues/227) - add index as third argument of `R.reduce` typings
Expand Down
26 changes: 0 additions & 26 deletions files/benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,32 +115,6 @@ if (options.compose){
.run()
}

const contains = new Benchmark.Suite

options.contains = true

if (options.contains){
const holder = [ 1, 2, 3, 4 ]
const a = 4

contains.add('Rambda.contains', () => {
R.contains(a, holder)
})
.add('Ramda', () => {
Ramda.contains(a, holder)
})
.add('Lodash.includes', () => {
_.includes(holder, a)
})
.on('cycle', event => {
benchmarks.add(event.target)
})
.on('complete', () => {
benchmarks.log()
})
.run()
}

const drop = new Benchmark.Suite

options.drop = true
Expand Down
41 changes: 1 addition & 40 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,6 @@ declare namespace R {
T extends Evolver ? Evolvable<T> :
never;

interface Placeholder { __isRamdaPlaceholder__: true; }

interface Reduced<T> {
'@@transducer/value': T;
'@@transducer/reduced': true;
Expand All @@ -84,12 +82,6 @@ declare namespace R {
type Merge<Primary, Secondary> = { [K in keyof Primary]: Primary[K] } & { [K in Exclude<keyof Secondary, CommonKeys<Primary, Secondary>>]: Secondary[K] };

interface Static {
/**
* Placeholder. When used with functions like curry, or op, the second argument is applied to the second
* position, and it returns a function waiting for its first argument.
*/
__: Placeholder; /* This is used in examples throughout the docs, but I it only seems to be directly explained here: https://ramdajs.com/0.9/docs/#op */

/**
* Adds two numbers (or strings). Equivalent to a + b but curried.
*/
Expand Down Expand Up @@ -141,8 +133,6 @@ declare namespace R {
/**
* Makes a shallow clone of an object, setting or overriding the specified property with the given value.
*/
assoc<T, U>(__: Placeholder, val: T, obj: U): <K extends string>(prop: K) => Record<K, T> & U;
assoc<U, K extends string>(prop: K, __: Placeholder, obj: U): <T>(val: T) => Record<K, T> & U;
assoc<T, U, K extends string>(prop: K, val: T, obj: U): Record<K, T> & U;
assoc<T, K extends string>(prop: K, val: T): <U>(obj: U) => Record<K, T> & U;
assoc<K extends string>(prop: K): <T, U>(val: T, obj: U) => Record<K, T> & U;
Expand Down Expand Up @@ -226,28 +216,11 @@ declare namespace R {
* Returns a new list consisting of the elements of the first list followed by the elements
* of the second.
*/
concat<T>(placeholder: Placeholder): (list2: ReadonlyArray<T>, list1: ReadonlyArray<T>) => T[];
concat<T>(placeholder: Placeholder, list2: ReadonlyArray<T>): (list1: ReadonlyArray<T>) => T[];
concat<T>(list1: ReadonlyArray<T>, list2: ReadonlyArray<T>): T[];
concat<T>(list1: ReadonlyArray<T>): (list2: ReadonlyArray<T>) => T[];
concat(list1: string, list2: string): string;
concat(list1: string): (list2: string) => string;

/**
* Returns `true` if the specified item is somewhere in the list, `false` otherwise.
* Equivalent to `indexOf(a)(list) > -1`. Uses strict (`===`) equality checking.
*
* @deprecated since 0.26 in favor of includes
*/
contains(__: Placeholder, list: string): (a: string) => boolean;
contains<T>(__: Placeholder, list: T[]): (a: T) => boolean;
contains(__: Placeholder): (list: string, a: string) => boolean;
contains<T>(__: Placeholder): (list: T[], a: T) => boolean;
contains(a: string, list: string): boolean;
contains<T>(a: T, list: ReadonlyArray<T>): boolean;
contains(a: string): (list: string) => boolean;
contains<T>(a: T): (list: ReadonlyArray<T>) => boolean;

/**
* Returns a curried equivalent of the provided function. The curried function has two unusual capabilities.
* First, its arguments needn't be provided one at a time.
Expand Down Expand Up @@ -276,8 +249,6 @@ declare namespace R {
/**
* Divides two numbers. Equivalent to a / b.
*/
divide(__: Placeholder, b: number): (a: number) => number;
divide(__: Placeholder): (b: number, a: number) => number;
divide(a: number, b: number): number;
divide(a: number): (b: number) => number;

Expand Down Expand Up @@ -388,8 +359,6 @@ declare namespace R {
/**
* Returns whether or not an object has an own property with the specified name.
*/
has<T>(__: Placeholder, obj: T): (s: string) => boolean;
has<T>(__: Placeholder): (obj: T, s: string) => boolean;
has<T>(s: string, obj: T): boolean;
has(s: string): <T>(obj: T) => boolean;

Expand All @@ -404,8 +373,7 @@ declare namespace R {
head<T extends Readonly<any> | string>(list: T): T extends string ? string : (T[0] | undefined);

/**
* A function that does nothing but return the parameter supplied to it. Good as a default
* or placeholder function.
* A function that does nothing but return the parameter supplied to it.
*/
identity<T>(a: T): T;

Expand Down Expand Up @@ -546,8 +514,6 @@ declare namespace R {
*
* @deprecated since 0.26 in favor of mergeRight
*/
merge<T2>(__: Placeholder, b: T2): <T1>(a: T1) => Merge<T2, T1>;
merge(__: Placeholder): <T1, T2>(b: T2, a: T1) => Merge<T2, T1>;
merge<T1, T2>(a: T1, b: T2): Merge<T2, T1>;
merge<T1>(a: T1): <T2>(b: T2) => Merge<T2, T1>;

Expand All @@ -571,8 +537,6 @@ declare namespace R {
* Note that this functions preserves the JavaScript-style behavior for
* modulo. For mathematical modulo see `mathMod`
*/
modulo(__: Placeholder, b: number): (a: number) => number;
modulo(__: Placeholder): (b: number, a: number) => number;
modulo(a: number, b: number): number;
modulo(a: number): (b: number) => number;

Expand Down Expand Up @@ -858,7 +822,6 @@ declare namespace R {
/**
* Returns a function that when supplied an object returns the indicated property of that object, if it exists.
*/
prop<T>(__: Placeholder, obj: T): <P extends keyof T>(p: P) => T[P];
prop<P extends keyof T, T>(p: P, obj: T): T[P];
prop<P extends string>(p: P): <T>(obj: Record<P, T>) => T;
prop<P extends string, T>(p: P): (obj: Record<P, T>) => T;
Expand Down Expand Up @@ -962,8 +925,6 @@ declare namespace R {
/**
* Subtracts two numbers. Equivalent to `a - b` but curried.
*/
subtract(__: Placeholder, b: number): (a: number) => number;
subtract(__: Placeholder): (b: number, a: number) => number;
subtract(a: number, b: number): number;
subtract(a: number): (b: number) => number;

Expand Down
35 changes: 0 additions & 35 deletions src/contains.js

This file was deleted.

21 changes: 0 additions & 21 deletions src/contains.spec.js

This file was deleted.

12 changes: 6 additions & 6 deletions src/identical.spec.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { identical } from './identical'
import {_isInteger }from './internal/_isInteger'
import {_objectIs} from './internal/_objectIs'
import {F} from './F'
import {T} from './T'
import { _isInteger } from './internal/_isInteger'
import { _objectIs } from './internal/_objectIs'
import { F } from './F'
import { T } from './T'

test('small', () => {
expect(F()).toBe(false)
Expand All @@ -15,8 +15,8 @@ test('is integer internal', () => {
})

test('object is internal', () => {
expect(_objectIs(1,1)).toBe(true)
expect(_objectIs(NaN,NaN)).toBe(true)
expect(_objectIs(1, 1)).toBe(true)
expect(_objectIs(NaN, NaN)).toBe(true)
})

test('identical', () => {
Expand Down
18 changes: 14 additions & 4 deletions src/includes.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { equals } from './equals'
/**
* Returns `true` if the specified value is equal, in [`R.equals`](#equals)
* terms, to at least one element of the given list; `false` otherwise.
Expand All @@ -18,11 +19,20 @@
* R.includes('ba', 'banana'); //=>true
*/
export function includes(target, list){
if (arguments.length === 1) return _list => includes(target, _list)
if (arguments.length === 1) return _input => includes(target, _input)

const ok = Array.isArray(list) || typeof list === 'string'
if (typeof list === 'string'){
return list.includes(target)
}
if (!Array.isArray(list)) return false

if (!ok) return false
let index = -1

return list.includes(target)
while (++index < list.length){
if (equals(list[ index ], target)){
return true
}
}

return false
}
9 changes: 8 additions & 1 deletion src/includes.spec.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
import { includes } from './includes'
import R from 'ramda'

test('includes with string', () => {
const str = 'more is less'

expect(includes('less')(str)).toBeTruthy()

expect(R.includes('less')(str)).toBeTruthy()
expect(includes('never', str)).toBeFalsy()
expect(R.includes('never', str)).toBeFalsy()
})

test('includes with array', () => {
const arr = [ 1, 2, 3 ]

expect(includes(2)(arr)).toBeTruthy()
expect(R.includes(2)(arr)).toBeTruthy()

expect(includes(4, arr)).toBeFalsy()
expect(R.includes(4, arr)).toBeFalsy()
})

test('return false if input is falsy', () => {
expect(includes(2, null)).toBeFalsy()
expect(() => R.includes(2, null)).toThrow()
expect(includes(4, undefined)).toBeFalsy()
expect(() => R.includes(4, undefined)).toThrow()
})

0 comments on commit d916411

Please sign in to comment.