Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or 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 last word in a string | |
* @param {String} str The string | |
* @returns {String} The last word | |
*/ | |
function getLastWord (str) { | |
return str.trim().split(/\s+/).pop(); | |
} |
This file contains hidden or 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 hidden or 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
/** | |
* 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); |
This file contains hidden or 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
/** | |
* 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); |
This file contains hidden or 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
/** | |
* 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); |
This file contains hidden or 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
/** | |
* 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); |
This file contains hidden or 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
/** | |
* 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(" ") |
This file contains hidden or 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 hidden or 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()); | |
} |