Skip to content

Instantly share code, notes, and snippets.

View hperrin's full-sized avatar

Hunter Perrin hperrin

View GitHub Profile
@hperrin
hperrin / splitn.js
Last active January 12, 2022 07:35
splitn.js, a split function that returns everything after the limit in the last element.
export default const splitn = (s, d, n = Infinity) => {
const a = s.split(d);
return [
...a.slice(0, n - 1),
...(a.length >= n ? [a.slice(n - 1).join(d)] : [])
];
};
@hperrin
hperrin / percenttwenty.js
Created August 12, 2021 21:57
Useful functions written in the most ridiculous way.
export const repeat = (str, num) => `${Array(num + 1)}`.replaceAll(/,/g, str);
export const kebabCase = (string, delimiter = " ") =>
string
.split(delimiter)
.map((word) => [
`${Number.NEGATIVE_INFINITY}`.slice(0, 1)[0],
word.toLowerCase(),
])
.flat()
function verses(bottles) {
const count = (b, cap) => `${b || `${cap ? 'N' : 'n'}o more`} bottle${b == 1 ? '' : 's'}`;
return [...new Array(bottles + 1)].map((v, i) => bottles - i).map(b => [
`${count(b, true)} of beer on the wall, ${count(b)} of beer.`,
b > 0
? `Take one down and pass it around, ${count(b - 1)} of beer on the wall.`
: `Go to the store and buy some more, ${count(bottles)} of beer on the wall.`
]);
}
// Returns true about 50% of the time, false about 50% of the time.
export default const maybe = () => Math.random() < .5;
// This should be closer to .5 the more iterations you run.
export const tester = iterations => {
let yes = 0;
for (let i = 0; i < iterations; i++) maybe() && yes++;
return yes/iterations;
}
@hperrin
hperrin / index.js
Last active May 9, 2018 00:01
JavaScript strRepeat. (Ridiculously efficient.)
export default function strRepeat (str, repeat) {
let curStr = str, i = 1, result = '';
while (true) {
if (i & repeat) result += curStr;
i *= 2;
if (i >= repeat) break;
curStr += curStr;
}
return result;
}
@hperrin
hperrin / index.js
Created March 14, 2018 01:26
insertInOrder JavaScript Array method. (Insert item into a sorted array.)
Array.prototype.insertInOrder = function(item) {
for (var i = 0; this[i] < item && i < this.length; i++);
this.splice(i, 0, item);
};
@hperrin
hperrin / index.html
Last active January 1, 2018 17:52 — forked from anonymous/index.html
<html>
<body style="font-size: 16pt;">
<p style="text-align: center;">Email: <a href="mailto:chark1212@gmail.com">chark1212@gmail.com</a></p>
<p style="text-align: center;">Cell: <a href="tel:+18582292679">858-229-2679</a></p>
</body>
</html>
@hperrin
hperrin / bootstrap-spotter.css
Last active May 11, 2017 00:09
Spot Bootstrap with ease.
/*!
* Bootstrap v3.3.7 (http://getbootstrap.com)
* Copyright 2011-2016 Twitter, Inc.
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
*/
/*! normalize.css v3.0.3 | MIT License | github.com/necolas/normalize.css */
html {
font-family: sans-serif;
-webkit-text-size-adjust: 100%;
-ms-text-size-adjust: 100%;
@hperrin
hperrin / README.md
Last active June 8, 2017 01:30
Radical Mode
@hperrin
hperrin / frontend.js
Last active May 10, 2018 19:56
Equivalent SQL Query from Frontend
function titleSeach (title, archived) {
return fetch(
'/api/titlesearch.php?title=' +
encodeURIComponent(title) +
'&archived=' +
encodeURIComponent(JSON.stringify(archived))
).then(res => {
if (!res.ok) return Promise.reject(res);
return res.json()
});