Skip to content

Commit

Permalink
add functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
lxhunter committed Aug 12, 2015
1 parent 4380e71 commit ebd8311
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 5 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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.
Expand Down
100 changes: 100 additions & 0 deletions docs/underscore_string_functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,104 @@ Converts HTML special characters to their entity equivalents. This function supp
# => '<div>Blah & \"blah\" & 'blah'</div>'
```

#### 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 '
```
34 changes: 34 additions & 0 deletions underscore_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:]
Expand All @@ -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):
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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')
Expand Down

0 comments on commit ebd8311

Please sign in to comment.