Skip to content

Instantly share code, notes, and snippets.

View vlucas's full-sized avatar
🗯️
It's either a binary trap, or it's not.

Vance Lucas vlucas

🗯️
It's either a binary trap, or it's not.
View GitHub Profile
@vlucas
vlucas / domEl.ts
Last active May 8, 2023 20:28
Shortcut function to create a return a DOM element. Handles children, CSS via `style` attribute, and `text` and `html` attributes also
/**
* Shortcut function to create and return a DOM element
*/
function domEl(tag: string, attributes: { [key: string]: any } = {}, children: any[] = []) {
const el = document.createElement(tag);
for (const attr in attributes) {
if (attr === 'text') {
el.appendChild(document.createTextNode(attributes[attr]));
continue;
@vlucas
vlucas / login.tsx
Created June 8, 2022 18:53
Next.js Login Page (POSTs back to itself and handles everything)
import { GetServerSidePropsContext } from 'next';
import React from 'react';
import AuthLayout from 'src/layouts/AuthLayout';
import AlertErrors from 'src/components/AlertErrors';
import { addRequestBody } from 'src/server/bodyParser';
import { userLogin, userSessionInsert } from 'src/server/queries';
import { redirectUserToApp, setUserAuthCookie } from 'src/server/auth';
import Link from 'next/link';
// Types
@vlucas
vlucas / ApiForm.tsx
Created May 10, 2022 20:50
ApiForm React Component for native <form> onSubmit support
import type { FormEvent } from 'react';
export type onFormSubmit = (e: FormEvent, data: { [key: string]: any}) => void
export type ApiFormProps = {
children: any,
onSubmit: onFormSubmit,
[key: string]: any, // Allows any other props to be passed through to element
}
/**
@vlucas
vlucas / SheetQuery-InserRows.js
Created April 14, 2021 15:21
SheetQuery-InsertRows
sheetQuery()
.from('Transactions')
.insertRows([
{
Amount: -554.23,
Name: 'BigBox, inc.'
},
{
Amount: -29.74,
Name: 'Fast-n-greasy Food Spot'
@vlucas
vlucas / SheetQuery-DeleteRows.js
Created April 9, 2021 14:41
SheetQuery-DeleteRows
// Delete specific matching rows
sheetQuery()
.from('Transactions')
.where(row => row.Category === 'DELETEME')
.deleteRows();
@vlucas
vlucas / sheetquery-updaterows.js
Last active April 9, 2021 14:46
SheetQuery-UpdateRows
// Update rows with criteria
sheetQuery()
.from('Transactions')
.where(row => row.Business.toLowerCase().includes('starbucks'))
.updateRows(row => {
row.Category = 'Coffee Shops'
});
@vlucas
vlucas / SheetQuery-getRows.js
Last active April 9, 2021 14:46
SheetQuery-getRows
const query = sheetQuery()
.from('Transactions')
.where(row => row.Category === 'Shops');
const data = query.getRows(); // Array of objects, i.e. [ { Name: "Somestore", Amount: 45.29, Category: "Shops" } ]
@vlucas
vlucas / routes.js
Created February 13, 2018 21:00
Simple JS router (client+node.js)
'use strict';
let pathToRegexp = require('path-to-regexp');
let defaultRoute;
let routes = [];
/**
* GET route
*
* @param {String} pattern - Express.js style route pattern or bare URL string
// ES5 way
t: function () {
let key = _.first(arguments);
let replacements = _.rest(arguments);
let translation = translations[key];
if (translation && replacements.length) {
translation = format.apply(null, [translation].concat(replacements));
}
@vlucas
vlucas / index.js
Last active May 16, 2017 19:45
JS Questions
// stuff
function slug (title) {
// do something
}
slug('Some Post');
class Post {
slug () {