Skip to content

Instantly share code, notes, and snippets.

Avatar

Kieran Barker kieranbarker

View GitHub Profile
@kieranbarker
kieranbarker / serve
Last active Sep 5, 2021
.zshrc alias for simple Python server
View serve
# Alias for simple Python server
serve() {
python3 -m http.server $1
}
@kieranbarker
kieranbarker / array_at.js
Created May 16, 2021
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.
View array_at.js
/**
* 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 Feb 1, 2021
Get a random truthy/falsy value
View fiftyFifty.js
/**
* 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 Jan 25, 2021
Get the common ancestor of two or more elements
View getCommonAncestor.js
/**
* 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 Apr 12, 2021
Convert a string to title case
View toTitleCase.js
/**
* Convert a string to title case
* {@link 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(' ').map(function(word) {
return word.charAt(0).toUpperCase() + word.slice(1);
}).join(' ');
@kieranbarker
kieranbarker / serializeJSON.js
Last active Mar 11, 2021
Serialize all form data into a JSON string
View serializeJSON.js
/**
* Serialize all form data into a JSON string
* {@link 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 Jan 11, 2021
Serialize all form data into an object
View serializeObject.js
/**
* 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 Jan 11, 2021
Serialize all form data into an array
View serializeArray.js
/**
* 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 Jan 11, 2021
Serialize all form data into a query string
View serialize.js
/**
* 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 Feb 24, 2021
Format a JSON string for readability
View formatJSON.js
/**
* 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);
}