Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

util-ex

Package Overview
Dependencies
Maintainers
0
Versions
58
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

util-ex

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

  • 2.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
50K
decreased by-15.03%
Maintainers
0
Weekly downloads
 
Created
Source

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

NameTypeDescription
aOrgFuncFunctionThe function to be wrapped.
aBeforeExecFunctionA function to be executed before the wrapped function aOrgFunc.
aAfterExecFunctionA 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

NameTypeDescription
aObjectanythe target object to inject
aMethodNamestringthe target method to inject
aNewMethodFunctionthe 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}
})

Keywords

FAQs

Package last updated on 06 Oct 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc