From ebd831185a232b188e451c1128308868906c36ff Mon Sep 17 00:00:00 2001 From: Alexander Jaeger Date: Wed, 12 Aug 2015 17:43:11 +0200 Subject: [PATCH] add functionality --- README.md | 10 +-- docs/underscore_string_functions.md | 100 ++++++++++++++++++++++++++++ underscore_string.py | 34 ++++++++++ 3 files changed, 139 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 1cc7da3..6ace3b0 100644 --- a/README.md +++ b/README.md @@ -124,14 +124,13 @@ Overview * [filesizeformat] (docs/jinja_functions.md#filesizeformat) - Format file sizes into ‘human-readable’ * [forceescape] (docs/jinja_functions.md#forceescape) - Enforce HTML escaping. * [format] (docs/jinja_functions.md#format) - Apply python string formatting on an object. -* [humanize] -* [include] +* [humanize] (docs/underscore_string_functions.md#humanize) - Converts an underscored, camelized, or dasherized string into a humanized one. * [indent] (docs/jinja_functions.md#indent) - Indent a string. -* [insert] +* [insert] (docs/underscore_string_functions.md#insert) - Insert word in string at the defined position. * [list] (docs/jinja_functions.md#list) - Convert the value into a list. * [lower] (docs/jinja_functions.md#lower) - Convert a value to lowercase. -* [lpad] -* [lrpad] +* [lpad] (docs/underscore_string_functions.md#lpad) - Return the string left justified in a string of length width. +* [lrpad] - See [center] (docs/jinja_functions.md#center) * [ltrim] * [pad] * [predecessor] @@ -185,6 +184,7 @@ Overview * [equalto] (docs/jinja_tests.md#equalto) - Check if an object has the same value as another object. * [escaped] (docs/jinja_tests.md#escaped) - Check if the value is escaped. * [even] (docs/jinja_tests.md#even) - Return true if the variable is even. +* [includes] (docs/underscore_string_functions.md#includes) - Tests if string contains a substring. * [isBlank] * [isnan] (docs/ansible_functions.md#isnan) - To see if something is actually a number * [iterable] (docs/jinja_tests.md#iterable) - Check if it’s possible to iterate over an object. diff --git a/docs/underscore_string_functions.md b/docs/underscore_string_functions.md index 89a947a..4a19d70 100644 --- a/docs/underscore_string_functions.md +++ b/docs/underscore_string_functions.md @@ -175,4 +175,104 @@ Converts HTML special characters to their entity equivalents. This function supp # => '
Blah & \"blah\" & 'blah'
' ``` +#### humanize +###### method: humanize(string) +> custom implementation / needs to be installed + +Converts an underscored, camelized, or dasherized string into a humanized one. Also removes beginning and ending whitespace, and removes the postfix '_id'. + +```python +{{ 'the_humanize_string_method' | humanize }} +# => 'The humanize string method' + +{{ 'ThehumanizeStringMethod' | humanize }} +# => 'Thehumanize string method' + +{{ 'the humanize_id string method_id' | humanize }} +# => 'Thehumanize string method' +{{ ' capitalize dash-CamelCase_underscore trim ' | humanize }} +# => 'Capitalize dash camel case underscore trim' +``` + +#### includes +###### method: includes(haystack, needle) +> custom implementation / needs to be installed + +Tests if string contains a substring. + +```python +{{ 'foobar' | includes('ob') }} +# => True + +{{ 'foobar' | includes('qux') }} +# => False + +{{ 'foobar' | includes('bar') }} +# => True + +{{ 'foobar' | includes('buzz') }} +# => False + +{{ 12345 | includes(34) }} +# => True +``` + +#### insert +###### method: insert(string, index, substring) +> custom implementation / needs to be installed + +Insert word in string at the defined position. + +```python +{{ 'foo ' | insert(4, 'bar') }} +# => 'foo bar' + +{{ 'Hello ' | insert(6, 'Jessy') }} +# => 'Hello Jessy' + +{{ 'Hello ' | insert(100, 'Jessy') }} +# => 'Hello Jessy' +``` + +#### lpad +###### method: lpad(width[, fillchar]) +> custom implementation / needs to be installed + +_Using Python Standard Library string rjust_ + +Return the string left justified in a string of length width. Padding is done using the specified fillchar (default is a space). The original string is returned if width is less than or equal to len(s). + +```python +{{ '1' | lpad(8) }} +# => ' 1' + +{{ 1 | lpad(8) }} +# => ' 1' + +{{ '1' | lpad(8, '0') }} +# => '00000001' + +``` + +#### ltrim +###### method: lpad(width[, fillchar]) +> custom implementation / needs to be installed + +_Using Python Standard Library string lstrip_ + +Return a copy of the string with leading characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace. + +```python +{{ ' foo' | ltrim }} +# => 'foo' + +{{ ' foo' | ltrim }} +# => 'foo' + +{{ 'foo ' | ltrim }} +# => 'foo ' + +{{ ' foo ' | ltrim }} +# => 'foo ' +``` diff --git a/underscore_string.py b/underscore_string.py index ec2081d..4d89cdf 100644 --- a/underscore_string.py +++ b/underscore_string.py @@ -124,6 +124,17 @@ def lines(string): string = string_sanity_check(string) return re.split('\r\n?|\n', string) +''' Return the string left justified in a string of length width. ''' +def lpad(string, width, fillchar=' '): + string = string_sanity_check(string) + return string.rjust(width, fillchar) + +def ltrim(string,chars=None): + string = string_sanity_check(string) + if isinstance( chars, int): + chars = str(chars) + return string.lstrip(chars) + ''' Replace string in string ''' def splice(string, index, how_many, substring): return string[:index] + substring + string[index + how_many:] @@ -144,6 +155,7 @@ def successor(string): ''' Returns a copy of the string in which all the case-based characters have had their case swapped.''' def swap_case(string): + string = string_sanity_check(string) return string.swapcase() def transliterate(string): @@ -193,6 +205,8 @@ def filters(self): 'includes': includes, 'insert': insert, 'lines': lines, + 'lpad': lpad, + 'ltrim': ltrim, 'splice': splice, 'starts_with': starts_with, 'successor': successor, @@ -361,6 +375,26 @@ def test_lines(self): self.assertEqual(len(lines('Hello\r\rWorld')), 3); self.assertEqual(len(lines(123)), 1); + def test_lpad(self): + self.assertEqual(lpad('1', 8), ' 1'); + self.assertEqual(lpad(1, 8), ' 1'); + self.assertEqual(lpad('1', 8, '0'), '00000001'); + self.assertEqual(lpad('', 2), ' '); + self.assertEqual(lpad(None, 2), ' '); + + def test_ltrim(self): + self.assertEqual(ltrim(' foo'), 'foo'); + self.assertEqual(ltrim(' foo'), 'foo'); + self.assertEqual(ltrim('foo '), 'foo '); + self.assertEqual(ltrim(' foo '), 'foo '); + self.assertEqual(ltrim(''), '', 'ltrim empty string should return empty string'); + self.assertEqual(ltrim(None), '', 'ltrim null should return empty string'); + self.assertEqual(ltrim('ffoo', 'f'), 'oo'); + self.assertEqual(ltrim('ooff', 'f'), 'ooff'); + self.assertEqual(ltrim('ffooff', 'f'), 'ooff'); + self.assertEqual(ltrim('_-foobar-_', '_-'), 'foobar-_'); + self.assertEqual(ltrim(123, 1), '23'); + def test_splice(self): self.assertEqual(splice('http://github.com/lxhunter/string', 18, 8, 'awesome'), 'http://github.com/awesome/string')