1.0.3 • Published 10 days ago

util-ex v1.0.3

Weekly downloads
10,336
License
MIT
Repository
github
Last release
10 days 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-alpha.14

10 days ago

2.0.0-alpha.13

30 days ago

2.0.0-alpha.12

30 days ago

2.0.0-alpha.11

1 month ago

2.0.0-alpha.9

2 months ago

2.0.0-alpha.10

2 months ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago

0.3.17

1 year ago

0.3.16

1 year ago

2.0.0-alpha.7

1 year ago

2.0.0-alpha.8

1 year ago

1.0.3

1 year ago

2.0.0-alpha.3

1 year ago

2.0.0-alpha.4

1 year ago

2.0.0-alpha.5

1 year ago

2.0.0-alpha.6

1 year ago

2.0.0-alpha.0

1 year ago

2.0.0-alpha.1

1 year ago

2.0.0-alpha.2

1 year ago

0.3.18

1 year 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

9 years ago

0.3.3

9 years ago

0.3.2

9 years ago

0.3.1

9 years ago

0.3.0

9 years ago

0.2.9

9 years ago

0.2.8

9 years ago

0.2.7

9 years ago

0.2.6

9 years ago

0.2.5

9 years ago

0.2.4

9 years ago

0.2.3

9 years ago

0.2.2

9 years ago

0.2.1

9 years ago

0.2.0

9 years ago

0.1.1

9 years ago

0.1.0

9 years ago

0.0.1

9 years ago