Skip to content

Instantly share code, notes, and snippets.

View royling's full-sized avatar
🔬
Keep practicing and learning everyday!

Roy Ling royling

🔬
Keep practicing and learning everyday!
View GitHub Profile
@royling
royling / gitr
Last active March 6, 2025 03:16
run git command in multiple repositories
#! /bin/bash
CWD=`pwd`
printUsage() {
echo "gitr - Git for multiple repositories made easy!"
echo -e "Usage: gitr <branch|status|push|pull|fetch|merge|stash|checkout [options] [arguments]> [--repos <repo>...] [--gitr-base <path>]"
}
println() {
echo "$1"
@royling
royling / interweave.js
Last active February 25, 2018 13:49
Interweave arrays
// inspired when reading https://leanpub.com/understandinges6/read#leanpub-auto-define-tags
// this is the behavior that template tags may interweave literals and subsitutions arrays
var interweave = (a, b) => {
let min = Math.min(a.length, b.length);
return Array.apply(null, Array(min)).reduce((result, value, index) => {
result.push(a[index], b[index]);
return result;
}, []).concat((a.length > min ? a : b).slice(min));
};
@royling
royling / spec.js
Last active August 29, 2015 14:08
#explain-js-type-coercion-in-js: convert to boolean
describe("toBoolean", function() {
var toBool = toBoolean;
it("should return true for all objects", function() {
expect(toBool({})).toBe(true);
expect(toBool([])).toBe(true);
});
it("should return false for falsy values", function() {
@royling
royling / isPrimitiveSpec.js
Last active August 29, 2015 14:08
#explain-js-type-coercion-in-js: EcmaScript Internal [[ToPrimitive]]
describe("isPrimitive", function() {
it("should return true for primitive values", function() {
expect(isPrimitive(0)).toBe(true);
expect(isPrimitive('')).toBe(true);
expect(isPrimitive(false)).toBe(true);
expect(isPrimitive(null)).toBe(true);
expect(isPrimitive(undefined)).toBe(true);
expect(isPrimitive(NaN)).toBe(true);
});
@royling
royling / iterate.js
Last active August 29, 2015 14:07
iterate iterator (created from generator function) in for-of/while/for loops
function* generatorFunction() {
// ...
}
let iterator = generatorFunction();
// for-of loop
for (let k of iterator) {
// ...
}
@royling
royling / connect.js
Last active August 29, 2015 14:06
use mongoose with co
var mongoose = require('mongoose');
var db;
var connect = module.exports = function () {
mongoose.connect('mongodb://localhost/events');
db = mongoose.connection;
db.on('error', function(err) {
console.error('Error', err);
});
db.once('open', function() {
@royling
royling / format.js
Last active August 29, 2015 14:04
format number to add thousand separator (e.g. `,`) in js -- inspired by http://www.mredkj.com/javascript/nfbasic.html
function format(num, fmt) {
if (isNaN(Number(num))) {
throw new TypeError('Not a number');
}
if (typeof fmt.scale == 'number') {
num = Number(num).toFixed(fmt.scale);
}
num = String(num).replace('.', fmt.outDecSep||'.');
var regexp = /(\d+)(\d{3})/;
while(regexp.test(num)) {
@royling
royling / load_template.js
Last active October 26, 2015 15:06
Use <link> to define templates and load them ondomready
$(function() {
var tplts = Object.create(null), importLinks = $('link[rel="import"]');
// load all imported templates
importLinks.each(function(idx, elem) {
$.get(elem.href, function(data) {
var view = elem.getAttribute('href');
tplts[view] = data;
if (Object.keys(tplts).length == importLinks.length) {
// all are loaded
console.info("all templates are loaded:", tplts);
@royling
royling / check_guess.coffee
Last active August 29, 2015 13:57
sudoku: check if a guess is valid or not
validGuess = (sudoku, x, y, value) ->
row = sudoku[y]
column = row[x] for row in sudoku
cube = (sudoku[~~(y/3) + i][~~(x/3) + j] for i in [0..2] for j in [0..2]).reduce (result, curr) -> result.concat(curr)
value not in [].concat row, column, cube
guess = (sudoku, x, y, value) ->
sudoku[y][x] = value if validGuess sudoku, x, y, value
# sudoku = genValidSudoku()
@royling
royling / index.html
Created May 27, 2013 13:35
An example of using seajs with jquery & jquery plugin (dataTables)
<!doctype html>
<html>
<head>
<style type="text/css">
#tbl1 {
table-layout: fixed;
width: 500px;
border-collapse: separate;
border-spacing: 0;
border: 1px solid #afafaf;