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

config_masticator

v1.0.0

Published

a tool for mashing up inheritable configurations

Downloads

2

Readme

config_masticator

a library for mashing inheritable config up into final specs

#what is this for?

say you have a bunch of different things that are being configured. Maybe you are using a configurable dependency injection library like the-works.

In a lot of cases those things will have a bunch of parts which are all the same, stuff like api endpoint urls, or logging specs or something like that. Wouldn't it be nice to be able to specify a base set, and then just write out the differences?

say you were writing an api integration, with some api that had a few methods, and you have a generic library that needs to be configured with a url to each different method. You could specify each out individually:


var getUsersConfig = {
	'protocol':'http:',
	'method':'GET'
	'host':'stupidapi.dumb',
	'path':'/users'
};

var getDocsConfig = {
	'protocol':'http:',
	'method':'GET'
	'host':'stupidapi.dumb',
	'path':'/horse_specs'
};

var getHorseBettingForm = {
	'protocol':'http:',
	'method':'GET'
	'host':'stupidapi.dumb',
	'path':'/betting_form'
};

this is ok, and pretty easy thanks to text editors and copy-pasting. However when it's time to change things it gets quite cumbersome, say you wanted to change the host, you would have to remember to change it in all 3 places. Using the config masticator you would specify a base config, and then overlay the changes onto it. Then you only have to change the base config to change everything else.

var apiBase = {
	'protocol':'http:',
	'method':'GET'
	'host':'stupidapi.dumb',
};

var getUsersConfig = configMasticator.overlay([apiBase],{
	'path':'/users'
}]);

var getDocsConfig = configMasticator.overlay([apiBase],{
	'path':'/horse_specs'
}]);

var getHorseBettingForm = configMasticator.overlay([apiBase],{
	'path':'/betting_form'
}]);

#why do you pass an array in?

in case you have more than one base set! Consider the above example with a bit of modification, now our api funcitons need a url, an databaseConnection, and a logger spec

var urlBase = {
	'api_url':{
		'protocol':'http:',
		'method':'GET'
		'host':'stupidapi.dumb',
	}
};

var dbConfig = {
	'dbConnection':{
		'host':localhost,
		'port':69420
	}
}

var loggerBase = {
	'logger':{
		'transport':'syslog',
		'facility':17
	}
}

var getUsersConfig = configMasticator.overlay([urlBase,dbConfig,loggerBase,{
 'api_url':{
 		'path':'/users'
	},
	'dbConnection':{
		'table':'horse_gamblers'
	},
	'logger':{
		'level':'debug',
		'prefix':'api_get_users'
	}
}]);

var getDocsConfig = configMasticator.overlay([urlBase,dbConfig,loggerBase,{
 'api_url':{
 		'path':'/horse_specs'
	},
	'dbConnection':{
		'table':'horse_stats'
	},
	'logger':{
		'level':'debug',
		'prefix':'api_get_docs'
	}
}])

var getHorseBettingForm = configMasticator.overlay([urlBase,dbConfig,loggerBase,{
 'api_url':{
 		'path':'/betting_form'
	},
	'dbConnection':{
		'table':'bets'
	},
	'logger':{
		'level':'debug',
		'prefix':'api_get_bets'
	}
}])

once again, in each new piece, you only had to specify what was different.

#what are the rules by which configs get merged.

For some mysterious reason the rules closely match _.merge from lodash, with a couple of exceptions however.

first: Arrays are treated as single values, and are not merged. eg:

	var base = {'a':[1,2,3]};
	var derived = configMasticator.overlay([base,{
		'a':['a','b','c']
	}]);

	derived.a -> ['a','b','c']

second: null values are stripped out, and never show up in a final merge (meaning you can use null to get rid of thigns you don't want) eg:

	var base = {
		'a':[1,2,3],
		'b':{
			'ducks':true,
			'dogs':false
		}
	};

	var derived = configMasticator.overlay([base,{
		'b':null
	}]);

	derived.a -> [1,2,3]
	derived.hasOwnProperty('b') -> false

#what if I want to get rid of a lot of stuff, and only use a few parts from the base set?

we export a different function for that! It's called trimout, and it takes 2 arguments, the first is a list of configs exactly like overlay, and the last is the final bit of config you care about. Say in our previous "api" example you wanted to add a new thing which didn't use the logger or the database


var weirdRouteConfig = configMasticator.trimout([urlBase,dbConfig,loggerBase],{
	'api_url':{
		'path':'/what/the/heck/man'
	}
});

weirdRouteConfig.dbConnection -> undefined;
weirdRouteConfig.logger -> undefined;
weirdRouteConfig.api_url -> {
		'protocol':'http:',
		'method':'GET'
		'host':'stupidapi.dumb',
		'path': '/what/the/heck/man'
	}
};