This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as fs from 'fs'; | |
import * as fsPromises from 'fs/promises'; | |
const file = 'hello_world.txt'; | |
const data = 'Hello, World!'; | |
// | |
// Callback API | |
// |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
"alabama", | |
"alaska", | |
"arizona", | |
"arkansas", | |
"california", | |
"colorado", | |
"connecticut", | |
"delaware", | |
"florida", |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Alias for simple Python server | |
serve() { | |
python3 -m http.server $1 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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()); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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])?)*$/; |