Skip to content

Instantly share code, notes, and snippets.

View jamesdaily's full-sized avatar

James Daily jamesdaily

View GitHub Profile
@jamesdaily
jamesdaily / phonePartsUSA.js
Last active October 24, 2023 16:13
phonePartsUSA.js JavaScript function to split a US phone number into its component parts
function phonePartsUSA(phoneNumberInput) {
const phoneNumber = ("" + phoneNumberInput).replace(/[^0-9]/g, "");
if (phoneNumber.length != 10) throw new Error("phoneNumberInput must have 10 digits");
const areacode = phoneNumber.slice(0, 3);
const prefix = phoneNumber.slice(3, 6);
const exchange = phoneNumber.slice(6, 10);
const dialnumber = prefix + exchange;
@jamesdaily
jamesdaily / randomDateOfBirth.js
Created October 24, 2023 14:04
randomDateOfBirth JavaScript function for generating sample DOB within an age range
function assert(condition, message) {
if (!condition) {
throw new Error("Assertion failed: [" + (message || "no message") + "]" );
}
}
function randomDate(start, end) {
assert(start < end,"start < end");
return new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime()));
}
@jamesdaily
jamesdaily / bash_lowercase_csv_headers.sh
Created September 11, 2019 14:31
Bash command to lowercase the first line (column headers) of each CSV file in a directory, and export them to a "lower" subdirectory
for f in *.csv; do (head -n1 $f |tr '[A-Z]' '[a-z]'; tail -n +2 $f) > ./lower/$f; done
@jamesdaily
jamesdaily / html-to-pdf-using-chrome.bat
Created February 7, 2019 19:55
Windows Batch file to invoke "print-to-pdf" feature in headless Chrome
@echo off
IF %1.==. GOTO NoHtmlFile
SET CURRENTDIR=%cd%
set htmlFile=%1
start "C:\Program Files (x86)\Google\Chrome\Application" chrome.exe --headless --disable-gpu --no-margins --print-to-pdf="%CURRENTDIR%\%htmlFile%.pdf" "%CURRENTDIR%\%htmlFile%"
GOTO End1
:NoHtmlFile
ECHO Must specify HTML file or URL
@jamesdaily
jamesdaily / FilesListExample.java
Last active June 4, 2018 21:05
Example of Files.list() resource leaks for Spot Bugs
package com.jamesdaily.spotbugs.FileDescriptorLeak;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;
public class FilesListExample {
@jamesdaily
jamesdaily / assertEquals.plsql
Last active March 25, 2016 16:20
PL/SQL assertEquals function for comparing values in SQL queries
CREATE OR REPLACE FUNCTION assertEquals (pinInput VARCHAR2, pinExpectedValue VARCHAR2)
RETURN VARCHAR2
IS
/*
James Daily 3/25/2016
Helper function for comparing values in SQL queries
Copyright (c) 2016 James David Daily
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
@jamesdaily
jamesdaily / Stick.java
Created November 10, 2014 00:17
Cut the sticks! (HackerRank)
package com.jamesdaily.study;
public class Stick implements Comparable<Stick> {
protected Integer length;
public Stick(Integer length) {
if (length == 0)
throw new IllegalArgumentException("Stick cannot be length 0!");
if (length < 0)
throw new IllegalArgumentException(