Skip to content

Instantly share code, notes, and snippets.

View TorbjornHoltmon's full-sized avatar
🐻

Torbjørn Holtmon TorbjornHoltmon

🐻
View GitHub Profile
const W3CEmailRegExp = /^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/;
@TorbjornHoltmon
TorbjornHoltmon / pretty.ts
Created February 22, 2023 09:14
pretty type
type Prettify<T> = {
[K in keyof T]: T[K];
} & {};
@TorbjornHoltmon
TorbjornHoltmon / parse-url.js
Last active January 27, 2023 14:50
Quickly parse url in console
const urlToTest = new URL("AddUrl");
let query = {};
for ([key, value] of urlToTest.searchParams) {
query[key] = value;
}
console.log({
host: urlToTest.host,
query: query,
path: urlToTest.pathname,
@TorbjornHoltmon
TorbjornHoltmon / json.ts
Created January 13, 2023 13:11
JSON type
type JsonPrimitives = string | number | boolean | String | Number | Boolean | null;
type NonJsonPrimitives = undefined | Function | symbol;
type SerializeType<T> = T extends JsonPrimitives
? T
: T extends NonJsonPrimitives
? never
: T extends { toJSON(): infer U }
? U
: T extends []
let startTime = Date.now();
const prestand = () => {
const result = Date.now() - startTime;
startTime = Date.now();
return `${result} ms`;
};
let startTime = performance.now();
const prestand2 = () => {
const result = performance.now() - startTime;
@TorbjornHoltmon
TorbjornHoltmon / async-streaming.ts
Last active June 4, 2024 10:44
Streaming data with async generators
async function returnWait<T>(timeToDelay: number, returnValue: T): Promise<T> {
return new Promise((resolve) => setTimeout(() => resolve(returnValue), timeToDelay));
}
const encoder = new TextEncoder();
async function* iterator() {
let index = 0;
if (index === 0) {
const thing = await returnWait(1000, { hello: "first" });
function createIsomorphicDestructible(obj, arr) {
const clone = { ...obj, map: arr.map };
Object.defineProperty(clone, Symbol.iterator, {
enumerable: false,
value() {
let index = 0;
return {
next: () => ({
value: arr[index++],
@TorbjornHoltmon
TorbjornHoltmon / ForEachKeyValue.ts
Last active June 29, 2022 08:48
for each key value loop
const testObject = { a: 1, b: 2, c: 3, d: 4, e: 5 };
// Object.entries({}) lets you loop through the properties of the object with access to the key and value of each entry
for (const [key, value] of Object.entries(testObject)) {
console.log(key, value); // a, 1. b, 2... etc...
}
const customArray = [
{ key: "a", value: 1 },
{ key: "a", value: 1 },
@TorbjornHoltmon
TorbjornHoltmon / export-demo.ts
Last active April 26, 2022 06:35
export default
// Export everything from this file
export * from "C:/Prosjekter/DEMO2/packages/shared/src/index";
// Export the default export from this file
export { default } from "C:/Prosjekter/DEMO2/packages/shared/src/index";
// Export the default export from this file as a named export
export { default as Named } from "C:/Prosjekter/DEMO2/packages/shared/src/index";
@TorbjornHoltmon
TorbjornHoltmon / asyncCallback.ts
Last active July 26, 2023 13:08
Async callback
export async function CallBackTest<CallBackReturnValue>(callback: (asdasd: number) => Promise<CallBackReturnValue>) {
const result = await callback(123);
return { ...result, done: "YES!" };
}
const callbackFunction = async (theNumber) => {
const anv = theNumber;
return returnWait(1000, { Hello: "worlaaaaad" });
};