Skip to content

Commit

Permalink
Refactor formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Apr 14, 2017
1 parent b325807 commit 99585a6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 34 deletions.
71 changes: 38 additions & 33 deletions lib/Format.coffee
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 99585a6

Please sign in to comment.