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

console-extend

v1.0.5

Published

this acts as a proxy for the console object so that you can execute functions while also console.(log|warn|error)

Downloads

13

Readme

console-extend

Description: This module allows you to proxy the console.log,console.warn and console.error methods to add in your own function calls that are executed before the origial console(log|warn|error) (can all set it to not do the origional call at all). Also exposes console.dev as a new method you can use to have a custome console call. If you need more then just one new method you can call addCustom to add any new console method by name. Ex console.test or console.jordan.

Steps:

1. Include console-extend
2. Create a constructor file(or do it all in the file you will be logging from)
3. Call .console() to retireve new console object with proxied methods attached.
4. Replace origional console with new console returned.

Example:

consoleConstructor.js

var consoleProxy = require('./console-extend');

consoleProxy.log(function(arg) {
	var tempArg = arg.replace(/\s/g, '');
	console.error(tempArg);
},false)

consoleProxy.warn([
	function() {
		var args = Array.prototype.slice.call(arguments);
		var sum = 0;
		args.forEach(x => {
			sum = sum+ x;
		})
		console.error(sum);
	},
	function() {
		var args = Array.prototype.slice.call(arguments);
		newArr = args.map(x => {
			return (x % 2 === 0) ? 'even' : 'odd'
		})	
		console.error(newArr)
	}
	
	],false);

module.exports = consoleProxy.console();

The above overrides console.log to console.error what was passed in without spaces && console.warn will return sum off all numbers that were arguments and an array showing if they started as even or odd.

Example in use:

test.js
//replace console object with the new one returned from consoleConstructor
console = require('./consoleConstructor');

//below should now replace all spaces in string
console.log('hiiowihefpoqwehif pqowhiefpoqwehifpo qweifh poqwehf')

//below should log out the sum of the numbers and also tell if each number is even or odd
console.warn(1,23,45,234234,3)		

Methods included in console-extend:

.log([fn],shouldStillLog)

args:
	firstArg: array of functions or a single function that will run before orig log.
	secondArg: true or false that decideds if it should still do origional log at the end.	
	=====================
	
.warn([fn],shouldStillLog)

args:
	firstArg: array of functions or a single function that will run before orig log.
	secondArg: true or false that decideds if it should still do origional log at the end.	
	=====================
	
.error([fn],shouldStillLog)

args:
	firstArg: array of functions or a single function that will run before orig log.
	secondArg: true or false that decideds if it should still do origional log at the end.	
	=====================
	
	.dev([fn],shouldStillLog) //give you a new custom log called with console.dev

args:
	firstArg: array of functions or a single function that will run before orig log.
	secondArg: true or false that decideds if it should still do origional log at the end.	
	=====================
	.addCustom('name',[fn],shouldStillLog)

args:
	firstArg: name for new console method. example 'test' = console.test.
	secondArg: array of functions or a single function that will run before orig log.
	thirdArg: true or false that decideds if it should still do origional log at the end.	
	=====================

.console()

args:none

returns new console object;

Things to note that are important:

  1. each function that is used as a proxy method will be passed the same args that the orig log function was passed. So in order to interact with them make sure to use the arguments object and not try to set params on the functions.
  2. This happens async so if you are to pass an array of functions to be ran they may or may not run in order. They are executed inside a forEach loop.
  3. The logging functions can include a wide range of types and arguments so in order to not get hard fails make sure to type check everything inside your proxy function(unlike above). Ex if your are calling .toUpperCase() type check to make sure its a string your calling it on.
  4. Last, Do not call console.(log|warn|error) inside the function your proxing or else it will recursively call itself until it fills the stack killing the program.