Utilily library for blink

$b.VERSION

Returns

  • (string): Returns version of Blink, e.g. '1.0.22'.

$b.isFunction(value)

Checks if value is classified as a Function object.

Arguments

  • value (*): The value to check.

Returns

  • (boolean): Returns true if value is a function, else false.
$b.isFunction(myFunc)

$b.isBoolean(value)

Checks if value is classified as a boolean primitive or object.

Arguments

  • value (*): The value to check.

Returns

  • (boolean): Returns true if value is a function, else false.
$b.isBoolean(myFunc)

$b.isArray(value)

Checks if value is classified as an Array object.

Arguments

  • value (*): The value to check.

Returns

  • (boolean): Returns true if value is an array, else false.

Returns true:

$b.isArray([1, 2, 3]);

Returns false:

$b.isArray(document.body.children);

$b.isUndefined(value)

Checks if value is undefined.

Arguments

  • value (*): The value to check.

Returns

  • (boolean): Returns true if value is undefined, else false.

Returns true:

$b.isUndefined(void 0)

Returns false:

$b.isUndefined(null)

$b.attempt(func, [[args]])

Attempts to invoke func, returning either the result or the caught error object. Any additional arguments are provided to func when it’s invoked.

Arguments

  • func (Function): The function to attempt.
  • [args] (...*): The arguments to invoke func with.

Returns

  • (*): Returns the func result or error object.

Executes function if found:

window.myFunc1 = function (arg1, arg2) { window.alert(arg2) } $b.attempt(window.myFunc1, undefined, 'Hello World')

Doesn’t execute function if it’s absent:

$b.attempt(window.myFunc2, undefined, 'Hello World')

$b.debounce(func, [wait=0], [options={}])

Creates a debounced function that delays invoking func until after wait milliseconds have elapsed since the last time the debounced function was invoked. The debounced function comes with a cancel method to cancel delayed func invocations and a flush method to immediately invoke them. Provide options to indicate whether func should be invoked on the leading and/or trailing edge of the wait timeout. The func is invoked with the last arguments provided to the debounced function. Subsequent calls to the debounced function return the result of the last func invocation.

Note: If leading and trailing options are true, func is invoked on the trailing edge of the timeout only if the debounced function is invoked more than once during the wait timeout.

If wait is 0 and leading is false, func invocation is deferred until to the next tick, similar to setTimeout with a timeout of 0.

Arguments

  • func (Function): The function to debounce.
  • [wait=0] (number): The number of milliseconds to delay.
  • [options={}] (Object): The options object.
  • [options.leading=false] (boolean): Specify invoking on the leading edge of the timeout.
  • [options.maxWait] (number): The maximum time func is allowed to be delayed before it’s invoked.
  • [options.trailing=true] (boolean): Specify invoking on the trailing edge of the timeout.

Returns

  • (Function): Returns the new debounced function.

Example

// Avoid costly calculations while the window size is in flux. window.onresize = $b.debounce(calculateLayout, 150) // Invoke `sendMail` when clicked, debouncing subsequent calls. document.getElementById('my-el').addEventListener('click', $b.debounce( sendMail, 300, { 'leading': true, 'trailing': false } ))

$b.throttle(func, [wait=0], [options={}])

Creates a throttled function that only invokes func at most once per every wait milliseconds. The throttled function comes with a cancel method to cancel delayed func invocations and a flush method to immediately invoke them. Provide options to indicate whether func should be invoked on the leading and/or trailing edge of the wait timeout. The func is invoked with the last arguments provided to the throttled function. Subsequent calls to the throttled function return the result of the last func invocation.

Note: If leading and trailing options are true, func is invoked on the trailing edge of the timeout only if the throttled function is invoked more than once during the wait timeout.

If wait is 0 and leading is false, func invocation is deferred until to the next tick, similar to setTimeout with a timeout of 0.

Arguments

  • func (Function): The function to throttle.
  • [wait=0] (number): The number of milliseconds to throttle invocations to.
  • [options={}] (Object): The options object.
  • [options.leading=true] (boolean): Specify invoking on the leading edge of the timeout.
  • [options.trailing=true] (boolean): Specify invoking on the trailing edge of the timeout.

Returns

  • (Function): Returns the new throttled function.

Example

// Avoid costly calculations while the window size is in flux. window.onresize = $b.throttle(calculateLayout, 150) // Invoke `sendMail` when clicked, debouncing subsequent calls. document.getElementById('my-el').addEventListener('click', $b.throttle( sendMail, 300, { 'leading': true, 'trailing': false } ));