0.2.5 • Published 12 years ago

common v0.2.5

Weekly downloads
10,676
License
-
Repository
github
Last release
12 years ago

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.step([
	function(next) { // next is the last argument, except in the last handler
		fs.readFile(__filename, 'utf-8', next);
	},
	function(file) {
		console.log(file);
	}
], function(err) {
	// any error received in a callback will be forwarded here
});

It also contains a shortcut to the EventEmitter prototype and a compatible implementation of this for the browser.

var MyEmitter = common.emitter(function() {
	this.foo = 42;
});

var me = new MyEmitter();

me.emit('foo', me.foo); // emits 'foo',42

There is also a more general method for extending prototypes called extend:

// this prototype is the same as above
var MyEmitter = common.extend(events.EventEmitter, function() {
	this.foo = 42;
});

If you want to use futures you can use the future function to create a future:

var fut = common.future();

fut.get(function(val) {
	console.log(val);
});
setTimeout(function() {
	fut.put(42); // results in the previous .get being called and all future .get's will be called synchroniously
}, 1000)

To do string formatting you can use format:

// you can parse the arguments to a pattern one by one
common.format('define {0} here', 'pattern'); // returns 'define pattern here'

// or as a map or array
common.format('define {foo} here', {foo:'pattern'}); // 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.gensym() // returns 's0'
common.gensym() // returns 's1'

If you instead of a weak symbol need a strong one use uuid:

common.uuid(); // returns a strong id, ex: ngDl6IdovME9JKvIxgED0FK1kzURxfZaCq48-0

Common can also encode integers into alphanumerical notation using encode:

common.encode(1000); // returns G8

To ensure that a method cannot be called more than once you can use the once function:

var fn = common.once(function() {
	console.log('hello');
});

fn(); // prints hello
fn(); // does nothing

Besides the above common implements two of the utilities mentioned in The Good Parts, memoizer and curry.

barchart-alerts-clientbarchart-instruments-client@zerocoder/monorepo-web@infinitebrahmanuniverse/nolb-commonuser-filter@everything-registry/sub-chunk-1362test-js-utiltest-js-accounttest-js-contractwebsockstorage-web-api@deval_13/common@deval_13/medium-blog@devank75/medium-commonjilojson-socketsjsonizesubdirectorystorage-api-clienttidepool-mmcsv-carelink-data@aryan01/common@aprilium/tripsm_country@avantika04/sthira-common@alexsamin/blogging-common@bkopyio/nft-api@abdulazizyass/common@abhilashtengli/medium-common@abhilashtengli/medium-common01@az2z/commonxsocketszft-lowcode-materialsvue3-test-app@hedy_6/add@hedy_6/sub@happy2301/medium-common@gama-academy/smash-native@jdfwarrior/polo@juphoon/web-room-sdk@rdevs/medium-common@raisemusic/common@satya1606/common-app@satya1606/work-app@smahtz/tsplus@snaaps/snapmongoose-formatmohaimin-skeletonminimed-connect-to-nightscoutminimed-connect-to-nightscout-mattster98minimed-connect-to-nightscout-szymjawmessage-socketsp2p-hubnpushseedoneservefileserver-themessaffronappservermarkotestngs-supergraphprescientpolopolo-discoverrexifyrex-cliredis.pubsubpubsub.iopubsub.io.clientpubsub.io.hubrikao-liuchengjinjjjfiejreact-qc-ivreact-qc-vshared-ragasa-connectgoogle-calendar-free-slots-schedularin-d-gestionhealth-clientlifeemedicommon@tikket4real/common@vinay-npm/common@vibins/medium-common@yashd10/commonairserverassetstreamasync-dnsjackasync-ejscomit-scriptscommonlernapackageforaniscorede-common-internifcorede-common-umbrellacrossmaniabarkbifcore-js-accountbifcore-js-contractbifcore-js-utilbuffoondrtyoshi-smash-native@shashwat_1205/medium-common@shahedofficial/expresscommon@shubhambhatt/common
0.2.5

12 years ago

0.2.4

12 years ago

0.2.3

12 years ago

0.2.1

14 years ago

0.2.0

14 years ago

0.1.4

14 years ago

0.1.3

14 years ago

0.1.2

14 years ago

0.1.1

14 years ago

0.1.0

14 years ago