Skip to content

Instantly share code, notes, and snippets.

View jasonbellamy's full-sized avatar

Jason Bellamy jasonbellamy

View GitHub Profile
@jasonbellamy
jasonbellamy / tail-with-pop.js
Last active September 3, 2016 16:25
Get the value of the last item in an array using the arrays pop method
const xs = [1, 2, 3, 4, 5];
const x = xs.pop(); //=> 5
@jasonbellamy
jasonbellamy / tail-with-index-and-length.js
Last active September 3, 2016 16:25
Get the value of the last item in an array using the arrays index and length
const xs = [1, 2, 3, 4, 5];
const x = xs[xs.length - 1]; //=> 5
const xs = [1, 2, 3, 4, 5];
const [ x ] = xs.slice(-1); //=> 5
const generator = (x, y = x) => {
return {x: `_${x}`, y};
};
var xs = {
template: [
{template: 'one', dest: 'two'},
{template: 'one', dest: 'two'}
],
copy: [
{template: 'one', dest: 'two'},
{template: 'one', dest: 'two'}
]
};
function functionFactory({ type, ...rest }) {
switch(type) {
case 'hi':
return hello(rest);
case 'bye':
return goodbye(rest);
}
};
function hello({ name }) {
@jasonbellamy
jasonbellamy / reduce-map.doge
Last active January 12, 2016 22:59
Implementation of map & reduce in dogescript - https://dogescript.com/
shh reduce
such reduce much fn xs accum index
rly index === xs.length -1
wow accum
wow reduce(fn, xs, fn(accum, xs[index + 1]), index + 1)
shh map
such map much fn xs
such _map much previous current
@jasonbellamy
jasonbellamy / function-using-bind.js
Last active October 19, 2015 16:02
An example of a function thats uses .bind instead of anonymous function wrappers.
var stateManger = function () {
var active = false;
var setState = function(state) {
active = state;
};
return {
toggle : setState.bind(this, !active),
setActive : setState.bind(this, true),
setInactive : setState.bind(this, false)
@jasonbellamy
jasonbellamy / function-using-anonymous-function-wrappers.js
Created October 14, 2015 01:10
An example of a function thats uses unneeded anonymous function wrappers.
var stateManger = function () {
var active = false;
var setState = function(state) {
active = state;
};
return {
toggle: function () {
return setState(!active);
},
@jasonbellamy
jasonbellamy / es5-arguments-imperative-sum.js
Last active October 9, 2015 00:43
Imperative sum function that converts the arguments object to an Array.
var sum = function () {
var result = 0;
var args = Array.prototype.slice.call(arguments); // convert arguments into array
args.forEach(function(number) {
result = result + number;
});
return result;
};