@litecanvas/utils
Version:
Utilities to help build litecanvas games
111 lines (97 loc) • 3.17 kB
JavaScript
(() => {
var __defProp = Object.defineProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
// src/_global.js
window.utils = window.utils || {};
window.utils.global = (overrides = true) => {
for (const key in window.utils) {
if ("global" === key) continue;
if (overrides || globalThis[key] === void 0) {
globalThis[key] = window.utils[key];
}
}
};
// src/collection/index.js
var index_exports = {};
__export(index_exports, {
choose: () => choose_default,
head: () => head_default,
includes: () => includes_default,
last: () => last_default,
length: () => length_default,
range: () => range_default,
shuffle: () => shuffle_default,
tail: () => tail_default
});
// src/collection/range.js
var range_default = (size, from = 0, step = 1) => [...Array(size | 0).keys()].map((i) => {
return from + step * i;
});
// src/collection/shuffle.js
var shuffle_default = (values, rng = window.rand || Math.random) => {
values = [...values];
for (let i = values.length - 1; i > 0; i--) {
let j = Math.floor(rng() * (i + 1));
let temp = values[i];
values[i] = values[j];
values[j] = temp;
}
return values;
};
// src/collection/choose.js
var choose_default = (values, rng = window.rand || Math.random) => {
return values[Math.floor(rng() * values.length)];
};
// src/collection/head.js
var head_default = (values) => values[0];
// src/collection/last.js
var last_default = (values) => values[values.length - 1];
// src/collection/tail.js
var tail_default = (values) => values.slice(1);
// src/debug/assert.js
var assert_default = (condition, message = "Assertion failed") => {
if (!condition) throw new Error(message);
};
// src/debug/is.js
var is_default = (value, type) => {
switch (type) {
case "function":
return value instanceof type;
case "array":
return Array.isArray(value);
case "int":
return Number.isInteger(value);
case "number":
return "number" === typeof value && !Number.isNaN(value);
case "infinity":
return "number" === typeof value && Infinity === Math.abs(value);
default:
return typeof value === type;
}
};
// src/collection/length.js
var length_default = (xs) => {
DEV: assert_default(
is_default(xs, "string") || is_default(xs, "array"),
"[litecanvas/utils] length() 1st param must be an array or string"
);
return ~~xs?.length;
};
// src/collection/includes.js
var includes_default = (xs, search, position = 0) => {
DEV: assert_default(
is_default(xs, "string") || is_default(xs, "array"),
"[litecanvas/utils] includes() 1st param must be an array or string"
);
DEV: assert_default(
is_default(xs, "int"),
"[litecanvas/utils] includes() 3rd param must be an integer"
);
return xs?.includes(search, position);
};
// src/collection/_web.js
window.utils = Object.assign(window.utils || {}, index_exports);
})();