Skip to content

Instantly share code, notes, and snippets.

View avilde's full-sized avatar
🚀

Andris Vilde avilde

🚀
View GitHub Profile
@avilde
avilde / capitalize-nordic.ts
Last active March 30, 2020 09:11
Capitalize text and words in text for Nordic countries.
/**
* Swedish/Finnish special characters: åäö
* Danish/Norwegian special characters: æøå
* Latin characters: a-z
* Other special characters: ç (e.g. country Curaçao)
*/
const wordRegex = /[åäöçæøa-z]+/gi;
export const capitalizeWord = (word: string): string =>
word.charAt(0).toUpperCase() + word.substring(1).toLowerCase();
@avilde
avilde / chunk-array.ts
Created November 2, 2019 16:39
Chunk an array into smaller arrays (Javascript, Typescript)
export const chunkArray = <T>(array: Array<T>, size: number) => {
const results = [];
while (array.length) {
results.push(array.splice(0, size));
}
return results;
};
@avilde
avilde / camel-to-kebab.ts
Created November 2, 2019 16:41
Convert text from camel to kebab case.
export const camelToKebabCase = (text: string) => {
return text.replace(/([a-z0-9]|(?=[A-Z]))([A-Z])/g, '$1-$2').toLowerCase();
};
@avilde
avilde / set-or-replace-favorite-icon.ts
Created November 2, 2019 16:42
Replace favorite icon for your web page in real time.
export const setOrReplaceFavoritesIcon = (icon: string = defaultFavIcon) => {
const link: HTMLLinkElement =
document.querySelector('link[rel*="icon"]') ||
document.createElement('link');
link.type = 'image/x-icon';
link.rel = 'shortcut icon';
link.href = icon;
document.getElementsByTagName('head')[0].appendChild(link);
};
@avilde
avilde / new-react-fc.json
Last active June 4, 2020 06:54
New React Typescript Functional Component Template
{
"New React FC": {
"prefix": "nfc",
"body": [
"import React from 'react';",
"import classes from './${1}.module.scss';",
"",
"interface ${1}Props {}",
"",
"const ${1} = (props: ${1}Props) => {",
@avilde
avilde / react-fc-test.json
Created June 4, 2020 06:56
Create new test file for React functional component template
{
"New Test React FC": {
"prefix": "ntfc",
"body": [
"import React from 'react';",
"import { render } from '@testing-library/react';",
"import ${1} from './${1}';",
"",
"it ('${1} renders without crashing', () => {",
"\trender(",
@avilde
avilde / BalanceState.ts
Created August 12, 2021 06:17
Mobx State Example
import { makeAutoObservable } from "mobx"
export class BalanceState {
balance = 1000
constructor() {
makeAutoObservable(this)
}
add(value: number) {
@avilde
avilde / actions.ts
Created August 12, 2021 06:24
Redux State Example
import { BalanceActionType, AddAction, WithdrawAction } from "./types"
export const add = (amount: number): AddAction => ({
type: BalanceActionType.ADD,
payload: amount,
})
export const withdraw = (amount: number): WithdrawAction => ({
type: BalanceActionType.WITHDRAW,
payload: amount,
@avilde
avilde / Developer.test.ts
Last active October 20, 2021 06:26
Simple Unit test - Developer
import { Developer } from "./Developer";
// name
test("should drink coffee", () => {
// setup
const developer = new Developer();
// action
const result = developer.drinkCoffee();
// expectation
expect(result).toContain("coffee ☕");
});
@avilde
avilde / Developer.ts
Created October 15, 2021 14:24
Simple Developer
export class Developer {
public drinkCoffee(): string {
return "Drinking a cup of coffee ☕";
}
}