@financial-times/o-utils
Advanced tools
Comparing version 2.1.1 to 2.2.0
# Changelog | ||
## [2.2.0](https://www.github.com/Financial-Times/origami/compare/o-utils-v2.1.1...o-utils-v2.2.0) (2023-03-31) | ||
### Features | ||
* Add new function `uidBuilder` to generate a unique id for a given component name ([7f8fbfc](https://www.github.com/Financial-Times/origami/commit/7f8fbfc20dfa62f15fb9f292dcf34ff1f4f3f502)) | ||
### [2.1.1](https://www.github.com/Financial-Times/origami/compare/o-utils-v2.1.0...o-utils-v2.1.1) (2022-01-13) | ||
@@ -4,0 +11,0 @@ |
72
main.js
/** | ||
* | ||
* Debounces function so it is only called after n milliseconds | ||
* without it not being called | ||
* | ||
* @example | ||
* Utils.debounce(myFunction() {}, 100); | ||
* | ||
* @param {Function} func - Function to be debounced | ||
* @param {number} wait - Time in miliseconds | ||
* | ||
* @returns {Function} - Debounced function | ||
*/ | ||
* | ||
* Debounces function so it is only called after n milliseconds | ||
* without it not being called | ||
* | ||
* @example | ||
* Utils.debounce(myFunction() {}, 100); | ||
* | ||
* @param {Function} func - Function to be debounced | ||
* @param {number} wait - Time in miliseconds | ||
* | ||
* @returns {Function} - Debounced function | ||
*/ | ||
function debounce(func, wait) { | ||
let timeout; | ||
return function() { | ||
return function () { | ||
const args = arguments; | ||
@@ -28,16 +28,16 @@ const later = () => { | ||
/** | ||
* | ||
* Throttle function so it is only called once every n milliseconds | ||
* | ||
* @example | ||
* Utils.throttle(myFunction() {}, 100); | ||
* | ||
* @param {Function} func - Function to be throttled | ||
* @param {number} wait - Time in miliseconds | ||
* | ||
* @returns {Function} - Throttled function | ||
*/ | ||
* | ||
* Throttle function so it is only called once every n milliseconds | ||
* | ||
* @example | ||
* Utils.throttle(myFunction() {}, 100); | ||
* | ||
* @param {Function} func - Function to be throttled | ||
* @param {number} wait - Time in miliseconds | ||
* | ||
* @returns {Function} - Throttled function | ||
*/ | ||
function throttle(func, wait) { | ||
let timeout; | ||
return function() { | ||
return function () { | ||
if (timeout) { | ||
@@ -56,5 +56,25 @@ return; | ||
/** | ||
* Generates a unique ID string by concatenating the given component name, prefix, and a random number. | ||
* | ||
* @param {string} componentName - The name of the component to be included in the ID string. | ||
* @returns {function} A function that takes a prefix string and returns a unique ID string. | ||
* | ||
* @example | ||
* | ||
* const generateId = uidBuilder('myComponent'); | ||
* const id = generateId('prefix'); | ||
* console.log(id); // 'myComponent-prefix1234567890' | ||
*/ | ||
const uidBuilder = (componentName) => prefix => { | ||
const uid = String(Math.random()).split('.')[1]; | ||
return `${componentName}-${prefix}${uid}`; | ||
}; | ||
export { | ||
debounce, | ||
throttle | ||
throttle, | ||
uidBuilder | ||
}; |
@@ -5,7 +5,10 @@ { | ||
"type": "module", | ||
"version": "2.1.1", | ||
"version": "2.2.0", | ||
"description": "Provides JS helper functions, such as throttling and debouncing", | ||
"keywords": [ | ||
"debounce", | ||
"throttle" | ||
"throttle", | ||
"uid", | ||
"unique", | ||
"id" | ||
], | ||
@@ -12,0 +15,0 @@ "homepage": "https://registry.origami.ft.com/components/o-utils", |
6223
82