Skip to content

Instantly share code, notes, and snippets.

Avatar
🦉
Working on something exciting

Nathaniel Blackburn nblackburn

🦉
Working on something exciting
View GitHub Profile
View clipboard.js
const PERMISSION_WRITE = 'clipboard-write';
const canUseAsyncAPI = () => {
return (
navigator.clipboard &&
navigator.clipboard.writeText &&
navigator.permissions &&
navigator.permissions.request
);
};
@nblackburn
nblackburn / chunk.js
Created Mar 10, 2020
Break up an array into chunks of a given size
View chunk.js
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([]);
}
View contrast.js
module.exports = (foreground, background) => {
const l1 = Math.max(foreground, background);
const l2 = Math.min(foreground, background);
return (l1 + 0.05) / (l2 + 0.05);
};
View luminence.js
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);
View hexToRGB.js
module.exports = hex => {
if (hex.charAt(0) === '#') {
hex = hex.substring(1);
}
if (hex.length !== 3 && hex.length !== 6) {
return false;
}
if (hex.length === 3) {
View rgbToHex.js
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('');
};
View scheduler.js
let events = [];
let timeout = null;
let interval = 100;
const getScheduled = () => {
let now = Date.now();
return events.filter(event => {
return (event.lastInvoked + event.interval) < now;
});
@nblackburn
nblackburn / randomWeight.js
Last active Oct 15, 2018
Select a random item from an array of weights
View randomWeight.js
module.exports = weights => {
let seed = Math.random() * weights.length;
return weights.find(weight => (seed - weight) <= 0);
};
View consecutive-search.js
module.exports = (query, result, insensitive) => {
let index = 0;
let last = 0;
let matched = false;
// Normalize the strings we are comparing to each other.
if (insensitive) {
query = query.toLowerCase();
result = result.toLowerCase();
}
View blockstack.md
You can’t perform that action at this time.