From 0238bbea2cf64071e9ef7c616085450febc56246 Mon Sep 17 00:00:00 2001 From: fatsu Date: Mon, 27 Feb 2017 21:48:54 +0100 Subject: [PATCH 1/2] - path should be (string | number)[] - some currying problems with path related functions --- ramda/index.d.ts | 32 +++++++++++++------------- ramda/ramda-tests.ts | 53 +++++++++++++++++++++++++++++++++----------- 2 files changed, 56 insertions(+), 29 deletions(-) diff --git a/ramda/index.d.ts b/ramda/index.d.ts index 114ba38d6d..44d4c33304 100644 --- a/ramda/index.d.ts +++ b/ramda/index.d.ts @@ -8,6 +8,8 @@ declare var R: R.Static; declare namespace R { type Ord = number | string | boolean; + type Path = number | string; + interface ListIterator { (value: T, index: number, list: T[]): TResult; } @@ -221,9 +223,9 @@ declare namespace R { * Makes a shallow clone of an object, setting or overriding the nodes required to create the given path, and * placing the specific value at the tail end of that path. */ - assocPath(path: string[], val: T, obj: U): U; - assocPath(path: string[]): (val: T, obj: U) => U; - assocPath(path: string[], val: T): (obj: U) => U; + assocPath(path: Path[], val: T, obj: U): U; + assocPath(path: Path[], val: T): (obj: U) => U; + assocPath(path: Path[]): CurriedFunction2; /** * Wraps a function of any arity (including nullary) in a function that accepts exactly 2 @@ -439,8 +441,8 @@ declare namespace R { /** * Makes a shallow clone of an object, omitting the property at the given path. */ - dissocPath(path: string[], obj: any): T; - dissocPath(path: string[]): (obj: any) => T; + dissocPath(path: Path[], obj: any): T; + dissocPath(path: Path[]): (obj: any) => T; /** * Divides two numbers. Equivalent to a / b. @@ -838,7 +840,7 @@ declare namespace R { * Returns a lens whose focus is the specified path. * See also view, set, over. */ - lensPath(path: string[]): Lens; + lensPath(path: Path[]): Lens; /** * lensProp creates a lens that will focus on property k of the source object. @@ -1129,26 +1131,24 @@ declare namespace R { /** * Retrieve the value at a given path. */ - path(path: string[], obj: any): T; - path(path: string[]): (obj: any) => T; + path(path: Path[], obj: any): T; + path(path: Path[]): (obj: any) => T; /** * Determines whether a nested path on an object has a specific value, * in `R.equals` terms. Most likely used to filter a list. */ - pathEq(path: string[], val: any, obj: any): boolean; - pathEq(path: string[], val: any): (obj: any) => boolean; - pathEq(path: string[]): (val: any, obj: any) => boolean; - pathEq(path: string[]): (val: any) => (obj: any) => boolean; + pathEq(path: Path[], val: any, obj: any): boolean; + pathEq(path: Path[], val: any): (obj: any) => boolean; + pathEq(path: Path[]): CurriedFunction2; /** * If the given, non-null object has a value at the given path, returns the value at that path. * Otherwise returns the provided default value. */ - pathOr(d: T, p: string[], obj: any): T|any; - pathOr(d: T, p: string[]): (obj: any) => T|any; - pathOr(d: T): (p: string[], obj: any) => T|any; - + pathOr(d: T, p: Path[], obj: any): T|any; + pathOr(d: T, p: Path[]): (obj: any) => T|any; + pathOr(d: T): CurriedFunction2; /** * Returns a partial copy of an object containing only the keys specified. If the key does not exist, the diff --git a/ramda/ramda-tests.ts b/ramda/ramda-tests.ts index e42ae16c63..13aeb222f7 100644 --- a/ramda/ramda-tests.ts +++ b/ramda/ramda-tests.ts @@ -491,6 +491,14 @@ R.times(i, 5); R.findLastIndex((x: number) => x === 1, [1, 2, 3]); } () => { + const testPath = ['x', 0, 'y']; + const testObj = {x: [{y: 2, z: 3}, {y: 4, z: 5}]}; + + R.pathEq(testPath, 2, testObj); // => true + R.pathEq(testPath, 2)(testObj); // => true + R.pathEq(testPath)(2)(testObj); // => true + R.pathEq(testPath)(2, testObj); // => true + var user1 = { address: { zipCode: 90210 } }; var user2 = { address: { zipCode: 55555 } }; var user3 = { name: 'Bob' }; @@ -1028,9 +1036,13 @@ type Pair = KeyValuePair } () => { - const a = R.assocPath(['a', 'b', 'c'], 42, {a: {b: {c: 0}}}); //=> {a: {b: {c: 42}}} - const b = R.assocPath(['a', 'b', 'c'])(42, {a: {b: {c: 0}}}); //=> {a: {b: {c: 42}}} - const c = R.assocPath(['a', 'b', 'c'], 42)({a: {b: {c: 0}}}); //=> {a: {b: {c: 42}}} + const testPath = ['x', 0, 'y']; + const testObj = {x: [{y: 2, z: 3}, {y: 4, z: 5}]}; + + R.assocPath(testPath, 42, testObj); //=> {x: [{y: 42, z: 3}, {y: 4, z: 5}]} + R.assocPath(testPath, 42)(testObj); //=> {x: [{y: 42, z: 3}, {y: 4, z: 5}]} + R.assocPath(testPath)(42)(testObj); //=> {x: [{y: 42, z: 3}, {y: 4, z: 5}]} + R.assocPath(testPath)(42, testObj); //=> {x: [{y: 42, z: 3}, {y: 4, z: 5}]} } () => { @@ -1038,6 +1050,12 @@ type Pair = KeyValuePair // optionally specify return type const a2 = R.dissocPath<{a :{ b: number}}>(['a', 'b', 'c'], {a: {b: {c: 42}}}); //=> {a: {b: {}}} const a3 = R.dissocPath(['a', 'b', 'c'])({a: {b: {c: 42}}}); //=> {a: {b: {}}} + + const testPath = ['x', 0, 'y']; + const testObj = {x: [{y: 2, z: 3}, {y: 4, z: 5}]}; + + R.dissocPath(testPath, testObj); //=> {x: [{z: 3}, {y: 4, z: 5}]} + R.dissocPath(testPath)(testObj); //=> {x: [{z: 3}, {y: 4, z: 5}]} } () => { @@ -1163,11 +1181,12 @@ class Rectangle { } () => { - const xyLens = R.lensPath(['x', 'y']); + const xyLens = R.lensPath(['x', 0, 'y']); + const testObj = {x: [{y: 2, z: 3}, {y: 4, z: 5}]}; - R.view(xyLens, {x: {y: 2, z: 3}}); //=> 2 - R.set(xyLens, 4, {x: {y: 2, z: 3}}); //=> {x: {y: 4, z: 3}} - R.over(xyLens, R.negate, {x: {y: 2, z: 3}}); //=> {x: {y: -2, z: 3}} + R.view(xyLens, testObj); //=> 2 + R.set(xyLens, 4, testObj); //=> {x: [{y: 4, z: 3}, {y: 4, z: 5}]} + R.over(xyLens, R.negate, testObj); //=> {x: [{y: -2, z: 3}, {y: 4, z: 5}]} } () => { @@ -1242,10 +1261,15 @@ class Rectangle { } () => { - const a1 = R.pathOr('N/A', ['a', 'b'], {a: {b: 2}}); //=> 2 - const a2 = R.pathOr('N/A', ['a', 'b'])({a: {b: 2}}); //=> 2 - const a3 = R.pathOr('N/A', ['a', 'b'], {c: {b: 2}}); //=> "N/A" - const a4 = R.pathOr({c:2})(['a', 'b'], {c: {b: 2}}); //=> "N/A" + const orValue = 'N/A'; + const testPath = ['x', 0, 'y']; + const testObj = {x: [{y: 2, z: 3}, {y: 4, z: 5}]}; + + R.pathOr(orValue, testPath, testObj); //=> 2 + R.pathOr(orValue, testPath)(testObj); //=> 2 + R.pathOr(orValue)(testPath)(testObj); //=> 2 + R.pathOr(orValue)(testPath, testObj); //=> 2 + R.pathOr(orValue, testPath, {c: {b: 2}}); //=> "N/A" } () => { @@ -1586,8 +1610,11 @@ matchPhrases(['foo', 'bar', 'baz']); } () => { - R.path(['a', 'b'], {a: {b: 2}}); //=> 2 - R.path(['a', 'b'])({a: {b: 2}}); //=> 2 + const testPath = ['x', 0, 'y']; + const testObj = {x: [{y: 2, z: 3}, {y: 4, z: 5}]}; + + R.path(testPath, testObj); //=> 2 + R.path(testPath)(testObj); //=> 2 } () => { From 09898b0b428078dd8b0a4335e2e4df04ab1068ab Mon Sep 17 00:00:00 2001 From: Tom Goemaes Date: Tue, 28 Feb 2017 09:49:00 +0100 Subject: [PATCH 2/2] Update index.d.ts --- ramda/index.d.ts | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/ramda/index.d.ts b/ramda/index.d.ts index 44d4c33304..80fad023cc 100644 --- a/ramda/index.d.ts +++ b/ramda/index.d.ts @@ -8,7 +8,7 @@ declare var R: R.Static; declare namespace R { type Ord = number | string | boolean; - type Path = number | string; + type Path = (number | string)[]; interface ListIterator { (value: T, index: number, list: T[]): TResult; @@ -223,9 +223,9 @@ declare namespace R { * Makes a shallow clone of an object, setting or overriding the nodes required to create the given path, and * placing the specific value at the tail end of that path. */ - assocPath(path: Path[], val: T, obj: U): U; - assocPath(path: Path[], val: T): (obj: U) => U; - assocPath(path: Path[]): CurriedFunction2; + assocPath(path: Path, val: T, obj: U): U; + assocPath(path: Path, val: T): (obj: U) => U; + assocPath(path: Path): CurriedFunction2; /** * Wraps a function of any arity (including nullary) in a function that accepts exactly 2 @@ -441,8 +441,8 @@ declare namespace R { /** * Makes a shallow clone of an object, omitting the property at the given path. */ - dissocPath(path: Path[], obj: any): T; - dissocPath(path: Path[]): (obj: any) => T; + dissocPath(path: Path, obj: any): T; + dissocPath(path: Path): (obj: any) => T; /** * Divides two numbers. Equivalent to a / b. @@ -840,7 +840,7 @@ declare namespace R { * Returns a lens whose focus is the specified path. * See also view, set, over. */ - lensPath(path: Path[]): Lens; + lensPath(path: Path): Lens; /** * lensProp creates a lens that will focus on property k of the source object. @@ -1131,24 +1131,24 @@ declare namespace R { /** * Retrieve the value at a given path. */ - path(path: Path[], obj: any): T; - path(path: Path[]): (obj: any) => T; + path(path: Path, obj: any): T; + path(path: Path): (obj: any) => T; /** * Determines whether a nested path on an object has a specific value, * in `R.equals` terms. Most likely used to filter a list. */ - pathEq(path: Path[], val: any, obj: any): boolean; - pathEq(path: Path[], val: any): (obj: any) => boolean; - pathEq(path: Path[]): CurriedFunction2; + pathEq(path: Path, val: any, obj: any): boolean; + pathEq(path: Path, val: any): (obj: any) => boolean; + pathEq(path: Path): CurriedFunction2; /** * If the given, non-null object has a value at the given path, returns the value at that path. * Otherwise returns the provided default value. */ - pathOr(d: T, p: Path[], obj: any): T|any; - pathOr(d: T, p: Path[]): (obj: any) => T|any; - pathOr(d: T): CurriedFunction2; + pathOr(d: T, p: Path, obj: any): T|any; + pathOr(d: T, p: Path): (obj: any) => T|any; + pathOr(d: T): CurriedFunction2; /** * Returns a partial copy of an object containing only the keys specified. If the key does not exist, the