Skip to content

Commit

Permalink
refactor and updated documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Mar 14, 2015
1 parent 222e72d commit 6824d6a
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 59 deletions.
68 changes: 68 additions & 0 deletions .jscsrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
{
"disallowSpacesInNamedFunctionExpression": {
"beforeOpeningRoundBrace": true
},
"disallowSpacesInFunctionExpression": {
"beforeOpeningRoundBrace": true
},
"disallowSpacesInAnonymousFunctionExpression": {
"beforeOpeningRoundBrace": true
},
"disallowSpacesInFunctionDeclaration": {
"beforeOpeningRoundBrace": true
},
"disallowEmptyBlocks": true,
"disallowSpacesInsideArrayBrackets": true,
"disallowSpacesInsideParentheses": true,
"disallowQuotedKeysInObjects": true,
"disallowSpaceAfterObjectKeys": true,
"disallowSpaceAfterPrefixUnaryOperators": true,
"disallowSpaceBeforePostfixUnaryOperators": true,
"disallowSpaceBeforeBinaryOperators": [
","
],
"disallowMixedSpacesAndTabs": true,
"disallowTrailingWhitespace": true,
"disallowTrailingComma": true,
"disallowYodaConditions": true,
"disallowKeywords": [ "with" ],
"disallowMultipleLineBreaks": true,
"disallowMultipleVarDecl": true,
"requireSpaceBeforeBlockStatements": true,
"requireParenthesesAroundIIFE": true,
"requireSpacesInConditionalExpression": true,
"requireBlocksOnNewline": 1,
"requireCommaBeforeLineBreak": true,
"requireSpaceBeforeBinaryOperators": true,
"requireSpaceAfterBinaryOperators": true,
"requireCamelCaseOrUpperCaseIdentifiers": true,
"requireLineFeedAtFileEnd": true,
"requireCapitalizedConstructors": true,
"requireDotNotation": true,
"requireSpacesInForStatement": true,
"requireSpaceBetweenArguments": true,
"requireCurlyBraces": [
"do"
],
"requireSpaceAfterKeywords": [
"if",
"else",
"for",
"while",
"do",
"switch",
"case",
"return",
"try",
"catch",
"typeof"
],
"requirePaddingNewLinesBeforeLineComments": {
"allExcept": "firstAfterCurly"
},
"requirePaddingNewLinesAfterBlocks": true,
"safeContextKeyword": "_this",
"validateLineBreaks": "LF",
"validateQuoteMarks": "'",
"validateIndentation": 2
}
4 changes: 2 additions & 2 deletions .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"esnext": true,
"bitwise": true,
"camelcase": true,
"curly": true,
"eqeqeq": true,
"curly": false,
"eqeqeq": false,
"immed": true,
"indent": 2,
"latedef": true,
Expand Down
31 changes: 28 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,24 @@ console.log(acho.messages.info)
// => ['this message is printed and stored']
```

because `messages` is public you can define your own way to print the messages:
You also can redefine the print method, not exist limits!

```json
acho.print = function() {
// You are in the acho scope, so you can use the properties
// of the object
console.log();
var _this = this;
Object.keys(this.types).forEach(function(type) {
// if (isSuccessOrInfoMessage(type)) console.log();
_this.messages[type].forEach(function(message) {
_this.printLine(type, message);
});
});
};
```

Also you can do anything you need: change the color of a type, add or modify the types, changes the priorities,... **you have the power**.
Do whatever you need for adapt the library of yours requisites: changes colors, add more types, sort the priorities... the internal structure of the object is public and you can edit dynamically. **You have the power**.


### Stablish the level
Expand Down Expand Up @@ -166,6 +178,7 @@ Create a new logger. The options that you can provide are:
- types **{Object}**: You can provide the types and priorities.
- outputType **{Function}**: For customize the type in the output.
- outputMessage **{Function}**: For customize the message in the output.
- print **{Function}**: Provide a function for print the messages.

### .push({String} <type>, {String} <message>)

Expand Down Expand Up @@ -203,9 +216,21 @@ Output a `debug` message.

Output a `silly` message.

### .isPrintable

Determines if a type of message should be outputted.

### .colorize

Determines is a instance of `acho` is outputted with colors.

### .print

Output all messages stores internally.
Default loop for print the messages that are stored internally that uses `.printLine` in each message iteration.

### .printLine

Combine `.isPrintable` and `.colorize` for print a line correctly.

## License

Expand Down
76 changes: 25 additions & 51 deletions lib/Acho.coffee
Original file line number Diff line number Diff line change
@@ -1,47 +1,25 @@
'use strict'

chalk = require 'chalk'

DEFAULT =
OUTPUT_TYPE: (type) -> "#{type}\t: "
OUTPUT_MESSAGE: (message) -> message
LEVEL: 'info'
COLOR: false
TYPES:
error:
level : 0
color : 'red'
warning:
level : 1
color : 'yellow'
success:
level : 2
color : 'green'
info:
level : 3
color : 'white'
verbose:
level : 4
color : 'cyan'
debug:
level : 5
color : 'blue'
silly:
level : 6
color : 'rainbow'
DEFAULT = require './default'

module.exports = class Acho

constructor: (options) ->
constructor: (options = {}) ->
@color = if options.color? then options.color else DEFAULT.COLOR
@level = if options.level? then options.level else DEFAULT.LEVEL
@types = if options.types? then options.types else DEFAULT.TYPES
@messages = {}
@messages[type] = [] for type of @types
@print = if options.print? then options.print else DEFAULT.PRINT
if options.messages?
@messages = options.messages
else
@messages = {}
@messages[type] = [] for type of @types
@outputType = if options.outputType? then options.outputType else DEFAULT.OUTPUT_TYPE
@outputMessage = if options.outputMessage? then options.outputMessage else DEFAULT.OUTPUT_MESSAGE
this

@DEFAULT: DEFAULT

push: (type, message) ->
@messages[type].push message
this
Expand All @@ -52,42 +30,38 @@ module.exports = class Acho
this

error: (message) ->
@_messageBuilder 'error', message
@printLine 'error', message
this

warning: (message) ->
@_messageBuilder 'warning', message
@printLine 'warning', message
this

success: (message) ->
@_messageBuilder 'success', message
@printLine 'success', message
this

info: (message) ->
@_messageBuilder 'info', message
@printLine 'info', message
this

verbose: (message) ->
@_messageBuilder 'verbose', message
@printLine 'verbose', message
this

debug: (message) ->
@_messageBuilder 'debug', message
@printLine 'debug', message
this

silly: (message) ->
@_messageBuilder 'silly', message
@printLine 'silly', message
this

print: ->
for type of @types
@_messageBuilder(type, message) for message in @messages[type]

_isInLevel: (type) ->
isPrintable: (type) ->
return false if @level is 'silent'
@types[type].level <= @types[@level].level

_colorizeMessage: (color, message) ->
colorize: (color, message) ->
return message unless @color
return chalk[color](message) unless color is 'rainbow'
# rainbow is the core of this library
Expand All @@ -98,11 +72,11 @@ module.exports = class Acho
message[position] = chalk[color](char)
message.join('')

_messageBuilder: (type, message) ->
return unless @_isInLevel type
printLine: (type, message) ->
return unless @isPrintable type
colorType = @types[type].color
messageType = @outputType(type)
messageType = @_colorizeMessage(colorType, messageType)
message = @outputMessage(message)
message = @_colorizeMessage('gray', message)
messageType = @outputType type
messageType = @colorize colorType, messageType
message = @outputMessage message
message = @colorize 'gray', message
console.log messageType + message
32 changes: 32 additions & 0 deletions lib/default.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict'

module.exports =
OUTPUT_TYPE: (type) -> "#{type}\t: "
OUTPUT_MESSAGE: (message) -> message
PRINT: ->
for type of @types
@printLine(type, message) for message in @messages[type]
LEVEL: 'info'
COLOR: false
TYPES:
error:
level : 0
color : 'red'
warning:
level : 1
color : 'yellow'
success:
level : 2
color : 'green'
info:
level : 3
color : 'white'
verbose:
level : 4
color : 'cyan'
debug:
level : 5
color : 'blue'
silly:
level : 6
color : 'rainbow'
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
"url": "Kikobeats/acho/issues"
},
"dependencies": {
"chalk": "*"
"chalk": "*",
"coffee-script": "*"
},
"devDependencies": {
"browserify": "*",
"coffee-script": "*",
"coffeeify": "*",
"gulp": "*",
"gulp-header": "*",
Expand All @@ -29,7 +29,10 @@
"node": "*"
},
"homepage": "Kikobeats/acho",
"keywords": ["log", "logging"],
"keywords": [
"log",
"logging"
],
"license": "MIT",
"main": "./index.js",
"repository": "Kikobeats/acho",
Expand Down

0 comments on commit 6824d6a

Please sign in to comment.