Skip to content

Instantly share code, notes, and snippets.

View henrik1's full-sized avatar
💯

Henrik Larsen Toft henrik1

💯
View GitHub Profile
@henrik1
henrik1 / typescript-generics.ts
Created June 19, 2022 20:14
Generics in typescript
const sortBy = function<T> ((a, b): boolean, list: T[]): T[] {
return ...
};
@henrik1
henrik1 / interfaces-usage.ts
Created June 19, 2022 20:03
Interfaces usage
interface User {
name: string; // allow only string
readonly email: string // email is also a string, but its immutable and cannot be changed
// functions can be specified in two ways
new: () => User;
new(): User;
// Any property not described is assumed to exists, but it's value must be either string or number
@henrik1
henrik1 / multiple-inheritance.ts
Created June 19, 2022 19:56
Typescript multiple inheritance
interface Person {
name: string
}
interface User {
id: string;
password: string;
}
interface Admin extends Person, User {
@henrik1
henrik1 / cfa-conditionals.ts
Last active June 19, 2022 19:47
CFA conditionals
// typeof, for primitives
const input = getUserInput() //= input is either string | number;
if (typeof input === 'string') {
input //= string
}
const inputLength = (typeof input === 'string')
? input.length //= string
: `${number}`.length; //= number
@henrik1
henrik1 / type-guards.ts
Last active August 16, 2022 11:19
TypeScript type guards
interface Customer {
name: string;
}
function isCustomer(partner: any): partner is Customer {
return partner instanceof Customer;
}
function signIn(user: any): string {
if (isCustomer(user)) {
@henrik1
henrik1 / CFA-assertions.ts
Created June 19, 2022 18:39
CFA-assertions
// Function that either guarantees the response is a SuccessResponse or throws and error
function assertSuccess = (obj: any): asserts obj is SuccessResponse {
if (!(obj instanceof SuccessResponse)) {
throw new Error("Something went wrong");
}
}
interface SuccessResponse {
data: Record<string, any>
}
@henrik1
henrik1 / descending.js
Created June 2, 2022 19:19
Creates a descending comparator-function given a valuating function.
const descending = (fn) => (a, b) => {
const valA = fn(b);
const valB = fn(a);
return valA < valB ? -1 : valA > valB ? 1 : 0;
}
const byPrice = descending(val => val.price);
[{ price: 300 }, { price: 100 }, { price: 200 }].sort(byPrice);
// = [{ price: 300 }, { price: 200 }, { price: 100 }]
@henrik1
henrik1 / ascending.js
Last active July 21, 2022 16:59
Creates an ascending comparator function given a valuating function
const ascending = (fn) => (a, b) => {
const valA = fn(a);
const valB = fn(b);
return valA < valB ? -1 : valA > valB ? 1 : 0;
}
const byPrice = ascending(val => val.price);
[{ price: 300 }, { price: 100 }, { price: 200 }].sort(byPrice);
// = [{ price: 100 }, { price: 200 }, { price: 300 }]
@henrik1
henrik1 / findKey.js
Last active July 21, 2022 16:59
Find the first key within an index that satisfies a given predicate.
const findKey = (predicate, index) => Object
.keys(index)
.find(key => predicate(index[key], key, index));
findKey(
car => !car.available,
{
tesla: { available: true },
ford: { available: false },
gm: { available: true }
@henrik1
henrik1 / bifurcateBy.js
Created June 2, 2022 18:54
Split the values of a given list into two lists, one containing values the predicate function evaluates to truthy, the other one containing the falsy
const bifurcateBy = (predicate, list) =>
list.reduce((acc, val, i) => (
acc[predicate(val, i) ? 0 : 1].push(val), acc),
[[], []]
);
bifurcateBy(val => val > 0, [-1, 2, -3, 4]);
// = [[2, 4], [-1, -3]]