npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

bauer-factory

v2.0.6

Published

General utilities for nodejs.

Downloads

123

Readme

node-bauer-factory

General utilities for nodejs.

Installation

npm install bauer-factory

Usage

var factory = require("bauer-factory");

.type

Returned value can be string, number, boolean, date, regexp, error, array, object, arguments, null, undefined or function.

// returns type of given argument
var type = factory.type(arg);

.isNull

factory.isNull(arg)
// same as
factory.type(arg) === "null"

.isDefined

factory.isDefined(arg)
// same as
factory.type(arg) !== "undefined"

.isUndefined

factory.isUndefined(arg)
// same as
factory.type(arg) === "undefined"

.isDate

factory.isDate(arg)
// same as
factory.type(arg) === "date"

.isError

factory.isError(arg)
// same as
factory.type(arg) === "error"

.isBoolean

factory.isBoolean(arg)
// same as
factory.type(arg) === "boolean"

.isArray

factory.isArray(arg)
// same as
factory.type(arg) === "array"

.isNumber

factory.isNumber(arg)
// same as
factory.type(arg) === "number"

.isString

factory.isString(arg)
// same as
factory.type(arg) === "string"

.isObject

factory.isObject(arg)
// same as
factory.type(arg) === "object"

.isRegExp

factory.isRegExp(arg)
// same as
factory.type(arg) === "regexp"

.isFunction

factory.isFunction(arg)
// same as
factory.type(arg) === "function"

.isArguments

factory.isArguments(arg)
// same as
factory.type(arg) === "arguments"

.createMethod

Accepts an object containing types/lengths as keys and values as functions.

var func = factory.createMethod({
	0: function() {}, // executed if called with zero arguments
	s: function(s) {}, // executed if called with one string
	sf: function(s) {}, // executed if called with a string and a function
	_: function() {}, // executed if none of the above is matched
});

Letters are taken as the first character of the argument's type as returned by factory.type. Any combination can be used to route the function execution. This takes priority over argument's length routing.

var func = factory.createMethod({
	o: function() {}, // executed if called with object
	a: function(s) {}, // executed if called with array or arguments
	sffb: function(s,f0,f1,b) {}, // executed if called with a string, two functions and a boolean
});

Numbers are taken as the length of the arguments object. Nested rules are supported.

var func = factory.createMethod({
	5: { // executed if called with five arguments
		sssss: function() {}, // five strings
		assss: function() {}, // one array and four strings
	},
	1: function(arg) {}, // executed if called with one argument
});

Underscore holds the default code. If no rule is matched and there's no _ throws an ReferenceError.

var func = factory.createMethod({
	_: function() {},
});

Strings can be used as code. They are converted to functions internally with the defined arguments.

var func = factory.createMethod({
	s: "return this.get(s)", // the given string can be refered as 's'
	ss: "return this.both(s0,s1)", // if it's two strings, just add the index
	f: "this.on('ready',f)", // easy to define aliases
});

If the code does not use any external vars its possible to optimize the generated function by passing a second argument as true.

var optimized = factory.createMethod({
	s: "return this.get(s)",
	ss: "return this.both(s0,s1)",
	f: "this.on('ready',f)",
},true); // tell factory.method that this function does not use any external var

.createClass

Creates a class with given methods, constructor and inheritance.

var Bauer = factory.createClass({

	// requires 'events' and inherits EventEmitter from it
	// also accepts functions
	inherits: require("events").EventEmitter,

	// called when new Bauer() is executed
	// it can also be routed by factory.method if needed
	constructor: function() {
	},

	// methods are created by factory.method
	killTerrorists: {
	},

	tortureSuspects: {
	},

	doWhateverIsNecessary: function() {},

});

The created class can be instantiated and inherited just like any other class.

var jack = new Bauer();

jack.killTerrorists();

jack.tortureSuspects();

jack.doWhateverIsNecessary();

.createObject

Creates a class just like .class does and returns an instance of it.

// accepts same arguments as .createClass
var jack = factory.createObject({

	// requires 'events' and inherits EventEmitter from it
	// also accepts functions
	inherits: require("events").EventEmitter,

	// called when new Bauer() is executed
	// it can also be routed by factory.method if needed
	constructor: function() {
	},

	// methods are created by factory.method
	killTerrorists: {
		s: function() {},
		n: function() {},
	},

	tortureSuspects: {
		1: function() {},
		2: function() {},
	},

	doWhateverIsNecessary: function() {},

});