diff --git a/lib/Format.coffee b/lib/Format.coffee index fe16fad..81fa373 100644 --- a/lib/Format.coffee +++ b/lib/Format.coffee @@ -1,69 +1,74 @@ 'use strict' +slice = require 'sliced' chalk = require 'chalk' -REGEX = - escape: /%{2,2}/g - type: /(%?)(%([jds]))/g +ESCAPE_REGEX = /%{2,2}/g +TYPE_REGEX = /(%?)(%([jds]))/g -hasWhiteSpace = (s) -> - s.indexOf(' ') isnt -1 +isString = (obj) -> typeof obj is 'string' +isSymbol = (obj) -> typeof obj is 'symbol' +isObject = (obj) -> typeof obj is 'object' +isFalsy = (value) -> [null, undefined, false].indexOf(value) isnt -1 +isArray = (arr) -> Array.isArray(arr) +colorize = (value, color) -> chalk[color](value) +hasWhiteSpace = (s) -> s.indexOf(' ') isnt -1 serialize = (color, obj, key) -> # symbols cannot be directly casted to strings - if typeof key is 'symbol' - key = key.toString() - if typeof obj is 'symbol' - obj = obj.toString() - if obj is null - obj = 'null' - else if obj is undefined - obj = 'undefined' - else if obj is false - obj = 'false' - 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 + key = key.toString() if isSymbol key + obj = obj.toString() if isSymbol obj + obj = JSON.stringify obj if isFalsy obj + + if !isObject obj + obj = "'#{obj}'" if key and isString obj 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') - if obj instanceof Error - return obj.message or obj + obj = obj.toString 'base64' + return if key then "#{key}=#{obj}" else obj + + return obj.message or obj if obj instanceof Error + msg = '' keys = Object.keys(obj) length = keys.length i = 0 while i < length - if Array.isArray(obj[keys[i]]) - msg += keys[i] + '=[' + key = keys[i] + value = obj[key] + + if isArray(value) + msg += key + '=[' j = 0 - l = obj[keys[i]].length + l = value.length while j < l - msg += serialize(color, obj[keys[i]][j]) + msg += serialize(color, value[j]) if j < l - 1 msg += ' ' j++ msg += ']' - else if obj[keys[i]] instanceof Date - msg += keys[i] + '=' + obj[keys[i]] + else if value instanceof Date + msg += key + '=' + value else - msg += serialize(color, obj[keys[i]], chalk[color](keys[i])) + msg += serialize(color, value, colorize(key, color)) if i < length - 1 msg += ' ' i++ msg format = (fmt) -> - args = Array::slice.call(arguments, 1) + args = slice arguments, 1 color = args.pop() if args.length - fmt = fmt.replace(REGEX.type, (match, escaped, ptn, flag) -> + fmt = fmt.replace(TYPE_REGEX, (match, escaped, ptn, flag) -> arg = args.shift() switch flag when 's' - arg = '' + arg + arg = colorize(String(arg), color) when 'd' - arg = Number(arg) + arg = colorize(Number(arg), color) when 'j' arg = serialize color, arg return arg if !escaped @@ -72,7 +77,7 @@ format = (fmt) -> ) fmt += ' ' + serialize color, arg for arg in args if args.length - fmt = fmt.replace(REGEX.escape, '%') if fmt.replace? + fmt = fmt.replace(ESCAPE_REGEX, '%') if fmt.replace? serialize color, fmt module.exports = format diff --git a/package.json b/package.json index 543a94a..36b1a30 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,8 @@ "dependencies": { "chalk": "~1.1.1", "coffee-script": "~1.12.4", - "pretty-ms": "~2.1.0" + "pretty-ms": "~2.1.0", + "sliced": "~1.0.1" }, "devDependencies": { "acho-skin-cli": "latest",