Skip to content

Instantly share code, notes, and snippets.

View kieranbarker's full-sized avatar

Kieran Barker kieranbarker

View GitHub Profile
@kieranbarker
kieranbarker / index.html
Created March 20, 2022 19:19
A simple demo of the top-level await keyword in vanilla JS.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Users</title>
</head>
<body>
<h1>Users</h1>
<p>Check the JavaScript console.</p>
@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);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HTML</title>
</head>
<body>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Kieran Barker</title>
</head>
<body>
@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 / file_exists.js
Last active April 14, 2022 19:11
Asynchronously check if a file exists in Node.js.
//
// Callback API
//
import { access, constants } from 'fs';
const file = 'package.json';
access(file, constants.F_OK, error => {
if (error) {
@kieranbarker
kieranbarker / xss.html
Created March 23, 2022 17:33
A simple demo of a cross-site scripting (XSS) attack.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>XSS</title>
</head>
<body></body>
<script>
@kieranbarker
kieranbarker / getJSON.js
Last active March 21, 2022 09:42
Get the JSON data from a Response object.
/**
* Get the JSON data from a Response object.
* @param {Response} response The Response object.
* @returns {Promise} The JSON data or an Error.
*/
async function getJSON(response) {
if (response.ok) {
const data = await response.json();
return Promise.resolve(data);
}
@kieranbarker
kieranbarker / inspect.js
Last active March 14, 2022 09:07
A demo of the util.inspect() method in Node.js.
const { inspect } = require('util');
const restaurants = [
{
name: 'Nando\'s',
menus: {
starters: [
{ name: 'Halloumi Sticks &amp; Dip', price: 425 },
{ name: 'Houmous with PERi-PERi Drizzle', price: 425 },
{ name: 'Sweet Potato Wedges with Garlic PERinaise', price: 425 }