From 310ff7f2b25eab341004ef20a03efde6faaf2995 Mon Sep 17 00:00:00 2001 From: zyd Date: Sat, 16 Jul 2022 10:19:35 +0900 Subject: [PATCH] add method replace --- README.md | 2 +- ramda/__init__.py | 1 + ramda/replace.py | 5 +++++ test/test_replace.py | 23 +++++++++++++++++++++++ 4 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 ramda/replace.py create mode 100644 test/test_replace.py diff --git a/README.md b/README.md index fb8285c..05706cc 100644 --- a/README.md +++ b/README.md @@ -582,7 +582,7 @@ R.propEq(1, 'v1', {'v1': 1}) # True - [x] 0.1.2 reject - [x] 0.2.2 remove - [x] 0.1.4 repeat -- [ ] replace +- [x] replace - [x] 0.1.2 reverse - [ ] scan - [ ] sequence diff --git a/ramda/__init__.py b/ramda/__init__.py index 3e06a9f..0ff45f2 100644 --- a/ramda/__init__.py +++ b/ramda/__init__.py @@ -98,6 +98,7 @@ from .reject import reject from .remove import remove from .repeat import repeat +from .replace import replace from .reverse import reverse from .slice import slice from .sort import sort diff --git a/ramda/replace.py b/ramda/replace.py new file mode 100644 index 0000000..aae6528 --- /dev/null +++ b/ramda/replace.py @@ -0,0 +1,5 @@ +import re + +from .private._curry3 import _curry3 + +replace = _curry3(re.sub) diff --git a/test/test_replace.py b/test/test_replace.py new file mode 100644 index 0000000..2d5f2cf --- /dev/null +++ b/test/test_replace.py @@ -0,0 +1,23 @@ + +import unittest + +import ramda as R + +""" +https://github.com/ramda/ramda/blob/master/test/replace.js +""" + + +class TestReplace(unittest.TestCase): + def test_replaces_substrings_of_the_input_string(self): + self.assertEqual('one two three', R.replace('1', 'one', '1 two three')) + + def test_replaces_regex_matches_of_the_input_string(self): + self.assertEqual('num num three', R.replace(r'\d+', 'num', '1 2 three')) + + def test_replaces_curry(self): + self.assertEqual('num num three', R.replace(r'\d+')('num')('1 2 three')) + + +if __name__ == '__main__': + unittest.main()