FunctionHelper

Provides functionality for working with functions

Functions

8
afterstatic

Inserts a function after the specified method is called on an object. To remove this hook, invoke the function returned by this method.

 class A {
     method() {
         console.log('method');
     }
 }

 let instance = new A();

 let detach = FunctionHelper.after(instance, 'method', () => { console.log('after') });

 instance.method();
 > method
 > after

 detach();
 instance.method();
 > method

The value returned by the original method is passed as the first argument to fn followed by all the arguments passed by the caller.

If fn returns a value (not undefined), that value is returned from the method call instead of the value returned by the original method.

 class A {
     method(x) {
         console.log('method', x);
         return x * 2
     }
 }

 let instance = new A();

 let detach = FunctionHelper.after(instance, 'method', (ret, x) => {
     console.log('after', ret, x);
     return x / 2;
 });

 console.log(instance.method(50));
 > method 50
 > after 100 50
 > 25

 detach();
 console.log(instance.method(50));
 > method 50
 > 100
ParameterTypeDescription
objectObject

The object to hook.

methodString

The name of the method on object to hook.

fnfunction | String

The function or method name (on thisObj) to call after method.

thisObjObject

The this pointer value for calling fn.

optionsObject

Additional options

options.returnBoolean

Specify false to not include the return value of the hooked method as the first argument to fn.

Returns: function -

The function to call to remove the hook.

animatestatic

Calls the passed function on every animation frame while animating the progress value from 0 to 1 over the specified duration.

The function is called with the current progress value as the first argument. That value changes from 0 to 1 over the duration of the animation at a rate determined by the easing function.

The called function may change the value of any property or perform some other action based on the current progress value.

The function should return false to indicate that the animation should be stopped immediately.

// Increase hourHeight by 20 pixels in a visually appealing way
const
    { activeView } = this,
    { hourHeight } = activeView;

FunctionHelper.animate(
    1000,
    progress => activeView.hourHeight = hourHeight * progress * 20,
    null,
    'bounce'
);
ParameterTypeDescription
durationNumber

The duration of the animation in milliseconds.

fnfunction

The function to call for each animation frame.

fn.progressNumber

The current progress value from 0 to 1.

thisObjObject

The this reference for the function.

easinglinear | easeInBack | easeOutBack | easeInOutBack | easeOutBounce | easeInOutBounce | easeInCirc | easeOutCirc | easeInOutCirc | easeInCubic | easeOutCubic | easeInOutCubic | easeInElastic | easeOutElastic | easeInOutElastic | easeInExpo | easeOutExpo | easeInOutExpo | easeInQuad | easeOutQuad | easeInOutQuad | easeInQuart | easeOutQuart | easeInOutQuart | easeInQuint | easeOutQuint | easeInOutQuint | easeInSine | easeOutSine | easeInOutSine

The easing function to use.

Returns: Promise -

A promise which resolves when the animation completes.

beforestatic

Inserts a function before the specified method is called on an object. To remove this hook, invoke the function returned by this method.

 class A {
     method() {
         console.log('method');
     }
 }

 let instance = new A();

 let detach = FunctionHelper.before(instance, 'method', () => { console.log('before') });

 instance.method();
 > before
 > method

 detach();
 instance.method();
 > method

If fn returns false, the original method is not invoked and false is returned to the caller.

 class A {
     method(x) {
         console.log('method', x);
         return x * 2;
     }
 }

 let instance = new A();

 let detach = FunctionHelper.before(instance, 'method', x => {
     console.log('before', x);
     return false;
 });

 console.log(instance.method(50));
 > before 50
 > false

 detach();
 console.log(instance.method(50));
 > method 50
 > 100
ParameterTypeDescription
objectObject

The object to hook.

methodString

The name of the method on object to hook.

fnfunction | String

The function or method name (on thisObj) to call before method.

thisObjObject

The this pointer value for calling fn.

Returns: function -

The function to call to remove the hook.

Combines all the passed filtering functions into a single function which returns false if any of the passed functions returns false, otherwise it returns true.

If only one function is passed, it is returned without being wrapped in another function.

If no functions are passed, undefined is returned - this is useful for cases where a filter function is optional and we don't want to add the overhead of an extra function call when there is no filter.

ParameterTypeDescription
fnsfunction

The filter functions to combine. The combined filter function will return false if any of the passed functions returns false, otherwise it returns true.

Returns: -

Function A function which returns false if any of the passed functions returns false, otherwise it returns true.

Create a "debounced" function which will call on the "trailing edge" of a timer period. When first invoked will wait until the buffer period has expired to call the function, and more calls within that time will restart the timer.

This is useful for responding to keystrokes, but deferring action until the user pauses typing.

ParameterTypeDescription
fnfunction

The function to call.

bufferNumber

The milliseconds to wait after each execution before another execution takes place.

thisObjObject

this reference for the function.

argsArray

The argument list to append to those passed to the function.

Returns: function -

A function which calls the passed fn when at least the passed buffer milliseconds has elapsed since its last invocation.

Returns a function which calls the passed interceptor function first, and the passed original after as long as the interceptor does not return false.

ParameterTypeDescription
originalfunction

The function to call second.

interceptorfunction

The function to call first.

thisObjObject

The this reference when the functions are called.

Returns: function -

A function which yields the return value from the original function if it was called, else false.

Returns a function which calls the passed sequence function after calling the passed original.

ParameterTypeDescription
originalfunction

The function to call first.

sequencefunction

The function to call second.

thisObjObject

Overrides the this reference when the original function is called.

sequenceThisObjObject

The this reference when the sequence function is called (Defaults to thisObj).

Returns: function -

A function which yields the value returned from the sequence if it returned a value, else the return value from the original function.

Create a "debounced" function which will call on the "leading edge" of a timer period. When first invoked will call immediately, but invocations after that inside its buffer period will be rejected, and one invocation will be made after the buffer period has expired.

This is useful for responding immediately to a first mousemove, but from then on, only calling the action function on a regular timer while the mouse continues to move.

ParameterTypeDescription
fnfunction

The function to call.

bufferNumber

The milliseconds to wait after each execution before another execution takes place.

thisObjObject

this reference for the function.

extraArgsArray

The argument list to append to those passed to the function.

altfunction

A function to call when the invocation is rejected due to buffer time not having expired.

Returns: function -

A function which calls the passed fn only if at least the passed buffer milliseconds has elapsed since its last invocation.