@typed-tabletop-simulator/lib
Version:
Library with some helping modules for working with Tabletop Simulator
49 lines (48 loc) • 1.32 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.applyDefaults = exports.shuffle = exports.rangeInclusive = exports.range = void 0;
const range = (start, end) => {
if (!end) {
end = start;
start = 0;
}
const result = [];
for (let i = start; i < end; i++) {
result.push(i);
}
return result;
};
exports.range = range;
const rangeInclusive = (start, end) => {
if (!end) {
end = start;
start = 0;
}
return (0, exports.range)(start, end + 1);
};
exports.rangeInclusive = rangeInclusive;
const shuffle = (array) => {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
const temp = array[i];
array[i] = array[j];
array[j] = temp;
}
};
exports.shuffle = shuffle;
const applyDefaults = (source, defaults) => {
const target = { ...source };
for (const [k, v] of Object.entries(defaults)) {
if (source[k]) {
if (isObject(source[k]) && isObject(v)) {
target[k] = (0, exports.applyDefaults)(source[k], v);
}
}
else {
target[k] = v;
}
}
return target;
};
exports.applyDefaults = applyDefaults;
const isObject = (a) => typeof a === "object";