Skip to content

Commit

Permalink
Provide event utils
Browse files Browse the repository at this point in the history
  • Loading branch information
Schascha committed Feb 20, 2024
1 parent 7f55b6c commit 55ff7c6
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 23 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '')`
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
2 changes: 2 additions & 0 deletions src/event.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export declare function debounce(fn: Function, delay: number): Function;
export declare function throttled(fn: Function, delay: number): Function;
31 changes: 31 additions & 0 deletions src/event.js
Original file line number Diff line number Diff line change
@@ -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);
};
}
46 changes: 23 additions & 23 deletions src/filter.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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) => {
Expand Down

0 comments on commit 55ff7c6

Please sign in to comment.