diff --git a/README.md b/README.md index 8e89072..d513bb9 100644 --- a/README.md +++ b/README.md @@ -618,7 +618,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 eef3dde..ded25ba 100644 --- a/ramda/__init__.py +++ b/ramda/__init__.py @@ -101,6 +101,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()