-
-
Notifications
You must be signed in to change notification settings - Fork 21
lint, update namespace/debug to be plugins, and their logic #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
'use strict'; | ||
|
||
var Base = require('./index'); | ||
|
||
function Templates() { | ||
Base.call(this); | ||
this.is('templates'); | ||
} | ||
Base.extend(Templates); | ||
|
||
function Assemble() { | ||
Templates.call(this); | ||
this.is('assemble'); | ||
} | ||
Templates.extend(Assemble); | ||
|
||
function Generate() { | ||
Assemble.call(this); | ||
this.is('generate'); | ||
} | ||
Assemble.extend(Generate); | ||
|
||
function Verb() { | ||
Generate.call(this); | ||
this.is('verb'); | ||
} | ||
Generate.extend(Verb); | ||
|
||
var verb = new Verb(); | ||
|
||
console.log(verb._namespace); | ||
//=> base:templates:assemble:generate:verb | ||
|
||
var one = verb.debug('one'); | ||
one('debugging with %s Mike Reagent', 'Charlike'); | ||
//=> base:templates:assemble:generate:verb:one debugging with %s Mike Reagent | ||
|
||
verb.debug.helper('loading foo helper'); | ||
//=> base:templates:assemble:generate:verb:helper loading foo helper | ||
|
||
verb.debug('foo:bar', 123, 'baz:qux', 'zaz')('hello world'); | ||
//=> base:templates:assemble:generate:verb:foo:bar:baz:qux:zaz hello world | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you change There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sometimes I see people nitpick about stuff that seems almost as arbitrary... lol |
||
|
||
var templates = new Templates(); | ||
templates.debug('one:two', 'three:four')('okey, that is awesome'); | ||
//=> base:templates:one:two:three:four okey, that is awesome |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,7 @@ | ||
'use strict'; | ||
|
||
require('mocha'); | ||
require('should'); | ||
var assert = require('assert'); | ||
var utils = require('./utils'); | ||
var Base = require('./'); | ||
var Base = require('./index'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we don't all have There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It will also work if it is installed locally when just I don't know how much it is a bad practice, but hate to waste time to install shitty-mitty-bitty-biggy modules every time i'm going to test or pr to some project. But yea, preferences, religious things.. anyway. :) edit: Another question is that that mocha is bad in general, exactly because this globals. I'm agree here - that is bad practice. That's why there is absolutely better and smaller |
||
var base; | ||
|
||
describe('constructor', function() { | ||
|
@@ -312,68 +309,85 @@ describe('prototype methods', function() { | |
assert.equal(typeof base.debug, 'function'); | ||
}); | ||
|
||
it('should append child namespaces', function() { | ||
function Foo(options) { | ||
Base.call(this, null, options); | ||
this.is('foo'); | ||
} | ||
Base.extend(Foo); | ||
|
||
function Bar(options) { | ||
Foo.call(this, options); | ||
this.is('bar'); | ||
} | ||
Foo.extend(Bar); | ||
it('should debug return a `debug` function', function() { | ||
assert.equal(typeof base.debug('one'), 'function'); | ||
}); | ||
|
||
var bar = new Bar(); | ||
assert.equal(bar.debug.namespace, 'base:foo:bar'); | ||
it('should debug without arguments be base namespace', function() { | ||
var app = base.debug()('foo bar baz'); | ||
assert.equal(app._namespace, 'base'); | ||
assert.equal(base._namespace, 'base'); | ||
assert.equal(app._debugNamespace, 'base'); | ||
assert.equal(base._debugNamespace, 'base'); | ||
}); | ||
|
||
it('should not double-append the same child namespace', function() { | ||
function Foo(options) { | ||
Base.call(this, null, options); | ||
function Foo() { | ||
Base.call(this); | ||
this.is('foo'); | ||
} | ||
Base.extend(Foo); | ||
|
||
function Bar(options) { | ||
Foo.call(this, options); | ||
function Bar() { | ||
Foo.call(this); | ||
this.is('bar'); | ||
} | ||
Foo.extend(Bar); | ||
|
||
function Baz(options) { | ||
Bar.call(this, options); | ||
function Baz() { | ||
Bar.call(this); | ||
this.is('bar'); | ||
} | ||
Bar.extend(Baz); | ||
|
||
var baz = new Baz(); | ||
assert.equal(baz.debug.namespace, 'base:foo:bar'); | ||
assert.equal(baz._debugNamespace, 'base:foo:bar'); | ||
}); | ||
|
||
it('should `debug` method return a new `debug` function', function() { | ||
var one = base.debug('one'); | ||
var app1 = one('that is okey'); | ||
assert.equal(typeof one, 'function'); | ||
assert.equal(app1._debugNamespace, 'base:one'); | ||
|
||
/** | ||
* Be careful with the order of calling the things | ||
* if you move this two lines above respectively | ||
* below `one` and below `app1`, then `app1._debugNamespace` | ||
* will fail, that's logical. | ||
* | ||
* More guaranteed is always to lookup `app._namespace`, it is | ||
* real app namespace, but not the `debug` namespace. | ||
* One is sure, debug namespace is bigger and always starts | ||
* with `app._namespace`. | ||
*/ | ||
var two = base.debug('one:two', 123, 'three', 'four:five'); | ||
var app2 = two('that is awesome'); | ||
assert.equal(typeof two, 'function'); | ||
assert.equal(app2._debugNamespace, 'base:one:two:three:four:five'); | ||
}); | ||
|
||
it('should append a custom debug namespace', function() { | ||
function Foo(options) { | ||
Base.call(this, null, options); | ||
it('should append child namespaces', function() { | ||
function Foo() { | ||
Base.call(this); | ||
this.is('foo'); | ||
} | ||
Base.extend(Foo); | ||
|
||
function Bar(options) { | ||
Foo.call(this, options); | ||
function Bar() { | ||
Foo.call(this); | ||
this.is('bar'); | ||
} | ||
Foo.extend(Bar); | ||
|
||
function Baz(options) { | ||
Bar.call(this, options); | ||
this.is('bar'); | ||
this.debug.append('a:b:c'); | ||
function Baz() { | ||
Bar.call(this); | ||
this.is('baz'); | ||
} | ||
Bar.extend(Baz); | ||
|
||
var baz = new Baz(); | ||
assert.equal(baz.debug.namespace, 'base:foo:bar:a:b:c'); | ||
assert.equal(baz._debugNamespace, 'base:foo:bar:baz'); | ||
}); | ||
}); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,14 +35,91 @@ utils.pascal = function(name) { | |
return name.charAt(0).toUpperCase() + name.slice(1); | ||
}; | ||
|
||
utils.namespace = function(app, parent) { | ||
var parentSegs = parent ? parent.split(':') : []; | ||
var segs = app._name.split(':'); | ||
/** | ||
* Plugins, should be moved out | ||
*/ | ||
|
||
utils.namespacePlugin = function namespacePlugin(parent) { | ||
return function plugin(app) { | ||
parent = typeof parent === 'string' && parent.length ? parent : false; | ||
|
||
var parentSegs = parent ? parent.split(':') : []; | ||
var nameSegs = app._name.split(':'); | ||
var namespace = utils.union([], parentSegs, nameSegs); | ||
|
||
app.define('_namespace', namespace.join(':')); | ||
}; | ||
}; | ||
|
||
/** | ||
* Plugin that adds `debug` method to the instance | ||
* and `namespaces` are added to this method. | ||
* | ||
* ```js | ||
* app.debug()('debugging on main namespace') | ||
* //=> base debugging on main namespace | ||
* | ||
* app.debug.one = app.debug('one') | ||
* app.debug.one('testing %s, whatever', 'foo, bar') | ||
* //=> base:one testing foo, bar, whatever | ||
* | ||
* app.debug.mix = app.debug('two', 123 'three:four', 'five') | ||
* app.debug.mix('okey, %s awesome', 'this is') | ||
* //=> base:two:three:four:five okey, this is awesome | ||
* | ||
* app.debug.helper('is one of internal namespaces added') | ||
* //=> base:helper is one of internal namespaces added | ||
* ``` | ||
* | ||
* @name .debug | ||
* @param {Array} `namespaces` | ||
* @return {Function} | ||
*/ | ||
|
||
utils.debugPlugin = function debugPlugin(namespaces) { | ||
namespaces = Array.isArray(namespaces) ? namespaces : [namespaces]; | ||
|
||
var namespace = utils.union([], parentSegs, segs); | ||
app._namespace = namespace.join(':'); | ||
return function plugin(app) { | ||
app.define('_debugNamespace', app._namespace); | ||
app.define('debug', function debug() { | ||
return debugFactory.apply(app, arguments); | ||
}); | ||
if (namespaces.length) { | ||
var len = namespaces.length; | ||
var i = 0; | ||
|
||
while (i < len) { | ||
var ns = namespaces[i++]; | ||
app.debug[ns] = debugFactory.call(app, ns); | ||
app.debug[ns].color = app.debug.color; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this line is working the way you think. Since If you're going the approach that a user needs to do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Haha, actually yea, i noticed it.
It's intentional, to be used as compose thingy for other namespaces (methods on |
||
} | ||
} | ||
}; | ||
}; | ||
|
||
function debugFactory() { | ||
var app = this; | ||
var args = [].slice.call(arguments); | ||
var segs = app._namespace.split(':'); | ||
var len = args.length; | ||
var i = 0; | ||
|
||
while (i < len) { | ||
var val = args[i++]; | ||
if (typeof val === 'string') { | ||
segs.push.apply(segs, val.split(':')); | ||
} | ||
} | ||
|
||
return function debug() { | ||
var fn = require('debug'); | ||
var namespace = segs.join(':'); | ||
app.define('_debugNamespace', namespace); | ||
fn(namespace).apply(fn, arguments); | ||
return app; | ||
}; | ||
} | ||
|
||
/** | ||
* Expose `utils` modules | ||
*/ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
awesome, great example
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yea, I also very like the idea that the
this.debug
is as compose part, and others are just copy of originaldebug
function.