From 91187eef25fc733121ed1f14efc49aeb519d655a Mon Sep 17 00:00:00 2001 From: Zhang Yi Jiang Date: Sun, 10 Feb 2019 13:28:28 +0800 Subject: [PATCH] Add R.replace that uses a replacer function --- types/ramda/index.d.ts | 6 +++--- types/ramda/ramda-tests.ts | 5 +++++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/types/ramda/index.d.ts b/types/ramda/index.d.ts index 459a351c33..1f2a65e93e 100644 --- a/types/ramda/index.d.ts +++ b/types/ramda/index.d.ts @@ -2257,9 +2257,9 @@ declare namespace R { /** * Replace a substring or regex match in a string with a replacement. */ - replace(pattern: RegExp | string, replacement: string, str: string): string; - replace(pattern: RegExp | string, replacement: string): (str: string) => string; - replace(pattern: RegExp | string): (replacement: string) => (str: string) => string; + replace(pattern: RegExp | string, replacement: string | ((match: string, ...args: any[]) => string), str: string): string; + replace(pattern: RegExp | string, replacement: string | ((match: string, ...args: any[]) => string)): (str: string) => string; + replace(pattern: RegExp | string): (replacement: string | ((match: string, ...args: any[]) => string)) => (str: string) => string; /** * Returns a new list with the same elements as the original list, just in the reverse order. diff --git a/types/ramda/ramda-tests.ts b/types/ramda/ramda-tests.ts index e4039483da..6a03cbc3f0 100644 --- a/types/ramda/ramda-tests.ts +++ b/types/ramda/ramda-tests.ts @@ -2569,6 +2569,11 @@ class Rectangle { R.replace(/foo/g, "bar", "foo foo foo"); // => 'bar bar bar' R.replace(/foo/g, "bar")("foo foo foo"); // => 'bar bar bar' R.replace(/foo/g)("bar")("foo foo foo"); // => 'bar bar bar' + + // Using a function as the replacement value + R.replace(/([cfk])oo/g, (match, p1, offset) => `${p1}-${offset}`, "coo foo koo"); // => 'c0oo f4oo k8oo' + R.replace(/([cfk])oo/g, (match, p1, offset) => `${p1}-${offset}`)("coo foo koo"); // => 'c0oo f4oo k8oo' + R.replace(/([cfk])oo/g)((match, p1, offset) => `${p1}-${offset}`) ("coo foo koo"); // => 'c0oo f4oo k8oo' }; /*****************************************************************