From 305f4cb464a598df4e9167a8fef14e5aff246b21 Mon Sep 17 00:00:00 2001 From: Dejan Toteff Date: Tue, 4 Feb 2025 14:35:07 +0200 Subject: [PATCH] chore@small --- .github/README.md | 436 +++++++++++++++------------- README.md | 434 +++++++++++++++------------- dist/rambda.js | 8 +- dist/rambda.umd.js | 2 +- docs/README.md | 702 ++++++++++++++++++--------------------------- immutable.d.ts | 62 ++-- index.d.ts | 62 ++-- source/values.js | 1 - src/mergeWith.js | 7 +- src/values.js | 1 - 10 files changed, 859 insertions(+), 856 deletions(-) diff --git a/.github/README.md b/.github/README.md index 12fb6be9..2da2a928 100644 --- a/.github/README.md +++ b/.github/README.md @@ -1334,8 +1334,6 @@ describe('R.append/R.prepend', () => { it('curried', () => { // @ts-expect-error append(true)(listOfNumbersAndStrings) - append(4)(listOfNumbersAndStrings) // $ExpectType (string | number)[] - prepend(4)(listOfNumbersAndStrings) // $ExpectType (string | number)[] }) }) @@ -1346,15 +1344,6 @@ describe('R.append/R.prepend', () => { append('d', listOfNumbers) // $ExpectType (string | number)[] prepend('d', listOfNumbers) // $ExpectType (string | number)[] }) - - it('curried', () => { - // @ts-expect-error - append('d')(listOfNumbers) - const appendD = append('d') - appendD(listOfNumbers) // $ExpectType (string | number)[] - const prependD = prepend('d') - prependD(listOfNumbers) // $ExpectType (string | number)[] - }) }) }) ``` @@ -3059,12 +3048,12 @@ describe('R.countBy', () => { it('happy', () => { const result = countBy(transformFn, list) - result // $ExpectType Record + result // $ExpectType { [index: string]: number; } }) it('curried', () => { const result = countBy(transformFn)(list) - result // $ExpectType Record + result // $ExpectType { [index: string]: number; } }) }) ``` @@ -5181,6 +5170,9 @@ filter( filter( predicate: (value: T) => value is S, ): (list: T[]) => S[]; +filter( + predicate: BooleanConstructor, +): (list: readonly T[]) => NonNullable[]; filter( predicate: BooleanConstructor, ): (list: T[]) => NonNullable[]; @@ -5324,7 +5316,7 @@ test('bad inputs difference between Ramda and Rambda', () => { TypeScript test ```typescript -import { filter, pipe, piped } from 'rambda'; +import { filter, mapIndexed, pipe, piped } from 'rambda'; const list = [1, 2, 3]; @@ -5339,7 +5331,6 @@ describe('R.filter with array', () => { it('within piped', () => { const result = piped( list, - (x) => x, filter((x) => { x; // $ExpectType number return x > 1; @@ -5347,6 +5338,66 @@ describe('R.filter with array', () => { ); result; // $ExpectType number[] }); + it('narrowing type', () => { + interface Foo{ + a: number + } + interface Bar extends Foo{ + b: string + } + + let testList = [{a: 1}, {a: 2}, {a: 3}] + let filterBar = (x: unknown): x is Bar => { + return typeof (x as Bar).b === 'string' + } + const result = piped( + testList, + mapIndexed((x, i) => { + return {a: x.a, b: `${ i }`} + }), + filter(filterBar), + ); + result; // $ExpectType Bar[] + }); + it('narrowing type - readonly', () => { + interface Foo{ + a: number + } + interface Bar extends Foo{ + b: string + } + + let testList = [{a: 1}, {a: 2}, {a: 3}] as const + let filterBar = (x: unknown): x is Bar => { + return typeof (x as Bar).b === 'string' + } + const result = piped( + testList, + mapIndexed((x, i) => { + return {a: x.a, b: `${ i }`} + }), + filter(filterBar), + ); + result; // $ExpectType Bar[] + }); + it('filtering NonNullable', () => { + let testList = [1, 2, null, undefined, 3] + const result = piped( + testList, + filter(Boolean), + ); + result; // $ExpectType number[] + }); + it('filtering NonNullable - readonly', () => { + let testList = [1, 2, null, undefined, 3] as const + const result = piped( + testList, + filter(Boolean), + ); + result; // $ExpectType NonNullable<1 | 2 | 3 | null | undefined>[] + // @ts-expect-error + result.includes(null) + }); it('within pipe requires explicit type', () => { pipe( (x) => x, @@ -8641,7 +8692,7 @@ test('bad inputs difference between Ramda and Rambda', () => { TypeScript test ```typescript -import { map, pipe, piped } from 'rambda'; +import { map, piped } from 'rambda'; const list = [1, 2, 3]; @@ -8651,7 +8702,7 @@ describe('R.map with array', () => { x; // $ExpectType number return x > 1; }, list); - result; // $ExpectType number[] + result; // $ExpectType boolean[] }); it('within piped', () => { const result = piped( @@ -8659,19 +8710,10 @@ describe('R.map with array', () => { (x) => x, map((x) => { x; // $ExpectType number - return x > 1; + return String(x); }), ); - result; // $ExpectType number[] - }); - it('within pipe requires explicit type', () => { - pipe( - (x) => x, - map((x) => { - x; // $ExpectType number - return x > 1; - }), - )(list); + result; // $ExpectType string[] }); }); ``` @@ -8755,37 +8797,37 @@ describe('R.mapObjIndexed', () => { const result = mapObjIndexed((x, prop, obj) => { x // $ExpectType number prop // $ExpectType string - obj // $ExpectType Dictionary + obj // $ExpectType Record | undefined return x + 2 }, obj) - result // $ExpectType Dictionary + result // $ExpectType Record }) it('without type transform - curried', () => { const result = mapObjIndexed((x, prop, obj) => { x // $ExpectType number prop // $ExpectType string - obj // $ExpectType Dictionary + obj // $ExpectType Record | undefined return x + 2 })(obj) - result // $ExpectType Dictionary + result // $ExpectType Record }) it('change of type', () => { const result = mapObjIndexed((x, prop, obj) => { x // $ExpectType number prop // $ExpectType string - obj // $ExpectType Dictionary + obj // $ExpectType Record | undefined return String(x + 2) }, obj) - result // $ExpectType Dictionary + result // $ExpectType Record }) it('change of type - curried', () => { const result = mapObjIndexed((x, prop, obj) => { x // $ExpectType number prop // $ExpectType string - obj // $ExpectType Dictionary + obj // $ExpectType Record | undefined return String(x + 2) })(obj) - result // $ExpectType Dictionary + result // $ExpectType Record }) }) ``` @@ -9614,30 +9656,40 @@ const result = R.mergeRight(target, newProps) ```typescript -mergeWith(fn: (x: any, z: any) => any): (a: U, b: V) => any +mergeWith(fn: (x: any, z: any) => any, a: object): (b: object) => T ``` It takes two objects and a function, which will be used when there is an overlap between the keys. ```javascript -const result = R.mergeWith( - R.concat, - {values : [ 10, 20 ]}, - {values : [ 15, 35 ]} +const result = mergeWithFn( + R.concat, + { + a : true, + values : [ 10, 20 ], + }, + { + b : true, + values : [ 15, 35 ], + } ) -// => [ 10, 20, 15, 35 ] +const expected = { + a : true, + b : true, + values : [ 10, 20, 15, 35 ], +} +// => `result` is equal to `expected` ``` -Try this R.mergeWith example in Rambda REPL +Try this R.mergeWith example in Rambda REPL
All TypeScript definitions ```typescript -mergeWith(fn: (x: any, z: any) => any): (a: U, b: V) => any; -mergeWith(fn: (x: any, z: any) => any, a: U): (b: V) => any; -mergeWith(fn: (x: any, z: any) => any, a: U, b: V): any; +mergeWith(fn: (x: any, z: any) => any, a: object): (b: object) => T; +mergeWith(fn: (x: any, z: any) => any, a: object, b: object): T; ```
@@ -9664,8 +9716,11 @@ export function mergeWithFn( Object.keys(b).forEach(key => { if (willReturn[ key ] !== undefined) return - if (a[ key ] === undefined) willReturn[ key ] = b[ key ] - else willReturn[ key ] = mergeFn(a[ key ], b[ key ]) + if (a[ key ] === undefined){ + willReturn[ key ] = b[ key ] + } else { + willReturn[ key ] = mergeFn(a[ key ], b[ key ]) + } }) return willReturn @@ -9745,13 +9800,8 @@ describe('acts as if nil values are simply empty objects', () => { TypeScript test ```typescript -import {concat, mergeWith} from 'rambda' +import {concat, MergeInsertions, mergeWith, piped} from 'rambda' -interface Output { - a: boolean, - b: boolean, - values: number[], -} const A = { a: true, values: [10, 20], @@ -9761,30 +9811,19 @@ const B = { values: [15, 35], } +type Output = MergeInsertions + describe('R.mergeWith', () => { - test('no curry | without explicit types', () => { - const result = mergeWith(concat, A, B) - result // $ExpectType Record - }) - test('no curry | with explicit types', () => { + test('no curry', () => { const result = mergeWith(concat, A, B) - result // $ExpectType Output + result // $ExpectType { a: boolean; values: number[]; b: boolean; } }) - test('curry 1 | without explicit types', () => { - const result = mergeWith(concat, A)(B) - result // $ExpectType Record - }) - test('curry 1 | with explicit types', () => { - const result = mergeWith(concat, A)(B) - result // $ExpectType Output - }) - test('curry 2 | without explicit types', () => { - const result = mergeWith(concat)(A, B) - result // $ExpectType Record - }) - test('curry 2 | with explicit types', () => { - const result = mergeWith(concat)(A, B) - result // $ExpectType Output + test('inside piped', () => { + const result = piped( + A, + mergeWith(concat, B), + ) + result // $ExpectType { a: boolean; values: number[]; b: boolean; } }) }) ``` @@ -10002,7 +10041,7 @@ describe('brute force', () => { TypeScript test ```typescript -import { add, identity, map, modify, pipe, toUpper } from 'rambda'; +import { modify } from 'rambda'; type Obj = { foo: string; @@ -10011,41 +10050,12 @@ type Obj = { describe('R.modify', () => { it('ramda tests', () => { - const result1 = modify('foo', toUpper, {} as Obj); - result1; // $ExpectType Obj - - const result2 = modify('bar', add(1), {} as Obj); - result2; // $ExpectType Obj - - const result3 = modify('foo', toUpper)({} as Obj); - result3; // $ExpectType Obj - - const result4 = modify('bar', add(1))({} as Obj); - result4; // $ExpectType Obj - - const result5 = modify('foo')(toUpper)({} as Obj); - result5; // $ExpectType Obj - - const result6 = modify('bar')(add(1))({} as Obj); - result6; // $ExpectType Obj - - const result7 = modify('foo')(toUpper, {} as Obj); - result7; // $ExpectType Obj - - const result8 = modify('bar')(add(1), {} as Obj); - result8; // $ExpectType Obj - - const result9 = modify('foo', identity, {} as Obj); - result9; // $ExpectType Obj - - // @ts-expect-error - modify('foo', add(1), {} as Obj); - // @ts-expect-error - modify('bar', toUpper, {} as Obj); - - const f = pipe(map(modify('foo', toUpper))); + const result1 = modify('foo', Number, {} as Obj); + result1.foo; // $ExpectType number + result1.bar; // $ExpectType number - f([] as Obj[]); // $ExpectType Obj[] + const result2 = modify('bar', String, {} as Obj); + result2.bar; // $ExpectType string }); }); ``` @@ -12718,11 +12728,13 @@ const result = R.piped( ```typescript -pluck(property: K, list: T[]): T[K][] +pluck(property: K): (list: T[]) => T[K][] ``` It returns list of the values of `property` taken from the all objects inside `list`. +> :boom: Typescript Note: Pass explicit type annotation when used with **R.pipe/R.compose** for better type inference + ```javascript const list = [{a: 1}, {a: 2}, {b: 3}] const property = 'a' @@ -12738,10 +12750,8 @@ const result = R.pluck(property, list) All TypeScript definitions ```typescript -pluck(property: K, list: T[]): T[K][]; -pluck(property: number, list: { [k: number]: T }[]): T[]; -pluck

(property: P): (list: Record[]) => T[]; -pluck(property: number): (list: { [k: number]: T }[]) => T[]; +pluck(property: K): (list: T[]) => T[K][]; +pluck(property: K, list: T[]): T[K][]; ``` @@ -12802,36 +12812,49 @@ test('with number', () => {

TypeScript test ```typescript -import {pluck} from 'rambda' +import {piped, pluck} from 'rambda' -describe('R.pluck', () => { - it('with object', () => { - interface ListMember { - a: number, - b: string, - } - const input: ListMember[] = [ - {a: 1, b: 'foo'}, - {a: 2, b: 'bar'}, - ] +describe('R.pluck - with property key', () => { + const input = [ + {a: 1, b: 'foo'}, + {a: 2, b: 'bar'}, + ] + it('inside piped', () => { + const result = piped( + input, + pluck('b') + ) + result // $ExpectType string[] + }) + it('without currying', () => { const resultA = pluck('a', input) - const resultB = pluck('b')(input) resultA // $ExpectType number[] - resultB // $ExpectType string[] - }) - it('with array', () => { - const input = [ - [1, 2], - [3, 4], - [5, 6], - ] - const result = pluck(0, input) - const resultCurry = pluck(0)(input) - result // $ExpectType number[] - resultCurry // $ExpectType number[] + // @ts-expect-error + pluck('b')(input) }) }) + +describe('R.pluck - with list index', () => { + const input = [ + [1, 2], + [3, 4], + ] + it('inside piped', () => { + const result = piped( + input, + pluck(0) + ) + result // $ExpectType number[] + }) + it('without currying', () => { + const resultA = pluck(0, input) + resultA // $ExpectType number[] + + // @ts-expect-error + pluck(1)(input) + }) +}) ``` @@ -13114,7 +13137,8 @@ describe('with number as prop', () => { ```typescript -propEq(valueToMatch: any, propToFind: K, obj: Record): boolean +propEq(val: T): { + (name: K): (obj: Record) => boolean ``` It returns true if `obj` has property `propToFind` and its value is equal to `valueToMatch`. @@ -13140,12 +13164,12 @@ const result = [ All TypeScript definitions ```typescript -propEq(valueToMatch: any, propToFind: K, obj: Record): boolean; -propEq(valueToMatch: any, propToFind: K): (obj: Record) => boolean; -propEq(valueToMatch: any): { - (propToFind: K, obj: Record): boolean; - (propToFind: K): (obj: Record) => boolean; +propEq(val: T): { + (name: K): (obj: Record) => boolean; + (name: K, obj: Record): boolean; }; +propEq(val: T, name: K): (obj: Record) => boolean; +propEq(val: U[K], name: K, obj: U): boolean; ``` @@ -13233,22 +13257,16 @@ describe('R.propEq', () => { const myObject: MyType = {} const valueToFind = '1111' - // @ts-expect-error propEq(valueToFind, 'optional', myObject) }) it('imported from @types/ramda', () => { - interface A { - foo: string | null, - } - const obj: A = { + const obj = { foo: 'bar', } const value = '' const result = propEq(value, 'foo')(obj) result // $ExpectType boolean - - // @ts-expect-error propEq(value, 'bar')(obj) }) }) @@ -13855,7 +13873,10 @@ const result = R.reduceBy( ```typescript -reject(predicate: Predicate, list: T[]): T[] +reject( + predicate: (value: T) => boolean, + list: T[], +): T[] ``` It has the opposite effect of `R.filter`. @@ -13879,10 +13900,19 @@ const result = [ All TypeScript definitions ```typescript -reject(predicate: Predicate, list: T[]): T[]; -reject(predicate: Predicate): (list: T[]) => T[]; -reject(predicate: Predicate, obj: Record): Record; -reject(predicate: Predicate): (obj: Record) => Record; +reject( + predicate: (value: T) => boolean, + list: T[], +): T[]; +reject( + predicate: BooleanConstructor, +): (list: readonly T[]) => ("" | null | undefined | false | 0)[]; +reject( + predicate: BooleanConstructor, +): (list: T[]) => ("" | null | undefined | false | 0)[]; +reject( + predicate: (value: T) => boolean, +): (list: T[]) => T[]; ``` @@ -13937,47 +13967,60 @@ test('with object', () => { TypeScript test ```typescript -import {reject} from 'rambda' - -describe('R.reject with array', () => { - it('happy', () => { - const result = reject( - x => { - x // $ExpectType number - return x > 1 - }, - [1, 2, 3] - ) - result // $ExpectType number[] - }) - it('curried require explicit type', () => { - const result = reject(x => { - x // $ExpectType number - return x > 1 - })([1, 2, 3]) - result // $ExpectType number[] - }) -}) +import { reject, mapIndexed, pipe, piped } from 'rambda'; -describe('R.reject with objects', () => { - it('happy', () => { - const result = reject( - x => { - x // $ExpectType number +const list = [1, 2, 3]; - return x > 1 - }, - {a: 1, b: 2} - ) - result // $ExpectType Dictionary - }) - it('curried require dummy type', () => { - const result = reject(x => { - return x > 1 - })({a: 1, b: 2}) - result // $ExpectType Dictionary - }) -}) +describe('R.reject with array', () => { + it('happy', () => { + const result = reject((x) => { + x; // $ExpectType number + return x > 1; + }, list); + result; // $ExpectType number[] + }); + it('within piped', () => { + const result = piped( + list, + reject((x) => { + x; // $ExpectType number + return x > 1; + }), + ); + result; // $ExpectType number[] + }); + it('rejecting NonNullable', () => { + let testList = [1, 2, null, undefined, 3] + const result = piped( + testList, + reject(Boolean), + ); + result; // $ExpectType (false | "" | 0 | null | undefined)[] + }); + it('rejecting NonNullable - readonly', () => { + let testList = [1, 2, null, undefined, 3] as const + const result = piped( + testList, + reject(Boolean), + ); + result; // $ExpectType (false | "" | 0 | null | undefined)[] + // @ts-expect-error + result.includes(1) + }); + it('within pipe requires explicit type', () => { + pipe( + (x) => x, + reject((x) => { + x; // $ExpectType number + return x > 1; + }), + reject((x: number) => { + x; // $ExpectType number + return x > 1; + }), + )(list); + }); +}); ``` @@ -18073,7 +18116,6 @@ values(obj: T): T[K][]; ```javascript import { type } from './type.js' -import * as a from 'remeda' export function values(obj){ if (type(obj) !== 'Object') return [] diff --git a/README.md b/README.md index b37e9c88..52dd0eea 100644 --- a/README.md +++ b/README.md @@ -1332,8 +1332,6 @@ describe('R.append/R.prepend', () => { it('curried', () => { // @ts-expect-error append(true)(listOfNumbersAndStrings) - append(4)(listOfNumbersAndStrings) // $ExpectType (string | number)[] - prepend(4)(listOfNumbersAndStrings) // $ExpectType (string | number)[] }) }) @@ -1344,15 +1342,6 @@ describe('R.append/R.prepend', () => { append('d', listOfNumbers) // $ExpectType (string | number)[] prepend('d', listOfNumbers) // $ExpectType (string | number)[] }) - - it('curried', () => { - // @ts-expect-error - append('d')(listOfNumbers) - const appendD = append('d') - appendD(listOfNumbers) // $ExpectType (string | number)[] - const prependD = prepend('d') - prependD(listOfNumbers) // $ExpectType (string | number)[] - }) }) }) ``` @@ -3045,12 +3034,12 @@ describe('R.countBy', () => { it('happy', () => { const result = countBy(transformFn, list) - result // $ExpectType Record + result // $ExpectType { [index: string]: number; } }) it('curried', () => { const result = countBy(transformFn)(list) - result // $ExpectType Record + result // $ExpectType { [index: string]: number; } }) }) ``` @@ -5157,6 +5146,9 @@ filter( filter( predicate: (value: T) => value is S, ): (list: T[]) => S[]; +filter( + predicate: BooleanConstructor, +): (list: readonly T[]) => NonNullable[]; filter( predicate: BooleanConstructor, ): (list: T[]) => NonNullable[]; @@ -5300,7 +5292,7 @@ test('bad inputs difference between Ramda and Rambda', () => { TypeScript test ```typescript -import { filter, pipe, piped } from 'rambda'; +import { filter, mapIndexed, pipe, piped } from 'rambda'; const list = [1, 2, 3]; @@ -5315,7 +5307,6 @@ describe('R.filter with array', () => { it('within piped', () => { const result = piped( list, - (x) => x, filter((x) => { x; // $ExpectType number return x > 1; @@ -5323,6 +5314,66 @@ describe('R.filter with array', () => { ); result; // $ExpectType number[] }); + it('narrowing type', () => { + interface Foo{ + a: number + } + interface Bar extends Foo{ + b: string + } + + let testList = [{a: 1}, {a: 2}, {a: 3}] + let filterBar = (x: unknown): x is Bar => { + return typeof (x as Bar).b === 'string' + } + const result = piped( + testList, + mapIndexed((x, i) => { + return {a: x.a, b: `${ i }`} + }), + filter(filterBar), + ); + result; // $ExpectType Bar[] + }); + it('narrowing type - readonly', () => { + interface Foo{ + a: number + } + interface Bar extends Foo{ + b: string + } + + let testList = [{a: 1}, {a: 2}, {a: 3}] as const + let filterBar = (x: unknown): x is Bar => { + return typeof (x as Bar).b === 'string' + } + const result = piped( + testList, + mapIndexed((x, i) => { + return {a: x.a, b: `${ i }`} + }), + filter(filterBar), + ); + result; // $ExpectType Bar[] + }); + it('filtering NonNullable', () => { + let testList = [1, 2, null, undefined, 3] + const result = piped( + testList, + filter(Boolean), + ); + result; // $ExpectType number[] + }); + it('filtering NonNullable - readonly', () => { + let testList = [1, 2, null, undefined, 3] as const + const result = piped( + testList, + filter(Boolean), + ); + result; // $ExpectType NonNullable<1 | 2 | 3 | null | undefined>[] + // @ts-expect-error + result.includes(null) + }); it('within pipe requires explicit type', () => { pipe( (x) => x, @@ -8605,7 +8656,7 @@ test('bad inputs difference between Ramda and Rambda', () => { TypeScript test ```typescript -import { map, pipe, piped } from 'rambda'; +import { map, piped } from 'rambda'; const list = [1, 2, 3]; @@ -8615,7 +8666,7 @@ describe('R.map with array', () => { x; // $ExpectType number return x > 1; }, list); - result; // $ExpectType number[] + result; // $ExpectType boolean[] }); it('within piped', () => { const result = piped( @@ -8623,19 +8674,10 @@ describe('R.map with array', () => { (x) => x, map((x) => { x; // $ExpectType number - return x > 1; + return String(x); }), ); - result; // $ExpectType number[] - }); - it('within pipe requires explicit type', () => { - pipe( - (x) => x, - map((x) => { - x; // $ExpectType number - return x > 1; - }), - )(list); + result; // $ExpectType string[] }); }); ``` @@ -8719,37 +8761,37 @@ describe('R.mapObjIndexed', () => { const result = mapObjIndexed((x, prop, obj) => { x // $ExpectType number prop // $ExpectType string - obj // $ExpectType Dictionary + obj // $ExpectType Record | undefined return x + 2 }, obj) - result // $ExpectType Dictionary + result // $ExpectType Record }) it('without type transform - curried', () => { const result = mapObjIndexed((x, prop, obj) => { x // $ExpectType number prop // $ExpectType string - obj // $ExpectType Dictionary + obj // $ExpectType Record | undefined return x + 2 })(obj) - result // $ExpectType Dictionary + result // $ExpectType Record }) it('change of type', () => { const result = mapObjIndexed((x, prop, obj) => { x // $ExpectType number prop // $ExpectType string - obj // $ExpectType Dictionary + obj // $ExpectType Record | undefined return String(x + 2) }, obj) - result // $ExpectType Dictionary + result // $ExpectType Record }) it('change of type - curried', () => { const result = mapObjIndexed((x, prop, obj) => { x // $ExpectType number prop // $ExpectType string - obj // $ExpectType Dictionary + obj // $ExpectType Record | undefined return String(x + 2) })(obj) - result // $ExpectType Dictionary + result // $ExpectType Record }) }) ``` @@ -9576,30 +9618,40 @@ const result = R.mergeRight(target, newProps) ```typescript -mergeWith(fn: (x: any, z: any) => any): (a: U, b: V) => any +mergeWith(fn: (x: any, z: any) => any, a: object): (b: object) => T ``` It takes two objects and a function, which will be used when there is an overlap between the keys. ```javascript -const result = R.mergeWith( - R.concat, - {values : [ 10, 20 ]}, - {values : [ 15, 35 ]} +const result = mergeWithFn( + R.concat, + { + a : true, + values : [ 10, 20 ], + }, + { + b : true, + values : [ 15, 35 ], + } ) -// => [ 10, 20, 15, 35 ] +const expected = { + a : true, + b : true, + values : [ 10, 20, 15, 35 ], +} +// => `result` is equal to `expected` ``` -Try this R.mergeWith example in Rambda REPL +Try this R.mergeWith example in Rambda REPL
All TypeScript definitions ```typescript -mergeWith(fn: (x: any, z: any) => any): (a: U, b: V) => any; -mergeWith(fn: (x: any, z: any) => any, a: U): (b: V) => any; -mergeWith(fn: (x: any, z: any) => any, a: U, b: V): any; +mergeWith(fn: (x: any, z: any) => any, a: object): (b: object) => T; +mergeWith(fn: (x: any, z: any) => any, a: object, b: object): T; ```
@@ -9626,8 +9678,11 @@ export function mergeWithFn( Object.keys(b).forEach(key => { if (willReturn[ key ] !== undefined) return - if (a[ key ] === undefined) willReturn[ key ] = b[ key ] - else willReturn[ key ] = mergeFn(a[ key ], b[ key ]) + if (a[ key ] === undefined){ + willReturn[ key ] = b[ key ] + } else { + willReturn[ key ] = mergeFn(a[ key ], b[ key ]) + } }) return willReturn @@ -9707,13 +9762,8 @@ describe('acts as if nil values are simply empty objects', () => { TypeScript test ```typescript -import {concat, mergeWith} from 'rambda' +import {concat, MergeInsertions, mergeWith, piped} from 'rambda' -interface Output { - a: boolean, - b: boolean, - values: number[], -} const A = { a: true, values: [10, 20], @@ -9723,30 +9773,19 @@ const B = { values: [15, 35], } +type Output = MergeInsertions + describe('R.mergeWith', () => { - test('no curry | without explicit types', () => { - const result = mergeWith(concat, A, B) - result // $ExpectType Record - }) - test('no curry | with explicit types', () => { + test('no curry', () => { const result = mergeWith(concat, A, B) - result // $ExpectType Output - }) - test('curry 1 | without explicit types', () => { - const result = mergeWith(concat, A)(B) - result // $ExpectType Record - }) - test('curry 1 | with explicit types', () => { - const result = mergeWith(concat, A)(B) - result // $ExpectType Output - }) - test('curry 2 | without explicit types', () => { - const result = mergeWith(concat)(A, B) - result // $ExpectType Record + result // $ExpectType { a: boolean; values: number[]; b: boolean; } }) - test('curry 2 | with explicit types', () => { - const result = mergeWith(concat)(A, B) - result // $ExpectType Output + test('inside piped', () => { + const result = piped( + A, + mergeWith(concat, B), + ) + result // $ExpectType { a: boolean; values: number[]; b: boolean; } }) }) ``` @@ -9964,7 +10003,7 @@ describe('brute force', () => { TypeScript test ```typescript -import { add, identity, map, modify, pipe, toUpper } from 'rambda'; +import { modify } from 'rambda'; type Obj = { foo: string; @@ -9973,41 +10012,12 @@ type Obj = { describe('R.modify', () => { it('ramda tests', () => { - const result1 = modify('foo', toUpper, {} as Obj); - result1; // $ExpectType Obj - - const result2 = modify('bar', add(1), {} as Obj); - result2; // $ExpectType Obj - - const result3 = modify('foo', toUpper)({} as Obj); - result3; // $ExpectType Obj - - const result4 = modify('bar', add(1))({} as Obj); - result4; // $ExpectType Obj - - const result5 = modify('foo')(toUpper)({} as Obj); - result5; // $ExpectType Obj - - const result6 = modify('bar')(add(1))({} as Obj); - result6; // $ExpectType Obj - - const result7 = modify('foo')(toUpper, {} as Obj); - result7; // $ExpectType Obj - - const result8 = modify('bar')(add(1), {} as Obj); - result8; // $ExpectType Obj - - const result9 = modify('foo', identity, {} as Obj); - result9; // $ExpectType Obj + const result1 = modify('foo', Number, {} as Obj); + result1.foo; // $ExpectType number + result1.bar; // $ExpectType number - // @ts-expect-error - modify('foo', add(1), {} as Obj); - // @ts-expect-error - modify('bar', toUpper, {} as Obj); - - const f = pipe(map(modify('foo', toUpper))); - - f([] as Obj[]); // $ExpectType Obj[] + const result2 = modify('bar', String, {} as Obj); + result2.bar; // $ExpectType string }); }); ``` @@ -12666,7 +12676,7 @@ const result = R.piped( ```typescript -pluck(property: K, list: T[]): T[K][] +pluck(property: K): (list: T[]) => T[K][] ``` It returns list of the values of `property` taken from the all objects inside `list`. @@ -12686,10 +12696,8 @@ const result = R.pluck(property, list) All TypeScript definitions ```typescript -pluck(property: K, list: T[]): T[K][]; -pluck(property: number, list: { [k: number]: T }[]): T[]; -pluck

(property: P): (list: Record[]) => T[]; -pluck(property: number): (list: { [k: number]: T }[]) => T[]; +pluck(property: K): (list: T[]) => T[K][]; +pluck(property: K, list: T[]): T[K][]; ``` @@ -12750,36 +12758,49 @@ test('with number', () => {

TypeScript test ```typescript -import {pluck} from 'rambda' +import {piped, pluck} from 'rambda' -describe('R.pluck', () => { - it('with object', () => { - interface ListMember { - a: number, - b: string, - } - const input: ListMember[] = [ - {a: 1, b: 'foo'}, - {a: 2, b: 'bar'}, - ] +describe('R.pluck - with property key', () => { + const input = [ + {a: 1, b: 'foo'}, + {a: 2, b: 'bar'}, + ] + it('inside piped', () => { + const result = piped( + input, + pluck('b') + ) + result // $ExpectType string[] + }) + it('without currying', () => { const resultA = pluck('a', input) - const resultB = pluck('b')(input) resultA // $ExpectType number[] - resultB // $ExpectType string[] - }) - it('with array', () => { - const input = [ - [1, 2], - [3, 4], - [5, 6], - ] - const result = pluck(0, input) - const resultCurry = pluck(0)(input) - result // $ExpectType number[] - resultCurry // $ExpectType number[] + // @ts-expect-error + pluck('b')(input) }) }) + +describe('R.pluck - with list index', () => { + const input = [ + [1, 2], + [3, 4], + ] + it('inside piped', () => { + const result = piped( + input, + pluck(0) + ) + result // $ExpectType number[] + }) + it('without currying', () => { + const resultA = pluck(0, input) + resultA // $ExpectType number[] + + // @ts-expect-error + pluck(1)(input) + }) +}) ``` @@ -13062,7 +13083,8 @@ describe('with number as prop', () => { ```typescript -propEq(valueToMatch: any, propToFind: K, obj: Record): boolean +propEq(val: T): { + (name: K): (obj: Record) => boolean ``` It returns true if `obj` has property `propToFind` and its value is equal to `valueToMatch`. @@ -13088,12 +13110,12 @@ const result = [ All TypeScript definitions ```typescript -propEq(valueToMatch: any, propToFind: K, obj: Record): boolean; -propEq(valueToMatch: any, propToFind: K): (obj: Record) => boolean; -propEq(valueToMatch: any): { - (propToFind: K, obj: Record): boolean; - (propToFind: K): (obj: Record) => boolean; +propEq(val: T): { + (name: K): (obj: Record) => boolean; + (name: K, obj: Record): boolean; }; +propEq(val: T, name: K): (obj: Record) => boolean; +propEq(val: U[K], name: K, obj: U): boolean; ``` @@ -13181,22 +13203,16 @@ describe('R.propEq', () => { const myObject: MyType = {} const valueToFind = '1111' - // @ts-expect-error propEq(valueToFind, 'optional', myObject) }) it('imported from @types/ramda', () => { - interface A { - foo: string | null, - } - const obj: A = { + const obj = { foo: 'bar', } const value = '' const result = propEq(value, 'foo')(obj) result // $ExpectType boolean - - // @ts-expect-error propEq(value, 'bar')(obj) }) }) @@ -13801,7 +13817,10 @@ const result = R.reduceBy( ```typescript -reject(predicate: Predicate, list: T[]): T[] +reject( + predicate: (value: T) => boolean, + list: T[], +): T[] ``` It has the opposite effect of `R.filter`. @@ -13825,10 +13844,19 @@ const result = [ All TypeScript definitions ```typescript -reject(predicate: Predicate, list: T[]): T[]; -reject(predicate: Predicate): (list: T[]) => T[]; -reject(predicate: Predicate, obj: Record): Record; -reject(predicate: Predicate): (obj: Record) => Record; +reject( + predicate: (value: T) => boolean, + list: T[], +): T[]; +reject( + predicate: BooleanConstructor, +): (list: readonly T[]) => ("" | null | undefined | false | 0)[]; +reject( + predicate: BooleanConstructor, +): (list: T[]) => ("" | null | undefined | false | 0)[]; +reject( + predicate: (value: T) => boolean, +): (list: T[]) => T[]; ``` @@ -13883,47 +13911,60 @@ test('with object', () => { TypeScript test ```typescript -import {reject} from 'rambda' - -describe('R.reject with array', () => { - it('happy', () => { - const result = reject( - x => { - x // $ExpectType number - return x > 1 - }, - [1, 2, 3] - ) - result // $ExpectType number[] - }) - it('curried require explicit type', () => { - const result = reject(x => { - x // $ExpectType number - return x > 1 - })([1, 2, 3]) - result // $ExpectType number[] - }) -}) +import { reject, mapIndexed, pipe, piped } from 'rambda'; -describe('R.reject with objects', () => { - it('happy', () => { - const result = reject( - x => { - x // $ExpectType number +const list = [1, 2, 3]; - return x > 1 - }, - {a: 1, b: 2} - ) - result // $ExpectType Dictionary - }) - it('curried require dummy type', () => { - const result = reject(x => { - return x > 1 - })({a: 1, b: 2}) - result // $ExpectType Dictionary - }) -}) +describe('R.reject with array', () => { + it('happy', () => { + const result = reject((x) => { + x; // $ExpectType number + return x > 1; + }, list); + result; // $ExpectType number[] + }); + it('within piped', () => { + const result = piped( + list, + reject((x) => { + x; // $ExpectType number + return x > 1; + }), + ); + result; // $ExpectType number[] + }); + it('rejecting NonNullable', () => { + let testList = [1, 2, null, undefined, 3] + const result = piped( + testList, + reject(Boolean), + ); + result; // $ExpectType (false | "" | 0 | null | undefined)[] + }); + it('rejecting NonNullable - readonly', () => { + let testList = [1, 2, null, undefined, 3] as const + const result = piped( + testList, + reject(Boolean), + ); + result; // $ExpectType (false | "" | 0 | null | undefined)[] + // @ts-expect-error + result.includes(1) + }); + it('within pipe requires explicit type', () => { + pipe( + (x) => x, + reject((x) => { + x; // $ExpectType number + return x > 1; + }), + reject((x: number) => { + x; // $ExpectType number + return x > 1; + }), + )(list); + }); +}); ``` @@ -18013,7 +18054,6 @@ values(obj: T): T[K][]; ```javascript import { type } from './type.js' -import * as a from 'remeda' export function values(obj){ if (type(obj) !== 'Object') return [] diff --git a/dist/rambda.js b/dist/rambda.js index 7caeb14b..96fecd8b 100644 --- a/dist/rambda.js +++ b/dist/rambda.js @@ -1,7 +1,5 @@ 'use strict'; -require('remeda'); - function F() { return false; } @@ -1699,7 +1697,11 @@ function mergeWithFn(mergeFn, aInput, bInput) { }); Object.keys(b).forEach(key => { if (willReturn[key] !== undefined) return; - if (a[key] === undefined) willReturn[key] = b[key];else willReturn[key] = mergeFn(a[key], b[key]); + if (a[key] === undefined) { + willReturn[key] = b[key]; + } else { + willReturn[key] = mergeFn(a[key], b[key]); + } }); return willReturn; } diff --git a/dist/rambda.umd.js b/dist/rambda.umd.js index 53962d16..a50cf985 100644 --- a/dist/rambda.umd.js +++ b/dist/rambda.umd.js @@ -1 +1 @@ -!function(n,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports,require("remeda")):"function"==typeof define&&define.amd?define(["exports","remeda"],r):r((n="undefined"!=typeof globalThis?globalThis:n||self).R={})}(this,function(n){function a(n,l){switch(n){case 0:return function(){return l.apply(this,arguments)};case 1:return function(n){return l.apply(this,arguments)};case 2:return function(n,r){return l.apply(this,arguments)};case 3:return function(n,r,t){return l.apply(this,arguments)};case 4:return function(n,r,t,e){return l.apply(this,arguments)};case 5:return function(n,r,t,e,u){return l.apply(this,arguments)};case 6:return function(n,r,t,e,u,i){return l.apply(this,arguments)};case 7:return function(n,r,t,e,u,i,o){return l.apply(this,arguments)};case 8:return function(n,r,t,e,u,i,o,c){return l.apply(this,arguments)};case 9:return function(n,r,t,e,u,i,o,c,f){return l.apply(this,arguments)};default:return function(n,r,t,e,u,i,o,c,f,a){return l.apply(this,arguments)}}}function t(r,n){if(1===arguments.length)return function(n){return t(r,n)};if(10>>0,r>>>=0,Array(u));++en(r)?t:r}var Hn=p(Jn);function $n(n){return n.reduce(function(n,r){return n+r},0)}function Gn(n){return $n(n)/n.length}function W(r,n){return 1===arguments.length?function(n){return W(r,n)}:Object.assign({},r||{},n||{})}function B(r,t){var e;return 1===arguments.length?function(n){return B(r,n)}:(e=b(r),Object.keys(t).forEach(function(n){"Object"===x(t[n])&&"Object"===x(r[n])?e[n]=B(r[n],t[n]):e[n]=t[n]}),e)}function Kn(r,n,t){var e=null!=n?n:{},u=null!=t?t:{},i={};return Object.keys(e).forEach(function(n){i[n]=void 0===u[n]?e[n]:r(e[n],u[n])}),Object.keys(u).forEach(function(n){void 0===i[n]&&(i[n]=void 0===e[n]?u[n]:r(e[n],u[n]))}),i}var Qn=p(Kn);function Vn(n,r,t){return n(t) 4")};var e},n.forEach=function r(t,n){if(1===arguments.length)return function(n){return r(t,n)};if(void 0!==n){if(!g(n))return Sn(t,n);for(var e=0,u=n.length;e>>0,r>>>=0,Array(u));++en(r)?t:r}var Hn=p(Jn);function $n(n){return n.reduce(function(n,r){return n+r},0)}function Gn(n){return $n(n)/n.length}function W(r,n){return 1===arguments.length?function(n){return W(r,n)}:Object.assign({},r||{},n||{})}function B(r,t){var e;return 1===arguments.length?function(n){return B(r,n)}:(e=b(r),Object.keys(t).forEach(function(n){"Object"===x(t[n])&&"Object"===x(r[n])?e[n]=B(r[n],t[n]):e[n]=t[n]}),e)}function Kn(r,n,t){var e=null!=n?n:{},u=null!=t?t:{},i={};return Object.keys(e).forEach(function(n){i[n]=void 0===u[n]?e[n]:r(e[n],u[n])}),Object.keys(u).forEach(function(n){void 0===i[n]&&(i[n]=void 0===e[n]?u[n]:r(e[n],u[n]))}),i}var Qn=p(Kn);function Vn(n,r,t){return n(t) 4")};var e},n.forEach=function r(t,n){if(1===arguments.length)return function(n){return r(t,n)};if(void 0!==n){if(!g(n))return Sn(t,n);for(var e=0,u=n.length;e { it('curried', () => { // @ts-expect-error append(true)(listOfNumbersAndStrings) - append(4)(listOfNumbersAndStrings) // $ExpectType (string | number)[] - prepend(4)(listOfNumbersAndStrings) // $ExpectType (string | number)[] }) }) @@ -1821,15 +1819,6 @@ describe('R.append/R.prepend', () => { append('d', listOfNumbers) // $ExpectType (string | number)[] prepend('d', listOfNumbers) // $ExpectType (string | number)[] }) - - it('curried', () => { - // @ts-expect-error - append('d')(listOfNumbers) - const appendD = append('d') - appendD(listOfNumbers) // $ExpectType (string | number)[] - const prependD = prepend('d') - prependD(listOfNumbers) // $ExpectType (string | number)[] - }) }) }) ``` @@ -2760,7 +2749,7 @@ describe('R.assoc', () => { const result1 = assoc('what')(2, {} as Record) result1.what // $ExpectType number - const result2 = assoc('str')('bar')(obj) + const result2 = assoc('str', 'bar')(obj) result2.str // $ExpectType string result2.num // $ExpectType number @@ -4522,139 +4511,6 @@ test('does return correct length of composed function', () => { -
- -TypeScript test - -```typescript -import { - add, - subtract, - compose, - map, - filter, - identity, - inc, - negate, - dissoc, -} from 'rambda' - -interface Input { - a: string, - b: string, -} -interface Output { - c: string, -} - -describe('R.compose with explicit types', () => { - it('with explicit types - complex', () => { - const obj = { - a: 'foo', - b: 'bar', - } - interface AfterInput { - a: number, - } - interface BeforeOutput { - b: string, - } - - const result = compose( - x => ({c: x.b + 'bar'}), - x => ({b: x.a + 'foo'}), - x => ({a: x.a.length + x.b.length}) - )(obj) - - result // $ExpectType Output - }) - it('with explicit types - correct', () => { - const obj = { - a: 'foo', - b: 'bar', - } - const result = compose(identity, input => { - input // $ExpectType Input - return input as unknown as Output - })(obj) - result // $ExpectType Output - }) - it('with explicit types - wrong', () => { - const obj: Input = { - a: 'foo', - b: 'bar', - } - - // @ts-expect-error - compose(identity, dissoc('b'))(obj) - }) -}) - -describe('R.compose', () => { - it('happy', () => { - const result = compose(subtract(11), add(1), add(1))(1) - result // $ExpectType number - }) - it('happy - more complex', () => { - const result = compose( - (x: number) => x + 1, - (x: string) => x.length + 1 - )('foo') - result // $ExpectType number - }) - - it('with R.filter', () => { - const result = compose( - filter(x => x > 2), - map(add(1)) - )([1, 2, 3]) - result // $ExpectType number[] - }) - - it('with native filter', () => { - const result = compose( - (list: number[]) => list.filter(x => x > 2), - (list: number[]) => { - list // $ExpectType number[] - return list - }, - map(add(1)) - )([1, 2, 3]) - - result // $ExpectType number[] - }) - - it('with void', () => { - const result = compose( - () => {}, - () => {} - )() - result // $ExpectType void - }) -}) - -describe('R.compose - @types/ramda tests', () => { - test('complex', () => { - const fn = compose( - inc, - inc, - inc, - inc, - inc, - inc, - inc, - inc, - negate, - Math.pow - ) - const result = fn(3, 4) - result // $ExpectType number - }) -}) -``` - -
- [![---------------](https://raw.githubusercontent.com/selfrefactor/rambda/master/files/separator.png)](#compose) ### composeWith @@ -5395,12 +5251,12 @@ describe('R.countBy', () => { it('happy', () => { const result = countBy(transformFn, list) - result // $ExpectType Record + result // $ExpectType { [index: string]: number; } }) it('curried', () => { const result = countBy(transformFn)(list) - result // $ExpectType Record + result // $ExpectType { [index: string]: number; } }) }) ``` @@ -9397,6 +9253,9 @@ filter( filter( predicate: (value: T) => value is S, ): (list: T[]) => S[]; +filter( + predicate: BooleanConstructor, +): (list: readonly T[]) => NonNullable[]; filter( predicate: BooleanConstructor, ): (list: T[]) => NonNullable[]; @@ -9540,7 +9399,7 @@ test('bad inputs difference between Ramda and Rambda', () => { TypeScript test ```typescript -import { filter, pipe, piped } from 'rambda'; +import { filter, mapIndexed, pipe, piped } from 'rambda'; const list = [1, 2, 3]; @@ -9555,7 +9414,6 @@ describe('R.filter with array', () => { it('within piped', () => { const result = piped( list, - (x) => x, filter((x) => { x; // $ExpectType number return x > 1; @@ -9563,6 +9421,66 @@ describe('R.filter with array', () => { ); result; // $ExpectType number[] }); + it('narrowing type', () => { + interface Foo{ + a: number + } + interface Bar extends Foo{ + b: string + } + + let testList = [{a: 1}, {a: 2}, {a: 3}] + let filterBar = (x: unknown): x is Bar => { + return typeof (x as Bar).b === 'string' + } + const result = piped( + testList, + mapIndexed((x, i) => { + return {a: x.a, b: `${ i }`} + }), + filter(filterBar), + ); + result; // $ExpectType Bar[] + }); + it('narrowing type - readonly', () => { + interface Foo{ + a: number + } + interface Bar extends Foo{ + b: string + } + + let testList = [{a: 1}, {a: 2}, {a: 3}] as const + let filterBar = (x: unknown): x is Bar => { + return typeof (x as Bar).b === 'string' + } + const result = piped( + testList, + mapIndexed((x, i) => { + return {a: x.a, b: `${ i }`} + }), + filter(filterBar), + ); + result; // $ExpectType Bar[] + }); + it('filtering NonNullable', () => { + let testList = [1, 2, null, undefined, 3] + const result = piped( + testList, + filter(Boolean), + ); + result; // $ExpectType number[] + }); + it('filtering NonNullable - readonly', () => { + let testList = [1, 2, null, undefined, 3] as const + const result = piped( + testList, + filter(Boolean), + ); + result; // $ExpectType NonNullable<1 | 2 | 3 | null | undefined>[] + // @ts-expect-error + result.includes(null) + }); it('within pipe requires explicit type', () => { pipe( (x) => x, @@ -15030,7 +14948,7 @@ test('bad inputs difference between Ramda and Rambda', () => { TypeScript test ```typescript -import { map, pipe, piped } from 'rambda'; +import { map, piped } from 'rambda'; const list = [1, 2, 3]; @@ -15040,7 +14958,7 @@ describe('R.map with array', () => { x; // $ExpectType number return x > 1; }, list); - result; // $ExpectType number[] + result; // $ExpectType boolean[] }); it('within piped', () => { const result = piped( @@ -15048,19 +14966,10 @@ describe('R.map with array', () => { (x) => x, map((x) => { x; // $ExpectType number - return x > 1; + return String(x); }), ); - result; // $ExpectType number[] - }); - it('within pipe requires explicit type', () => { - pipe( - (x) => x, - map((x) => { - x; // $ExpectType number - return x > 1; - }), - )(list); + result; // $ExpectType string[] }); }); ``` @@ -15144,37 +15053,37 @@ describe('R.mapObjIndexed', () => { const result = mapObjIndexed((x, prop, obj) => { x // $ExpectType number prop // $ExpectType string - obj // $ExpectType Dictionary + obj // $ExpectType Record | undefined return x + 2 }, obj) - result // $ExpectType Dictionary + result // $ExpectType Record }) it('without type transform - curried', () => { const result = mapObjIndexed((x, prop, obj) => { x // $ExpectType number prop // $ExpectType string - obj // $ExpectType Dictionary + obj // $ExpectType Record | undefined return x + 2 })(obj) - result // $ExpectType Dictionary + result // $ExpectType Record }) it('change of type', () => { const result = mapObjIndexed((x, prop, obj) => { x // $ExpectType number prop // $ExpectType string - obj // $ExpectType Dictionary + obj // $ExpectType Record | undefined return String(x + 2) }, obj) - result // $ExpectType Dictionary + result // $ExpectType Record }) it('change of type - curried', () => { const result = mapObjIndexed((x, prop, obj) => { x // $ExpectType number prop // $ExpectType string - obj // $ExpectType Dictionary + obj // $ExpectType Record | undefined return String(x + 2) })(obj) - result // $ExpectType Dictionary + result // $ExpectType Record }) }) ``` @@ -16468,30 +16377,40 @@ describe('R.mergeRight', () => { ```typescript -mergeWith(fn: (x: any, z: any) => any): (a: U, b: V) => any +mergeWith(fn: (x: any, z: any) => any, a: object): (b: object) => T ``` It takes two objects and a function, which will be used when there is an overlap between the keys. ```javascript -const result = R.mergeWith( - R.concat, - {values : [ 10, 20 ]}, - {values : [ 15, 35 ]} +const result = mergeWithFn( + R.concat, + { + a : true, + values : [ 10, 20 ], + }, + { + b : true, + values : [ 15, 35 ], + } ) -// => [ 10, 20, 15, 35 ] +const expected = { + a : true, + b : true, + values : [ 10, 20, 15, 35 ], +} +// => `result` is equal to `expected` ``` -Try this R.mergeWith example in Rambda REPL +Try this R.mergeWith example in Rambda REPL
All TypeScript definitions ```typescript -mergeWith(fn: (x: any, z: any) => any): (a: U, b: V) => any; -mergeWith(fn: (x: any, z: any) => any, a: U): (b: V) => any; -mergeWith(fn: (x: any, z: any) => any, a: U, b: V): any; +mergeWith(fn: (x: any, z: any) => any, a: object): (b: object) => T; +mergeWith(fn: (x: any, z: any) => any, a: object, b: object): T; ```
@@ -16518,8 +16437,11 @@ export function mergeWithFn( Object.keys(b).forEach(key => { if (willReturn[ key ] !== undefined) return - if (a[ key ] === undefined) willReturn[ key ] = b[ key ] - else willReturn[ key ] = mergeFn(a[ key ], b[ key ]) + if (a[ key ] === undefined){ + willReturn[ key ] = b[ key ] + } else { + willReturn[ key ] = mergeFn(a[ key ], b[ key ]) + } }) return willReturn @@ -16599,13 +16521,8 @@ describe('acts as if nil values are simply empty objects', () => { TypeScript test ```typescript -import {concat, mergeWith} from 'rambda' +import {concat, MergeInsertions, mergeWith, piped} from 'rambda' -interface Output { - a: boolean, - b: boolean, - values: number[], -} const A = { a: true, values: [10, 20], @@ -16615,30 +16532,19 @@ const B = { values: [15, 35], } +type Output = MergeInsertions + describe('R.mergeWith', () => { - test('no curry | without explicit types', () => { - const result = mergeWith(concat, A, B) - result // $ExpectType Record - }) - test('no curry | with explicit types', () => { + test('no curry', () => { const result = mergeWith(concat, A, B) - result // $ExpectType Output - }) - test('curry 1 | without explicit types', () => { - const result = mergeWith(concat, A)(B) - result // $ExpectType Record - }) - test('curry 1 | with explicit types', () => { - const result = mergeWith(concat, A)(B) - result // $ExpectType Output + result // $ExpectType { a: boolean; values: number[]; b: boolean; } }) - test('curry 2 | without explicit types', () => { - const result = mergeWith(concat)(A, B) - result // $ExpectType Record - }) - test('curry 2 | with explicit types', () => { - const result = mergeWith(concat)(A, B) - result // $ExpectType Output + test('inside piped', () => { + const result = piped( + A, + mergeWith(concat, B), + ) + result // $ExpectType { a: boolean; values: number[]; b: boolean; } }) }) ``` @@ -17010,7 +16916,7 @@ describe('brute force', () => { TypeScript test ```typescript -import { add, identity, map, modify, pipe, toUpper } from 'rambda'; +import { modify } from 'rambda'; type Obj = { foo: string; @@ -17019,41 +16925,12 @@ type Obj = { describe('R.modify', () => { it('ramda tests', () => { - const result1 = modify('foo', toUpper, {} as Obj); - result1; // $ExpectType Obj - - const result2 = modify('bar', add(1), {} as Obj); - result2; // $ExpectType Obj - - const result3 = modify('foo', toUpper)({} as Obj); - result3; // $ExpectType Obj - - const result4 = modify('bar', add(1))({} as Obj); - result4; // $ExpectType Obj - - const result5 = modify('foo')(toUpper)({} as Obj); - result5; // $ExpectType Obj - - const result6 = modify('bar')(add(1))({} as Obj); - result6; // $ExpectType Obj - - const result7 = modify('foo')(toUpper, {} as Obj); - result7; // $ExpectType Obj + const result1 = modify('foo', Number, {} as Obj); + result1.foo; // $ExpectType number + result1.bar; // $ExpectType number - const result8 = modify('bar')(add(1), {} as Obj); - result8; // $ExpectType Obj - - const result9 = modify('foo', identity, {} as Obj); - result9; // $ExpectType Obj - - // @ts-expect-error - modify('foo', add(1), {} as Obj); - // @ts-expect-error - modify('bar', toUpper, {} as Obj); - - const f = pipe(map(modify('foo', toUpper))); - - f([] as Obj[]); // $ExpectType Obj[] + const result2 = modify('bar', String, {} as Obj); + result2.bar; // $ExpectType string }); }); ``` @@ -17066,7 +16943,7 @@ describe('R.modify', () => { ```typescript -modifyPath(path: [], fn: (value: U) => T, obj: U): T +modifyPath(path: string[], fn: (value: unknown) => unknown): (obj: object) => T ``` It changes a property of object on the base of provided path and transformer function. @@ -17083,6 +16960,7 @@ const result = R.modifyPath('a.b.c', x=> x+1, {a:{b: {c:1}}}) All TypeScript definitions ```typescript +modifyPath(path: string[], fn: (value: unknown) => unknown): (obj: object) => T; modifyPath(path: [], fn: (value: U) => T, obj: U): T; modifyPath(path: [K0], fn: (value: U[K0]) => T, obj: U): DeepModify<[K0], U, T>; modifyPath< @@ -17213,22 +17091,37 @@ test('with array', () => { TypeScript test ```typescript -import {modifyPath} from 'rambda' +import {modifyPath, piped} from 'rambda' const obj = {a: {b: {c: 1}}} describe('R.modifyPath', () => { it('happy', () => { - const result = modifyPath('a.b.c', (x: number) => x + 1, obj) - result // $ExpectType Record - }) - it('explicit return type', () => { - interface Foo extends Record { - a: 1, - } - const result = modifyPath('a.b.c', (x: number) => x + 1, obj) - result // $ExpectType Foo + const result = modifyPath(['a', 'b', 'c'], (x: number) => String(x), obj) + result.a.b.c // $ExpectType string }) + it('inside piped - require explicit return type', () =>{ + interface Output { + a: { + b: { + c: string + } + } + } + + let result = piped( + obj, + modifyPath(['a', 'b', 'c'], (x) => String(x)) + ) + result.a.b.c // $ExpectType string + }) + it('inside piped - without explicit return type', () =>{ + let result = piped( + obj, + modifyPath(['a', 'b', 'c'], (x) => String(x)) + ) + result // $ExpectType object + }) }) ``` @@ -17888,7 +17781,7 @@ describe('R.nth - string', () => { ```typescript -objOf(key: K, value: T): Record +objOf(key: K, value: T) : { [P in K]: T } ``` It creates an object with a single key-value pair. @@ -17905,8 +17798,8 @@ const result = R.objOf('foo', 'bar') All TypeScript definitions ```typescript -objOf(key: K, value: T): Record; -objOf(key: K): (value: T) => Record; +objOf(key: K, value: T) : { [P in K]: T }; +objOf(key: K): (value: T) => { [P in K]: T }; ``` @@ -17980,7 +17873,7 @@ describe('brute force', () => { TypeScript test ```typescript -import {objOf} from 'rambda' +import {objOf, piped} from 'rambda' const key = 'foo' const value = 42 @@ -17994,11 +17887,15 @@ describe('R.objOf', () => { // @ts-expect-error result.bar }) - it('curried', () => { - const result = objOf(key)(value) - - result.foo // $ExpectType number - }) + it('inside piped', () => { + const result = piped( + value, + objOf(key) + ) + result.foo // $ExpectType number + // @ts-expect-error + result.bar + }) }) ``` @@ -20900,55 +20797,6 @@ test('with bad input', () => { -
- -TypeScript test - -```typescript -import { add, filter, map, pipe } from 'rambda'; - -describe('R.pipe', () => { - it('with R.filter', () => { - const result = pipe( - filter((x) => x > 2), - map(add(1)), - )([1, 2, 3]); - result; // $ExpectType number[] - }); - - it('with native filter', () => { - const result = pipe( - (list: number[]) => list.filter((x) => x > 2), - (list: number[]) => { - list; // $ExpectType number[] - return list; - }, - map(add(1)), - )([1, 2, 3]); - - result; // $ExpectType number[] - }); - - it('with void', () => { - const result = pipe( - () => {}, - () => {}, - )(); - result; // $ExpectType void - }); -}); - -describe('R.pipe - @types/ramda tests', () => { - test('complex', () => { - const fn = pipe(Math.pow, negate, inc, inc, inc, inc, inc, inc, inc, inc); - const result = fn(3, 4); - result; // $ExpectType number - }); -}); -``` - -
- [![---------------](https://raw.githubusercontent.com/selfrefactor/rambda/master/files/separator.png)](#pipe) ### piped @@ -21440,7 +21288,8 @@ function tapFn(transformFn: (x: T) => U, fn: (a: T, b: U) => void): (x: T) fn(x, result); return x; }; -} +} + describe('real use cases - books', () => { it('case 1', () => { const result = piped( @@ -21454,7 +21303,7 @@ describe('real use cases - books', () => { }), tapFn(union([awardedBrothersKaramazov]),(a, b) => { a; // $ExpectType Book[] - b; // $ExpectType boolean + b; // $ExpectType Book[] }), find(x => { x // $ExpectType Book @@ -21512,11 +21361,13 @@ describe('real use cases - books', () => { ```typescript -pluck(property: K, list: T[]): T[K][] +pluck(property: K): (list: T[]) => T[K][] ``` It returns list of the values of `property` taken from the all objects inside `list`. +> :boom: Typescript Note: Pass explicit type annotation when used with **R.pipe/R.compose** for better type inference + ```javascript const list = [{a: 1}, {a: 2}, {b: 3}] const property = 'a' @@ -21532,10 +21383,8 @@ const result = R.pluck(property, list) All TypeScript definitions ```typescript -pluck(property: K, list: T[]): T[K][]; -pluck(property: number, list: { [k: number]: T }[]): T[]; -pluck

(property: P): (list: Record[]) => T[]; -pluck(property: number): (list: { [k: number]: T }[]) => T[]; +pluck(property: K): (list: T[]) => T[K][]; +pluck(property: K, list: T[]): T[K][]; ``` @@ -21596,36 +21445,49 @@ test('with number', () => {

TypeScript test ```typescript -import {pluck} from 'rambda' +import {piped, pluck} from 'rambda' -describe('R.pluck', () => { - it('with object', () => { - interface ListMember { - a: number, - b: string, - } - const input: ListMember[] = [ - {a: 1, b: 'foo'}, - {a: 2, b: 'bar'}, - ] +describe('R.pluck - with property key', () => { + const input = [ + {a: 1, b: 'foo'}, + {a: 2, b: 'bar'}, + ] + it('inside piped', () => { + const result = piped( + input, + pluck('b') + ) + result // $ExpectType string[] + }) + it('without currying', () => { const resultA = pluck('a', input) - const resultB = pluck('b')(input) resultA // $ExpectType number[] - resultB // $ExpectType string[] - }) - it('with array', () => { - const input = [ - [1, 2], - [3, 4], - [5, 6], - ] - const result = pluck(0, input) - const resultCurry = pluck(0)(input) - result // $ExpectType number[] - resultCurry // $ExpectType number[] + // @ts-expect-error + pluck('b')(input) }) }) + +describe('R.pluck - with list index', () => { + const input = [ + [1, 2], + [3, 4], + ] + it('inside piped', () => { + const result = piped( + input, + pluck(0) + ) + result // $ExpectType number[] + }) + it('without currying', () => { + const resultA = pluck(0, input) + resultA // $ExpectType number[] + + // @ts-expect-error + pluck(1)(input) + }) +}) ``` @@ -21908,7 +21770,8 @@ describe('with number as prop', () => { ```typescript -propEq(valueToMatch: any, propToFind: K, obj: Record): boolean +propEq(val: T): { + (name: K): (obj: Record) => boolean ``` It returns true if `obj` has property `propToFind` and its value is equal to `valueToMatch`. @@ -21934,12 +21797,12 @@ const result = [ All TypeScript definitions ```typescript -propEq(valueToMatch: any, propToFind: K, obj: Record): boolean; -propEq(valueToMatch: any, propToFind: K): (obj: Record) => boolean; -propEq(valueToMatch: any): { - (propToFind: K, obj: Record): boolean; - (propToFind: K): (obj: Record) => boolean; +propEq(val: T): { + (name: K): (obj: Record) => boolean; + (name: K, obj: Record): boolean; }; +propEq(val: T, name: K): (obj: Record) => boolean; +propEq(val: U[K], name: K, obj: U): boolean; ``` @@ -22027,22 +21890,16 @@ describe('R.propEq', () => { const myObject: MyType = {} const valueToFind = '1111' - // @ts-expect-error propEq(valueToFind, 'optional', myObject) }) it('imported from @types/ramda', () => { - interface A { - foo: string | null, - } - const obj: A = { + const obj = { foo: 'bar', } const value = '' const result = propEq(value, 'foo')(obj) result // $ExpectType boolean - - // @ts-expect-error propEq(value, 'bar')(obj) }) }) @@ -22748,7 +22605,7 @@ test('returns the accumulator for an undefined list', () => { TypeScript test ```typescript -import {reduce, reduceStopper} from 'rambda' +import {reduce} from 'rambda' describe('R.reduce', () => { it('happy', () => { @@ -22809,21 +22666,6 @@ describe('R.reduce', () => { result // $ExpectType number }) - it('using `reduceStopper` to stop the loop', () => { - const result = reduce( - (acc, elem, i) => { - acc // $ExpectType number - elem // $ExpectType number - i // $ExpectType number - return acc + elem > 1 ? reduceStopper(elem) : acc - }, - 1, - [1, 2, 3] - ) - - result // $ExpectType number - }) - it('fallback', () => { const result = reduce( (acc, val) => { @@ -23126,7 +22968,10 @@ test('R.reduceBy', () => { ```typescript -reject(predicate: Predicate, list: T[]): T[] +reject( + predicate: (value: T) => boolean, + list: T[], +): T[] ``` It has the opposite effect of `R.filter`. @@ -23150,10 +22995,19 @@ const result = [ All TypeScript definitions ```typescript -reject(predicate: Predicate, list: T[]): T[]; -reject(predicate: Predicate): (list: T[]) => T[]; -reject(predicate: Predicate, obj: Record): Record; -reject(predicate: Predicate): (obj: Record) => Record; +reject( + predicate: (value: T) => boolean, + list: T[], +): T[]; +reject( + predicate: BooleanConstructor, +): (list: readonly T[]) => ("" | null | undefined | false | 0)[]; +reject( + predicate: BooleanConstructor, +): (list: T[]) => ("" | null | undefined | false | 0)[]; +reject( + predicate: (value: T) => boolean, +): (list: T[]) => T[]; ``` @@ -23208,47 +23062,60 @@ test('with object', () => { TypeScript test ```typescript -import {reject} from 'rambda' - -describe('R.reject with array', () => { - it('happy', () => { - const result = reject( - x => { - x // $ExpectType number - return x > 1 - }, - [1, 2, 3] - ) - result // $ExpectType number[] - }) - it('curried require explicit type', () => { - const result = reject(x => { - x // $ExpectType number - return x > 1 - })([1, 2, 3]) - result // $ExpectType number[] - }) -}) +import { reject, mapIndexed, pipe, piped } from 'rambda'; -describe('R.reject with objects', () => { - it('happy', () => { - const result = reject( - x => { - x // $ExpectType number +const list = [1, 2, 3]; - return x > 1 - }, - {a: 1, b: 2} - ) - result // $ExpectType Dictionary - }) - it('curried require dummy type', () => { - const result = reject(x => { - return x > 1 - })({a: 1, b: 2}) - result // $ExpectType Dictionary - }) -}) +describe('R.reject with array', () => { + it('happy', () => { + const result = reject((x) => { + x; // $ExpectType number + return x > 1; + }, list); + result; // $ExpectType number[] + }); + it('within piped', () => { + const result = piped( + list, + reject((x) => { + x; // $ExpectType number + return x > 1; + }), + ); + result; // $ExpectType number[] + }); + it('rejecting NonNullable', () => { + let testList = [1, 2, null, undefined, 3] + const result = piped( + testList, + reject(Boolean), + ); + result; // $ExpectType (false | "" | 0 | null | undefined)[] + }); + it('rejecting NonNullable - readonly', () => { + let testList = [1, 2, null, undefined, 3] as const + const result = piped( + testList, + reject(Boolean), + ); + result; // $ExpectType (false | "" | 0 | null | undefined)[] + // @ts-expect-error + result.includes(1) + }); + it('within pipe requires explicit type', () => { + pipe( + (x) => x, + reject((x) => { + x; // $ExpectType number + return x > 1; + }), + reject((x: number) => { + x; // $ExpectType number + return x > 1; + }), + )(list); + }); +}); ``` @@ -28630,7 +28497,6 @@ values(obj: T): T[K][]; ```javascript import { type } from './type.js' -import * as a from 'remeda' export function values(obj){ if (type(obj) !== 'Object') return [] diff --git a/immutable.d.ts b/immutable.d.ts index 1b384723..72163ba9 100644 --- a/immutable.d.ts +++ b/immutable.d.ts @@ -19,6 +19,22 @@ export type EntryForKey = Key extends number | string export type Entry = Simplify<{ readonly [P in keyof T]-?: EntryForKey }[keyof T]>; +export type DeepModify = + Keys extends readonly [infer K, ...infer Rest] + ? K extends keyof U + ? Rest extends readonly [] + ? Omit & Record + : Rest extends readonly PropertyKey[] + ? Omit & Record> + : never + : never + : never; + +export type MergeInsertions = +T extends object + ? { readonly [K in keyof T]: MergeInsertions } + : T + export type IndexedIterator = (x: T, i: number) => U; export type ObjectIterator = (x: T, prop: string, inputObj: Record) => U; type Ord = number | string | boolean | Date; @@ -671,6 +687,9 @@ export function filter( export function filter( predicate: BooleanConstructor, ): (list: readonly T[]) => readonly NonNullable[]; +export function filter( + predicate: BooleanConstructor, +): (list: readonly T[]) => readonly NonNullable[]; export function filter( predicate: (value: T) => boolean, ): (list: readonly T[]) => readonly T[]; @@ -1140,9 +1159,8 @@ export function mergeRight(target: any): (newProps: any) => Output; /** * It takes two objects and a function, which will be used when there is an overlap between the keys. */ -export function mergeWith(fn: (x: any, z: any) => any): (a: U, b: V) => any; -export function mergeWith(fn: (x: any, z: any) => any, a: U): (b: V) => any; -export function mergeWith(fn: (x: any, z: any) => any, a: U, b: V): any; +export function mergeWith(fn: (x: any, z: any) => any, a: object): (b: object) => T; +export function mergeWith(fn: (x: any, z: any) => any, a: object, b: object): T; /** * It returns the lesser value between `x` and `y`. @@ -1173,6 +1191,7 @@ export function modify( /** * It changes a property of object on the base of provided path and transformer function. */ +export function modifyPath(path: readonly string[], fn: (value: unknown) => unknown): (obj: object) => T; export function modifyPath(path: readonly [], fn: (value: U) => T, obj: U): T; export function modifyPath(path: readonly [K0], fn: (value: U[K0]) => T, obj: U): DeepModify; export function modifyPath< @@ -1276,8 +1295,8 @@ export function nth(n: number): { /** * It creates an object with a single key-value pair. */ -export function objOf(key: K, value: T): Record; -export function objOf(key: K): (value: T) => Record; +export function objOf(key: K, value: T) : { readonly [P in K]: T }; +export function objOf(key: K): (value: T) => { readonly [P in K]: T }; export function of(x: T): readonly T[]; @@ -1845,10 +1864,8 @@ export function piped(property: K, list: readonly T[]): readonly T[K][]; -export function pluck(property: number, list: readonly { readonly [k: number]: T }[]): readonly T[]; -export function pluck

(property: P): (list: readonly Record[]) => readonly T[]; -export function pluck(property: number): (list: readonly { readonly [k: number]: T }[]) => readonly T[]; +export function pluck(property: K): (list: readonly T[]) => readonly T[K][]; +export function pluck(property: K, list: readonly T[]): readonly T[K][]; /** * It adds element `x` at the beginning of `list`. @@ -1871,12 +1888,12 @@ export function prop(p: keyof never): (value: unknown) => V; /** * It returns true if `obj` has property `propToFind` and its value is equal to `valueToMatch`. */ -export function propEq(valueToMatch: any, propToFind: K, obj: Record): boolean; -export function propEq(valueToMatch: any, propToFind: K): (obj: Record) => boolean; -export function propEq(valueToMatch: any): { - (propToFind: K, obj: Record): boolean; - (propToFind: K): (obj: Record) => boolean; +export function propEq(val: T): { + (name: K): (obj: Record) => boolean; + (name: K, obj: Record): boolean; }; +export function propEq(val: T, name: K): (obj: Record) => boolean; +export function propEq(val: U[K], name: K, obj: U): boolean; /** * It returns `true` if `property` of `obj` is from `target` type. @@ -1946,10 +1963,19 @@ export function reduceBy( /** * It has the opposite effect of `R.filter`. */ -export function reject(predicate: Predicate, list: readonly T[]): readonly T[]; -export function reject(predicate: Predicate): (list: readonly T[]) => readonly T[]; -export function reject(predicate: Predicate, obj: Record): Record; -export function reject(predicate: Predicate): (obj: Record) => Record; +export function reject( + predicate: (value: T) => boolean, + list: readonly T[], +): readonly T[]; +export function reject( + predicate: BooleanConstructor, +): (list: readonly T[]) => readonly ("" | null | undefined | false | 0)[]; +export function reject( + predicate: BooleanConstructor, +): (list: readonly T[]) => readonly ("" | null | undefined | false | 0)[]; +export function reject( + predicate: (value: T) => boolean, +): (list: readonly T[]) => readonly T[]; /** * It returns a copy of `list` input with removed `index`. diff --git a/index.d.ts b/index.d.ts index 21ead739..a9a3b0de 100644 --- a/index.d.ts +++ b/index.d.ts @@ -19,6 +19,22 @@ export type EntryForKey = Key extends number | string export type Entry = Simplify<{ [P in keyof T]-?: EntryForKey }[keyof T]>; +export type DeepModify = + Keys extends [infer K, ...infer Rest] + ? K extends keyof U + ? Rest extends readonly [] + ? Omit & Record + : Rest extends readonly PropertyKey[] + ? Omit & Record> + : never + : never + : never; + +export type MergeInsertions = +T extends object + ? { [K in keyof T]: MergeInsertions } + : T + export type IndexedIterator = (x: T, i: number) => U; export type ObjectIterator = (x: T, prop: string, inputObj: Record) => U; type Ord = number | string | boolean | Date; @@ -668,6 +684,9 @@ export function filter( export function filter( predicate: (value: T) => value is S, ): (list: T[]) => S[]; +export function filter( + predicate: BooleanConstructor, +): (list: readonly T[]) => NonNullable[]; export function filter( predicate: BooleanConstructor, ): (list: T[]) => NonNullable[]; @@ -1140,9 +1159,8 @@ export function mergeRight(target: any): (newProps: any) => Output; /** * It takes two objects and a function, which will be used when there is an overlap between the keys. */ -export function mergeWith(fn: (x: any, z: any) => any): (a: U, b: V) => any; -export function mergeWith(fn: (x: any, z: any) => any, a: U): (b: V) => any; -export function mergeWith(fn: (x: any, z: any) => any, a: U, b: V): any; +export function mergeWith(fn: (x: any, z: any) => any, a: object): (b: object) => T; +export function mergeWith(fn: (x: any, z: any) => any, a: object, b: object): T; /** * It returns the lesser value between `x` and `y`. @@ -1173,6 +1191,7 @@ export function modify( /** * It changes a property of object on the base of provided path and transformer function. */ +export function modifyPath(path: string[], fn: (value: unknown) => unknown): (obj: object) => T; export function modifyPath(path: [], fn: (value: U) => T, obj: U): T; export function modifyPath(path: [K0], fn: (value: U[K0]) => T, obj: U): DeepModify<[K0], U, T>; export function modifyPath< @@ -1276,8 +1295,8 @@ export function nth(n: number): { /** * It creates an object with a single key-value pair. */ -export function objOf(key: K, value: T): Record; -export function objOf(key: K): (value: T) => Record; +export function objOf(key: K, value: T) : { [P in K]: T }; +export function objOf(key: K): (value: T) => { [P in K]: T }; export function of(x: T): T[]; @@ -1845,10 +1864,8 @@ export function piped(property: K, list: T[]): T[K][]; -export function pluck(property: number, list: { [k: number]: T }[]): T[]; -export function pluck

(property: P): (list: Record[]) => T[]; -export function pluck(property: number): (list: { [k: number]: T }[]) => T[]; +export function pluck(property: K): (list: T[]) => T[K][]; +export function pluck(property: K, list: T[]): T[K][]; /** * It adds element `x` at the beginning of `list`. @@ -1871,12 +1888,12 @@ export function prop(p: keyof never): (value: unknown) => V; /** * It returns true if `obj` has property `propToFind` and its value is equal to `valueToMatch`. */ -export function propEq(valueToMatch: any, propToFind: K, obj: Record): boolean; -export function propEq(valueToMatch: any, propToFind: K): (obj: Record) => boolean; -export function propEq(valueToMatch: any): { - (propToFind: K, obj: Record): boolean; - (propToFind: K): (obj: Record) => boolean; +export function propEq(val: T): { + (name: K): (obj: Record) => boolean; + (name: K, obj: Record): boolean; }; +export function propEq(val: T, name: K): (obj: Record) => boolean; +export function propEq(val: U[K], name: K, obj: U): boolean; /** * It returns `true` if `property` of `obj` is from `target` type. @@ -1946,10 +1963,19 @@ export function reduceBy( /** * It has the opposite effect of `R.filter`. */ -export function reject(predicate: Predicate, list: T[]): T[]; -export function reject(predicate: Predicate): (list: T[]) => T[]; -export function reject(predicate: Predicate, obj: Record): Record; -export function reject(predicate: Predicate): (obj: Record) => Record; +export function reject( + predicate: (value: T) => boolean, + list: T[], +): T[]; +export function reject( + predicate: BooleanConstructor, +): (list: readonly T[]) => ("" | null | undefined | false | 0)[]; +export function reject( + predicate: BooleanConstructor, +): (list: T[]) => ("" | null | undefined | false | 0)[]; +export function reject( + predicate: (value: T) => boolean, +): (list: T[]) => T[]; /** * It returns a copy of `list` input with removed `index`. diff --git a/source/values.js b/source/values.js index 6c72b907..235195e6 100644 --- a/source/values.js +++ b/source/values.js @@ -1,5 +1,4 @@ import { type } from './type.js' -import * as a from 'remeda' export function values(obj){ if (type(obj) !== 'Object') return [] diff --git a/src/mergeWith.js b/src/mergeWith.js index 718e08da..dd84477c 100644 --- a/src/mergeWith.js +++ b/src/mergeWith.js @@ -15,8 +15,11 @@ export function mergeWithFn( Object.keys(b).forEach(key => { if (willReturn[ key ] !== undefined) return - if (a[ key ] === undefined) willReturn[ key ] = b[ key ] - else willReturn[ key ] = mergeFn(a[ key ], b[ key ]) + if (a[ key ] === undefined){ + willReturn[ key ] = b[ key ] + } else { + willReturn[ key ] = mergeFn(a[ key ], b[ key ]) + } }) return willReturn diff --git a/src/values.js b/src/values.js index 6c72b907..235195e6 100644 --- a/src/values.js +++ b/src/values.js @@ -1,5 +1,4 @@ import { type } from './type.js' -import * as a from 'remeda' export function values(obj){ if (type(obj) !== 'Object') return []