Skip to content

Instantly share code, notes, and snippets.

@Richienb
Richienb / index.js
Last active November 18, 2021 15:11
Wrap words
const string = ``;
const wrapWords = (string, width) => string.replace(new RegExp(`(?![^\\n]{1,${width}}$)([^\\n]{1,${width}})\\s`, 'g'), '$1\n');
console.log(string.split('\n').map(value => wrapWords(value, 80)).join('\n\n'));
function split(array, groups = 2) {
array = array.sort((a, b) => b - a);
const parts = Array.from({length: groups}, () => []);
const partLengths = Array.from({length: groups}, () => 0);
for (const item of array) {
const partIndex = partLengths.indexOf(Math.min(...partLengths));
parts[partIndex].push(item);
partLengths[partIndex] += item;
@Richienb
Richienb / index.user.js
Last active September 1, 2022 11:21
I'm not a Fortnite player
// ==UserScript==
// @name I'm not a Fortnite player
// @namespace https://github.com/Richienb
// @version 0.1
// @description Change ReCAPTCHA and hCaptcha labels to be something more epic
// @author Richie Bendall
// @match *://www.google.com/recaptcha/api2/anchor*
// @match *://newassets.hcaptcha.com/captcha/v1/*/static/hcaptcha-checkbox.html*
// @grant none
// ==/UserScript==
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Finding distinct items in an array - See https://stackoverflow.com/questions/63396741/how-do-i-get-the-numbers-that-do-not-have-a-duplicated-value-in-an-array-of-numb/63396901</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/1.0.0/benchmark.min.js"></script>
<script src="./suite.js"></script>
</head>
<body>
<h1>Open the console to view the results</h1>
@Richienb
Richienb / index.js
Created July 28, 2020 10:44
scratch-http v0.1.0
/* https://github.com/Richienb/scratch-http v0.1.0 */
'use strict';(function(){function n(e){"@babel/helpers - typeof";n="function"===typeof Symbol&&"symbol"===typeof Symbol.iterator?function(c){return typeof c}:function(c){return c&&"function"===typeof Symbol&&c.constructor===Symbol&&c!==Symbol.prototype?"symbol":typeof c};return n(e)}function K(e,c){if(!(e instanceof c))throw new TypeError("Cannot call a class as a function");}function L(e,c){for(var b=0;b<c.length;b++){var a=c[b];a.enumerable=a.enumerable||!1;a.configurable=!0;"value"in a&&(a.writable=
!0);Object.defineProperty(e,a.key,a)}}function V(e,c,b){c&&L(e.prototype,c);b&&L(e,b);return e}function M(e,c){var b=Object.keys(e);if(Object.getOwnPropertySymbols){var a=Object.getOwnPropertySymbols(e);c&&(a=a.filter(function(a){return Object.getOwnPropertyDescriptor(e,a).enumerable}));b.push.apply(b,a)}return b}function y(e){for(var c=1;c<arguments.length;c++){var b=null!=arguments[c]?arguments[c]:{};c%2?M(Object(b),!0).forEach(function(a){var c=b[a];a in
@Richienb
Richienb / index.js
Last active July 22, 2020 05:09
Synchronously copy text to the clipboard in browsers
function copyText(text) {
const copyElement = document.createElement("textarea")
document.querySelector("body").appendChild(copyElement)
copyElement.textContent = text
copyElement.select()
document.execCommand("copy")
document.querySelector("body").removeChild(copyElement)
}
@Richienb
Richienb / syncfork.sh
Created July 19, 2020 07:55
Synchronise the current branch of a fork with the upstream repository.
UPSTREAM_BRANCH=$1
if [ -z "$UPSTREAM_BRANCH" ]; then
UPSTREAM_BRANCH=$(git branch --show-current)
fi
git fetch upstream &&
git merge upstream/${UPSTREAM_BRANCH}
@Richienb
Richienb / index.js
Created July 16, 2020 17:19
Get total month NPM download count for user
const { search } = require("npms-api")
const got = require("got")
const totalled = require("totalled")
const pMap = require("p-map")
const npmUsername = "richienb"
const downloadCount = async packageName => {
try {
const {body} = await got(packageName, {
@Richienb
Richienb / index.js
Last active July 16, 2020 17:20
Promisify a task
module.exports = taskFunction => {
return (...arguments_) => module.exports.task(taskFunction(...arguments_))
}
module.exports.task = task => new Promise((resolve, reject) => {
task.fork(reject, resolve)
})