Skip to content

Instantly share code, notes, and snippets.

View amackintosh's full-sized avatar

Adam Mackintosh amackintosh

View GitHub Profile
@amackintosh
amackintosh / debounce.js
Created November 26, 2017 00:24
Debounce Function
// Remember, debounced function will not run until it stops getting called for `wait` milliseconds
const debounce = (func, wait) => {
let timeout
return (...args) => {
clearTimeout(timeout)
timeout = setTimeout(() => func.apply(this, args), wait)
}
}
@amackintosh
amackintosh / webpack.config.js
Created November 7, 2017 05:31
SCSS Webpack Config
const webpack = require('webpack');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const preferEntry = true;
module.exports = {
entry: ['babel-polyfill', './src/root.js'],
output: {
path: `${__dirname}/dist`,
publicPath: '/',
@amackintosh
amackintosh / requireAuthHOCnative.js
Created November 1, 2017 20:55
requireAuth Higher-order Component for React Native
import React, { Component } from 'react'
import { connect } from 'react-redux'
import { NavigationActions } from 'react-navigation'
export default function (ComposedComponent) {
class Authentication extends Component {
componentWillMount() {
if (!this.props.isAuthenticated) {
return this.props.navigation.dispatch(NavigationActions.reset({
index: 0,
@amackintosh
amackintosh / functionPromiseAndCallback.js
Created October 31, 2017 19:10
JavaScript Function that returns a Promise or Callback depending if a callback was specified
function myFunc(obj, callback) { // call back is an optional param
if (!callback) { // if callback omitted, we return a promise
const performWork = async () => obj // this simulates work being done before resolving
return new Promise(async (resolve, reject) => { // return an async promise wrapper
const value = await performWork() // do the work
resolve(value) // resolve the promise
})
}
const cb = obj // if callback present, we return it
callback(cb)
@amackintosh
amackintosh / PasswordQuality.js
Created October 23, 2017 02:05
Password Quality Checker for React Native
// Password Quality Checker
// Inspired by Amy - @thecrafty_coder on Twitter
import React from 'react'
import { View, Text } from 'react-native'
const testPassword = (p) => {
const hasSpecial = /[^a-zA-Z0-9]/.test(p)
const hasNumber = /[0-9]/.test(p)
const hasMixedCase = /[A-Z]/.test(p) && /[a-z]/.test(p)
@amackintosh
amackintosh / eventemitter.js
Created September 4, 2017 01:24
To control when and how to fire logic, we can use event emitters.
const fs = require('fs')
const EventEmitter = require('events')
const lazy = new EventEmitter()
const someTask = async () => true
const anotherTask = async () => true
const files = ['file1.txt', 'file2.txt', 'file3.txt']
const readFile = (file) => {
try {
return new Promise(async (resolve, reject) =>
@amackintosh
amackintosh / asyncAwait.js
Last active June 14, 2017 18:32
Promise & Async/Await Demo
// Async function that checks input for no reason
// and then resolves or rejects the promise depending on the input
async function asyncWin(string) {
var input = string;
// Private helper method that displays pointless message
function _pointless() {
console.log('We just waited 1.337 seconds for no reason.')
};
// Invoke helper method after 1.337 seconds and wait until it is done
var ok = await new Promise((resolve, reject) => {
@amackintosh
amackintosh / getRandomWord.js
Created June 13, 2017 05:44
Random Word Generator from Dictionary File
var fs = require('fs');
sampleFile = './words.txt';
function getRandomWord(relativeFilePath) {
try {
// Read file and get words
var text = fs.readFileSync(relativeFilePath, 'utf8');
var words = text.split(/\s/);