Skip to content

Instantly share code, notes, and snippets.

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

Bret Cameron BretCameron

🏠
Working from home
  • YuLife
  • London
View GitHub Profile
@BretCameron
BretCameron / test.js
Last active August 13, 2019 18:33
Using Mocha in Node.js
const Mocha = require('mocha');
const test = new Mocha();
test.addFile('./assert.js');
let runner = test.run();
const passed = [];
const failed = [];
@BretCameron
BretCameron / actions.js
Created August 6, 2019 12:42
A Redux action that returns a function, which is made possible by Redux Thunk middleware
import axios from 'axios';
export const createCourse = (item) => {
return function (dispatch) {
return axios.post('create', item).then(
res => dispatch({ type: 'CREATE_ITEM', payload: res.data }),
error => console.log(error)
);
};
};
@BretCameron
BretCameron / store.js
Created August 6, 2019 12:36
Add redux-thunk middleware to your Redux store
import { createStore, applyMiddleware, compose } from 'redux';
import thunk from 'redux-thunk';
import rootReducer from './reducers';
const initialState = {};
const middleware = [thunk];
let store;
@BretCameron
BretCameron / isAnagram.js
Last active December 8, 2020 16:50
Return true if two strings are anagrams of one another
const isAnagram = (str1, str2) => {
if (str1.length !== str2.length) return false;
const map = new Map();
for (let char of str1) {
const count = map.has(char) ? map.get(char) + 1 : 1;
map.set(char, count);
};
@BretCameron
BretCameron / email-regex.js
Last active February 1, 2020 18:26
A regex string which covers the RFC specification for possible valid email addressses
/(?:(?:\r\n)?[ \t])*(?:(?:(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*))*@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*|(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*)*\<(?:(?:\r\n)?[ \t])*(?:@(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|
@BretCameron
BretCameron / parent.html
Created July 16, 2019 16:20
An HTML file containing an iframe, the height of which changes automatically.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta content="width=device-width, initial-scale=1.0" name="viewport">
<meta content="ie=edge" http-equiv="X-UA-Compatible">
<title>Parent File</title>
</head>
@BretCameron
BretCameron / child.html
Last active July 27, 2019 13:48
Part 2: A simple dummy text generator, where people can click a button to add another paragraph of lorem ipsum text.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta content="width=device-width, initial-scale=1.0" name="viewport">
<meta content="ie=edge" http-equiv="X-UA-Compatible">
<title>Lorem Ipsum Generator</title>
</head>
@BretCameron
BretCameron / child.html
Last active July 16, 2019 15:54
Part 1: A simple dummy text generator, where people can click a button to add another paragraph of lorem ipsum text.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Lorem Ipsum Generator</title>
</head>
@BretCameron
BretCameron / server.js
Created June 27, 2019 16:57
A simple server with CRUD enpoints built using Node.js, Express, MongoDB and Mongoose
const express = require('express');
const mongoose = require('mongoose');
const config = require('config');
const app = express();
app.use(express.json());
const db = config.get('mongoURI');
const Animal = require('./models/Animal');
@BretCameron
BretCameron / server.js
Last active June 27, 2019 14:38
Connect to MongoDB using Mongoose
const express = require('express');
const mongoose = require('mongoose');
const path = require('path');
const config = require('config');
const app = express();
const db = config.get('mongoURI');
mongoose