Created
December 7, 2016 14:44
-
-
Save banduk/f8dfb955e42860ef29a51e89fd3921ac to your computer and use it in GitHub Desktop.
callTimes
This file contains hidden or 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
function CalledMoreThanExpected(meta = {}) { | |
this.name = 'CalledMoreThanExpected' | |
this.message = 'Function called more than the expected times' | |
this.meta = meta | |
this.stack = (new Error()).stack | |
} | |
CalledMoreThanExpected.prototype = Error.prototype | |
CalledMoreThanExpected.prototype.constructor = CalledMoreThanExpected | |
function CalledWithNoFunction(meta = {}) { | |
this.name = 'CalledWithNoFunction' | |
this.message = 'callTimes called with argument thats not a function' | |
this.meta = meta | |
this.stack = (new Error()).stack | |
} | |
CalledWithNoFunction.prototype = Error.prototype | |
CalledWithNoFunction.prototype.constructor = CalledWithNoFunction | |
const callTimes = function (fn, times= 1) { | |
const self = this | |
const onceDate = new Date() | |
if (typeof(fn) !== 'function') { | |
throw new CalledMoreThanExpected({ fn, date: onceDate }) | |
} | |
const onceCallerFunctionBody = fn.toString() | |
const onceCallerFunctionName = fn.name || '__annonymous__' | |
// l = l.child({ onceCallerFunctionBody, onceCallerFunctionName }) | |
const f = function (...args) { | |
f._calledTimes += 1 | |
const now = new Date() | |
const calledTimes = f._calledTimes | |
const callInfo = { date: now, args, calledTimes } | |
f._callHistory = Object.assign(callInfo, { previousCall: f._callHistory }) | |
const context = { | |
allowedTimes: times, | |
onceInfo: { | |
callerFunctionBody: onceCallerFunctionBody, | |
callerFunctionName: onceCallerFunctionName, | |
date: onceDate, | |
}, | |
callHistory: f._callHistory, | |
} | |
if (f._calledTimes > times) { | |
throw new CalledMoreThanExpected(context) | |
} | |
console.info('Calling function', context) | |
const result = fn.apply(self, args) | |
context.callHistory.result = result | |
return result | |
} | |
f._callHistory = {} | |
f._calledTimes = 0 | |
return f | |
} | |
module.exports = callTimes |
This file contains hidden or 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
import callTimes from './callTimes' | |
const pow2 = (n) => n * n | |
const c = callTimes(pow2) | |
const c1 = callTimes(pow2, 1) | |
const c2 = callTimes(pow2, 2) | |
console.assert(c(2) === 4) // OK | |
console.assert(c(2) === 4) // Error | |
console.assert(c1(2) === 4) // OK | |
console.assert(c1(2) === 4) // Error | |
console.assert(c2(2) === 4) // OK | |
console.assert(c2(2) === 4) // OK | |
console.assert(c2(2) === 4) // Error |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment