-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
102 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,52 @@ | ||
(function initLazyAss() { | ||
|
||
var lazyAss = function (condition) { | ||
var args = [].slice.call(arguments, 1); | ||
function formMessage(args) { | ||
var msg = args.reduce(function (total, arg, k) { | ||
if (k) { | ||
total += ' '; | ||
} | ||
if (typeof arg === 'string') { | ||
return total + arg; | ||
} | ||
if (typeof arg === 'function') { | ||
return total + arg(); | ||
} | ||
if (Array.isArray(arg)) { | ||
return total + JSON.stringify(arg); | ||
} | ||
return total + JSON.stringify(arg, null, 2); | ||
}, ''); | ||
return msg; | ||
} | ||
|
||
var lazyAss = function lazyAss(condition) { | ||
if (!condition) { | ||
var args = [].slice.call(arguments, 1); | ||
throw new Error(formMessage(args)); | ||
} | ||
}; | ||
|
||
var lazyAssync = function lazyAssync(condition) { | ||
if (!condition) { | ||
var msg = args.reduce(function (total, arg, k) { | ||
if (k) { | ||
total += ' '; | ||
} | ||
if (typeof arg === 'string') { | ||
return total + arg; | ||
} | ||
if (typeof arg === 'function') { | ||
return total + arg(); | ||
} | ||
if (Array.isArray(arg)) { | ||
return total + JSON.stringify(arg); | ||
} | ||
return total + JSON.stringify(arg, null, 2); | ||
}, ''); | ||
throw new Error(msg); | ||
var args = [].slice.call(arguments, 1); | ||
setTimeout(function () { | ||
throw new Error(formMessage(args)); | ||
}, 0); | ||
} | ||
}; | ||
|
||
function register(value, name) { | ||
if (typeof window === 'object') { | ||
/* global window */ | ||
window[name] = value; | ||
} else if (typeof module === 'object') { | ||
module.exports = value; | ||
} else if (typeof global === 'object') { | ||
global[name] = value; | ||
} else { | ||
throw new Error('Do not know how to register ' + name); | ||
} | ||
} | ||
|
||
register(lazyAss, 'lazyAss'); | ||
register(lazyAssync, 'lazyAssync'); | ||
|
||
}()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* global lazyAssync */ | ||
if (typeof window === 'undefined') { | ||
require('..'); // Node | ||
} | ||
if (typeof lazyAssync === 'undefined') { | ||
throw new Error('Cannot find lazyAssync global varible'); | ||
} | ||
if (typeof expect === 'undefined') { | ||
var expect = require('expect.js'); | ||
} | ||
|
||
describe('lazyAssync', function () { | ||
describe('lazyAssync function itself', function () { | ||
it('is a function', function () { | ||
expect(lazyAssync).not.to.be(undefined); | ||
expect(lazyAssync).to.be.a('function'); | ||
}); | ||
|
||
it('does not throw if condition is true', function () { | ||
expect(function () { | ||
lazyAssync(true); | ||
}).not.to.throwError(); | ||
}); | ||
|
||
it('does not evaluate function if condition is true', function () { | ||
function foo() { | ||
throw new Error('Foo has been called'); | ||
} | ||
lazyAssync(true, foo); | ||
}); | ||
}); | ||
}); |