Skip to content

Commit fcbba74

Browse files
committed
Implemented pad_left() and pad_right() functions
padx
1 parent 546a190 commit fcbba74

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

jmespath/functions.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,28 @@ 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': 'number'},
314+
{'type': 'string', 'optional': True})
315+
def _func_pad_left(self, text, width, padding = ' '):
316+
return self._pad_impl(lambda : text.rjust(width, padding), padding)
317+
318+
@signature(
319+
{'type': 'string'},
320+
{'type': 'number'},
321+
{'type': 'string', 'optional': True})
322+
def _func_pad_right(self, text, width, padding = ' '):
323+
return self._pad_impl(lambda : text.ljust(width, padding), padding)
324+
325+
def _pad_impl(self, func, padding):
326+
if len(padding) != 1:
327+
raise exceptions.JMESPathError(
328+
'syntax-error: pad_right() expects $padding to have a '
329+
'single character, but received `{}` instead.'
330+
.format(padding))
331+
return func()
332+
311333
@signature(
312334
{'type': 'string'},
313335
{'type': 'string'},

0 commit comments

Comments
 (0)