Skip to content

Instantly share code, notes, and snippets.

View leonardreidy's full-sized avatar

Leonard M Reidy leonardreidy

View GitHub Profile
class IterableIterator {
constructor(obj){
this.index = 0
this.data = Object.keys(obj).map((key) => { return { [key]: obj[key] } })
}
next = () => {
if(this.index < this.data.length){
return { value: this.data[this.index++], done: false }
} else {
this.index = 0
// An object that respects the iterable protocol
var anIterableObject = {
[Symbol.iterator]: function(){ return something }
};
// An object that respects the iterator protocol and the iterable protocol
var anIterableIterator = {
next: function() { return {value: somevalue, done: trueorfalse },
[Symbol.iterator]: function(){ return something }
}
// @@iterator method
| Property | Value
----------------------------------------------------------------------------------------------------------------
[Symbol.iterator] | A zero arguments function that returns an object, conforming to the iterator protocol
----------------------------------------------------------------------------------------------------------------
let ob = { age: 41, name: 'Leonard', profession: 'JavaScript Developer' };
let looped = []
for(const key of Object.keys(ob)){
looped.push({ [key]: ob[key] })
}
let ob = { age: 41, name: 'Leonard', profession: 'JavaScript Developer' };
let properties = Object.keys(ob).map((key) => {
return { [key]: ob[key] }
}); // returns [ { age: 41 }, { name: 'Leonard' }, { profession: 'JavaScript Developer' } ]
let ob = { age: 41, name: 'Leonard', profession: 'JavaScript Developer' };
let obit = iteratorFactory(ob);
obit.next(); // returns { value: { age: 41 }, done: false }
obit.next(); // returns { value: { name: 'Leonard'}, done: false }
obit.next(); // returns { value: { profession: 'JavaScript Developer'}, done: false }
obit.next(); // returns { value: undefined, done: true }
for(const o of obit){
console.log(o);
} // Throws
// Uncaught TypeError: obit is not iterable
let ob = { age: 41, name: 'Leonard', profession: 'JavaScript Developer' };
let obit = iteratorFactory(ob);
obit.next(); // returns { value: { age: 41 }, done: false }
obit.next(); // returns { value: { name: 'Leonard'}, done: false }
obit.next(); // returns { value: { profession: 'JavaScript Developer'}, done: false }
obit.next(); // returns { value: undefined, done: true }
function iteratorFactory(obj) {
var nextIndex = 0;
var keys = Object.keys(obj);
var end = keys.length;
var next = function(){
return nextIndex < end ? { value: { [keys[nextIndex]]: obj[keys[nextIndex++]]}, done: false } : { value: undefined, done: true }
}
return { next: next };
}
@leonardreidy
leonardreidy / useful-rails-commands.sh
Last active September 5, 2017 16:33
Useful commands for Rails 4.2
# Asset cleaning
# Clean old assets leaving three most recent versions of assets
rake assets:clean
# Nuke old assets
rake assets:clobber
@leonardreidy
leonardreidy / email_validation.js
Created August 28, 2017 10:16
Email validation in JS
// Email Regex lifted from Parsely.js:
var regexp = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$/i
// The regex above handles all the standard characters to