Skip to content

Instantly share code, notes, and snippets.

View olegchursin's full-sized avatar
🐬
Common sense isn't very common.

Oleg Chursin olegchursin

🐬
Common sense isn't very common.
View GitHub Profile
@olegchursin
olegchursin / stringToId.ts
Created October 11, 2022 03:43
Convert a random string to camelized stringId
function removeSpecialChars(str: string): string {
return str.replace(/[^0-9a-zA-Z]/g, '');
}
function camelize(str: string): string {
return str
.replace(/(?:^\w|[A-Z]|\b\w)/g, function (word, index) {
return index === 0 ? word.toLowerCase() : word.toUpperCase();
})
.replace(/\s+/g, '');
@olegchursin
olegchursin / objectUtils.ts
Created July 26, 2022 16:47
Object utils
type ScoreObj = { [key: string]: number };
// Since we are sure that "scoreObj" is always of type {[key: string]: number;}
// we can use our own typing for Object.entries().
// It is still using a type assertion ("as any") which is not ideal,
// but gives us the right typings in subsequnt methods
type Entries<T> = { [K in keyof T]: [K, T[K]] }[keyof T];
function objectEntries<T extends object>(t: T): Entries<T>[] {
return Object.entries(t) as any;
}
@olegchursin
olegchursin / datetimeDefaultValue.ts
Created July 10, 2022 01:23
Get datetime default value for input field.
export const getDatetimeDefaultValue = (timestamp: Date | string): string => {
let ts = new Date();
if (timestamp) {
ts = new Date(timestamp);
}
ts.setMinutes(ts.getMinutes() - ts.getTimezoneOffset());
ts.setMilliseconds(null);
ts.setSeconds(null);
return ts.toISOString().slice(0, -1);
};
@olegchursin
olegchursin / useEffect.ts
Last active June 22, 2022 20:19
Use useEffect right
// Fire useEffect only once on mount
cosnt executedRef = useRef(false);
useEffect(() => {
if(executedRef.current) {
return;
}
// doSomething();
@olegchursin
olegchursin / hasSubstring.ts
Created June 17, 2022 14:44
Match multiple substrings RegEx
function matcMultiple(subject: string, strings: string[]) {
const regexMetachars = /[(){[*+?.\\^$|]/g;
for (let i = 0; i < strings.length; i++) {
strings[i] = strings[i].replace(regexMetachars, '\\$&');
}
const regex = new RegExp('\\b(?:' + strings.join('|') + ')\\b', 'gi');
return subject.match(regex) || [];
@olegchursin
olegchursin / night-owl-slack-theme.txt
Created January 4, 2022 21:02
NightOwl Slack theme
#001627,#01151A,#1D3B53,#D5DDEA,#01121F,#D5DDEA,#77CEBF,#F0546F,#001627,#D5DDEA
@olegchursin
olegchursin / datetime-format.ts
Last active December 16, 2021 16:55
Native Datetime Formatter
// define formatter
const locales: string | string[] = 'en-US';
const options: Intl.DateTimeFormatOptions = {
year: 'numeric',
month: 'short',
day: 'numeric',
hour: 'numeric',
minute: '2-digit'
};
const formatter: Intl.DateTimeFormat = new Intl.DateTimeFormat(
@olegchursin
olegchursin / oc.json
Created December 10, 2021 08:05
iTerm2 Profile
{
"Right Option Key Sends" : 0,
"Tags" : [
],
"Ansi 12 Color" : {
"Red Component" : 0.50980395078659058,
"Color Space" : "sRGB",
"Blue Component" : 1,
"Alpha Component" : 1,
@olegchursin
olegchursin / modern-css-reset.css
Created December 6, 2021 21:11
Modern CSS reset (Josh Comeau's)
/*
Josh Comeau's Modern CSS Reset
https://www.joshwcomeau.com/css/custom-css-reset/
*/
*, *::before, *::after {
box-sizing: border-box;
}
* {
margin: 0;
}
{
"arrowParens": "avoid",
"bracketSpacing": true,
"singleQuote": true,
"tabWidth": 2,
"trailingComma": "none"
}