Made matchCase its own available function.

This commit is contained in:
Jeremy Fincher 2004-01-01 19:47:13 +00:00
parent 8366d740d5
commit bcbfd2130e
2 changed files with 16 additions and 6 deletions

View File

@ -306,7 +306,7 @@ def ellipsisify(s, n):
return (textwrap.wrap(s, n-3)[0] + '...')
plurals = TwoWayDictionary({})
def _matchCase(s1, s2):
def matchCase(s1, s2):
"""Matches the case of s1 in s2"""
if s1.isupper():
return s2.upper()
@ -314,7 +314,7 @@ def _matchCase(s1, s2):
L = list(s2)
for (i, char) in enumerate(s1[:len(s2)]):
if char.isupper():
L[i] = char.upper()
L[i] = L[i].upper()
return ''.join(L)
def pluralize(s, i=2):
@ -326,17 +326,17 @@ def pluralize(s, i=2):
else:
lowered = s.lower()
if lowered in plurals:
return _matchCase(s, plurals[lowered])
return matchCase(s, plurals[lowered])
elif any(lowered.endswith, ['ch', 'ss']):
return _matchCase(s, s+'es')
return matchCase(s, s+'es')
else:
return _matchCase(s, s+'s')
return matchCase(s, s+'s')
def depluralize(s):
"""Returns the singular of s."""
lowered = s.lower()
if lowered in plurals:
return _matchCase(s, plurals[lowered])
return matchCase(s, plurals[lowered])
elif any(lowered.endswith, ['ches', 'sses']):
return s[:-2]
else:

View File

@ -35,6 +35,16 @@ import sets
import utils
class UtilsTest(unittest.TestCase):
def testMatchCase(self):
f = utils.matchCase
self.assertEqual('bar', f('foo', 'bar'))
self.assertEqual('Bar', f('Foo', 'bar'))
self.assertEqual('BAr', f('FOo', 'bar'))
self.assertEqual('BAR', f('FOO', 'bar'))
self.assertEqual('bAR', f('fOO', 'bar'))
self.assertEqual('baR', f('foO', 'bar'))
self.assertEqual('BaR', f('FoO', 'bar'))
def testPluralize(self):
f = utils.pluralize
self.assertEqual('bike', f('bike', 1))