Skip to content

Commit

Permalink
refactor(cosmic-swingset): DRY out parseParams
Browse files Browse the repository at this point in the history
  • Loading branch information
gibson042 committed Nov 8, 2024
1 parent 50103ea commit c8a4508
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions packages/cosmic-swingset/src/params.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
import { X, Fail, makeError } from '@endo/errors';
import { Nat, isNat } from '@endo/nat';

/**
* @template {number | bigint} T
* @param {T} n
* @returns {T}
*/
const requireNat = n => (isNat(n) ? n : Fail`${n} must be a positive integer`);

/**
* @param {string} s
* @returns {bigint}
Expand Down Expand Up @@ -37,14 +44,10 @@ const recordFromEntries = (
}),
);

/** @param {{key: string, size: number}[]} queueSizeEntries */
export const parseQueueSizes = queueSizeEntries =>
Object.fromEntries(
queueSizeEntries.map(({ key, size }) => {
typeof key === 'string' || Fail`Key ${key} must be a string`;
isNat(size) || Fail`Size ${size} is not a positive integer`;
return [key, size];
}),
export const parseQueueSizes = entryRecords =>
recordFromEntries(
entryRecords.map(({ key, size }) => [key, size]),
requireNat,
);

/** @param {Record<string, number>} queueSizes */
Expand All @@ -68,11 +71,9 @@ export const parseParams = params => {

Array.isArray(rawBeansPerUnit) ||
Fail`beansPerUnit must be an array, not ${rawBeansPerUnit}`;
const beansPerUnit = Object.fromEntries(
rawBeansPerUnit.map(({ key, beans }) => {
typeof key === 'string' || Fail`Key ${key} must be a string`;
return [key, stringToNat(beans)];
}),
const beansPerUnit = recordFromEntries(
rawBeansPerUnit.map(({ key, beans }) => [key, beans]),
stringToNat,
);

Array.isArray(rawFeeUnitPrice) ||
Expand Down

0 comments on commit c8a4508

Please sign in to comment.