Skip to content

Instantly share code, notes, and snippets.

View TCotton's full-sized avatar
🏠
Working from home

Andrew Walpole TCotton

🏠
Working from home
View GitHub Profile
var GLOBALNAMESPACE = {};
GLOBALNAMESPACE.moduleObject = (function ($) {
var _private = {
i: 5,
get: function() {
console.log('current value: ' + this.i);
@TCotton
TCotton / javascript_a_to_z.js
Created November 10, 2016 09:58
javascript a to z
// ES6:
Array(26).fill().map((_,i)=>String.fromCharCode(97+i))
// Lodash/fp:
map(unary(String.fromCharCode),range(97,123))
@TCotton
TCotton / object_to_array.js
Last active February 3, 2016 11:36
object to array (with filter)
// Objective: take returned object from reviews service, filter data for only defined keys, and create new array
// Problem: filter isn't the right array function for this job. It returns the keys, instead of the values
// example: [["LastModificationTime","Title","Rating","ReviewText"],["LastModificationTime","Title","Rating","ReviewText"],["LastModificationTime","Title","Rating","ReviewText"],["LastModificationTime","Title","Rating","ReviewText"],["LastModificationTime","Title","Rating","ReviewText"]]
// map returns everything so if the key doesn't meet the criteria it returns undefined in the array
Reviews.getReview().then((returnData) => {
this.reviews = returnData.data.Results;
let anArray = [];
@TCotton
TCotton / authentication_nodejs.js
Last active November 29, 2015 12:39
user authentication - node.js
/**
* User authentication code not in its own module
*/
var db = require('./db.js');
app.on('/createUser', function(req, res) {
var user = req.username,
pwd = req.password,
email = req.email;
@TCotton
TCotton / typecheck,js
Last active October 1, 2015 20:01
Dependable JavaScript tupe hek
/*! axis.js v1.1.0 | (c) 2014 @toddmotto | https://github.com/toddmotto/axis */
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define(factory);
} else if (typeof exports === 'object') {
module.exports = factory;
} else {
root.axis = factory();
}
})(this, function () {
@TCotton
TCotton / js-iteration.js
Created September 21, 2015 18:24
Plain objects are not iterable in ES6—that would clash with iterability of data structures. Helper is easy to write.
//Plain objects are not iterable in ES6—that would clash with iterability of data structures. Helper is easy to write.
function* objectEntries(obj) {
let keys = Reflect.ownKeys(obj);
for (let key of keys) {
yield [key, obj[key]];
}
}
@TCotton
TCotton / alernatives_to_switch.js
Last active September 16, 2015 13:41
using object literals as an alternative to a JavaScript Switch statement
function getDrink(type) {
var drinks = {
'coke': function() {
return 'Coke';
},
'pepsi': function() {
return 'Pepsi';
},
'lemonade': function() {
return 'Lemonade';
@TCotton
TCotton / object_mixin_with_cache.js
Created September 16, 2015 13:24
object mixin with cash
(function() {
'use strict';
let withRectangle = (function() {
function area() {
console.log('area');
return this.length * this.width;
}
@TCotton
TCotton / object_literal_mixin.js
Created September 16, 2015 13:01
Using a mixin as an object literal and merging with class
(function() {
'use strict';
var withCircle = {
label:'',
area:function() {
return Math.PI * this.radius * this.radius;
},
grow: function() {
@TCotton
TCotton / reactjs-es6classes-higherorder.js
Created September 15, 2015 14:48
Higher-order React component example:
// withViewport.js
import React, { Component } from 'react';
import { canUseDOM } from 'fbjs/lib/ExecutionEnvironment';
function withViewport(ComposedComponent) {
return class WithViewport extends Component {
constructor() {
super();