|
- function replaceChildren(element, newChildren) {
- removeChildren(element);
- addChildren(element, newChildren);
- }
-
- function addChildren(element, children) {
- for (let child of children) {
- element.appendChild(child);
- }
- }
-
- function removeChildren(element) {
- while (element.lastChild) {
- element.removeChild(element.lastChild);
- }
- }
-
- function round(val, places = 0) {
- return Math.round(val * Math.pow(10, places)) / Math.pow(10, places);
- }
-
- function mapObject(obj, func) {
- return deepFreeze(Object.keys(obj).reduce((o, k) => ({ ...o, [k]: func(obj[k])}), {}));
- }
-
- function deepFreeze(object) {
-
- // Retrieve the property names defined on object
- var propNames = Object.getOwnPropertyNames(object);
-
- // Freeze properties before freezing self
-
- for (let name of propNames) {
- let value = object[name];
-
- object[name] = value && typeof value === "object" ?
- deepFreeze(value) : value;
- }
-
- return Object.freeze(object);
- }
-
- function showBuilding(key) {
- let count = belongings[key].count;
- let name = count == 1 ? buildings[key].name : buildings[key].plural;
- return count + " " + name.toLowerCase()
- }
-
- function capitalize(string) {
- return string[0].toUpperCase() + string.slice(1)
- }
-
- function weekday() {
- return ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"][(new Date()).getDay()];
- }
-
- function weightedSelect(weights) {
- const total = weights.reduce((x,y) => x+y, 0);
- const rand = Math.random() * total;
- let counter = weights[0];
- let index = 0;
- while (counter < rand) {
- index += 1;
- counter += weights[index];
- }
-
- return index;
- }
|