Ticket #5426: uncapfirst-with-tests.diff

File uncapfirst-with-tests.diff, 2.6 KB (added by Petr Marhoun <petr.marhoun@…>, 17 years ago)
  • django/template/defaultfilters.py

    === modified file 'django/template/defaultfilters.py'
     
    172172    return truncate_html_words(value, length)
    173173truncatewords_html = stringfilter(truncatewords_html)
    174174
     175def uncapfirst(value):
     176    "Uncapitalizes the first character of the value"
     177    if len(value) > 1:
     178        if value[1].islower():
     179            return value[0].lower() + value[1:]
     180        else:
     181            return value
     182    else:
     183        return value.lower()
     184uncapfirst = stringfilter(uncapfirst)
     185
    175186def upper(value):
    176187    "Converts a string into all uppercase"
    177188    return value.upper()
     
    656667register.filter(title)
    657668register.filter(truncatewords)
    658669register.filter(truncatewords_html)
     670register.filter(uncapfirst)
    659671register.filter(unordered_list)
    660672register.filter(upper)
    661673register.filter(urlencode)
  • django/utils/text.py

    === modified file 'django/utils/text.py'
     
    88capfirst = lambda x: x and force_unicode(x)[0].upper() + force_unicode(x)[1:]
    99capfirst = allow_lazy(capfirst, unicode)
    1010
     11# Uncapitalizes the first letter of a string.
     12def uncapfirst(x):
     13    x = force_unicode(x)
     14    if len(x) > 1:
     15        if x[1].islower():
     16            return x[0].lower() + x[1:]
     17        else:
     18            return x
     19    else:
     20        return x.lower()
     21uncapfirst = allow_lazy(uncapfirst, unicode)
     22
    1123def wrap(text, width):
    1224    """
    1325    A word-wrap function that preserves existing line breaks and most spaces in
  • tests/regressiontests/defaultfilters/tests.py

    === modified file 'tests/regressiontests/defaultfilters/tests.py'
     
    490490u'123'
    491491>>> striptags(123)
    492492u'123'
    493 
     493>>> uncapfirst('Contact')
     494u'contact'
     495>>> uncapfirst('Some field with many words')
     496u'some field with many words'
     497>>> uncapfirst('ORM framework')
     498u'ORM framework'
    494499"""
    495500
    496501from django.template.defaultfilters import *
  • tests/regressiontests/text/tests.py

    === modified file 'tests/regressiontests/text/tests.py'
     
    3535iri_to_uri() is idempotent:
    3636>>> iri_to_uri(iri_to_uri(u'red%09ros\xe9#red'))
    3737'red%09ros%C3%A9#red'
     38
     39### uncapfirst ###########################################################
     40>>> uncapfirst('Contact')
     41u'contact'
     42>>> uncapfirst('Some field with many words')
     43u'some field with many words'
     44>>> uncapfirst('ORM framework')
     45u'ORM framework'
    3846"""
Back to Top