From 615a8e2c60d95b91d7e70885a58a477abcfa9f93 Mon Sep 17 00:00:00 2001 From: Deyan Totev Date: Sun, 18 Aug 2024 09:07:41 +0300 Subject: [PATCH] feat@fix modify --- source/dissoc-spec.ts | 48 +++++++++++++++++------------------ source/modify-spec.ts | 59 ++++++++++++++++++++++++++++++++----------- 2 files changed, 68 insertions(+), 39 deletions(-) diff --git a/source/dissoc-spec.ts b/source/dissoc-spec.ts index f37117db..a9287b33 100644 --- a/source/dissoc-spec.ts +++ b/source/dissoc-spec.ts @@ -1,31 +1,31 @@ -import {dissoc} from 'rambda' +import { dissoc } from 'rambda'; type Obj = { - str: string; - num: number; - opt?: boolean; - orUndefined: boolean | undefined; - orNull: boolean | null; + str: string; + num: number; + opt?: boolean; + orUndefined: boolean | undefined; + orNull: boolean | null; }; const obj: Obj = { str: 'foo', num: 1, orUndefined: true, orNull: true }; describe('R.dissoc', () => { - it('ramda tests', () => { - // @ts-expect-error - dissoc('str', obj) - // @ts-expect-error - dissoc('num', obj) - // @ts-expect-error - dissoc('orNull', obj) - - let result1 = dissoc('opt', obj) - result1 // $ExpectType Obj - // @ts-expect-error - dissoc('num')(obj) - let result2 = dissoc('orUndefined', obj) - result2 // $ExpectType Obj - let result3 = dissoc('opt')(obj) - result3 // $ExpectType Obj - }) -}) + it('ramda tests', () => { + // @ts-expect-error + dissoc('str', obj); + // @ts-expect-error + dissoc('num', obj); + // @ts-expect-error + dissoc('orNull', obj); + + const result1 = dissoc('opt', obj); + result1; // $ExpectType Obj + // @ts-expect-error + dissoc('num')(obj); + const result2 = dissoc('orUndefined', obj); + result2; // $ExpectType Obj + const result3 = dissoc('opt')(obj); + result3; // $ExpectType Obj + }); +}); diff --git a/source/modify-spec.ts b/source/modify-spec.ts index 02e95efe..6cd478b2 100644 --- a/source/modify-spec.ts +++ b/source/modify-spec.ts @@ -1,17 +1,46 @@ -import {modify, add} from 'rambda' -const person = {name: 'James', age: 20} +import { add, identity, map, modify, pipe, toUpper } from 'rambda'; + +type Obj = { + foo: string; + bar: number; +}; describe('R.modify', () => { - it('happy', () => { - const {age} = modify('age', add(1), person) - const {age: ageAsString} = modify('age', String, person) - - age // $ExpectType number - ageAsString // $ExpectType string - }) - it('curried', () => { - const {age} = modify('age', add(1))(person) - - age // $ExpectType number - }) -}) + 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))); + + f([] as Obj[]); // $ExpectType Obj[] + }); +});