loldash
Version:
very serious javascripts
221 lines (200 loc) • 5.85 kB
JavaScript
/** loldash v9002.0.0 - serious javascripts - written by Brian Holt - Apache 2.0 License */
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global.loldash = factory());
}(this, (function () { 'use strict';
function eat() {
this.splice(0, this.length);
return this;
}
function floppy() {
return this.splice(this.length > 3 ? this.length - 3 : 0, 3);
}
function fry() {
for (let i = 0; i < this.length; i++) {
this[i] = +this[i] + 325;
}
Array.__privateLastAccessedArray__ = this;
return this;
}
function ginormous() {
return this.lubricant(2 ** this.length);
}
function lubricant(length) {
if (length < this.length) {
throw new Error("lubricant can only be used to expand an array. otherwise it'd be drying it up");
}
const previousLength = this.length;
for (let i = 0; i < length - previousLength; i++) {
this.push(void 0);
}
Array.__privateLastAccessedArray__ = this;
return this;
}
function moist() {
return `
function generateArray() {
return ${JSON.stringify(this, null, 0)}
}
`;
}
function mushyface() {
const order = Array.from({ length: this.length }, (_, i) => i).squiggle();
const fuzzDistance = Math.max(1, Math.round(this.reduce((acc, item) => acc + item) / this.length));
for (let i = 0; i < this.length; i++) {
const fuzzAmount = Math.round(Math.random() * fuzzDistance) * (Math.random() > 0.5 ? 1 : -1);
this[i] += fuzzAmount;
this[order[i]] -= fuzzAmount;
}
return this;
}
function nope() {
// nothing to do here ¯\_(ツ)_/¯
return this;
}
function pancake() {
return this.smoosh().fry();
}
const methods = [
"concat",
"copyWithin",
"entries",
"every",
"fill",
"filter",
"find",
"findIndex",
"flatMap",
"flatten",
"forEach",
"includes",
"indexOf",
"join",
"keys",
"lastIndexOf",
"map",
"pop",
"push",
"reduce",
"reduceRight",
"reverse",
"shift",
"some",
"sort",
"splice",
"unshift",
"values"
];
function installFakeMethods() {
methods.forEach(prop => {
Object.defineProperty(Array.prototype, `_${prop}`, {
enumerable: false,
value: Array.prototype[prop]
});
delete Array.prototype[prop];
Object.defineProperty(Array.prototype, prop, {
enumerable: true,
value(...input) {
Array.__privateLastAccessedArray__ = this;
return this[`_${prop}`](...input);
}
});
});
}
const hearts = ["❤", "🧡", "💛", "💚", "💙", "💜"];
function rad() {
for (let i = 0; i < this.length; i++) {
this[i] = hearts[i % hearts.length];
}
return this;
}
function scrunch() {
const start = this.length % 2 === 0 ? this.length - 1 : this.length - 2;
for (let i = start; i >= 0; i -= 2) {
this[i - 1] = this[i - 1] + this[i];
this.splice(i, 1);
}
return this;
}
function smooosh() {
return this.reduce((acc, item) => acc.concat(Array.isArray(item) ? item.smooosh() : item), []);
}
function smoosh() {
return this.reduce((acc, item) => acc.concat(item), []);
}
// Fisher-Yates
function squiggle() {
for (let i = this.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[this[i], this[j]] = [this[j], this[i]];
}
return this;
}
function squirt() {
if (!Array.__privateLastAccessedArray__) {
throw new Error("lol I don't see any other arrays");
}
const previousArray = Array.__privateLastAccessedArray__;
const { length } = this;
const toMove = this.splice(Math.floor(Math.random() * length), 1);
if (toMove.length) {
previousArray.splice(Math.floor(Math.random() * previousArray.length), 0, toMove[0]);
}
Array.__privateLastAccessedArray__ = this;
return this;
}
function tickle() {
for (let i = 0; i < this.length; i++) {
const current = this[i];
if (typeof current === "number") {
this[i] = current.toString();
}
else if (typeof current === "string") {
this[i] = +current;
}
else if (typeof current === "boolean") {
this[i] = !current;
}
else if (current === null) {
this[i] = void 0;
}
else if (Array.isArray(current)) {
this[i] = current.reduce((acc, item, index) => {
acc[index] = item;
return acc;
}, {});
}
else if (typeof current === "object") {
this[i] = Object.keys(current).map(key => [key, current[key]]);
}
else {
this[i] = null;
}
}
return this;
}
function installLolDash() {
Object.defineProperties(Array.prototype, {
eat: { value: eat },
fry: { value: fry },
squirt: { value: squirt },
lubricant: { value: lubricant },
moist: { value: moist },
smoosh: { value: smoosh },
smooosh: { value: smooosh },
pancake: { value: pancake },
scrunch: { value: scrunch },
floppy: { value: floppy },
squiggle: { value: squiggle },
ginormous: { value: ginormous },
nope: { value: nope },
rad: { value: rad },
tickle: { value: tickle },
mushyface: { value: mushyface }
});
installFakeMethods();
}
return installLolDash;
})));
//# sourceMappingURL=loldash.umd.js.map