Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add zip code formatter

* Also, freeze json2ify at 0.0.1 (newer breaks JSON.stringify)

Change-Id: I6ff7b93acb8961b5061f10e437ab02dce598411a
  • Loading branch information...
commit 385e5014b5c50d737a8c5b139e2b324e7a3b355f 1 parent 7c8833f
Graham authored
Showing with 15 additions and 0 deletions.
  1. +9 −0 lib/format.coffee
  2. +6 −0 test/lib/format_spec.coffee
View
9 lib/format.coffee
@@ -29,6 +29,14 @@ boolean = (value) ->
money = (number) ->
"$#{formatNumber(number, 2)}"
+zipCode = (value) ->
+ if value.toString().indexOf('-') > -1
+ # ex: 19147-1234
+ ("00000" + "#{value}".replace(/[^0-9-]/g, '')).slice(-10)
+ else
+ # ex: 19147
+ ("00000" + "#{value}".replace(/[^0-9-]/g, '')).slice(-5)
+
number = (number) ->
formatNumber(number, 0)
@@ -55,3 +63,4 @@ module.exports =
money: money
number: number
modelNames: modelNames
+ zipCode: zipCode
View
6 test/lib/format_spec.coffee
@@ -42,3 +42,9 @@ describe 'Format library', ->
expect(format.boolean(true)).to.equal 'Yes'
expect(format.boolean(false)).to.equal 'No'
expect(format.boolean(undefined)).to.equal ' - '
+
+ it 'should format zip codes', ->
+ expect(format.zipCode(12345)).to.equal '12345'
+ expect(format.zipCode('12345-7890')).to.equal '12345-7890'
+ expect(format.zipCode('00123')).to.equal '00123'
+ expect(format.zipCode(1040)).to.equal '01040'
Please sign in to comment.
Something went wrong with that request. Please try again.