FunctionHelper
Provides functionality for working with functions
Functions
8
Functions
8Inserts 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
| Parameter | Type | Description |
|---|---|---|
object | Object | The object to hook. |
method | String | The name of the method on |
fn | function | String | The function or method name (on |
thisObj | Object | The |
options | Object | Additional options |
options.return | Boolean | Specify |
The function to call to remove the hook.
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'
);
| Parameter | Type | Description |
|---|---|---|
duration | Number | The duration of the animation in milliseconds. |
fn | function | The function to call for each animation frame. |
fn.progress | Number | The current progress value from |
thisObj | Object | The |
easing | linear | 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. |
A promise which resolves when the animation completes.
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
| Parameter | Type | Description |
|---|---|---|
object | Object | The object to hook. |
method | String | The name of the method on |
fn | function | String | The function or method name (on |
thisObj | Object | The |
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.
| Parameter | Type | Description |
|---|---|---|
fns | function | The filter functions to combine. The combined filter function will return |
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.
| Parameter | Type | Description |
|---|---|---|
fn | function | The function to call. |
buffer | Number | The milliseconds to wait after each execution before another execution takes place. |
thisObj | Object |
|
args | Array | The argument list to append to those passed to the 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.
| Parameter | Type | Description |
|---|---|---|
original | function | The function to call second. |
interceptor | function | The function to call first. |
thisObj | Object | The |
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.
| Parameter | Type | Description |
|---|---|---|
original | function | The function to call first. |
sequence | function | The function to call second. |
thisObj | Object | Overrides the |
sequenceThisObj | Object | The |
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.
| Parameter | Type | Description |
|---|---|---|
fn | function | The function to call. |
buffer | Number | The milliseconds to wait after each execution before another execution takes place. |
thisObj | Object |
|
extraArgs | Array | The argument list to append to those passed to the function. |
alt | function | A function to call when the invocation is rejected due to buffer time not having expired. |
A function which calls the passed fn only if at least the passed buffer
milliseconds has elapsed since its last invocation.