This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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)); |
This file contains hidden or 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
// 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); | |
} |
This file contains hidden or 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 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] }; | |
}))); |
This file contains hidden or 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
// Simple memoization, done | |
const memoize = (fn) => { | |
const memo = {}; | |
return function(...args){ | |
const hash = JSON.stringify(args); | |
if(hash in memo) return memo[hash]; | |
return fn(...args); | |
}; | |
}; |
This file contains hidden or 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 limitAsyncCalls = (fn, n) => { | |
const cap = n; | |
let executing_count = 0; | |
let waitings = []; | |
const wait = () => new Promise(res => waitings.push(res)); | |
const execute = async (fn, args) => { | |
executing_count++; | |
const result = await fn(...args); | |
executing_count--; |
This file contains hidden or 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 queue = (() => { | |
const promises = {}; | |
return async (name, f) => { | |
while(promises[name]) await promises[name]; | |
promises[name] = f(); | |
const res = await promises[name]; | |
promises[name] = undefined; | |
return res; | |
}; | |
})(); |