Skip to content

Instantly share code, notes, and snippets.

View TorbjornHoltmon's full-sized avatar
🐻

Torbjørn Holtmon TorbjornHoltmon

🐻
View GitHub Profile
@TorbjornHoltmon
TorbjornHoltmon / getWeekNumberFromISOString.ts
Last active February 22, 2023 09:15
Get week number from ISO string typescript
export function getWeekNumberFromISOString(date: string): number {
const newYear = new Date(new Date(date).getFullYear(), 0, 1);
let day = newYear.getDay() - 1; // the day of week the year begins on
day = day >= 0 ? day : day + 7;
const dayNumber: number =
Math.floor(
(new Date(date).getTime() -
newYear.getTime() -
(new Date(date).getTimezoneOffset() - newYear.getTimezoneOffset()) * 60000) /
86400000
@TorbjornHoltmon
TorbjornHoltmon / awaitableTimout.ts
Last active September 1, 2022 13:47
Awaitable timeout
async function voidWait(timeToDelay: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, timeToDelay));
}
async function returnWait<T>(timeToDelay: number, returnValue: T): Promise<T> {
return new Promise((resolve) => setTimeout(() => resolve(returnValue), timeToDelay));
}
@TorbjornHoltmon
TorbjornHoltmon / PartialBy.ts
Last active January 13, 2023 13:13
PartialBy, make properties in typescript type optional
/**
* Construct a type with the properties of T and make the properties K optional.
*/
type PartialBy<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
// Example:
type Person = {
name: string;
age: number;
gender: "male" | "female";
@TorbjornHoltmon
TorbjornHoltmon / predicate.ts
Created February 11, 2022 12:25
Type predicates
export function assertIAsset<T>(item: IAsset | IReference): item is IAsset {
return item.type === "typenavn her";
}
@TorbjornHoltmon
TorbjornHoltmon / Wow.tsx
Last active February 22, 2023 09:14
vue jsx test
import { defineComponent, ref, watch } from "vue";
const Wow = defineComponent({
props: {
limit: {
type: Number,
required: false,
default: 12,
},
},
@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" });
};
@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 / 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 },
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 / 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" });