Skip to content

Instantly share code, notes, and snippets.

View nicksheffield's full-sized avatar

Nick Sheffield nicksheffield

View GitHub Profile
@nicksheffield
nicksheffield / timeslots-generator.js
Created February 17, 2020 02:49
Generate an array of time slots from a beginning to an end, skipping forward a certain amount of time each time
import { isBefore, setHours, setMinutes, setSeconds, addMinutes, setMilliseconds } from 'date-fns'
const setTime = (x, h = 0, m = 0, s = 0, ms = 0) => setHours(setMinutes(setSeconds(setMilliseconds(x, ms), s), m), h)
const from = setTime(new Date(), 9)
const to = setTime(new Date(), 17)
const step = (x) => addMinutes(x, 30)
const blocks = []
let cursor = from
@nicksheffield
nicksheffield / getNestedItems.ts
Created March 20, 2023 02:29
turn flat structure into nested structure
type Item = {
id: string
parentId: string | undefined
}
type NestedItem = Item & {
children: NestedItem[]
}
const getNestedItems = (items: Item[], parentId: string | undefined = undefined) => {
@nicksheffield
nicksheffield / react.tsx
Last active January 23, 2023 05:29
react awaitable imperative modal paradigm
import * as React from 'react'
export type ModalResolveFn<T = unknown> = (answer?: T) => void
// the shape of a modal definition. A unique id and an element
export interface ModalDefinition<T> {
id: string
element: React.ReactNode
handleClose: ModalResolveFn<T>
}
@nicksheffield
nicksheffield / react.tsx
Last active January 23, 2023 05:26
react imperative modal paradigm
import * as React from 'react'
// the shape of a modal definition. A unique id and an element
interface ModalDefinition {
id: string
content: React.ReactNode
}
type AddModalFn = (def: ModalDefinition) => void
type RemoveModalFn = (id: string) => void
@nicksheffield
nicksheffield / gist:8fc6f3341a831b2944c1c2157b062ed8
Created December 29, 2022 06:24
clientX, clientY explanation
clientX/Y: 0,0 is top left of the browser window (under the toolbar/bookmarks bar)
offsetX/Y: 0,0 is top left of clicked element
screenX/Y: 0,0 is top left of monitor/display
pageX/Y: 0,0 is top left of page content, including scroll
@nicksheffield
nicksheffield / example.ts
Created October 31, 2022 09:42
Typed keyof output
const obj = {
str: 'hello',
num: 1,
bool: true
}
type MiscObject = typeof obj
const fun = <T extends keyof MiscObject>(key: T): MiscObject[T] => {
return obj[key]
@nicksheffield
nicksheffield / gist:1236690
Created September 23, 2011 03:32
Most evil javascript ever
(function(){
var elems = document.getElementsByTagName('*');
for(var i=-1;++i<elems.length;){
elems[i].style.fontFamily='"Comic Sans MS", "Comic Sans", cursive';
}
})();
// as a bookmarklet
javascript:(function(){var elems=document.getElementsByTagName('*');for(var i=-1;++i<elems.length;){elems[i].style.fontFamily='"Comic Sans MS", "Comic Sans", cursive';}})();
@nicksheffield
nicksheffield / edit_item.php
Created March 8, 2011 04:35
Edit in place using ajax and contenteditable
<?php
// quit script if you aren't accessing it with ajax
if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) == false){
die();
}
// include database class
include('database.class.php');
@nicksheffield
nicksheffield / gulpfile.js
Last active February 10, 2022 05:33
Stylus with gulp
// jshint asi: true
var gulp = require('gulp') // the main guy
var clone = require('gulp-clone') // used to fork a stream
var notify = require('gulp-notify') // OS-level notifications
var rename = require('gulp-rename') // rename files in a stream
var stylus = require('gulp-stylus') // turn stylus code into css
var plumber = require('gulp-plumber') // handle errors
var beautify = require('gulp-cssbeautify') // make files human readable
var sourcemap = require('gulp-sourcemaps') // write sourcemaps
var minifycss = require('gulp-minify-css') // minify css code
@nicksheffield
nicksheffield / gist:81cff2e6d57409a5ac185aed077e1f2b
Created September 26, 2021 22:04
get positional string from number, ie: first, second, third, thirteenth, twentieth, twenty-first etc
const prefix = {
1: 'First',
2: 'Second',
3: 'Third',
4: 'Fourth',
5: 'Fifth',
6: 'Sixth',
7: 'Seventh',
8: 'Eighth',
9: 'Ninth',