Skip to content

Instantly share code, notes, and snippets.

View kieranbarker's full-sized avatar

Kieran Barker kieranbarker

View GitHub Profile
@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 / 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 / toTitleCase.js
Last active May 15, 2022 15:32
Convert a string to title case
/**
* Convert a string to title case.
* https://gist.github.com/kieranbarker/293b74f1b3b46272315d2e1719786b03
* @param {string} str The string to convert.
* @returns {string} The converted string.
*/
function toTitleCase(str) {
return str
.toLowerCase()
.split(" ")
@kieranbarker
kieranbarker / serializeJSON.js
Last active May 25, 2023 11:15
Serialize all form data into a JSON string
/**
* Serialize all form data into a JSON string.
* https://gist.github.com/kieranbarker/98f8dadbf824236e42820419b1da58eb
* @param {HTMLFormElement} form The form to serialize.
* @returns {string} The serialized form data.
*/
function serializeJSON (form) {
// Create a new FormData object
const formData = new FormData(form);
@kieranbarker
kieranbarker / serializeObject.js
Last active January 11, 2021 15:15
Serialize all form data into an object
/**
* Serialize all form data into an object
* {@link https://gist.github.com/kieranbarker/245182a6e6af0b05be4233881c0aa60b}
* @param {HTMLFormElement} form The form to serialize
* @returns {Object} The serialized form data
*/
function serializeObject (form) {
// Create a new FormData object
const formData = new FormData(form);
@kieranbarker
kieranbarker / serializeArray.js
Last active October 27, 2023 02:00
Serialize all form data into an array
/**
* Serialize all form data into an array
* {@link https://gist.github.com/kieranbarker/16305b75e4fd8da6695d81865e1ba188}
* @param {HTMLFormElement} form The form to serialize
* @returns {Array} The serialized form data
*/
function serializeArray (form) {
// Create a new FormData object
const formData = new FormData(form);
@kieranbarker
kieranbarker / serialize.js
Last active January 11, 2021 12:30
Serialize all form data into a query string
/**
* Serialize all form data into a query string
* {@link https://gist.github.com/kieranbarker/bb7c54bd12156c069419f64a22e61552}
* @param {HTMLFormElement} form The form
* @returns {String} The query string
*/
function serialize (form) {
// Create a new FormData object
const formData = new FormData(form);
@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);
}