Skip to content

Instantly share code, notes, and snippets.

View ycmjason's full-sized avatar
😆

YCM Jason ycmjason

😆
View GitHub Profile
@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 / 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] };
})));
// 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 December 15, 2017 20:59
Transform a function into the same function but with limited async calls at the same time.
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 December 12, 2017 13:45
Queuing the promise!
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 June 28, 2017 16:59
Haskell's foldl1 equivalent in Javascript
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 August 2, 2020 16:01
Gitbook automatic generation of SUMMARY.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 19:56
Ensure multiple asynchronous call finishes before executing the callback
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);
}
}
@ycmjason
ycmjason / SplitNumber.js
Last active September 11, 2016 23:55
Split a number into parts that sums back to its own
function splitNumber(n, m_parts){
var arr = [];
var eachPart = Math.ceil(n / m_parts);
for(var i = 0; i < m_parts; i++){
arr.push(eachPart);
}
var diff = (eachPart * m_parts) - n;
for(var i = 0; i < diff; i++){
arr[i%arr.length]--;
}