Skip to content

Instantly share code, notes, and snippets.

Avatar

Kieran Barker kieranbarker

View GitHub Profile
@kieranbarker
kieranbarker / toTitleCase.js
Last active May 15, 2022
Convert a string to title case
View toTitleCase.js
/**
* 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 Apr 14, 2022
Asynchronously check if a file exists in Node.js.
View file_exists.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 Mar 23, 2022
A simple demo of a cross-site scripting (XSS) attack.
View xss.html
<!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 Mar 21, 2022
Get the JSON data from a Response object.
View getJSON.js
/**
* 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 / index.html
Created Mar 20, 2022
A simple demo of the top-level await keyword in vanilla JS.
View index.html
<!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 / inspect.js
Last active Mar 14, 2022
A demo of the util.inspect() method in Node.js.
View inspect.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 }
View getTodaysDate.js
/**
* Get today's date.
* https://gist.github.com/kieranbarker/2c300d73059697a4417e12bd40cdef75
* @returns {Date} A Date object representing today's date at midnight in UTC.
*/
function getTodaysDate() {
const dateString = new Date().toISOString().slice(0, 10);
return new Date(dateString);
}
@kieranbarker
kieranbarker / append_file.js
Last active Nov 8, 2021
Asynchronously append data to a file in Node.js.
View append_file.js
import * as fs from 'fs';
import * as fsPromises from 'fs/promises';
const file = 'hello_world.txt';
const data = 'Hello, World!';
//
// Callback API
//
@kieranbarker
kieranbarker / package.json
Last active Nov 1, 2021
Asynchronously read and write files in Node.js.
View package.json
{
"name": "read_write",
"version": "1.0.0",
"description": "Asynchronously read and write files.",
"private": true,
"main": "read_write.js",
"type": "module",
"scripts": {
"start": "node read_write.js",
"test": "echo \"Error: no test specified\" && exit 1"
@kieranbarker
kieranbarker / states.json
Created Oct 4, 2021
All 50 US states as a JSON array.
View states.json
[
"alabama",
"alaska",
"arizona",
"arkansas",
"california",
"colorado",
"connecticut",
"delaware",
"florida",