Skip to content

Instantly share code, notes, and snippets.

View chriswrightdesign's full-sized avatar

Chris Wright chriswrightdesign

View GitHub Profile
const webpackBuilds = {
awesomeBundle: {
files: ['js/src/awesome/app.js'],
outputName: 'awesome/app.min.js', // compiles to js/compiled/awesome/app.min.js
},
}
@chriswrightdesign
chriswrightdesign / reduceObjects.js
Last active June 23, 2017 03:49
Reduce objects
const obj = {
name: 'Kuro',
species: 'dog',
id: '135',
country: 'Australia'
};
const reject = (...rejectedKeys) => obj =>
// convert the keys of the object to an array and reduce over them
@chriswrightdesign
chriswrightdesign / flip.js
Last active June 23, 2017 07:23
Flip function
// our example array to work with
const myArr = [{
name: 'bob',
id: 5,
}, {
name: 'scott',
id: 4,
}, {
name: 'matt',
@chriswrightdesign
chriswrightdesign / domReducing.js
Last active June 23, 2017 03:40
DOM reducing
const renderSample = text => {
const div = document.createElement('div');
div.className = 'my-shiny-div';
const textNode = document.createTextNode(text);
div.appendChild(textNode);
return div;
}
const titles = ['hello', 'yep', 'nope', 'maybe', 'goodbye'];
@chriswrightdesign
chriswrightdesign / promiseSequence.js
Last active June 23, 2017 03:41
Promise sequence or waterfall
const first = () => Promise.resolve('this');
const second = res => Promise.resolve(`${res} is`);
const third = res => Promise.resolve(`${res} a sequence`);
const somePromises = [
first,
second,
third
];
// Make our function useful within a mapping function:
const prefix = prefix => value => `${prefix}${value}`;
// our function takes a prefix string, then returns a function which takes value,
// which then returns a string with the prefix and value together.
const animals = ['dog', 'cat', 'bird', 'giraffe'];
@chriswrightdesign
chriswrightdesign / closureEventListener.js
Last active June 23, 2017 07:25
Closure event listener
/**
* Imagine we had an event listener that would control toggling a visibility class of an element
**/
const toggleVisibleClass = event => {
console.log(event.target.value + 'changed');
someTarget.classList.toggle('is-visible');
@chriswrightdesign
chriswrightdesign / togglevisibility.js
Last active June 23, 2017 03:42
Toggle visibility - basic event listener
// Imagine we had an event listener that would control toggling a visibility class of an element
const toggleVisibleClass = event => {
console.log(event.target.value + 'changed');
someTarget.classList.toggle('is-visible');
}
checkbox.addEventListener('change', toggleVisible, false);
// our getValue function from earlier
const getValue = property => obj => obj && obj[property];
// Lets use a pipe function to combine the two functions:
const pipe = (func1, func2) => (...args) => func2(func1(...args));
// our list of user objects
const users = [{
name: 'sarah',
@chriswrightdesign
chriswrightdesign / functionfiveways.js
Last active June 23, 2017 03:42
Function five ways
const valueEquals = value => comparison => value === comparison;
const people = ['bob', 'sarah', 'kate', 'john', 'steve', 'stu', 'sam'];
people.filter(valueEquals('sarah'));
// returns an array value where any value equals sarah
people.find(valueEquals('bob'));
// returns the first value it finds ‘bob’