Skip to content

Instantly share code, notes, and snippets.

View michael-lynch's full-sized avatar
💭
I may be slow to respond.

Michael Lynch michael-lynch

💭
I may be slow to respond.
View GitHub Profile
// react
import { useCallback, useEffect, useRef } from 'react';
// props
export interface UseIdleProps {
events?: string[];
onIdle: () => void;
onPrompt?: () => void;
promptTimeout?: number;
timeout: number;
// react
import { useCallback, useEffect, useRef, useState } from 'react';
// props
export interface UseFetchParams {
isEnabled?: boolean;
isLazy?: boolean;
onComplete?: () => void;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
onError?: (error: any) => void;
@michael-lynch
michael-lynch / typeCheck.js
Last active September 12, 2022 21:16
Validate type of input
export const isDate = date => {
const dateTest = new Date(date);
return (
dateTest && Object.prototype.toString.call(dateTest) === '[object Date]' && !isNaN(dateTest)
);
};
export const isNumber = value => {
return typeof value === 'number' && !Number.isNaN(value);
};
@michael-lynch
michael-lynch / merge.js
Created September 12, 2022 21:12
Deep merge objects
export const merge = (current, updates) => {
for (const key of Object.keys(updates)) {
if (!current.hasOwnProperty(key) || typeof updates[key] !== 'object') {
current[key] = updates[key];
} else {
merge(current[key], updates[key]);
}
}
return current;
};
@michael-lynch
michael-lynch / capitalize.js
Created September 12, 2022 21:10
Capitalize each word in string
export const capitalize = sentence => {
const words = sentence.split(' ');
for (let i = 0; i < words.length; i++) {
if (words[i]) {
words[i] = words[i][0].toUpperCase() + words[i].substr(1).toLowerCase();
}
}
return words.join(' ');
};
@michael-lynch
michael-lynch / toCamelCase.js
Created September 12, 2022 21:09
Convert string to camelCase
export const toCamelCase = string => {
if (!string) {
return '';
}
let newString = string.toLowerCase();
newString = newString.replace(/ /g, '_').toLowerCase();
newString = newString.replace(/_([a-z])/g, g => {
return g[1].toUpperCase();
@michael-lynch
michael-lynch / cookie.js
Created September 12, 2022 21:05
Set, get and delete cookie
@michael-lynch
michael-lynch / toKebabCase.js
Created September 12, 2022 21:03
Convert string to kebab-case
// https://www.w3resource.com/javascript-exercises/fundamental/javascript-fundamental-exercise-123.php
export const toKebabCase = string => {
if (!string) {
return '';
}
const newString = string
.match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g)
.map(x => x.toLowerCase())
.join('-');
@michael-lynch
michael-lynch / jsonToCsv.js
Created September 12, 2022 21:02
JSON to CSV
// https://stackoverflow.com/a/31536517/2262604
export const jsonToCsv = json => {
const replacer = (key, value) => (value === null ? '' : value);
const header = Object.keys(json[0]);
const csv = [
header.join(','),
...json.map(row => header.map(fieldName => JSON.stringify(row[fieldName], replacer)).join(',')),
].join('\r\n');
return csv;
};
@michael-lynch
michael-lynch / downloadBlob.js
Created September 12, 2022 21:01
Download Blob
// https://stackoverflow.com/a/68146412/2262604
export const downloadBlob = ({ content, filename, contentType }) => {
// create blob
const blob = new Blob([content], {
type: contentType || 'text/csv;charset=utf-8;',
});
const url = URL.createObjectURL(blob);
// create link to download it
const anchor = document.createElement('a');