checkmark
A tiny library that may ease writing tests and debugging code a bit.
The primary motivation for the creation of checkmark was to help more easily write automated tests (especially for asynchronous code). However, in general, the library could be useful in all testing or debugging scenarios, as well as in production code. ` It acts as a counter by tracking the number of invocations of an utility function that checkmark provides which you can call at particular points in your code.
Checkmark is tiny in code size, has no dependencies, and can be used in different environmens (CommonJS, AMD, or as a global in the browser) thanks to utilizing the UMD pattern.
Installation
npm (Node.js)
npm install checkmark
Bower
bower install checkmark
Usage
CommonJS (Node.js)
var Checkmark = ; var check = count callback;
AMD
;
Browser (exposed as a global)
API (by example)
// Create a checkmark function // Argument 1 (count): positive integer (required) // Argument 2 (callback): function (optional); will be invoked when target count is reached // Will throw Error when target count exceeded var check = 4 { // when this callback gets called // the target of 4 counts will have been just reached }; // get current count value check; // => 0 // mark 1 ; check; // => 1 check; // => [ null ] // mark 2, with message ; check; // => 2 check; // => [ null, "2nd mark with message" ] // mark 3, with data ; check; // => 3 check; // => [ null, "2nd mark with message", { message: "3rd mark", now: "..." } ] // mark 4 ; // callback gets called check; // => 4 check; // => [ null, "2nd mark with message", { message: "3rd mark", now: "..." }, null ] // mark 5, after target has been reached ; // throws Error check; // => 4 check; // => [ null, "2nd mark with message", { message: "3rd mark", now: "..." }, null ]
Usage examples
Mocha test (for Sails.js)
;
Mocha test (for Sails.js)
;
Wait for two async functions to complete
var result1 = null;var result2 = null; var check = 2 { // both callbacks below have finished executing // use result1 and result2}; ; ;
Wait for two async functions to complete
var data = 1 23 7 4 563 ... ;var middle = datalength / 2;var data1 = data; // first half of datavar data2 = data; // second half of data var callback = 2 { var count = callback; // count[0] holds the result from the function that completed first // count[1] holds the result from the function that completed second console;}; // Imagine these execute in different threads// so that we achieve some parallelization.// In general, they may take different// amount of time to complete.; // get the number of prime numbers within data1 array; // get the number of prime numbers within data2 array
Ensure an event does not happen more than N times (with jQuery)
var registerEvent = 3; ;
Contributing
Any feedback or pull requests are welcome.
See CONTRIBUTING.md
file for more information.
License and Author
MIT © 2014 Radko Dinev
See LICENSE
file.