2.1.0 • Published 3 months ago

util-ex v2.1.0

Weekly downloads
10,336
License
MIT
Repository
github
Last release
3 months ago

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}
})
2.0.0

4 months ago

2.1.0

3 months ago

2.0.0-alpha.18

8 months ago

1.0.4

8 months ago

2.0.0-alpha.17

8 months ago

2.0.0-alpha.16

8 months ago

2.0.0-alpha.15

8 months ago

2.0.0-alpha.14

8 months ago

2.0.0-alpha.13

9 months ago

2.0.0-alpha.12

9 months ago

2.0.0-alpha.11

9 months ago

2.0.0-alpha.9

10 months ago

2.0.0-alpha.10

10 months ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

0.3.17

2 years ago

0.3.16

2 years ago

2.0.0-alpha.7

2 years ago

2.0.0-alpha.8

2 years ago

1.0.3

2 years ago

2.0.0-alpha.3

2 years ago

2.0.0-alpha.4

2 years ago

2.0.0-alpha.5

2 years ago

2.0.0-alpha.6

2 years ago

2.0.0-alpha.0

2 years ago

2.0.0-alpha.1

2 years ago

2.0.0-alpha.2

2 years ago

0.3.18

2 years ago

0.3.15

9 years ago

0.3.14

9 years ago

0.3.13

9 years ago

0.3.12

9 years ago

0.3.11

9 years ago

0.3.10

9 years ago

0.3.9

9 years ago

0.3.8

9 years ago

0.3.7

9 years ago

0.3.6

9 years ago

0.3.5

9 years ago

0.3.4

10 years ago

0.3.3

10 years ago

0.3.2

10 years ago

0.3.1

10 years ago

0.3.0

10 years ago

0.2.9

10 years ago

0.2.8

10 years ago

0.2.7

10 years ago

0.2.6

10 years ago

0.2.5

10 years ago

0.2.4

10 years ago

0.2.3

10 years ago

0.2.2

10 years ago

0.2.1

10 years ago

0.2.0

10 years ago

0.1.1

10 years ago

0.1.0

10 years ago

0.0.1

10 years ago