Skip to content

Commit 546a190

Browse files
committed
Implemented replace() function
1 parent bab2b5e commit 546a190

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

jmespath/functions.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,23 @@ def _func_keys(self, arg):
308308
# should we also return the indices of a list?
309309
return list(arg.keys())
310310

311+
@signature(
312+
{'type': 'string'},
313+
{'type': 'string'},
314+
{'type': 'string'},
315+
{'type': 'number', 'optional': True})
316+
def _func_replace(self, text, search, replacement, count = None):
317+
if count != None:
318+
if int(count) != count or int(count) < 0:
319+
raise exceptions.JMESPathError(
320+
'syntax-error: replace() expects $count to be a '
321+
'non-negative integer, but received {} instead.'
322+
.format(count))
323+
324+
if count != None:
325+
return text.replace(search, replacement, int(count))
326+
return text.replace(search, replacement)
327+
311328
@signature({"types": ['object']})
312329
def _func_values(self, arg):
313330
return list(arg.values())

0 commit comments

Comments
 (0)