Skip to content

Instantly share code, notes, and snippets.

🎯
Focusing

Jason Yu ycmjason

🎯
Focusing
Block or report user

Report or block ycmjason

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View recursiveRequestAnimationFrame.js
const recursiveRequestAnimationFrame = (fn) => {
let stop = () => {};
const id = window.requestAnimationFrame(() => {
fn();
stop = recursiveRequestAnimationFrame(fn)
})
return () => {
window.cancelAnimationFrame(id)
View a.html
<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">
View merge_sort.js
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 Mar 20, 2019
Retry async functions with timeout
View retryAndTImeout.js
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 Jan 16, 2019
Array.prototype.flatMap ponyfill
View flatMap.js
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];
@ycmjason
ycmjason / conver-svg.js
Last active Jun 29, 2018
A more modern way to convert svg to png/jpeg and obtain the data uri.
View conver-svg.js
// 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;
@ycmjason
ycmjason / db.js
Created Jun 27, 2018
A neat way to interact with mongoclient, without caring too many proxies.
View db.js
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));
@ycmjason
ycmjason / db.js
Created Jun 27, 2018
A neat way to interact with mongoclient, without caring too many proxies.
View db.js
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));
View CandyCrushish.js
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) {
@ycmjason
ycmjason / asyncStringReplace.js
Created Apr 24, 2018
Async version of `string.prototype.replace`
View asyncStringReplace.js
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));
You can’t perform that action at this time.