Skip to content

Commit

Permalink
Improve serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Apr 20, 2016
1 parent 644cddd commit c583358
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions lib/Format.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,23 @@ REGEX =
escape: /%{2,2}/g
type: /(%?)(%([jds]))/g

hasWhiteSpace = (s) ->
s.indexOf(' ') isnt -1

serialize = (obj, key) ->
# symbols cannot be directly casted to strings
if typeof key == 'symbol'
if typeof key is 'symbol'
key = key.toString()
if typeof obj == 'symbol'
if typeof obj is 'symbol'
obj = obj.toString()
if obj == null
if obj is null
obj = 'null'
else if obj == undefined
else if obj is undefined
obj = 'undefined'
else if obj == false
else if obj is false
obj = 'false'
if typeof obj != 'object'
if typeof obj isnt 'object'
obj = '\'' + obj + '\'' if key and typeof obj is 'string' and hasWhiteSpace(obj)
return if key then key + '=' + obj else obj
if obj instanceof Buffer
return if key then key + '=' + obj.toString('base64') else obj.toString('base64')
Expand All @@ -32,15 +36,15 @@ serialize = (obj, key) ->
while j < l
msg += serialize(obj[keys[i]][j])
if j < l - 1
msg += ', '
msg += ' '
j++
msg += ']'
else if obj[keys[i]] instanceof Date
msg += keys[i] + '=' + obj[keys[i]]
else
msg += serialize(obj[keys[i]], keys[i])
if i < length - 1
msg += ', '
msg += ' '
i++
msg

Expand Down

0 comments on commit c583358

Please sign in to comment.