Skip to content

Instantly share code, notes, and snippets.

@shaunwallace
shaunwallace / intersectionOberserver.js
Created November 9, 2017 14:36
Simple demo using the Intersection Observer API
"use strict";
(() => {
const root = document.querySelector("#root");
const docFragment = document.createDocumentFragment();
const arraySize = 100;
const items = Array.from(Array(arraySize).keys());
const options = {
root: null,
@shaunwallace
shaunwallace / telFormatter.js
Last active August 18, 2017 05:49
Telephone formatter
// eg (123) 456-7890
formatter(s) {
// strip out all formatting that exists on the string
const tel = s.replace(/[(\-)\s]/g, '');
// if there is nothing after sanitizing or non-numeric characters
// were entered then simply return the current state
if (!tel) {
return { tel: '' };
} else if (tel.match(/[^.\d]/g)) {
return this.state;
@shaunwallace
shaunwallace / typeof.js
Created July 21, 2017 08:36
Return values when using the typeof operator
typeof 'foo'; // "string"
typeof 42; // "number
typeof {}; // "object"
typeof true; // "boolean"
typeof null; // "object"
typeof undefined; // "undefined"
typeof () => {}; // "function"
typeof []; // "object"
typeof Symbol(); // "symbol"
@shaunwallace
shaunwallace / javascript prototypes.js
Created August 13, 2015 07:54
Prototypes in Javascript
var person = { kind : ‘person’ };
var shaun = Object.create(person);
person.isPrototypeOf(shaun); // true
person.kind // 'person'
shaun.kind // 'person'
shaun.sayHello = function(){ console.log(‘Hello’) };
@shaunwallace
shaunwallace / gist:8a827599c9d41becbf80
Created November 19, 2014 14:54
JSON.stringify()
JSON.stringify({foo : true, bar : false}); // returns "{"foo":true,"bar":false}"
JSON.stringify([1, "false", false]); // returns "[1,"false",false]" note the last false is not surrounded by quotes
JSON.stringify({ a: 2 }, null, " "); // returns '{\n "a": 2\n}'
JSON.stringify({ a: 2 }, null, 4); // returns '{\n "a": 2\n}'
function replacer(k,v) {
if( typeof v === "string") return;
if( typeof v === "number") return 2*v;
return v;
JSON.parse('{"foo" : "true", "bar" : "false"}'); // returns Object { foo : true, bar : false }
// returns Object {p: 10}
JSON.parse('{"p": 5}', function (k, v) {
if(k === "") return v; // if topmost value, return it,
return v * 2; // else return v * 2.
});
@shaunwallace
shaunwallace / gist:cee3c02f54de279ddb7b
Created November 19, 2014 14:44
Array.prototype.some()
function isBigEnough(element, index, array) {
return element >= 10;
}
var passed = [2, 5, 8, 1, 4].some(isBigEnough); // passed is false
passed = [12, 5, 8, 1, 4].some(isBigEnough); // passed is true
@shaunwallace
shaunwallace / gist:fef813c6c992e458a261
Created November 19, 2014 14:42
Array.prototype.reduceRight()
// will return 15
[1,2,3,4,5].reduceRight(function(previousValue, currentValue){
return previousValue + currentValue;
});
// will return 25 since we passed in the initial value
[1,2,3,4,5].reduceRight(function(previousValue, currentValue){
return previousValue + currentValue;
}, 10);
@shaunwallace
shaunwallace / gist:b8741ed93d13f41e6e70
Created November 19, 2014 14:40
Array.prototype.reduce()
// will return 15
[1,2,3,4,5].reduce(function(previousValue, currentValue){
return previousValue + currentValue;
});
// will return 25 since we passed in the initial value
[1,2,3,4,5].reduce(function(previousValue, currentValue){
return previousValue + currentValue;
}, 10);
@shaunwallace
shaunwallace / gist:02359278e06f80e0292e
Created November 19, 2014 14:18
Array.prototype.map()
// iterating through a collection of elements returned by querySelectorAll
var elems = document.querySelectorAll('select option:checked');
var values = [].map.call(elems, function(obj) {
return obj.value;
});
// reverse a string
var str = '12345';
[].map.call(str, function(x) {
return x;