Skip to content

Instantly share code, notes, and snippets.

View shawndumas's full-sized avatar

Shawn Dumas shawndumas

View GitHub Profile
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 February 21, 2024 11:53
Generator as a Method
class Range {
constructor(start, stop=start) {
if (start === undefined && stop === undefined) {
throw 'usage: range(start, stop) or range(stop)';
}
if (stop === start) {
start = 0;
}
@shawndumas
shawndumas / taggedTemplate.js
Last active February 21, 2024 11:53
Tagged Template
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 February 21, 2024 11:53
Logic Gates (es6 version)
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));
},
@shawndumas
shawndumas / async-await.js
Last active February 21, 2024 11:53
async await
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 21:38
EmberJS -- make all actions clickable
[data-ember-action] {
cursor: pointer;
}
@shawndumas
shawndumas / bind-data-attributes.js
Last active February 21, 2024 11:53
EmberJS -- bind attributes beginning with 'data-'
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));
}
});
@shawndumas
shawndumas / yieldStar.js
Created March 18, 2015 20:31
Yield* Example
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 February 21, 2024 11:54
Yield Example (A.K.A Will You Please Stop!?)
function* fromOneTo (max) {
var i = 0;
while (max--) {
yield ++i;
}
};
for (var i of fromOneTo(100)) {
var r = '';
@shawndumas
shawndumas / findNodesBy.js
Created January 7, 2015 23:00
findNodesBy.js
/* globals module */
'using strict';
module.exports = function (options) {
if (arguments.length === 2) {
options = {
body: arguments[0],
match: arguments[1]
};