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 / 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 / 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 / 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 / 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 / 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 / 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 / es6-generator-yield.js
Created August 12, 2015 05:13
try out ES2015 Generator - yield vs. yield* (delegation)
// `yield*`: yield delegation
function* gen1() {
yield gen2();
yield* gen2();
yield [3, 4];
yield* [3, 4];
}
function* gen2() {
yield 1;
@royling
royling / 1_iterable.js
Last active August 29, 2015 14:27
Implement an iterable and make the iterators also iterable which leads to continuable iteration
class YourConstruct {
constructor() {}
// implement iterable protocol
[Symbol.iterator]() {
// return an iterator
return {
// the iterator is iterable!
[Symbol.iterator]() {
return this;
function superhero(clazz) {
Object.defineProperty(clazz.prototype, 'isSuperHero', {value: true});
return clazz;
}
@superhero
class MySuperHero {
constructor(first, last) {
this.name = `${first} ${last}`;
}
@royling
royling / Minion in Pure CSS.markdown
Created September 6, 2015 01:30
Minion in Pure CSS

Minion in Pure CSS

CSS minion from Despicable Me recreated in pure CSS. This was created with only CSS3 shapes using pseudo classes and box-shadows.

A Pen by Rachel on CodePen.

License.