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

util-ex

v2.1.0

Published

Browser-friendly enhanced util fully compatible with standard node.js

Downloads

241,659

Readme

util-ex Build Status npm downloads license

Enhanced utils

This package modifies and enhances the standard util from node.js

API

Full API Documents is here: Docs

inject

inject(aOrgFunc, aBeforeExec, aAfterExec): Function

Wraps a function and executes code before and/or after the wrapped function.

Throws

If aAfterExec is not a function and an error occurs while executing the wrapped function.

Example

import { inject as injectFunc } from 'util-ex'

// Wrapping a function with injectFunc
const originalFunc = (a, b) => a + b;
const beforeFunc = (a, b) => console.log(`Before execution: a = ${a}, b = ${b}`);
const afterFunc = (result) => console.log(`After execution: result = ${result}`);
const wrappedFunc = injectFunc(originalFunc, beforeFunc, afterFunc);
const result = wrappedFunc(1, 2); // Logs "Before execution: a = 1, b = 2" and "After execution: result = 3"

Example

// Wrapping a function with injectFunc and modifying arguments and return value
const Arguments = injectFunc.Arguments
const originalFunc = (a, b) => a + b;
const beforeFunc = (a, b) => {
  console.log(`Before execution: a = ${a}, b = ${b}`);
  return new Arguments([a * 2, b * 3]);
};
const afterFunc = (result, isDenied) => {
  console.log(`After execution: result = ${result}, isDenied = ${isDenied}`);
  return result * 2;
};
const wrappedFunc = injectFunc(originalFunc, beforeFunc, afterFunc);
const result = wrappedFunc(1, 2); // Logs "Before execution: a = 1, b = 2", "After execution: result = 6, isDenied = false"
console.log(result); // Output: 12

Example

// Wrapping a function with injectFunc and not executing the original function
const originalFunc = (a, b) => a + b;
const beforeFunc = (a, b) => {
  console.log(`Before execution: a = ${a}, b = ${b}`);
  return "Not executing original function";
};
const afterFunc = (result, isDenied) => {
  console.log(`After execution: result = ${result}, isDenied = ${isDenied}`);
  return "Modified return value";
};
const wrappedFunc = injectFunc(originalFunc, beforeFunc, afterFunc);
const result = wrappedFunc(1, 2); // Logs "Before execution: a = 1, b = 2", "After execution: result = Modified return value, isDenied = true"
console.log(result); // Output: "Modified return value"

Example

// Wrapping a function with injectFunc and getting the original function's error
const originalFunc = () => {
  throw new Error("Original function error");
};
const beforeFunc = () => {
  console.log("Before execution");
};
const afterFunc = (result, isDenied) => {
  console.log(`After execution: result = ${result}, isDenied = ${isDenied}`);
};
const wrappedFunc = injectFunc(originalFunc, beforeFunc, afterFunc);
wrappedFunc(); // Logs "Before execution", "After execution: result = [Error: Original function error], isDenied = false"

inject Parameters

| Name | Type | Description | | :------ | :------ | :------ | | aOrgFunc | Function | The function to be wrapped. | | aBeforeExec | Function | A function to be executed before the wrapped function aOrgFunc. | | aAfterExec | Function | A function to be executed after the wrapped function aOrgFunc. |

inject Returns

Function

A new function that wraps the original function.

BeforeExec: If aBeforeExec is a function, it will be called with the same context and arguments as the wrapped function.

  • If it returns an Arguments object, the wrapped function will be called with the modified arguments.
  • If it returns a value other than undefined, the wrapped function will not be called and this value will be returned as result instead.

AfterExec: If aAfterExec is a function, it will be called with the same context, arguments with additional the result of the aOrgFunc and isDenied flag.

  • If the aOrgFunc throws an error, the result parameter will be an Error object.
  • If aAfterExec returns a value, it will be used as the final result of the wrapped function.
  • If isDenied parameter is true, it means aOrgFunc was not called during execution of the wrapped function.

injectMethod

injectMethod(aObject, aMethodName, aNewMethod): boolean

Injects method into an object. optionally preserving access to the original method via "super" and original instance via "self".

Note:

  • In the new method, you can use this.super() to call the original method, this.super() is already bound with original instance.
  • The this[aMethodName] is also the original method, but not bound yet.
  • this.self is the original instance!

Example

import { injectMethod } from 'util-ex'

var obj = {
  method1: function() {
    console.log('Hello');
  }
};

var newMethod = function() {
  this.super();
  console.log('World');
};

injectMethod(obj, 'method1', newMethod);

obj.method1(); // Output: Hello\nWorld

injectMethod Parameters

| Name | Type | Description | | :------ | :------ | :------ | | aObject | any | the target object to inject | | aMethodName | string | the target method to inject | | aNewMethod | Function | the new method to be injected into the aObject. |

injectMethod Returns

boolean

whether the injection is successful.

newFunction

newFunction(name, arguments, body[, scope[, values]])
newFunction(functionString[, scope[, values]])

Creates a new function with the given name, arguments, body, scope and values.

  • If only one argument is provided and it is a function, returns a new function with the same code.
  • If only one argument is provided and it is not a function, returns a new empty function with the given name.
  • If multiple arguments are provided, creates a new function with the given name, arguments and body.
import { newFunction } from 'util-ex'

var fn = newFunction('yourFuncName', ['arg1', 'arg2'], 'return log(arg1+arg2);', {log:console.log})
newFunction('function yourFuncName(){}')
newFunction('function yourFuncName(arg1, arg2){return log(arg1+arg2);}', {log:console.log})
newFunction('function yourFuncName(arg1, arg2){return log(arg1+arg2);}', ['log'], [console.log])

//fn.toString() is :
/*
 "function yourFuncName(arg1, arg2) {
    return log(arg1+arg2);
 }"
*/

defineProperty

defineProperty(object, key, value[, aOptions])

Define a property on the object. move to inherits-ex package.

usage

const defineProperty = require('util-ex/lib/defineProperty')

let propValue = ''
const obj = {}

defineProperty(obj, 'prop', 'simpleValue')
defineProperty(obj, 'prop', undefined, {
  get() {return propValue}
  set(value) {propValue = value}
})