All files utils.js

100% Statements 14/14
100% Branches 6/6
100% Functions 7/7
100% Lines 14/14
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35      1x     6x             4x 4x 4x     5x 3x 3x   2x   5x     4x   6x 6x 6x      
/**
 * Utility functions
 */
import requestAnimationFrame from './request-animation-frame';
 
export function filterUndefined(obj) {
    return Object.keys(obj)
        .map((key) => ({ key, value: obj[key] }))
        .filter(({ value }) => typeof value !== 'undefined')
        .reduce((acc, { key, value }) => ({ ...acc, [key]: value }), {});
}
 
export function throttle(callback) {
    let requestedFrame;
    let args;
    let self;
 
    function callbackCheck() {
        if (args || self) {
            requestedFrame = requestAnimationFrame(callbackCheck);
            callback.apply(self, args);
        } else {
            requestedFrame = null;
        }
        args = self = null;
    }
 
    return function throttleProxy(...a) {
        // eslint-disable-next-line no-unused-expressions
        requestedFrame || (requestedFrame = requestAnimationFrame(callbackCheck));
        args = a;
        self = this;
    };
}