Skip to content

Instantly share code, notes, and snippets.

🤡
HAHAHAHAHHAA

Jason Yu ycmjason

🤡
HAHAHAHAHHAA
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
@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));
@ycmjason
ycmjason / transitions.scss
Created Feb 3, 2018 — forked from tobiasahlin/transitions.scss
Sass multiple transitions mixin
View transitions.scss
// 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 / objectAssignDeep.js
Created Jan 28, 2018
Deep version of Object.assign.
View objectAssignDeep.js
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] };
})));
View memoize.js
// 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);
};
};
@ycmjason
ycmjason / limitAsyncCalls.js
Last active Dec 15, 2017
Transform a function into the same function but with limited async calls at the same time.
View limitAsyncCalls.js
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--;
@ycmjason
ycmjason / queue_promises
Last active Dec 12, 2017
Queuing the promise!
View queue_promises
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;
};
})();
@ycmjason
ycmjason / foldl1.js
Created Jun 28, 2017
Haskell's foldl1 equivalent in Javascript
View foldl1.js
function foldl(arr, f){
return arr.reduce((arr, x, i) => {
if(i === 0) return [x];
return arr.concat([f(arr[i - 1], x)]);
}, []);
}
@ycmjason
ycmjason / README.md
Last active Jun 21, 2017
Gitbook automatic generation of SUMMARY.md
View README.md

Gitbook automatic generation of SUMMARY.md

This script generates the SUMMARY.md assuming the following folder structure. Paths ending with / denotes directories. ** denotes multiply layers or directories.

  • ./ - gitbook root
  • ./*/ - parts, will be generated as # Name of the directory
  • ./**/*.md - articles
  • ./*/**/ - chapters

Name operations

@ycmjason
ycmjason / wait.js
Created May 30, 2017
Ensure multiple asynchronous call finishes before executing the callback
View wait.js
function wait(n, cb){
if(n <= 0) cb();
let aggregated_result = {};
let count = 0;
return function done(res){
Object.assign(aggregated_result, res);
if(++count === n) cb(aggregated_result);
}
}
You can’t perform that action at this time.