Utilities Packages #62
Replies: 12 comments 13 replies
-
Description of your idea:I always have trouble creating a unique code. A simple sample code:const code = getUniqeCode('number', 5);
console.log(code); // 54321 |
Beta Was this translation helpful? Give feedback.
-
await waitFor(1000); // waitFor 1s |
Beta Was this translation helpful? Give feedback.
-
function setCookie(name: string, value: string, expire?: string): void function getCookie(cookieName: string): string |
Beta Was this translation helpful? Give feedback.
-
Debounce and Throttle https://css-tricks.com/debouncing-throttling-explained-examples/ |
Beta Was this translation helpful? Give feedback.
-
export const deepClone = <T>(data: T): T =>
{
return JSON.parse(JSON.stringify(data)) as T;
}; |
Beta Was this translation helpful? Give feedback.
-
ایده این تابع مال خودمه و عشق منه export interface RangeConfig
{
in: [number, number];
out: [number, number];
bounded?: boolean;
}
export const getInRange = (x: number, config: RangeConfig): number =>
{
let y = (config.out[1] - config.out[0]) * (x - config.in[0]) / (config.in[1] - config.in[0]) + config.out[0];
if (config.bounded === true)
{
if (y < config.out[0]) { y = config.out[0]; }
if (y > config.out[1]) { y = config.out[1]; }
}
return y;
}; اگر ریاضیت خوب باشه |
Beta Was this translation helpful? Give feedback.
-
My 1Utils from 10 years ago!!! |
Beta Was this translation helpful? Give feedback.
-
/**
* Utility method that calls a callback whenever a media-query matches in response
* to the viewport size changing. The callback should take a boolean parameter
* (with `true` meaning the media query is matched).
*
* @example
* installMediaQueryWatcher(`(min-width: 600px)`, (matches) => {
* console.log(matches ? 'wide screen' : 'narrow sreen');
* });
*/
export const installMediaQueryWatcher = (mediaQuery: string, layoutChangedCallback: (mediaQueryMatches: boolean) => void): void =>
{
const mql = window.matchMedia(mediaQuery);
mql.addEventListener('change', (event: MediaQueryListEvent) => layoutChangedCallback(event.matches));
layoutChangedCallback(mql.matches);
}; |
Beta Was this translation helpful? Give feedback.
-
export const rand = (start: number, end: number): number =>
{
return Math.floor(Math.random() * (end - start + 1) + start);
}; |
Beta Was this translation helpful? Give feedback.
-
export const rangeArray = (start: number, end: number, step: number = 1): Array<number> =>
{
return Array.from({ length: (end - start) / step + 1 }, (_, i) => start + i * step);
}; |
Beta Was this translation helpful? Give feedback.
-
/**
* Scroll window to top
*/
export function scrollTop(): void
{
if (window.scrollY === 0) { return; }
requestAnimationFrame(() =>
{
scrollTo({
top: 0,
left: 0,
behavior: 'smooth'
});
});
} |
Beta Was this translation helpful? Give feedback.
-
https://github.com/WithIO/wasync/blob/develop/src/debounce.js |
Beta Was this translation helpful? Give feedback.
-
Please present all your ideas for this package in the following format so that managers can comment
Beta Was this translation helpful? Give feedback.
All reactions