From 55ff7c66a364cdcbc45ed0f695994e0be441d879 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sascha=20K=C3=BCnstler?= Date: Tue, 20 Feb 2024 09:45:37 +0100 Subject: [PATCH] Provide `event` utils --- README.md | 18 ++++++++++++++++++ index.js | 1 + src/event.d.ts | 2 ++ src/event.js | 31 +++++++++++++++++++++++++++++++ src/filter.js | 46 +++++++++++++++++++++++----------------------- 5 files changed, 75 insertions(+), 23 deletions(-) create mode 100644 src/event.d.ts create mode 100644 src/event.js diff --git a/README.md b/README.md index 7887302..273413b 100644 --- a/README.md +++ b/README.md @@ -234,6 +234,24 @@ Get the number of days in a month. Format a date as YYYY-MM-DD or a custom format. +### Event + +#### `debounce(fn, delay)` + +Debounce function to prevent multiple calls in a short period of time. + +```javascript +import { debounce } from '@schascha/brabbelback'; +``` + +#### `throttled(fn, delay)` + +Throttle function to reduce the trigger rate. + +```javascript +import { throttled } from '@schascha/brabbelback'; +``` + ### Filter #### `compare(a, b, operator = '')` diff --git a/index.js b/index.js index ffcbd03..aa6399a 100644 --- a/index.js +++ b/index.js @@ -3,6 +3,7 @@ export * from './src/classnames'; export * from './src/cookie'; export * from './src/copy'; export * from './src/date'; +export * from './src/event'; export * from './src/filter'; export * from './src/scroll'; export * from './src/text'; diff --git a/src/event.d.ts b/src/event.d.ts new file mode 100644 index 0000000..334d3f1 --- /dev/null +++ b/src/event.d.ts @@ -0,0 +1,2 @@ +export declare function debounce(fn: Function, delay: number): Function; +export declare function throttled(fn: Function, delay: number): Function; diff --git a/src/event.js b/src/event.js new file mode 100644 index 0000000..72e35de --- /dev/null +++ b/src/event.js @@ -0,0 +1,31 @@ +/** + * Debounce function to prevent multiple calls in a short period of time + * @param {Function} fn - The function to debounce + * @param {number} delay - The delay in milliseconds + * @returns {Function} The debounced function + */ +export function debounce(fn, delay) { + let timer = 0; + return (...args) => { + window.clearTimeout(timer); + timer = setTimeout(() => fn(...args), delay); + }; +} + +/** + * Throttle function to reduce the trigger rate + * @param {Function} fn - The function to throttle + * @param {number} delay - The delay in milliseconds + * @returns {Function} The throttled function + */ +export function throttled(fn, delay) { + let timer = 0; + return (...args) => { + const now = (new Date).getTime(); + if (now - timer < delay) { + return; + } + timer = now; + return fn(...args); + }; +} diff --git a/src/filter.js b/src/filter.js index b0f1c64..d07543f 100644 --- a/src/filter.js +++ b/src/filter.js @@ -1,11 +1,11 @@ import { has } from './array'; /** - * Compare a with b. - * @param {String|String[]} a - * @param {String|String[]} b - * @param {String} [operator=''] - * @return {*} + * Compare a with b using the given operator + * @param {String|String[]} a - The value or array of values to compare + * @param {String|String[]} b - The value or array of values to compare with + * @param {String} operator - The operator to use, default is '' + * @return {Boolean} Returns true if the comparison is true, otherwise false */ export function compare(a, b, operator = '') { if (!a || !b) { @@ -29,32 +29,32 @@ export function compare(a, b, operator = '') { } /** - * Filters an array of objects with multiple criteria. - * @param {Object[]} obj - * @param {{field: string, value: any, operator: string}[]} filters - * @return {Object} + * Filters an array of objects with multiple criteria + * @param {Object[]} obj - The array of objects to filter + * @param {{field: string, value: any, operator: string}[]} filters - The filters to apply + * @return {Object} The filtered array */ export function filter(obj, filters) { return filters && filters.length ? obj.filter((el) => { - return filters.every((filter) => { - if (Array.isArray(filter.field)) { - return filter.field.find((item) => - compare(el[item], filter.value, filter.operator) - ); - } else { - return compare(el[filter.field], filter.value, filter.operator); - } - }); - }) + return filters.every((filter) => { + if (Array.isArray(filter.field)) { + return filter.field.find((item) => + compare(el[item], filter.value, filter.operator) + ); + } else { + return compare(el[filter.field], filter.value, filter.operator); + } + }); + }) : obj; } /** - * Group array items by key. - * @param {Object[]} array An array with objects to query. - * @param {String} key The key to group the array items. - * @return {Object} Returns a object with grouped arrays by the given key. + * Group array items by key + * @param {Object[]} array - The array with objects to group + * @param {String} key - The key to group by + * @return {Object} The grouped array */ export function groupBy(array, key) { return array.reduce((rv, x) => {