Skip to content

Instantly share code, notes, and snippets.

View jkeefe's full-sized avatar

John Keefe jkeefe

View GitHub Profile
@jkeefe
jkeefe / round.js
Created August 3, 2023 18:00
Sweet dayjs rounding function
// from https://github.com/iamkun/dayjs/issues/1619#issuecomment-1185714859
const round: PluginFunc = (option, dayjsClass) => {
dayjsClass.prototype.round = function (amount, unit) {
const mod = this.get(unit as UnitType) % amount;
if(mod < amount / 2) {
return this.subtract(mod, unit).startOf(unit);
}
@jkeefe
jkeefe / import_mapshaper.js
Created July 12, 2023 21:09
Importing mapshaper in ES6
// npm install mapshaper --save
import mapshaper from 'mapshaper'
// Example: converting a directory of Shapefiles to GeoJSON
mapshaper.runCommands('-i shapefiles/*.shp -o geojson/ format=geojson');
@jkeefe
jkeefe / access.json
Last active May 3, 2024 19:22
Access One Bucket Policy
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:ListAllMyBuckets"
],
"Resource": "arn:aws:s3:::*"
},
@jkeefe
jkeefe / cors.json
Last active October 25, 2023 22:38
Open CORS policy for AWS S3
[
{
"AllowedHeaders": [
"*"
],
"AllowedMethods": [
"GET",
"HEAD"
],
"AllowedOrigins": [
@jkeefe
jkeefe / delay.js
Created January 29, 2023 21:23
Node delay function
function delay(time) {
return new Promise(resolve => setTimeout(resolve, time));
}
// usage:
console.log("Thing one")
await delay(2000)
console.log("Thing two, two seconds later")
@jkeefe
jkeefe / niceRounder.js
Created December 28, 2022 20:09
Rounds whole numbers to nice figures for publication
// based on: https://stackoverflow.com/a/67136959
const niceRounder = (number) => {
let near = 1
if (number > 100) near = 10
if (number > 1000) near = 100
if (number > 10000) near = 1000
if (number % near === 0) return number;
@jkeefe
jkeefe / nyt_states.json
Created December 27, 2022 21:53
Translation json for NYT state names
[{"name":"Alabama","nyt":"Ala.","postal":"AL"},
{"name":"Alaska","nyt":"Alaska","postal":"AK"},
{"name":"Arizona","nyt":"Ariz.","postal":"AZ"},
{"name":"Arkansas","nyt":"Ark.","postal":"AR"},
{"name":"California","nyt":"Calif.","postal":"CA"},
{"name":"Colorado","nyt":"Colo.","postal":"CO"},
{"name":"Connecticut","nyt":"Conn.","postal":"CT"},
{"name":"Delaware","nyt":"Del.","postal":"DE"},
{"name":"District of Columbia","nyt":"D.C.","postal":"DC"},
{"name":"Florida","nyt":"Fla.","postal":"FL"},
@jkeefe
jkeefe / time_hash.js
Last active April 20, 2023 14:14
Cool hex time hash for cache busting, unique, etc.
const time_hash = Date.now().toString(36);
// reverse it for more wordiness:
const time_hash2 = Date.now().toString(36).split("").reverse().join("")
@jkeefe
jkeefe / last_day_of_month.js
Created April 29, 2022 20:00
Test to see if a date is the last day of the month with dayjs()
const dayjs = require('dayjs')
const customParseFormat = require('dayjs/plugin/customParseFormat')
dayjs.extend(customParseFormat)
const reading_date = dayjs(reading.t, 'M/D/YYYY HH:mm:ss A')
// is the month of adding a day the same as the month of adding a month?
if ( reading_date.add(1, 'day').get('month') == reading_date.add(1, 'month').get('month') ){
@jkeefe
jkeefe / require_trick.js
Created March 31, 2022 23:27
nifty trick to bring in json files using require in a module
// nifty trick to bring in json files using require in a module
import { createRequire } from "module"; // Bring in the ability to create the 'require' method
const require = createRequire(import.meta.url); // construct the require method
const trainData = require('./src/data/latest.json')