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
@TCotton
TCotton / donut_chart_react.jsx
Created September 14, 2018 06:48
An example of creating a donut chart with React
import React from 'react';
import { scaleOrdinal } from 'd3-scale';
import { arc as d3Arc, pie as d3Pie } from 'd3-shape';
import { csvParse } from 'd3-dsv';
// Same as data.csv
import dataCsv from './data';
import './chart.css';
@TCotton
TCotton / reducer.js
Created September 2, 2018 14:46
redux asynchronous action generator
// import React from 'react';
import { combineReducers, createStore, compose } from 'redux';
import axios from 'axios';
const nameReducer = (state = 'Anonymous', action) => {
switch (action.type) {
case 'CHANGE_NAME':
return action.name;
default:
return state;
@TCotton
TCotton / webpack.config.js
Created May 9, 2018 16:28 — forked from timurcatakli/webpack.config.js
An Easy to Understand Webpack 4+ Configuration File with Comments
const publicPath = 'public';
// Node os module
// The os module provides a number of operating system-related utility methods.
// It can be accessed using:
const os = require('os');
// Using a single monolithic configuration file impacts comprehension and
// removes any potential for reusability.
// As the needs of your project grow, you have to figure out the means to manage
// webpack configuration more effectively.
@TCotton
TCotton / for_map.js
Created August 12, 2017 19:36
#2: I need to create a new array from a given one and transform all the elements
// Given the array of prices, return a new array with the prices n % lower.
const discount = (originalPrices, discountAmount) => {
const multiplier = 1 - discountAmount;
// we must clone the array
let result = new Array(originalPrices);
for (let i = 0; i < originalPrices.length; i++) {
result[i] = originalPrices[i] * multiplier;
}
return result;
}
@TCotton
TCotton / for_reduce.js
Created August 12, 2017 19:33
#1: I need to go over an array and get a single value as a result
// for loop
const sum = (array) => {
let result = 0;
for (let i = 0; i < array.length; i++) {
result += array[i];
}
return result;
}
const numbers = [5, 25, 8, 18];
@TCotton
TCotton / chart.js
Created August 12, 2017 19:05
A better way to structure D3 code (updated with ES6 and D3 v4)
class Chart {
constructor(opts) {
// load in arguments from config object
this.data = opts.data;
this.element = opts.element;
// create the chart
this.draw();
}
@TCotton
TCotton / filter_map.js
Created August 12, 2017 18:55
filter and map example
const newReleases = [{
"id": 70111470,
"title": "Die Hard",
"boxart": "http://cdn-0.nflximg.com/images/2891/DieHard.jpg",
"uri": "http://api.netflix.com/catalog/titles/movies/70111470",
"rating": 4.0,
"bookmark": []
},
{
"id": 654356453,
@TCotton
TCotton / reduce.js
Last active August 12, 2017 18:43
clear reduce example
let footballPlayers = [{
gender: "M",
name: "Karim Benzema",
age: 29,
club: "Real Madrid CF",
country: "Spain"
},
{
gender: "M",
name: "Samir Nasri",
@TCotton
TCotton / penguinController.ts
Created July 22, 2017 15:30
Mode Vew Controller
import { PenguinModel } from './penguinModel';
import { PenguinView } from './penguinView';
interface IPenguinViewModel {
name: string;
imageUrl: string;
size: string;
favoriteFood: string;
previousIndex: number;
nextIndex: number;
@TCotton
TCotton / builderPattern.ts
Last active July 21, 2017 22:11
builder pattern in TypeScript
interface BurgerBoy {
size: number;
cheese: boolean;
pepperoni: boolean;
lettuce: boolean;
tomato: boolean;
addPepperoni(): any;
addCheese(): any;
addLettuce(): any;
addTomato(): any;