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 / tempCSS.js
Created Sep 4, 2016
Temporarily replace a css property of an element and restore that after fn is called.
View tempCSS.js
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 Sep 8, 2016
Return a list of colors([r, g, b]) that transit from/to all colors(hex) in the list.
View demo.js
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 / SplitNumber.js
Last active Sep 11, 2016
Split a number into parts that sums back to its own
View SplitNumber.js
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 / ArrayUtils.js
Last active Sep 11, 2016
Swap, insertBefore and insertAfter
View ArrayUtils.js
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 / 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);
}
}
@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 / 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 / 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 / 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--;
You can’t perform that action at this time.