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 December 7, 2024 21:08
dotObj

Example

const foo = {
    bar: {
        baz: true
    }
}

dotObj('foo.bar.baz', foo); // true
export const encodeQueryString = (params) => {
const keys = Object.keys(params);
const encode = (param) => {
const value = params[param];
const pair = [param];
if (value) {
pair.push(value);
}
@nblackburn
nblackburn / README.md
Last active December 7, 2024 21:08
Group By

Group By

Group items by the key returned in the callback.

const data = [
    { id: 'a' },
    { id: 'b' },
    { id: 'a' }
];
@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
Last active November 5, 2024 22:27
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
Last active August 26, 2024 06:58
Break up an array into chunks of a given size.
module.exports = (data, size = 10) => {
let chunk = [];
return data.reduce((chunks, item, index) => {
chunk.push(item);
if (chunk.length >= size || index === data.length - 1) {
chunks.push(chunk);
chunk = [];
}
@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);
};