mirror of
https://github.com/status-im/sourcecred.git
synced 2025-03-01 04:30:29 +00:00
Use MapUtil
functions where appropriate (#496)
Summary: These call sites were selected from `git grep Map`. In this commit, we only add usage of the utility functions; we do not change any existing object types to maps. Test Plan: Running `yarn travis --full` passes. wchargin-branch: use-map-util
This commit is contained in:
parent
812b2d322e
commit
9a1fee285c
@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
import stringify from "json-stable-stringify";
|
import stringify from "json-stable-stringify";
|
||||||
|
|
||||||
|
import * as MapUtil from "../util/map";
|
||||||
|
|
||||||
export interface AddressModule<Address> {
|
export interface AddressModule<Address> {
|
||||||
/**
|
/**
|
||||||
* Assert at runtime that the provided address is actually a valid
|
* Assert at runtime that the provided address is actually a valid
|
||||||
@ -106,8 +108,9 @@ export function makeAddressModule(options: Options): AddressModule<string> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const nonceWithSeparator = nonce + separator;
|
const nonceWithSeparator = nonce + separator;
|
||||||
const otherNoncesWithSeparators = new Map();
|
const otherNoncesWithSeparators = MapUtil.mapKeys(
|
||||||
for (const [otherNonce, otherName] of otherNonces.entries()) {
|
otherNonces,
|
||||||
|
(otherNonce) => {
|
||||||
if (otherNonce === nonce) {
|
if (otherNonce === nonce) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`primary nonce listed as otherNonce: ${stringify(nonce)}`
|
`primary nonce listed as otherNonce: ${stringify(nonce)}`
|
||||||
@ -118,8 +121,9 @@ export function makeAddressModule(options: Options): AddressModule<string> {
|
|||||||
`invalid otherNonce (contains NUL): ${stringify(otherNonce)}`
|
`invalid otherNonce (contains NUL): ${stringify(otherNonce)}`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
otherNoncesWithSeparators.set(otherNonce + separator, otherName);
|
return otherNonce + separator;
|
||||||
}
|
}
|
||||||
|
);
|
||||||
|
|
||||||
function assertValid(address: Address, what?: string): void {
|
function assertValid(address: Address, what?: string): void {
|
||||||
// TODO(perf): If this function becomes a bottleneck, consider
|
// TODO(perf): If this function becomes a bottleneck, consider
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
import {type Edge, type Graph, type NodeAddressT, NodeAddress} from "../graph";
|
import {type Edge, type Graph, type NodeAddressT, NodeAddress} from "../graph";
|
||||||
import type {Distribution, SparseMarkovChain} from "./markovChain";
|
import type {Distribution, SparseMarkovChain} from "./markovChain";
|
||||||
|
import * as MapUtil from "../../util/map";
|
||||||
|
|
||||||
export type Probability = number;
|
export type Probability = number;
|
||||||
export type Contributor =
|
export type Contributor =
|
||||||
@ -132,10 +133,8 @@ export function createContributions(
|
|||||||
function createNodeAddressMarkovChain(
|
function createNodeAddressMarkovChain(
|
||||||
ntc: NodeToContributions
|
ntc: NodeToContributions
|
||||||
): NodeAddressMarkovChain {
|
): NodeAddressMarkovChain {
|
||||||
const result: NodeAddressMarkovChain = new Map();
|
return MapUtil.mapValues(ntc, (target, contributions) => {
|
||||||
for (const [target, contributions] of ntc.entries()) {
|
|
||||||
const inNeighbors = new Map();
|
const inNeighbors = new Map();
|
||||||
result.set(target, inNeighbors);
|
|
||||||
for (const contribution of contributions) {
|
for (const contribution of contributions) {
|
||||||
const source = contributorSource(target, contribution.contributor);
|
const source = contributorSource(target, contribution.contributor);
|
||||||
inNeighbors.set(
|
inNeighbors.set(
|
||||||
@ -143,8 +142,8 @@ function createNodeAddressMarkovChain(
|
|||||||
contribution.weight + (inNeighbors.get(source) || 0)
|
contribution.weight + (inNeighbors.get(source) || 0)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
return inNeighbors;
|
||||||
return result;
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function nodeAddressMarkovChainToOrderedSparseMarkovChain(
|
function nodeAddressMarkovChainToOrderedSparseMarkovChain(
|
||||||
|
@ -11,6 +11,7 @@ import {
|
|||||||
normalizeNeighbors,
|
normalizeNeighbors,
|
||||||
permute,
|
permute,
|
||||||
} from "./graphToMarkovChain";
|
} from "./graphToMarkovChain";
|
||||||
|
import * as MapUtil from "../../util/map";
|
||||||
|
|
||||||
import {advancedGraph} from "../graphTestUtil";
|
import {advancedGraph} from "../graphTestUtil";
|
||||||
|
|
||||||
@ -124,12 +125,7 @@ describe("core/attribution/graphToMarkovChain", () => {
|
|||||||
{contributor: {type: "OUT_EDGE", edge: e4}, weight: 3 / 16},
|
{contributor: {type: "OUT_EDGE", edge: e4}, weight: 3 / 16},
|
||||||
]);
|
]);
|
||||||
const canonicalize = (map) =>
|
const canonicalize = (map) =>
|
||||||
new Map(
|
MapUtil.mapValues(map, (_, v) => sortBy(v, (x) => JSON.stringify(x)));
|
||||||
Array.from(map.entries()).map(([k, v]) => [
|
|
||||||
k,
|
|
||||||
sortBy(v, (x) => JSON.stringify(x)),
|
|
||||||
])
|
|
||||||
);
|
|
||||||
expect(canonicalize(actual)).toEqual(canonicalize(expected));
|
expect(canonicalize(actual)).toEqual(canonicalize(expected));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -15,6 +15,7 @@ import type {
|
|||||||
} from "./nodes";
|
} from "./nodes";
|
||||||
import * as Q from "./graphql";
|
import * as Q from "./graphql";
|
||||||
import * as GitNode from "../git/nodes";
|
import * as GitNode from "../git/nodes";
|
||||||
|
import * as MapUtil from "../../util/map";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
reviewUrlToId,
|
reviewUrlToId,
|
||||||
@ -191,14 +192,14 @@ export class RelationalView {
|
|||||||
|
|
||||||
toJSON(): RelationalViewJSON {
|
toJSON(): RelationalViewJSON {
|
||||||
const rawJSON = {
|
const rawJSON = {
|
||||||
repos: addressMapToObject(this._repos),
|
repos: MapUtil.toObject(this._repos),
|
||||||
issues: addressMapToObject(this._issues),
|
issues: MapUtil.toObject(this._issues),
|
||||||
pulls: addressMapToObject(this._pulls),
|
pulls: MapUtil.toObject(this._pulls),
|
||||||
reviews: addressMapToObject(this._reviews),
|
reviews: MapUtil.toObject(this._reviews),
|
||||||
comments: addressMapToObject(this._comments),
|
comments: MapUtil.toObject(this._comments),
|
||||||
userlikes: addressMapToObject(this._userlikes),
|
userlikes: MapUtil.toObject(this._userlikes),
|
||||||
references: addressMapToObject(this._mapReferences),
|
references: MapUtil.toObject(this._mapReferences),
|
||||||
referencedBy: addressMapToObject(this._mapReferencedBy),
|
referencedBy: MapUtil.toObject(this._mapReferencedBy),
|
||||||
};
|
};
|
||||||
return toCompat(COMPAT_INFO, rawJSON);
|
return toCompat(COMPAT_INFO, rawJSON);
|
||||||
}
|
}
|
||||||
@ -206,14 +207,14 @@ export class RelationalView {
|
|||||||
static fromJSON(compatJson: RelationalViewJSON): RelationalView {
|
static fromJSON(compatJson: RelationalViewJSON): RelationalView {
|
||||||
const json = fromCompat(COMPAT_INFO, compatJson);
|
const json = fromCompat(COMPAT_INFO, compatJson);
|
||||||
const rv = new RelationalView();
|
const rv = new RelationalView();
|
||||||
rv._repos = objectToAddressMap(json.repos);
|
rv._repos = MapUtil.fromObject(json.repos);
|
||||||
rv._issues = objectToAddressMap(json.issues);
|
rv._issues = MapUtil.fromObject(json.issues);
|
||||||
rv._pulls = objectToAddressMap(json.pulls);
|
rv._pulls = MapUtil.fromObject(json.pulls);
|
||||||
rv._reviews = objectToAddressMap(json.reviews);
|
rv._reviews = MapUtil.fromObject(json.reviews);
|
||||||
rv._comments = objectToAddressMap(json.comments);
|
rv._comments = MapUtil.fromObject(json.comments);
|
||||||
rv._userlikes = objectToAddressMap(json.userlikes);
|
rv._userlikes = MapUtil.fromObject(json.userlikes);
|
||||||
rv._mapReferences = objectToAddressMap(json.references);
|
rv._mapReferences = MapUtil.fromObject(json.references);
|
||||||
rv._mapReferencedBy = objectToAddressMap(json.referencedBy);
|
rv._mapReferencedBy = MapUtil.fromObject(json.referencedBy);
|
||||||
return rv;
|
return rv;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -838,27 +839,6 @@ export type ParentEntity = Repo | Issue | Pull | Review;
|
|||||||
export type ChildEntity = Issue | Pull | Review | Comment;
|
export type ChildEntity = Issue | Pull | Review | Comment;
|
||||||
export type ReferentEntity = Repo | Issue | Pull | Review | Comment | Userlike;
|
export type ReferentEntity = Repo | Issue | Pull | Review | Comment | Userlike;
|
||||||
|
|
||||||
function addressMapToObject<T>(
|
|
||||||
x: Map<N.RawAddress, T>
|
|
||||||
): AddressEntryMapJSON<T> {
|
|
||||||
const result: {[N.RawAddress]: T} = {};
|
|
||||||
for (const [address, entry] of x.entries()) {
|
|
||||||
result[address] = entry;
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
function objectToAddressMap<T>(
|
|
||||||
x: AddressEntryMapJSON<T>
|
|
||||||
): Map<N.RawAddress, T> {
|
|
||||||
const result = new Map();
|
|
||||||
for (const key of Object.keys(x)) {
|
|
||||||
const address: N.RawAddress = (key: any);
|
|
||||||
result.set(address, x[address]);
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
export opaque type AddressEntryMapJSON<T> = {[N.RawAddress]: T};
|
export opaque type AddressEntryMapJSON<T> = {[N.RawAddress]: T};
|
||||||
export opaque type RelationalViewJSON = Compatible<{|
|
export opaque type RelationalViewJSON = Compatible<{|
|
||||||
+repos: AddressEntryMapJSON<RepoEntry>,
|
+repos: AddressEntryMapJSON<RepoEntry>,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user