Skip to content

Instantly share code, notes, and snippets.

View ycmjason's full-sized avatar
😆

YCM Jason ycmjason

😆
View GitHub Profile
@ycmjason
ycmjason / AppLink.vue
Last active April 4, 2023 22:22
A totally over-engineered way to make a """type-safe""" `AppLink` with vue-router
@ycmjason
ycmjason / groupBy.ts
Created March 23, 2023 23:23
A beautiful implementation of groupBy in typescript
export const groupBy = <X, K extends PropertyKey>(
getKey: (x: X) => K,
xs: readonly X[],
): { readonly [k in K]?: readonly (X | undefined)[] } => {
const grouped: { [k in K]?: (X | undefined)[] } = {};
for (const x of xs) {
const key = getKey(x);
grouped[key] ??= [];
// we have initialised ^ so grouped[key] won't be undefined
@ycmjason
ycmjason / machine.js
Created January 19, 2023 12:09
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
@ycmjason
ycmjason / removeKeyRecursive.ts
Created November 26, 2020 17:11
removeKeyRecursive.ts
type OmitRecursive<T extends object, O extends string> = Omit<
{ [K in keyof T]: T[K] extends object ? OmitRecursive<T[K], O> : T[K] },
O
>
const removeKeysRecursive = <T extends object, K extends readonly string[]>(
object: T,
keys: K,
): OmitRecursive<T, K[number]> => {
@ycmjason
ycmjason / BASIC_CONFIG.md
Last active October 1, 2019 10:57
Basic Configs

This file contains some config files.

export const requestAnimationFrameRecursive = (
fn: (
highResTimestamp: DOMHighResTimeStamp,
additionalInfo: {
stop: () => void;
totalTimelapse: DOMHighResTimeStamp;
timelapseSinceLast: DOMHighResTimeStamp;
},
) => void,
o?: {
<div ref="container">
<Card v-for="(card, i) of cards" ref="cards">
<IntersectionObserver :getTarget="() => this.$refs.cards[i]" @intersect="liftCard" @disintersect="unliftCard" />
...
</Card>
</div>
<!-- vs -->
<div ref="container">
@ycmjason
ycmjason / merge_sort.js
Last active April 24, 2019 15:32
Merge sort
const mergeSort = (xs) => {
if (xs.length <= 1) return xs;
const midIndex = Math.trunc(xs.length / 2);
return merge(
mergeSort(xs.slice(0, midIndex)),
mergeSort(xs.slice(midIndex)),
);
}
const merge = (xs, ys) => {
@ycmjason
ycmjason / retryAndTImeout.js
Last active March 20, 2019 15:32
Retry async functions with timeout
const retryAsyncWithTimeout = (fns, ms) => new Promise((res, rej) => {
let hasTimeout = false;
const retryAsyncUntilSucceed = fn => new Promise((res, rej) => {
fn().then(res).catch(() => {
if (!hasTimeout) {
retryAsyncUntilSucceed(fn)
}
});
});
@ycmjason
ycmjason / flatMap.js
Created January 16, 2019 13:07
Array.prototype.flatMap ponyfill
const flatMap = (xs, fn, thisArg) => {
if (typeof Array.prototype.flatMap === 'function') {
return Array.prototype.flatMap.call(xs, fn, thisArg);
}
const bFn = fn.bind(thisArg);
return xs.reduce((acc, x) => {
const r = bFn(x);
if (Array.isArray(r)) return [...acc, ...r];
return [...acc, r];