Common
A utility module for both node.js and the browser.
It is available through npm:
npm install common
Or as minified js file for the browser:
<script src='common.min.js'></script>
This module among other things contains a fork of step that also provides error handling
common;
It also contains a shortcut to the EventEmitter
prototype and a compatible implementation of this for the browser.
var MyEmitter = common;var me = ;me; // emits 'foo',42
There is also a more general method for extending prototypes called extend
:
// this prototype is the same as abovevar MyEmitter = common;
If you want to use futures you can use the future
function to create a future:
var fut = common;fut;
To do string formatting you can use format
:
// you can parse the arguments to a pattern one by onecommon; // returns 'define pattern here'// or as a map or arraycommon; // same as above
There is a log
method that just accepts the does the same as format
except it prints out the result using console.log
if available
To generate a simple weak symbols (often used when generating keys for a map) use gensym
common // returns 's0'common // returns 's1'
If you instead of a weak symbol need a strong one use uuid
:
common; // returns a strong id, ex: ngDl6IdovME9JKvIxgED0FK1kzURxfZaCq48-0
Common can also encode integers into alphanumerical notation using encode
:
common; // returns G8
To ensure that a method cannot be called more than once you can use the once
function:
var fn = common;; // prints hello; // does nothing
Besides the above common implements two of the utilities mentioned in The Good Parts, memoizer
and curry
.