Skip to content

Instantly share code, notes, and snippets.

View ycmjason's full-sized avatar
😆

YCM Jason ycmjason

😆
View GitHub Profile
@ycmjason
ycmjason / objectAssignDeep.js
Created January 28, 2018 22:23
Deep version of Object.assign.
const objectAssignDeep = (...objs) => objs.reduce((acc, obj) => Object.assign(acc, ...Object.keys(obj).map(key => {
if (acc[key] instanceof Object && obj[key] instanceof Object) {
return { [key]: objectAssignDeep({}, acc[key], obj[key]) };
}
return { [key]: obj[key] };
})));
@ycmjason
ycmjason / transitions.scss
Created February 3, 2018 11:57 — forked from tobiasahlin/transitions.scss
Sass multiple transitions mixin
// Usage: @include transition(width, height 0.3s ease-in-out);
// Output: -webkit-transition(width 0.2s, height 0.3s ease-in-out);
// transition(width 0.2s, height 0.3s ease-in-out);
//
// Pass in any number of transitions
@mixin transition($transitions...) {
$unfoldedTransitions: ();
@each $transition in $transitions {
$unfoldedTransitions: append($unfoldedTransitions, unfoldTransition($transition), comma);
}
@ycmjason
ycmjason / asyncStringReplace.js
Created April 24, 2018 21:47
Async version of `string.prototype.replace`
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));
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 / db.js
Created June 27, 2018 21:50
A neat way to interact with mongoclient, without caring too many proxies.
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 June 27, 2018 21:50
A neat way to interact with mongoclient, without caring too many proxies.
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 / conver-svg.js
Last active June 29, 2018 16:03
A more modern way to convert svg to png/jpeg and obtain the data uri.
// 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 / 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];
@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 / 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) => {