Move deepmerge to utility

This commit is contained in:
Andrei Andreev 2022-02-06 19:41:15 +03:00
parent a5d195a3ee
commit fc78a12e74
9 changed files with 53 additions and 45 deletions

View File

@ -1,9 +1,10 @@
import { GameMechanicState } from "../../game-mechanics/index.js";
import { deepmergeAll } from "@/utility/deepmerge";
class SingularityMilestoneState extends GameMechanicState {
constructor(config) {
const effect = config.effect;
const configCopy = deepmerge.all([{}, config]);
const configCopy = deepmergeAll([{}, config]);
configCopy.effect = () => effect(this.completions);
super(configCopy);
this._rawEffect = effect;

View File

@ -1,5 +1,6 @@
import { GameMechanicState } from "./game-mechanics/index.js";
import { DC } from "./constants.js";
import { deepmergeAll } from "@/utility/deepmerge";
export function startEternityChallenge() {
initializeChallengeCompletions();
@ -27,7 +28,7 @@ export function startEternityChallenge() {
class EternityChallengeRewardState extends GameMechanicState {
constructor(config, challenge) {
const effect = config.effect;
const configCopy = deepmerge.all([{}, config]);
const configCopy = deepmergeAll([{}, config]);
configCopy.effect = () => effect(challenge.completions);
super(configCopy);
this._challenge = challenge;

View File

@ -5,6 +5,8 @@
* So, this interface is implemented by a real and fake RNG class; after creating one and
* using it, call finalize on it to write the seed out.
*/
import { deepmerge } from "@/utility/deepmerge";
class GlyphRNG {
static get SECOND_GAUSSIAN_DEFAULT_VALUE() {
return 1e6;

View File

@ -1,6 +1,7 @@
import { GlyphTypes } from "./glyph-effects.js";
import { AUTOMATOR_MODE, AUTOMATOR_TYPE } from "./automator/automator-backend.js";
import { DC } from "./constants.js";
import { deepmergeAll } from "@/utility/deepmerge";
// This is actually reassigned when importing saves
// eslint-disable-next-line prefer-const
@ -845,7 +846,7 @@ window.player = {
};
export const Player = {
defaultStart: deepmerge.all([{}, player]),
defaultStart: deepmergeAll([{}, player]),
get isInMatterChallenge() {
return NormalChallenge(11).isRunning || InfinityChallenge(6).isRunning;

View File

@ -1,4 +1,5 @@
import { GameStorage } from "./storage.js";
import { deepmergeAll } from "@/utility/deepmerge";
// WARNING: Don't use state accessors and functions from global scope here, that's not safe in long-term
GameStorage.migrations = {
@ -906,7 +907,7 @@ GameStorage.migrations = {
patch(saveData) {
this.prePatch(saveData);
// This adds all the undefined properties to the save which are in player.js
const player = deepmerge.all([Player.defaultStart, saveData]);
const player = deepmergeAll([Player.defaultStart, saveData]);
const versions = Object.keys(this.patches)
.map(parseFloat)
.sort();

View File

@ -1,4 +1,5 @@
import * as ADNotations from "@antimatter-dimensions/notations";
import { deepmergeAll } from "@/utility/deepmerge";
export const GameStorage = {
currentSlot: 0,
@ -184,7 +185,7 @@ export const GameStorage = {
// eslint-disable-next-line no-console
console.log(`Savefile was invalid and has been reset - ${checkString}`);
}
player = deepmerge.all([{}, Player.defaultStart]);
player = deepmergeAll([{}, Player.defaultStart]);
player.records.gameCreatedTime = Date.now();
player.lastUpdate = Date.now();
if (isDevEnvironment()) this.devMigrations.setLatestTestVersion(player);

View File

@ -2,6 +2,7 @@ import { playFabLogin } from "./core/playfab.js";
import { DC } from "./core/constants.js";
import { SpeedrunMilestones } from "./core/speedrun.js";
import TWEEN from "tween.js";
import { deepmergeAll } from "@/utility/deepmerge";
if (GlobalErrorHandler.handled) {
throw new Error("Initialization failed");
@ -829,7 +830,7 @@ function recursiveTimeOut(fn, iterations, endFn) {
function afterSimulation(seconds, playerBefore) {
if (seconds > 600) {
const playerAfter = deepmerge.all([{}, player]);
const playerAfter = deepmergeAll([{}, player]);
Modal.awayProgress.show({ playerBefore, playerAfter, seconds });
}
@ -858,7 +859,7 @@ export function simulateTime(seconds, real, fast) {
ticks = 50;
}
const playerStart = deepmerge.all([{}, player]);
const playerStart = deepmergeAll([{}, player]);
let totalGameTime;

View File

@ -65,7 +65,6 @@
<script src="https://www.gstatic.com/firebasejs/8.8.0/firebase-database.js"></script>
<script type="text/javascript" src="lib/vue.js"></script>
<script type="text/javascript" src="../javascripts/core/storage/deepmerge.js"></script>
<script type="text/javascript" src="lib/codemirror.js"></script>
<script type="text/javascript" src="lib/simple.js"></script>
<script type="text/javascript" src="lib/show-hint.js"></script>

View File

@ -1,4 +1,4 @@
// deepmerge library modified for Antimatter Dimensions usage (mainly Decimal integration)
// Deepmerge library modified for Antimatter Dimensions usage (mainly Decimal integration)
// Source: https://github.com/TehShrike/deepmerge
function emptyTarget(val) {
@ -12,25 +12,23 @@ function cloneUnlessOtherwiseSpecified(value, options) {
if (value instanceof Set) {
return new Set(value);
}
return (options.clone !== false && options.isMergeableObject(value)) ?
deepmerge(emptyTarget(value), value, options) :
value;
return (options.clone !== false && options.isMergeableObject(value))
? deepmerge(emptyTarget(value), value, options)
: value;
}
function defaultArrayMerge(target, source, options) {
return target.concat(source).map(function(element) {
return cloneUnlessOtherwiseSpecified(element, options);
});
return target.concat(source).map(element => cloneUnlessOtherwiseSpecified(element, options));
}
function mergeObject(target, source, options) {
const destination = {};
if (options.isMergeableObject(target)) {
Object.keys(target).forEach(function(key) {
Object.keys(target).forEach(key => {
destination[key] = cloneUnlessOtherwiseSpecified(target[key], options);
});
}
Object.keys(source).forEach(function(key) {
Object.keys(source).forEach(key => {
if (target[key] && target[key] instanceof Decimal) {
destination[key] = new Decimal(source[key]);
} else if (target[key] && target[key] instanceof Set) {
@ -44,8 +42,7 @@ function mergeObject(target, source, options) {
return destination;
}
function deepmerge(target, source, options) {
options = options || {};
export function deepmerge(target, source, options = {}) {
options.arrayMerge = options.arrayMerge || defaultArrayMerge;
options.isMergeableObject = options.isMergeableObject || isMergeableObject;
@ -63,51 +60,55 @@ function deepmerge(target, source, options) {
if (!sourceAndTargetTypesMatch) {
return cloneUnlessOtherwiseSpecified(source, options);
} else if (sourceIsArray) {
return options.arrayMerge(target, source, options);
} else {
return mergeObject(target, source, options);
}
if (sourceIsArray) {
return options.arrayMerge(target, source, options);
}
return mergeObject(target, source, options);
}
deepmerge.all = function deepmergeAll(array, options) {
export function deepmergeAll(array, options) {
if (!Array.isArray(array)) {
throw new Error('first argument should be an array');
throw new Error("first argument should be an array");
}
if (!options) {
const deepCloneMerge = (destinationArray, sourceArray, options) => {
return sourceArray.map(function(element, index) {
if (destinationArray[index] && destinationArray[index] instanceof Decimal) {
return new Decimal(element);
} else if (destinationArray[index] && destinationArray[index] instanceof Set) {
return new Set(element);
} else if (!options.isMergeableObject(element) || !destinationArray[index]) {
return cloneUnlessOtherwiseSpecified(element, options);
} else {
return deepmerge(destinationArray[index], element, options);
}
});
};
// eslint-disable-next-line no-shadow
const deepCloneMerge = (destinationArray, sourceArray, options) => sourceArray.map((element, index) => {
if (destinationArray[index] && destinationArray[index] instanceof Decimal) {
return new Decimal(element);
}
if (destinationArray[index] && destinationArray[index] instanceof Set) {
return new Set(element);
}
if (!options.isMergeableObject(element) || !destinationArray[index]) {
return cloneUnlessOtherwiseSpecified(element, options);
}
return deepmerge(destinationArray[index], element, options);
});
// eslint-disable-next-line no-param-reassign
options = {
arrayMerge: deepCloneMerge
};
}
return array.reduce(function(prev, next) {
return deepmerge(prev, next, options);
}, {});
};
return array.reduce((prev, next) => deepmerge(prev, next, options), {});
}
function isMergeableObject(value) {
return isNonNullObject(value) && !isSpecial(value);
}
function isNonNullObject(value) {
return !!value && typeof value === 'object';
return Boolean(value) && typeof value === "object";
}
function isSpecial(value) {
const stringValue = Object.prototype.toString.call(value);
return stringValue === '[object RegExp]' || stringValue === '[object Date]';
}
return stringValue === "[object RegExp]" || stringValue === "[object Date]";
}