Skip to content

Commit

Permalink
Prevent modify defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Sep 13, 2017
1 parent 8cd0a97 commit 3aa9e8e
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 31 deletions.
18 changes: 11 additions & 7 deletions lib/Acho.coffee
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
'use strict'

clone = require 'lodash.clonedeep'

DEFAULT = require './Default'
CONST = require './Constants'

Acho = (params = {}) ->
return new Acho params unless this instanceof Acho

acho = Object.assign({}, DEFAULT, params)
Acho = (opts = {}) ->
return new Acho opts unless this instanceof Acho

# TODO: Merge instead of clonee
acho = Object.assign({}, DEFAULT(), opts)
acho.diff = [] if acho.diff
acho[key] = value for key, value of acho

acho.messages = do ->
messages = {}
for type of acho.types
messages[type] = params.messages?[type] or []
messages[type] = opts.messages?[type] or []
acho[type] = acho.generateTypeMessage type
messages

Expand All @@ -30,10 +33,11 @@ Acho = (params = {}) ->

acho

# TODO: Remove it, unnecessary
Acho.skin = (skinFn) ->
skin = skinFn(CONST)
(params = {}) ->
Acho(Object.assign({}, params, skin))
(opts = {}) ->
Acho(Object.assign({}, opts, skin))

Acho.defaults = DEFAULT

Expand Down
8 changes: 3 additions & 5 deletions lib/Default.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ ms = require 'pretty-ms'
CONST = require './Constants'
format = require './Format'

module.exports =
module.exports = () ->
print: ->
for type of @types
@transport @generateMessage type, message for message in @messages[type]
Expand Down Expand Up @@ -115,9 +115,7 @@ module.exports =
colorize: (colors, message) ->
return message unless @color
colors = colors.split ' '
stylize = getColor
(stylize = stylize color) for color in colors

(stylize = getColor color) for color in colors
stylize message

isPrintable: (type) ->
Expand Down Expand Up @@ -160,7 +158,7 @@ module.exports =

error:
level : 1
color : '#ff6633'
color : '#FF3333'
symbol : CONST.FIGURE.error

fatal:
Expand Down
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,15 @@
"simple",
"tiny"
],
"files": [
"index.js",
"lib"
],
"dependencies": {
"chalk": "~2.1.0",
"coffee-script": "~1.12.4",
"fmt-obj": "~2.0.0",
"lodash.clonedeep": "~4.5.0",
"pretty-ms": "~3.0.0",
"sliced": "~1.0.1"
},
Expand All @@ -47,10 +52,6 @@
"engines": {
"node": ">= 4"
},
"files": [
"index.js",
"lib"
],
"scripts": {
"clean": "rm -rf node_modules",
"coveralls": "nyc report --reporter=text-lcov | coveralls",
Expand Down
40 changes: 25 additions & 15 deletions test/test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ Acho = require '..'
util = require './util'
should = require 'should'

createAcho = ->
Acho
color: true
outputType: (type) -> "[#{type}] » "
transport: util.createFakeTransport()


describe 'Acho ::', ->

beforeEach ->
opts =
color: true
outputType: (type) -> "[#{type}] » "
transport: util.createFakeTransport()
@acho = Acho opts
@acho = createAcho()

describe 'initialization', ->

Expand All @@ -31,7 +34,7 @@ describe 'Acho ::', ->

instance.print()
instance.transport.store.length.should.be.equal 1
expected = ' \u001b[34minfo\u001b[39m \u001b[90minfo message\u001b[39m'
expected = ' \u001b[38;2;51;204;255minfo\u001b[39m \u001b[90minfo message\u001b[39m'
instance.transport.store[0].should.be.equal expected

it '.push: add message into a internal level collection', ->
Expand All @@ -44,7 +47,7 @@ describe 'Acho ::', ->
@acho.transport.store.length.should.be.equal 1
@acho.messages.error.length.should.be.equal 1

expected = '\u001b[31m[error] » \u001b[39m \u001b[90mhello world\u001b[39m'
expected = '\u001b[38;2;255;51;51m[error] » \u001b[39m \u001b[90mhello world\u001b[39m'
@acho.transport.store[0].should.be.equal expected
@acho.messages.error[0].should.be.equal 'hello world'

Expand All @@ -54,17 +57,19 @@ describe 'Acho ::', ->
@acho.warn 'warn message'

@acho.transport.store.length.should.be.equal 1
expected = ' \u001b[33m[warn] » \u001b[39m \u001b[90mwarn message\u001b[39m'
expected = ' \u001b[38;2;255;204;51m[warn] » \u001b[39m \u001b[90mwarn message\u001b[39m'
@acho.transport.store[0].should.be.equal expected

it 'change the color behavior', ->
@acho.types.error.color = 'red bold'
@acho.push 'error', 'hello world'
@acho.print()
acho = createAcho()
acho.types.error.color = 'underline bgRed'
acho.push 'error', 'hello world'
acho.print()

@acho.transport.store.length.should.be.equal 1
expected = '\u001b[31m\u001b[1m[error] » \u001b[22m\u001b[39m \u001b[90mhello world\u001b[39m'
@acho.transport.store[0].should.be.equal expected
acho.transport.store.length.should.be.equal 1
expected = '\u001b[41m[error] » \u001b[49m \u001b[90mhello world\u001b[39m'
acho.transport.store[0].should.be.equal expected
Acho.defaults().types.error.color.should.be.equal('#FF3333')

it 'no ouptut messages out of the level', ->
level = @acho.level
Expand All @@ -89,7 +94,12 @@ describe 'Acho ::', ->
instance.keyword.should.be.equal 'symbol'

it 'enabling diff between logs', (done) ->
acho = Acho color: true, diff: true, timestamp: true, align: false
acho = Acho
color: true
diff: true
timestamp: true
align: false

printWarn = -> acho.warn 'hello world'
printErr = -> acho.error 'oh noes!'

Expand Down

0 comments on commit 3aa9e8e

Please sign in to comment.