diff --git a/src/analysis/weights.js b/src/analysis/weights.js index 8f589b4..82197b1 100644 --- a/src/analysis/weights.js +++ b/src/analysis/weights.js @@ -48,6 +48,14 @@ export function defaultWeights(): Weights { }; } +export function copy(w: Weights): Weights { + return { + nodeTypeWeights: new Map(w.nodeTypeWeights), + edgeTypeWeights: new Map(w.edgeTypeWeights), + nodeManualWeights: new Map(w.nodeManualWeights), + }; +} + export type WeightsJSON = Compatible<{| +nodeTypeWeights: {[NodeAddressT]: NodeWeight}, +edgeTypeWeights: {[EdgeAddressT]: EdgeWeight}, diff --git a/src/analysis/weights.test.js b/src/analysis/weights.test.js index 14bd91a..326f277 100644 --- a/src/analysis/weights.test.js +++ b/src/analysis/weights.test.js @@ -2,9 +2,20 @@ import stringify from "json-stable-stringify"; import {NodeAddress, EdgeAddress} from "../core/graph"; -import {toJSON, fromJSON, defaultWeights} from "./weights"; +import {toJSON, fromJSON, defaultWeights, copy} from "./weights"; describe("analysis/weights", () => { + it("copy makes a copy", () => { + const w = defaultWeights(); + const w1 = copy(w); + w1.nodeTypeWeights.set(NodeAddress.empty, 33); + w1.edgeTypeWeights.set(EdgeAddress.empty, {forwards: 34, backwards: 39}); + w1.nodeManualWeights.set(NodeAddress.empty, 35); + expect(w1).not.toEqual(w); + expect(w1.nodeTypeWeights).not.toEqual(w.nodeTypeWeights); + expect(w1.edgeTypeWeights).not.toEqual(w.edgeTypeWeights); + expect(w1.nodeManualWeights).not.toEqual(w.nodeManualWeights); + }); describe("toJSON/fromJSON", () => { it("works for the default weights", () => { const weights = defaultWeights();