mirror of
https://github.com/status-im/sourcecred.git
synced 2025-02-03 08:13:59 +00:00
MapUtil: permit read-only input to fromObject
(#1676)
Summary: Because `fromObject` does not mutate its input, it’s safe to accept read-only inputs. This is needed for parts of the CredRank Markov process graph implementation. Test Plan: Typing test added. wchargin-branch: maputil-readonly-input
This commit is contained in:
parent
976eaf05ec
commit
1c182d4121
@ -21,7 +21,7 @@ export function toObject<K: string, V, InK: K, InV: V>(
|
||||
* iteration order, as returned by `Object.keys`.
|
||||
*/
|
||||
export function fromObject<K, V, InK: K & string, InV: V>(object: {
|
||||
[InK]: InV,
|
||||
+[InK]: InV,
|
||||
}): Map<K, V> {
|
||||
const result = new Map();
|
||||
const keys = (((Object.keys(object): string[]): any): InK[]);
|
||||
|
@ -71,6 +71,10 @@ describe("util/map", () => {
|
||||
const o: {|[string]: number|} = ({a: 1}: any);
|
||||
const _: Map<string, number> = MapUtil.fromObject(o);
|
||||
});
|
||||
it("can accept a read-only object", () => {
|
||||
const o: {+[string]: number} = {a: 1};
|
||||
const _: Map<string, number> = MapUtil.fromObject(o);
|
||||
});
|
||||
it("statically rejects a map with keys not a subtype of string", () => {
|
||||
const input: {[number]: string} = {};
|
||||
input[12] = "not okay";
|
||||
|
Loading…
x
Reference in New Issue
Block a user