Skip to content

Instantly share code, notes, and snippets.

View AitorAlejandro's full-sized avatar

Aitor Alejandro Herrera AitorAlejandro

View GitHub Profile
@AitorAlejandro
AitorAlejandro / snakeToCamel.ts
Created March 11, 2022 10:09
Converts a snake string to camel string
const snakeToCamel = (s: string): string => s.toLowerCase().replace(/(_\w)/g, (w) => w.toUpperCase().substring(1));
snakeToCamel('foo_bar'); // -> fooBar
@AitorAlejandro
AitorAlejandro / shuffleArray.ts
Created March 11, 2022 10:07
Shuffles an array of any type
const shuffleArray = <T>(arr: T[]): T[] => arr.sort(() => Math.random() - 0.5);
shuffleArray(<number[]>[1, 2, 3, 4, 5]); // -> [ 4, 5, 2, 1, 3 ]
@AitorAlejandro
AitorAlejandro / slugify.ts
Created March 11, 2022 09:40
Converts a string into a slugified string
const slugify = (str: string): string => str.toLowerCase().replace(/\s+/g, '-').replace(/[^\w-]+/g, '');
slugify('Hello World'); // -> hello-world
@AitorAlejandro
AitorAlejandro / basic-accordion.html
Created November 29, 2021 21:30
A basic html accordion
<details>
<summary>Epcot Center</summary>
<p>Epcot is a theme park at Walt Disney World Resort featuring exciting attractions, international pavilions, award-winning fireworks and seasonal special events.</p>
</details>
@AitorAlejandro
AitorAlejandro / isDenseArray.js
Created November 2, 2021 10:14
isDenseArray to check if the array doesn't have any hole inside
function isDenseArray(array) {
for (let index = 0; index < array.length; index++) {
if (!(index in array)) {
return false;
}
}
return true;
}
@AitorAlejandro
AitorAlejandro / keys.js
Created July 14, 2021 06:47
secrets keys by env config
if (process.env.NODE_ENV === "production") {
module.exports = require("./keys_prod");
} else {
module.exports = require("./keys_dev");
}
@AitorAlejandro
AitorAlejandro / UI.js
Created July 14, 2021 06:45
An object for UI interactions
const UI = {
searchFail(failure) {
if (failure === "query") {
this.showAlert("Please enter a search query", "danger");
this.resetPage();
this.clearElement(".alert");
} else if (failure === "results") {
this.showAlert("No books found", "danger");
this.resetPage();
this.clearElement(".alert");
@AitorAlejandro
AitorAlejandro / using_keyof_operator.ts
Created June 29, 2021 22:20
Using the keyof operator
interface Foo {
bar: number;
baz: string;
qux: Date;
}
type FooKey keyof Foo; // "bar" | "baz" | "qux"
const fooLookUp = (key: FooKey) => foo[key]
@AitorAlejandro
AitorAlejandro / avoidContextHell.js
Created June 29, 2021 14:50
Avoid Context Hell
ReactDOM.render(
<Provider3>
<Provider2>
<Provider1>
<App />
</Provider1>
</Provider2>
</Provider3>,
rootElement
);
@AitorAlejandro
AitorAlejandro / reactContextCustomHook.js
Created June 29, 2021 14:47
Make React Context with a Custom Hook example
import React from "react";
const UserContext = React.createContext();
function UserProvider({ children }) {
const user = { name: "Reed" };
return <UserContext.Provider value={user}>{children}</UserContext.Provider>;
}
function useUser() {