Skip to content

Instantly share code, notes, and snippets.

View kieranbarker's full-sized avatar

Kieran Barker kieranbarker

View GitHub Profile
@kieranbarker
kieranbarker / getTodaysDate.js
Created December 3, 2021 16:30
Get today's date.
/**
* Get today's date.
* https://gist.github.com/kieranbarker/2c300d73059697a4417e12bd40cdef75
* @returns {Date} A Date object representing today's date at midnight in UTC.
*/
function getTodaysDate() {
const dateString = new Date().toISOString().slice(0, 10);
return new Date(dateString);
}
@kieranbarker
kieranbarker / append_file.js
Last active November 8, 2021 09:09
Asynchronously append data to a file in Node.js.
import * as fs from 'fs';
import * as fsPromises from 'fs/promises';
const file = 'hello_world.txt';
const data = 'Hello, World!';
//
// Callback API
//
@kieranbarker
kieranbarker / package.json
Last active November 1, 2021 08:55
Asynchronously read and write files in Node.js.
{
"name": "read_write",
"version": "1.0.0",
"description": "Asynchronously read and write files.",
"private": true,
"main": "read_write.js",
"type": "module",
"scripts": {
"start": "node read_write.js",
"test": "echo \"Error: no test specified\" && exit 1"
@kieranbarker
kieranbarker / states.json
Created October 4, 2021 17:34
All 50 US states as a JSON array.
[
"alabama",
"alaska",
"arizona",
"arkansas",
"california",
"colorado",
"connecticut",
"delaware",
"florida",
@kieranbarker
kieranbarker / serve
Last active September 5, 2021 07:30
.zshrc alias for simple Python server
# Alias for simple Python server
serve() {
python3 -m http.server $1
}
@kieranbarker
kieranbarker / array_at.js
Created May 16, 2021 13:22
Return the item at the given index in the array, allowing for positive and negative integers. Negative integers count back from the last item in the array.
/**
* Return the item at the given index in the array, allowing for positive and
* negative integers. Negative integers count back from the last item
* in the array.
*
* {@link https://gist.github.com/kieranbarker/1bb4ae463f3cd8138b4805983de49f4d}
*
* @param {Array} array The array
* @param {Number} index The index
* @returns {*} The item at the given index
@kieranbarker
kieranbarker / formatJSON.js
Last active February 24, 2021 11:34
Format a JSON string for readability
/**
* Format a JSON string for readability
* @param {String} text The JSON string
* @param {Number|String} space The number of spaces, or a string, to indent by
* @returns {String} The newly formatted JSON string
* @license MIT
*/
function formatJSON (text, space = 2) {
return JSON.stringify(JSON.parse(text), null, space);
}
@kieranbarker
kieranbarker / fiftyFifty.js
Last active February 1, 2021 10:31
Get a random truthy/falsy value
/**
* Get a random truthy/falsy value
* {@link https://gist.github.com/kieranbarker/25b1b4b4cef14c65b10dcefdc462f6ee}
* @returns {Number} 1 (truthy) or 0 (falsy)
*/
function fiftyFifty () {
return Math.round(Math.random());
}
@kieranbarker
kieranbarker / getCommonAncestor.js
Last active January 25, 2021 10:21
Get the common ancestor of two or more elements
/**
* Get the common ancestor of two or more elements
* {@link https://gist.github.com/kieranbarker/cd86310d0782b7c52ce90cd7f45bb3eb}
* @param {String} selector A valid CSS selector
* @returns {Element} The common ancestor
*/
function getCommonAncestor (selector) {
// Get the elements matching the selector
const elems = document.querySelectorAll(selector);
@kieranbarker
kieranbarker / isValidEmail.js
Last active January 20, 2021 20:49
Check if a string is a valid email address
/**
* Check if a string is a valid email address
* {@link https://gist.github.com/kieranbarker/55a12cac034c386a5b3669b991290bf6}
* @param {String} str The string
* @returns {Boolean} Whether the string is a valid email address
*/
function isValidEmail (str) {
// The regular expression used by [type="email"]
// https://html.spec.whatwg.org/multipage/input.html#valid-e-mail-address
const regex = /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;