Skip to content

Instantly share code, notes, and snippets.

View ycmjason's full-sized avatar
😆

YCM Jason ycmjason

😆
View GitHub Profile
@ycmjason
ycmjason / tempCSS.js
Created September 4, 2016 21:40
Temporarily replace a css property of an element and restore that after fn is called.
function tempCSS(element, stylename, temp_value, fn){
// Temporarily replace a css property and restore it after fn();
var ret;
var original_value = element.css(stylename);
element.css(stylename, temp_value);
ret = fn();
element.css(stylename, original_value);
return ret;
};
@ycmjason
ycmjason / demo.js
Last active September 8, 2016 16:57
Return a list of colors([r, g, b]) that transit from/to all colors(hex) in the list.
spectrum = getSpectrum(20, ['#2E7D32', '#ffee58', '#B71C1C']);
console.log(spectrum);
// [[46,125,50],[67,136,54],[88,148,58],[109,159,61],[130,170,65],[151,182,69],[171,193,73],[192,204,77],[213,215,80],[234,227,84],[255,238,88],[248,217,82],[241,196,76],[233,175,70],[226,154,64],[219,133,58],[212,112,52],[205,91,46],[197,70,40],[190,49,34]]
/* we can apply the color to css */
$('div').css('background-color', 'rgb(' + spectrum[3] + ')');
@ycmjason
ycmjason / ArrayUtils.js
Last active September 11, 2016 23:55
Swap, insertBefore and insertAfter
var swap = function(arr, i, j){
var temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
return arr;
};
var insertBefore = function(arr, i, j){
if(i == j || i == j - 1) return arr;
else if(i < j) return insertBefore(swap(arr, i, i+1), i+1, j);
else if(i > j) return insertBefore(swap(arr, i, i-1), i-1, j);
@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]--;
}
@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 / 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 / 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 / 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 / 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--;