This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const requestAnimationFrameRecursive = ( | |
fn: ( | |
highResTimestamp: DOMHighResTimeStamp, | |
additionalInfo: { | |
stop: () => void; | |
totalTimelapse: DOMHighResTimeStamp; | |
timelapseSinceLast: DOMHighResTimeStamp; | |
}, | |
) => void, | |
o?: { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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"> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://github.com/ycmjason/svg-to-img | |
const getImageDataURL = (image, type = 'png') => { | |
// pre: image is loaded | |
if (type === 'jpg') type = 'jpeg'; | |
const { width, height } = image; | |
const canvas = document.createElement('canvas'); | |
const context = canvas.getContext('2d'); | |
canvas.width = width; | |
canvas.height = height; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const MongoClient = require('mongodb').MongoClient; | |
const DB_NAME = 'mydb'; | |
const MONGO_URL = process.env.MONGO_URL; | |
const dbPromise = MongoClient.connect( | |
MONGO_URL, | |
{ useNewUrlParser: true }, | |
).then(client => client.db(DB_NAME)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const MongoClient = require('mongodb').MongoClient; | |
const DB_NAME = 'mydb'; | |
const MONGO_URL = process.env.MONGO_URL; | |
const dbPromise = MongoClient.connect( | |
MONGO_URL, | |
{ useNewUrlParser: true }, | |
).then(client => client.db(DB_NAME)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const crushOnce = (xs) => { | |
if (xs.length < 3) return xs; | |
let count = 0; | |
for (const x of xs) { | |
if (x !== xs[0]) break; | |
count++; | |
} | |
if (count >= 3) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const asyncStringReplace = async (str, regex, aReplacer) => { | |
regex = new RegExp(regex, regex.flags + regex.flags.includes('g')? '': 'g'); | |
const replacedParts = []; | |
let match; | |
let i = 0; | |
while ((match = regex.exec(str)) !== null) { | |
// put non matching string | |
replacedParts.push(str.slice(i, match.index)); | |
// call the async replacer function with the matched array spreaded | |
replacedParts.push(aReplacer(...match)); |