Skip to content

Instantly share code, notes, and snippets.

View kieranbarker's full-sized avatar

Kieran Barker kieranbarker

View GitHub Profile
@kieranbarker
kieranbarker / favicon.svg
Created October 29, 2020 18:20
A simple snippet for an SVG favicon
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@kieranbarker
kieranbarker / getLastWord.js
Created November 27, 2020 10:09
Get the last word in a string
/**
* Get the last word in a string
* @param {String} str The string
* @returns {String} The last word
*/
function getLastWord (str) {
return str.trim().split(/\s+/).pop();
}
@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 / 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 / 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 / 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 / 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 / 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 / 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 / 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());
}