Skip to content

Instantly share code, notes, and snippets.

View magalhini's full-sized avatar

Ricardo Magalhães magalhini

View GitHub Profile
@magalhini
magalhini / webpack.js
Created October 14, 2015 12:42 — forked from Couto/webpack.js
Fetch polyfill with webpack
var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var path = require('path');
var folders = {
APP: path.resolve(__dirname, '../app'),
BUILD: path.resolve(__dirname, '../build'),
BOWER: path.resolve(__dirname, '../bower_components'),
NPM: path.resolve(__dirname, '../node_modules')
};
@magalhini
magalhini / TemplateStore.js
Last active October 27, 2015 14:53
Testing a Flux Store using Mocha and Sinon. It uses rewire to fetch a new copy of the Store every time, as to maintain a clean state before each test. Also includes a sample for the Gulp task to run it in proper TDD fashion.
// Mock our global environment using JSDOM
require('../dom-mock')('<html><body></body></html>');
let jsdom = require('mocha-jsdom');
import {
React,
sinon,
assert,
expect,
@magalhini
magalhini / advent-day1.js
Created December 3, 2015 16:17
Advent O fCode
const fs = require('fs');
const file = fs.readFileSync('./1.txt').toString('utf8');
let level = 0;
let findWay = (char) => {
if (char === '(') return 1;
else if (char === ')') return -1;
}
@magalhini
magalhini / Enhance.js
Created September 5, 2016 15:27 — forked from sebmarkbage/Enhance.js
Higher-order Components
import { Component } from "React";
export var Enhance = ComposedComponent => class extends Component {
constructor() {
this.state = { data: null };
}
componentDidMount() {
this.setState({ data: 'Hello' });
}
render() {
@magalhini
magalhini / node-multiple-calls-render.js
Last active December 8, 2021 10:25
Example on how to render multiple responses into a route in node.js
const express = require('express')
const app = express()
const path = require('path')
const fetch = require('node-fetch')
const PORT = process.env.PORT || 3000
app.get('/api/user', (req, res) => {
res.json({ name: 'Richard' });
});
@magalhini
magalhini / pre-commit
Last active May 8, 2017 14:34
Git pre-commit hook to check against accidental describe.only() in tests
#!/bin/bash
# Check for .only() in this path
FOLDER="./webapp/src/js"
if git rev-parse --verify HEAD >/dev/null 2>&1
then
against=HEAD
else
# Initial commit: diff against an empty tree object
@magalhini
magalhini / synaptic-shape-training.js
Created September 6, 2017 17:57
Training a neural network to recognise 15 inputs worth of Shapes
const Layer = require('synaptic').Layer;
const Network = require('synaptic').Network;
const Trainer = require('synaptic').Trainer;
const inputLayer = new Layer(15);
const hiddenLayer = new Layer(40);
const outputLayer = new Layer(4);
inputLayer.project(hiddenLayer);
hiddenLayer.project(outputLayer);
@magalhini
magalhini / mochachaisinon.js
Created October 5, 2018 15:33 — forked from eswak/mochachaisinon.js
An example of asynchronous JavaScript testing using Mocha + Chai + Sinon
var chai = require('chai');
var assert = chai.assert;
var expect = chai.expect;
var should = chai.should();
var sinon = require('sinon');
function myAsyncFunction(callback) {
// 50ms delay before callback
setTimeout(function() {
console.log('hello');
const nlp = require('compromise');
function elizaBot(input) {
const doc = nlp(input);
if (doc.has('i #Adverb? (am|feel) #Adverb? #Adjective')) {
const feeling = doc.match('i #Adverb? (am|feel) #Adverb? [#Adjective]').out('normal');
return `When did you become ${feeling}?`;
} else if (doc.has('i live in #Place')) {
const place = doc.match('[#Place]');