maplair
Version:
JavaScript library for mobile-friendly interactive maps
71 lines (70 loc) • 2.06 kB
JavaScript
import { __exportAll } from "../_virtual/_rolldown/runtime.js";
//#region src/core/Util.js
var Util_exports = /* @__PURE__ */ __exportAll({
falseFn: () => falseFn,
formatNum: () => formatNum,
lastId: () => lastId,
setOptions: () => setOptions,
splitWords: () => splitWords,
stamp: () => stamp,
template: () => template,
throttle: () => throttle,
wrapNum: () => wrapNum
});
let lastId = 0;
function stamp(obj) {
if (!("_maplair_id" in obj)) obj["_maplair_id"] = ++lastId;
return obj._maplair_id;
}
function throttle(fn, time, context) {
let lock, queuedArgs;
function later() {
lock = false;
if (queuedArgs) {
wrapperFn.apply(context, queuedArgs);
queuedArgs = false;
}
}
function wrapperFn(...args) {
if (lock) queuedArgs = args;
else {
fn.apply(context, args);
setTimeout(later, time);
lock = true;
}
}
return wrapperFn;
}
function wrapNum(x, range, includeMax) {
const max = range[1], min = range[0], d = max - min;
return x === max && includeMax ? x : ((x - min) % d + d) % d + min;
}
function falseFn() {
return false;
}
function formatNum(num, precision) {
if (precision === false) return num;
const pow = 10 ** (precision === void 0 ? 6 : precision);
return Math.round(num * pow) / pow;
}
const WHITESPACE_RE = /\s+/;
function splitWords(str) {
return str.trim().split(WHITESPACE_RE);
}
function setOptions(obj, options) {
if (!Object.hasOwn(obj, "options")) obj.options = obj.options ? Object.create(obj.options) : {};
for (const i in options) if (Object.hasOwn(options, i)) obj.options[i] = options[i];
return obj.options;
}
const templateRe = /\{ *([\w_ -]+) *\}/g;
function template(str, data) {
return str.replace(templateRe, (str, key) => {
let value = data[key];
if (value === void 0) throw new Error(`No value provided for variable ${str}`);
else if (typeof value === "function") value = value(data);
return value;
});
}
//#endregion
export { Util_exports, falseFn, formatNum, lastId, setOptions, splitWords, stamp, template, throttle, wrapNum };
//# sourceMappingURL=Util.js.map