Skip to content

Instantly share code, notes, and snippets.

Avatar

Shawn Dumas shawndumas

View GitHub Profile
View ES5 Inheritance (Working).js
var Point2D = function (x, y) {
if (x === null || x === undefined) { x = 1; }
if (y === null || y === undefined) { y = 1; }
this.x = x;
this.y = y;
};
Point2D.prototype = {
equals: function () {
@shawndumas
shawndumas / generatorMethod.js
Last active Aug 29, 2015
Generator as a Method
View generatorMethod.js
class Range {
constructor(start, stop=start) {
if (start === undefined && stop === undefined) {
throw 'usage: range(start, stop) or range(stop)';
}
if (stop === start) {
start = 0;
}
View taggedTemplate.js
const logicGates = {
nand (a, b) { return !(a && b); },
not (a) { return this.nand (a, a); },
and (a, b) { return this.not (this.nand (a, b)); },
or (a, b) { return this.nand (this.not (a), this.not(b)); },
nor (a, b) { return this.not (this.or (a, b)); },
xor (a, b) { return this.and (this.nand (a, b), this.or(a, b)); },
xnor (a, b) { return this.not (this.xor (a, b)); }
};
@shawndumas
shawndumas / logicGates.js
Last active Oct 16, 2019
Logic Gates (es6 version)
View logicGates.js
const logicGates = {
nand(a, b) {
return !(a && b);
},
not(a) {
return this.nand(a, a);
},
and(a, b) {
return this.not(this.nand(a, b));
},
View async-await.js
async function func () {
const url = 'https://maps.googleapis.com/maps/api/geocode/json?address=94043&sensor=false';
const { results: [ result ] } = await fetch(url).then((response) => response.json());
return JSON.stringify(result, null, 2);
}
func().then(console.log.bind(console));
/*
@shawndumas
shawndumas / clickable-actions.css
Created May 3, 2015
EmberJS -- make all actions clickable
View clickable-actions.css
[data-ember-action] {
cursor: pointer;
}
@shawndumas
shawndumas / bind-data-attributes.js
Last active Aug 29, 2015
EmberJS -- bind attributes beginning with 'data-'
View bind-data-attributes.js
export default Ember.View.reopen({
init() {
this._super.apply(this, arguments);
// bind attributes beginning with 'data-'
Em.keys(this)
.filter(key => key.substr(0, 5) === 'data-')
.forEach(key => this.get('attributeBindings').pushObject(key));
}
});
View yieldStar.js
function* generatorDelegate () {
var name = 'generatorDelegate';
yield `${name}: 1`;
yield `${name}: 2`;
yield `${name}: 3`;
}
function* generator () {
var name = 'generator';
yield `${name}: 1`;
@shawndumas
shawndumas / fizzBuzz.js
Last active Aug 29, 2015
Yield Example (A.K.A Will You Please Stop!?)
View fizzBuzz.js
function* fromOneTo (max) {
var i = 0;
while (max--) {
yield ++i;
}
};
for (var i of fromOneTo(100)) {
var r = '';
View findNodesBy.js
/* globals module */
'using strict';
module.exports = function (options) {
if (arguments.length === 2) {
options = {
body: arguments[0],
match: arguments[1]
};