From 1157bfbaa4436679c629db2fd00703ecf1544a97 Mon Sep 17 00:00:00 2001 From: Dejan Toteff Date: Mon, 3 Feb 2025 20:19:31 +0200 Subject: [PATCH] chore@small --- files/NEXT_VERSION_CHECKLIST.md | 8 +++++ files/index.d.ts | 22 ++++++------ source/modifyPath-spec.ts | 3 +- source/objOf-spec.ts | 16 +++++---- source/pipe-spec.ts | 8 ----- source/piped-spec.ts | 5 +-- source/pluck-spec.ts | 61 ++++++++++++++++++++------------- source/propEq-spec.ts | 8 +---- source/reduce-spec.ts | 17 +-------- 9 files changed, 70 insertions(+), 78 deletions(-) diff --git a/files/NEXT_VERSION_CHECKLIST.md b/files/NEXT_VERSION_CHECKLIST.md index c14408ada..32492e1fa 100644 --- a/files/NEXT_VERSION_CHECKLIST.md +++ b/files/NEXT_VERSION_CHECKLIST.md @@ -73,12 +73,14 @@ _ Regarding using object as input with TypeScript in methods such as `R.map/filt -- either -- filter -- forEach +-- pluck -- keys -- map -- mapObjIndexed -- mergeAll -- mergeWith -- modify +-- propEq -- modifyPath -- omit -- partition @@ -97,6 +99,12 @@ _ Regarding using object as input with TypeScript in methods such as `R.map/filt - Publish to JSR registry - https://jsr.io/@rambda/rambda - Replace Record with Record + +- Improve TypeScript definitions of: + +-- objOf +-- pluck + === Rambdax change diff --git a/files/index.d.ts b/files/index.d.ts index 699872d0d..e252c24bc 100644 --- a/files/index.d.ts +++ b/files/index.d.ts @@ -2856,8 +2856,8 @@ Notes: */ // @SINGLE_MARKER -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 }; /* Method: once @@ -3428,14 +3428,12 @@ const result = R.pluck(property, list) Categories: List, Object -Notes: +Notes: pipe */ // @SINGLE_MARKER -export function pluck(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][]; /* Method: prepend @@ -3534,12 +3532,12 @@ Notes: */ // @SINGLE_MARKER -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; /* Method: propIs diff --git a/source/modifyPath-spec.ts b/source/modifyPath-spec.ts index 60245005a..be3d256a4 100644 --- a/source/modifyPath-spec.ts +++ b/source/modifyPath-spec.ts @@ -4,8 +4,9 @@ const obj = {a: {b: {c: 1}}} describe('R.modifyPath', () => { it('happy', () => { - const result = modifyPath('a.b.c', (x: number) => x + 1, obj) + const result = modifyPath(['a', 'b', 'c'], (x: number) => String(x), obj) result // $ExpectType Record + result.a.b.c // $ExpectType string }) it('explicit return type', () => { interface Foo extends Record { diff --git a/source/objOf-spec.ts b/source/objOf-spec.ts index 01f74cbf6..37653e31a 100644 --- a/source/objOf-spec.ts +++ b/source/objOf-spec.ts @@ -1,4 +1,4 @@ -import {objOf} from 'rambda' +import {objOf, piped} from 'rambda' const key = 'foo' const value = 42 @@ -12,9 +12,13 @@ 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 + }) }) diff --git a/source/pipe-spec.ts b/source/pipe-spec.ts index 0d6283638..e4b5d1725 100644 --- a/source/pipe-spec.ts +++ b/source/pipe-spec.ts @@ -30,11 +30,3 @@ describe('R.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 - }); -}); diff --git a/source/piped-spec.ts b/source/piped-spec.ts index e4a9b8aee..0db734aa3 100644 --- a/source/piped-spec.ts +++ b/source/piped-spec.ts @@ -147,9 +147,6 @@ function tapFn(transformFn: (x: T) => U, fn: (a: T, b: U) => void): (x: T) }; } -/** -reject - */ describe('real use cases - books', () => { it('case 1', () => { const result = piped( @@ -163,7 +160,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 diff --git a/source/pluck-spec.ts b/source/pluck-spec.ts index 226d785f3..34e346fec 100644 --- a/source/pluck-spec.ts +++ b/source/pluck-spec.ts @@ -1,30 +1,43 @@ -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) + }) +}) diff --git a/source/propEq-spec.ts b/source/propEq-spec.ts index eeb714420..9308f2efc 100644 --- a/source/propEq-spec.ts +++ b/source/propEq-spec.ts @@ -24,22 +24,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) }) }) diff --git a/source/reduce-spec.ts b/source/reduce-spec.ts index 7ede14e0c..461bd9272 100644 --- a/source/reduce-spec.ts +++ b/source/reduce-spec.ts @@ -1,4 +1,4 @@ -import {reduce, reduceStopper} from 'rambda' +import {reduce} from 'rambda' describe('R.reduce', () => { it('happy', () => { @@ -59,21 +59,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) => {