Skip to content

Instantly share code, notes, and snippets.

View tomshaw's full-sized avatar
🎯
Focusing

Tom Shaw tomshaw

🎯
Focusing
View GitHub Profile
@tomshaw
tomshaw / AdhocController.php
Created May 22, 2014 06:51
ZF2 RandomUser.me Controller/Action
<?php
namespace Application\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\Http\Client;
use Zend\Http\Client\Adapter\Curl;
use Zend\Json\Json;
@tomshaw
tomshaw / smallest-index-value.js
Created May 31, 2014 17:44
Find the smallest index value in an array.
var array = [22, 44, 17, 47, 75, 26, 81, 15, 31, 23, 78, 95, 84, 29, 134, 64];
function indexOfSmallest(a) {
var lowest = 0;
for (var i = 1; i < a.length; i++) {
if (a[i] < a[lowest]) lowest = i;
}
return lowest;
}
@tomshaw
tomshaw / Select.js
Created July 10, 2016 23:41
Basic React Select
"use strict";
var React = require('react');
var Select = React.createClass({
propTypes: {
name: React.PropTypes.string.isRequired,
label: React.PropTypes.string.isRequired,
onChange: React.PropTypes.func.isRequired,
@tomshaw
tomshaw / index.js
Created October 8, 2017 01:29
Example using recompose to sort and conditionally render a component.
import React from 'react';
import {
compose,
withProps,
branch,
renderNothing
} from 'recompose';
import List from './list';
import Item from './item';
initDom() {
let output = $('div#m_ver_menu ul.m-menu__nav > li.m-menu__item--submenu').map(this.extractData).get();
console.log('output', output)
}
extractData() {
let $this = $(this);
let anchor = $this.find("a:first")[0];
let text = $(anchor).find("span:first").text();
let icon = $(anchor).find("i:first").attr('class');
@tomshaw
tomshaw / locationService.js
Last active January 23, 2019 17:18
React native geo location service.
let settings = {
enableHighAccuracy: true,
timeout: 2e3,
maximumAge: 0
}
let watchId = null;
let messages = [];
messages['enabled'] = 'Location service is not enabled on your device.';
@tomshaw
tomshaw / raf-engine.js
Created May 3, 2019 13:30
Nuxt.js raf-engine plugin
import RAFEngine from 'raf-engine';
const engine = new RAFEngine();
export default (ctx, inject) => {
ctx.$engine = engine;
inject('engine', engine);
}
@tomshaw
tomshaw / content.js
Created September 26, 2019 11:01
Chrome extension scan web page and highlight key words.
// Thoroughly untested.
let searchTerms = ['law', 'software', 'news', 'health'];
let elems = document.querySelectorAll("h1, h2, h3, h4, h5, h6, li, p, a")
for (let i = 0, total = elems.length; i < total; i++) {
let element = elems[i];
if (element && element.innerText) {
let innerText = element.innerText;
for (let j = 0; j < searchTerms.length; j++) {
@tomshaw
tomshaw / code.js
Created October 10, 2019 11:40
8 Must Know JavaScript Array Methods
// Web Dev Simplified
// 8 Must Know JavaScript Array Methods
// https://www.youtube.com/watch?v=R8rmfD9Y5-c
const items = [
{ name: 'Bike', price: 100 },
{ name: 'TV', price: 200 },
{ name: 'Album', price: 10 },
{ name: 'Book', price: 5 },
{ name: 'Phone', price: 500 },
@tomshaw
tomshaw / animate.js
Created December 10, 2019 12:09
My answer to Facebook JavaScript interview question on dev.to.
// https://dev.to/coderbyte/a-javascript-interview-question-asked-at-facebook-27po
var el = document.getElementById("myDiv");
function animate(el, totalTime, distance, startTime, position) {
if (position >= distance) return;
var expired = (new Date() - startTime) / 1000;
position = expired * distance / totalTime * 1000;