Skip to content

Commit

Permalink
chore@small
Browse files Browse the repository at this point in the history
  • Loading branch information
selfrefactor committed Feb 3, 2025
1 parent 48b4f48 commit 0b658d8
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 71 deletions.
2 changes: 1 addition & 1 deletion files/NEXT_VERSION_CHECKLIST.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ _ Regarding using object as input with TypeScript in methods such as `R.map/filt
-- map
-- mapObjIndexed
-- mergeAll
-- mergeWith
-- modify
-- propEq
-- modifyPath
Expand All @@ -104,6 +103,7 @@ _ Regarding using object as input with TypeScript in methods such as `R.map/filt

-- objOf
-- pluck
-- mergeWith

===
Rambdax
Expand Down
31 changes: 23 additions & 8 deletions files/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ export type DeepModify<Keys extends readonly PropertyKey[], U, T> =
: never
: never;

export type MergeInsertions<T> =
T extends object
? { [K in keyof T]: MergeInsertions<T[K]> }
: T

export type IndexedIterator<T, U> = (x: T, i: number) => U;
export type ObjectIterator<T, U> = (x: T, prop: string, inputObj: Record<PropertyKey, T>) => U;
type Ord = number | string | boolean | Date;
Expand Down Expand Up @@ -5246,12 +5251,23 @@ Explanation: It takes two objects and a function, which will be used when there
Example:
```
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`
```
Categories: Object
Expand All @@ -5260,9 +5276,8 @@ Notes:
*/
// @SINGLE_MARKER
export function mergeWith(fn: (x: any, z: any) => any): <U, V>(a: U, b: V) => any;
export function mergeWith<U>(fn: (x: any, z: any) => any, a: U): <V>(b: V) => any;
export function mergeWith<U, V>(fn: (x: any, z: any) => any, a: U, b: V): any;
export function mergeWith<T>(fn: (x: any, z: any) => any, a: object): (b: object) => T;
export function mergeWith<T>(fn: (x: any, z: any) => any, a: object, b: object): T;

/*
Method: juxt
Expand Down
16 changes: 8 additions & 8 deletions source/mapObject-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ describe('R.mapObject', () => {
(a, b, c) => {
a // $ExpectType number
b // $ExpectType string
c // $ExpectType Dictionary<number>
c // $ExpectType Record<PropertyKey, number>
return `${a}`
},
{a: 1, b: 2}
)
result // $ExpectType Dictionary<string>
result // $ExpectType Record<PropertyKey, string>
})
it('iterable with property argument', () => {
const result = mapObject(
Expand All @@ -22,7 +22,7 @@ describe('R.mapObject', () => {
},
{a: 1, b: 2}
)
result // $ExpectType Dictionary<number>
result // $ExpectType Record<PropertyKey, number>
})
it('iterable with no property argument', () => {
const result = mapObject(
Expand All @@ -32,24 +32,24 @@ describe('R.mapObject', () => {
},
{a: 1, b: 2}
)
result // $ExpectType Dictionary<string>
result // $ExpectType Record<PropertyKey, string>
})
it('curried requires explicit type', () => {
const result = mapObject<number>((a, b, c) => {
a // $ExpectType number
b // $ExpectType string
c // $ExpectType Dictionary<number>
c // $ExpectType Record<PropertyKey, number>
return a + 2
})({a: 1, b: 2})
result // $ExpectType Dictionary<number>
result // $ExpectType Record<PropertyKey, number>
})
it('curried requires explicit types', () => {
const result = mapObject<number, string>((a, b, c) => {
a // $ExpectType number
b // $ExpectType string
c // $ExpectType Dictionary<number>
c // $ExpectType Record<PropertyKey, number>
return `${a}`
})({a: 1, b: 2})
result // $ExpectType Dictionary<string>
result // $ExpectType Record<PropertyKey, string>
})
})
36 changes: 10 additions & 26 deletions source/mergeWith-spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
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],
Expand All @@ -14,29 +9,18 @@ const B = {
values: [15, 35],
}

type Output = MergeInsertions<typeof A & typeof B>

describe('R.mergeWith', () => {
test('no curry | without explicit types', () => {
const result = mergeWith(concat, A, B)
result // $ExpectType Record<string, unknown>
})
test('no curry | with explicit types', () => {
test('no curry', () => {
const result = mergeWith<Output>(concat, A, B)
result // $ExpectType Output
})
test('curry 1 | without explicit types', () => {
const result = mergeWith(concat, A)(B)
result // $ExpectType Record<string, unknown>
})
test('curry 1 | with explicit types', () => {
const result = mergeWith<Output>(concat, A)(B)
result // $ExpectType Output
})
test('curry 2 | without explicit types', () => {
const result = mergeWith(concat)(A, B)
result // $ExpectType Record<string, unknown>
})
test('curry 2 | with explicit types', () => {
const result = mergeWith<Output>(concat)(A, B)
result // $ExpectType Output
test('inside piped', () => {
const result = piped(
A,
mergeWith<Output>(concat, B),
)
result // $ExpectType Output
})
})
7 changes: 5 additions & 2 deletions source/mergeWith.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 3 additions & 26 deletions source/modify-spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { add, identity, map, modify, pipe, toUpper } from 'rambda';
import { add, identity, modify, toUpper } from 'rambda';

type Obj = {
foo: string;
Expand All @@ -13,34 +13,11 @@ describe('R.modify', () => {
const result2 = modify('bar', add(1), {} as Obj);
result2; // $ExpectType Obj

const result3 = modify('foo', toUpper)({} as Obj);
const result3 = modify('foo', identity, {} 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

modify('bar', toUpper, {} as Obj);
// @ts-expect-error
modify('foo', add(1), {} as Obj);
// @ts-expect-error
modify('bar', toUpper, {} as Obj);

const f = pipe(map<Obj, Obj>(modify('foo', toUpper)));

f([] as Obj[]); // $ExpectType Obj[]
});
});

0 comments on commit 0b658d8

Please sign in to comment.