From c583358653f30ccb8bf8d989894e4627e29140ee Mon Sep 17 00:00:00 2001 From: Kiko Beats Date: Wed, 20 Apr 2016 23:46:44 +0200 Subject: [PATCH] Improve serialization --- lib/Format.coffee | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/lib/Format.coffee b/lib/Format.coffee index b675b36..ff86b2e 100644 --- a/lib/Format.coffee +++ b/lib/Format.coffee @@ -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') @@ -32,7 +36,7 @@ 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 @@ -40,7 +44,7 @@ serialize = (obj, key) -> else msg += serialize(obj[keys[i]], keys[i]) if i < length - 1 - msg += ', ' + msg += ' ' i++ msg