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 / Form.php
Created April 7, 2021 20:33
Laravel telephone number validators.
$this->validate($request, [
'phone' => 'required|digits:10'
]);
$this->validate($request, [
'phone' => 'required|numeric|between:9,11'
]);
$this->validate($request, [
'phone' => 'required|min:10|numeric'
@tomshaw
tomshaw / circle-circumference.js
Created December 28, 2020 17:03
Set svg circle circumference stroke.
this.$svgCircle = document.querySelector("circle");
this.circumference = 2 * Math.PI * this.$svgCircle.getAttribute("r");
setCircleProgress(amount) {
let res = amount * this.circumference / 100 + ", " + this.circumference;
this.$svgCircle.setAttribute("stroke-dasharray", res)
}
@tomshaw
tomshaw / ReleaseDownload.js
Created March 3, 2020 17:56
Vanilla javascript download progress.
// ==========================================================================
// ReleaseDownload Progress Bar
// ==========================================================================
import AbstractObject from './AbstractObject';
export default class ReleaseDownload extends AbstractObject {
constructor(el, options) {
super(el, options);
this.el = el;
@tomshaw
tomshaw / webpack.mix1.js
Created February 17, 2020 18:47
Miscellaneous Laravel MIX configurations
/*
|--------------------------------------------------------------------------
| Tailwind configuration
|--------------------------------------------------------------------------
*/
const mix = require('laravel-mix');
const tailwindcss = require('tailwindcss')
mix.js('resources/frontend/scripts/app.js', 'public/js/app.js');
@tomshaw
tomshaw / savepage.js
Created January 16, 2020 21:14
Quickly export to PDF whatever page/url.
#!/usr/bin/env node
const puppeteer = require('puppeteer');
/**
* npm link/savepage url=https://stackoverflow.com/
* Margins default to Microsoft Word A4 margins.
*/
class SavePage {
@tomshaw
tomshaw / combinations.js
Created January 8, 2020 22:36
Find combinations of input value that can win 1st, 2nd, 3rd prize.
function combinations(n) {
if (n < 0) {
return 0;
} else if (n < 3) {
return n;
}
return n * (n - 1) * (n - 2);
}
console.log(combinations(8)) // (8 - 1 = 7) * (8 - 2 = 6) = (42 * 8) = 336
@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;
@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 / 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 / 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);
}