Skip to content

Instantly share code, notes, and snippets.

View nblackburn's full-sized avatar

Nathaniel Blackburn nblackburn

View GitHub Profile
@nblackburn
nblackburn / README.md
Last active July 31, 2022 11:31
Clone Object

Recursively clone an object.

Usage

const obj = {
    foo: 'bar'
}

cloneObj(obj);
@nblackburn
nblackburn / README.md
Created February 10, 2022 22:48
Is Palindrome

Checks if a given word is a palindrome.

Usage

const tests = ['hannah', 'anna', 'kayak', 'apple', 'mom', 'wow', 'rotator', 'radar']

tests.forEach((test) => {
    console.log('IS_PALINDROME', test, isPalindrome(test));
});
@nblackburn
nblackburn / chunk.js
Created March 18, 2021 20:43
Break up an array into chunks of a given size.
module.exports = (data, limit = 10) => {
let chunk = -1;
let chunks = [];
for (let index = 0; index < data.length; index++) {
if (index % limit === 0) {
chunk += 1;
chunks.push([]);
}
@nblackburn
nblackburn / README.md
Created March 18, 2021 20:35
Password Entropy

Password Entropy

Calculate the entropy of a password.

Usage

const passEnt = require('passEnt');

const H = passEnt('pswd'); // 18.801758872564367
@nblackburn
nblackburn / isPromise.js
Created November 30, 2020 21:02
Check if a value is a promise or not
module.exports = (value) => {
return value && typeof value.then === 'function' && typeof value.catch === 'function';
};
const PERMISSION_WRITE = 'clipboard-write';
const canUseAsyncAPI = () => {
return (
navigator.clipboard &&
navigator.clipboard.writeText &&
navigator.permissions &&
navigator.permissions.request
);
};
module.exports = (foreground, background) => {
const l1 = Math.max(foreground, background);
const l2 = Math.min(foreground, background);
return (l1 + 0.05) / (l2 + 0.05);
};
module.exports = (red, green, blue) => {
const rs = red / 255;
const gs = green / 255;
const bs = blue / 255;
const rn = rs <= 0.03928 ? rs / 12.92 : Math.pow((rs + 0.055) / 1.055, 2.4);
const gn = gs <= 0.03928 ? gs / 12.92 : Math.pow((gs + 0.055) / 1.055, 2.4);
const bn = bs <= 0.03928 ? bs / 12.92 : Math.pow((bs + 0.055) / 1.055, 2.4);
return (0.2126 * rn) + (0.7152 * gn) + (0.0722 * bn);
module.exports = hex => {
if (hex.charAt(0) === '#') {
hex = hex.substring(1);
}
if (hex.length !== 3 && hex.length !== 6) {
return false;
}
if (hex.length === 3) {
module.exports = (red, green, blue, preferShorthand = false) => {
let colours = [red, green, blue].map(value => value.toString(16));
let canBeCondensed = colours.every(value => value.charAt(0) === value.charAt(1));
if (canBeCondensed && preferShorthand) {
colours = colours.map(v => v.charAt(0));
}
return '#' + colours.join('');
};