Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View simonplend's full-sized avatar

Simon Plenderleith simonplend

View GitHub Profile
@simonplend
simonplend / console-dir-example.js
Created December 21, 2022 23:58
Example showing how to log a large, deep object with infinite depth in Node.js
/**
* This could be any object, perhaps the
* response from a request to an API.
*/
const deepObject = [
{imGoing:{deeperUnderground:{theresToo:{muchPanic:{inThis:{town:["I'm going deeper underground","There's too much panic in this town"]}}}}}},
{wellI:{gotToGo:{deeper:{gotToGo:{muchDeeper:true,theyGonna:{wreckItDown:true,theyGonna:["bring","it","down"]}}}}},rating:100}
];
// @see https://nodejs.org/api/console.html#consoledirobj-options
@simonplend
simonplend / 01-run-script-example.sh
Created October 12, 2022 08:49
Examples for: Command-line argument parsing with Node.js core
node script.mjs --name "Budgie" --verbose
@simonplend
simonplend / parseargs-example.mjs
Last active June 5, 2022 06:17
Example using new experimental Node.js parseArgs method (https://nodejs.org/api/util.html#utilparseargsconfig) - more new features covered at https://github.com/simonplend/whats-new-in-node-js-core
import { parseArgs } from "node:util";
const args = parseArgs({
options: {
name: {
type: "string",
},
verbose: {
type: "boolean",
short: "v",
@simonplend
simonplend / config.json
Last active February 9, 2022 23:16
Node.js v17.5.0 nightly build test - see instructions.sh
{
"url": "https://jsonplaceholder.typicode.com/todos/1"
}
@simonplend
simonplend / example-node-fetch-abortsignal.js
Last active September 2, 2021 08:30
Example of using AbortController and AbortSignal with node-fetch
import fetch from "node-fetch";
import { setTimeout } from "node:timers/promises";
const cancelTimeout = new AbortController();
const cancelRequest = new AbortController();
async function timeout(milliseconds) {
try {
await setTimeout(milliseconds, undefined, { signal: cancelTimeout.signal });
cancelRequest.abort();
@simonplend
simonplend / express-to-fastify-migration.diff
Last active March 11, 2021 14:17
Code changes required to start the migration of an Express application to Fastify
diff --git a/api-after/package.json b/api-after/package.json
index 8efee6f..f51a9c8 100644
--- a/api-after/package.json
+++ b/api-after/package.json
@@ -11,6 +11,9 @@
"license": "MIT",
"dependencies": {
"cors": "^2.8.5",
- "express": "^4.17.1"
+ "express": "^4.17.1",
@simonplend
simonplend / remark-template.html
Created February 17, 2021 11:15
Remark template which loads slides from a local slides.md file (https://github.com/gnab/remark)
<!DOCTYPE html>
<html>
<head>
<title>Title</title>
<meta charset="utf-8" />
<style>
@import url(https://fonts.googleapis.com/css?family=Yanone+Kaffeesatz);
@import url(https://fonts.googleapis.com/css?family=Droid+Serif:400,700,400italic);
@import url(https://fonts.googleapis.com/css?family=Ubuntu+Mono:400,700,400italic);
@simonplend
simonplend / main.js
Last active January 31, 2021 20:45
package-download-stats - Run in your terminal with: npx https://gist.github.com/simonplend/1c1259cf716d373eb78507dcbc228552 package-name
#! /usr/bin/env node
import { format as formatDate } from "timeago.js";
import {
getPackageDetails,
getPackageDownloadStats,
} from "./package-stats.js";
function getLatestVersion({ packageDetails }) {
@simonplend
simonplend / example-problem-detail-response.txt
Last active November 19, 2020 08:24
Example from RFC 7807 - Problem Details for HTTP APIs (https://tools.ietf.org/html/rfc7807)
HTTP/1.1 400 Bad Request
Content-Type: application/problem+json
Content-Language: en
{
"type": "https://example.net/validation-error",
"title": "Your request parameters didn't validate.",
"invalid-params": [
{
"name": "age",
@simonplend
simonplend / boolean-test-results.log
Last active October 30, 2020 22:21
Script and results for testing casting of ECMAScript standard defined types to booleans with Boolean() and !!
✅ Casting "" (type: string) with Boolean() === false
✅ Casting "" (type: string) with !! === false
✅ Casting "a" (type: string) with Boolean() === true
✅ Casting "a" (type: string) with !! === true
✅ Casting 0 (type: number) with Boolean() === false
✅ Casting 0 (type: number) with !! === false
✅ Casting 0.5 (type: number) with Boolean() === true
✅ Casting 0.5 (type: number) with !! === true
✅ Casting 9007199254740992 (type: bigint) with Boolean() === true
✅ Casting 9007199254740992 (type: bigint) with !! === true