Skip to content

Instantly share code, notes, and snippets.

View humphd's full-sized avatar
💭
Helping others get started

David Humphrey humphd

💭
Helping others get started
View GitHub Profile
@humphd
humphd / evaluate-math-expression.js
Last active November 3, 2024 23:00
Evaluate a mathjs expression and return the result
/**
* Evaluates a mathjs expression and returns the result
* @param expr A Math.js (https://mathjs.org/) compatible exression string
* @returns A Promise that resolves to the result of the math expression
*/
export async function evaluateMathExpressionWithMathJs(expr: string): Promise<number> {
/**
* Loads the Math.js library dynamically and returns a promise
* that resolves when the library is loaded. Currently not possible
* to load as ESM, see https://github.com/josdejong/mathjs/issues/1841
@humphd
humphd / forms.html
Created November 23, 2023 20:08
Web222 Week 11
<!DOCTYPE html>
<html>
<head>
<title>Form Validation</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Load Bootstrap's CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous"> <!-- Our stylesheet -->
<!-- Bootstrap Icons -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.2/font/bootstrap-icons.min.css">
@humphd
humphd / doi2json.ts
Last active November 3, 2024 22:35
Convert a DOI or DOI URL to JSON via CrossRef API
/**
* Gets JSON citation data from the CrossRef API for a DOI or DOI URL
* @param doiOrUrl A DOI or DOI URL
*/
export async function doi2json(doiOrUrl: string) {
// Extract the DOI from the URL or use it directly if it's not a URL
const doi = doiOrUrl.replace(/^https?:\/\/doi\.org\//, '');
// Fetch data from the CrossRef API
const response = await fetch(`https://api.crossref.org/works/${doi}`);
@humphd
humphd / runJS.js
Created July 24, 2023 15:36
CharCraft JS Runtime function
export const name = "runJS";
export const description = "Runs arbitrary JavaScript code in a browser context (no node.js)";
export const parameters = {
type: "object",
properties: {
"code": {
type: "string",
description: "JavaScript code to run in browser",
@humphd
humphd / sum.js
Created July 23, 2023 14:56
ChatCraft sum function
/**
* Example Function Module. Each function needs you to define 4 things:
*/
/* 1. Name of your function */
export const name = "sum";
/* 2. Description of function, used to describe what it does to an LLM */
export const description = "Adds all numbers passed to it, returning the total.";
@humphd
humphd / chimes.ino
Created February 12, 2022 20:13
Chimes Arduino Sketch
#include <Adafruit_NeoPixel.h>
#include <HTTPClient.h>
#include <WiFi.h>
// Define a pixel strip of 1 pixel
Adafruit_NeoPixel pixels(1, PIN_NEOPIXEL, NEO_GRB + NEO_KHZ800);
// Wifi
char ssid[] = "...";
char password[] = "...";
@humphd
humphd / elastic.js
Created February 2, 2022 20:14
Elastic mock brainstorm
const { Elastic } = require(...)
const { mock } = Elastic();
beforeEach(() => mock.clearAll())
test('..', async () => {
mock.add('.....');
await request.get('/serach?...')
@humphd
humphd / App.jsx
Created January 28, 2021 16:23
WEB422 Week 4 - React Events
import { useState } from 'react';
import ClickCounter from './ClickCounter';
import ClickHeading from './ClickHeading';
function App() {
// State: held by the parent
const [numClicks, setNumClicks] = useState(0);
const onClickHandler = (e) => setNumClicks(numClicks + 1);
@humphd
humphd / restaurantDB.js
Created January 13, 2021 15:18
WEB422 - A1 model
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const restaurantSchema = new Schema({
address: {
building: String,
coord: [Number],
street: String,
zipcode: String
},
@humphd
humphd / index.js
Created November 19, 2020 01:42
Testing code that uses node-fetch using Jest
// Simple module that uses fetch to do a HEAD request
const fetch = require('node-fetch');
module.exports.fn = async (url) => {
try {
const response = await fetch(url, { method: "HEAD" });
return response.ok;
} catch(err) {
return false;
}