From 7c67c22054d7d82700b5ff6986f590d37c7616b1 Mon Sep 17 00:00:00 2001
From: Nicolas ROCHE <nroche@entrouvert.com>
Date: Fri, 7 Aug 2020 17:00:06 +0200
Subject: [PATCH] templatetags: add removeprefix and removesuffix filters
 (#45780)

---
 combo/public/templatetags/combo.py | 20 ++++++++++++++++++++
 tests/test_public_templatetags.py  | 28 ++++++++++++++++++++++++++++
 2 files changed, 48 insertions(+)

diff --git a/combo/public/templatetags/combo.py b/combo/public/templatetags/combo.py
index 36fbf386..01a2e11f 100644
--- a/combo/public/templatetags/combo.py
+++ b/combo/public/templatetags/combo.py
@@ -293,16 +293,36 @@ def split(string, separator=' '):
 def strip(string, chars=None):
     if not string:
         return ''
     if chars:
         return force_text(string).strip(force_text(chars))
     else:
         return force_text(string).strip()
 
+@register.filter
+def removeprefix(string, prefix=None):
+    if not string:
+        return ''
+    value = force_text(string)
+    prefix = force_text(prefix)
+    if prefix and value.startswith(prefix):
+        return value[len(prefix):]
+    return value
+
+@register.filter
+def removesuffix(string, suffix=None):
+    if not string:
+        return ''
+    value = force_text(string)
+    suffix = force_text(suffix)
+    if suffix and value.endswith(suffix):
+        return value[:-len(suffix)]
+    return value
+
 @register.filter(name='get_group')
 def get_group(group_list, group_name):
     ret = []
     for group in group_list:
         if getattr(group, 'grouper', Ellipsis) == group_name:
             # Django >= 1.11, namedtuple
             ret.extend(group.list)
         elif not hasattr(group, 'grouper') and group['grouper'] == group_name:
diff --git a/tests/test_public_templatetags.py b/tests/test_public_templatetags.py
index 2489d3e6..4c59e7ff 100644
--- a/tests/test_public_templatetags.py
+++ b/tests/test_public_templatetags.py
@@ -136,16 +136,44 @@ def test_strip_templatetag():
     assert tmpl.render(Context({'foo': None})) == ''
     assert tmpl.render(Context({'foo': ' foo bar '})) == 'foo bar'
     assert tmpl.render(Context({'foo': ' foo bar\t'})) == 'foo bar'
     assert tmpl.render(Context({'foo': ' félé  '})) == 'félé'
     tmpl = Template('{{ foo|strip:"XY" }}')
     assert tmpl.render(Context({'foo': 'XXfoo barXYX'})) == 'foo bar'
     assert tmpl.render(Context({'foo': ' foo barXX'})) == ' foo bar'
 
+def test_removeprefix_templatetag():
+    tmpl = Template('{{ foo|removeprefix }}')
+    assert tmpl.render(Context()) == ''
+    assert tmpl.render(Context({'foo': None})) == ''
+    assert tmpl.render(Context({'foo': 'foo bar'})) == 'foo bar'
+    tmpl = Template('{{ foo|removeprefix:"" }}')
+    assert tmpl.render(Context({'foo': 'foo bar'})) == 'foo bar'
+    tmpl = Template('{{ foo|removeprefix:"XY" }}')
+    assert tmpl.render(Context({'foo': 'XYfoo barXY'})) == 'foo barXY'
+    assert tmpl.render(Context({'foo': 'foo bar'})) == 'foo bar'
+    assert tmpl.render(Context({'foo': 'xyfoo barXY'})) == 'xyfoo barXY'
+    assert tmpl.render(Context({'foo': ' XYfoo barXY'})) == ' XYfoo barXY'
+    assert tmpl.render(Context({'foo': 'XYXYfoo barXY'})) == 'XYfoo barXY'
+
+def test_removesuffix_templatetag():
+    tmpl = Template('{{ foo|removesuffix }}')
+    assert tmpl.render(Context()) == ''
+    assert tmpl.render(Context({'foo': None})) == ''
+    assert tmpl.render(Context({'foo': 'foo bar'})) == 'foo bar'
+    tmpl = Template('{{ foo|removesuffix:"" }}')
+    assert tmpl.render(Context({'foo': 'foo bar'})) == 'foo bar'
+    tmpl = Template('{{ foo|removesuffix:"XY" }}')
+    assert tmpl.render(Context({'foo': 'XYfoo barXY'})) == 'XYfoo bar'
+    assert tmpl.render(Context({'foo': 'foo bar'})) == 'foo bar'
+    assert tmpl.render(Context({'foo': 'XYfoo barxy'})) == 'XYfoo barxy'
+    assert tmpl.render(Context({'foo': 'XYfoo barXY '})) == 'XYfoo barXY '
+    assert tmpl.render(Context({'foo': 'XYfoo barXYXY'})) == 'XYfoo barXY'
+
 def test_get_group():
     context = Context({'cities': [
         {'name': 'Mumbai', 'population': '19,000,000', 'country': 'India'},
         {'name': 'New York', 'population': '20,000,000', 'country': 'USA'},
         {'name': 'Calcutta', 'population': '15,000,000', 'country': 'India'},
         {'name': 'Chicago', 'population': '7,000,000', 'country': 'USA'},
         {'name': 'Tokyo', 'population': '33,000,000', 'country': 'Japan'},
     ]})
-- 
2.27.0

