UNPKG

ecclesia

Version:

Framework for political and electoral simulations

1 lines 2.58 kB
{"version":3,"sources":["../../../src/election/attribution/base.ts"],"sourcesContent":["import { type Counter } from \"@gouvernathor/python/collections\";\r\nimport { type Ballots } from \"../ballots\";\r\n\r\n/**\r\n * To be thrown when an attribution fails to attribute seats,\r\n * but only in a case where it's an expected limitation of the attribution method.\r\n * The typical example being the Condorcet standoff, or a majority threshold,\r\n * but a tie could be another example (though they are usually not checked in provided implementations).\r\n *\r\n * Other errors may and will be raised by the attribution methods for other reasons,\r\n * such as invalid input data, division by zero...\r\n */\r\nexport class AttributionFailure extends Error { }\r\n\r\n/**\r\n * A function to manage how the results from the ballots\r\n * translate into an allocation of seats.\r\n *\r\n * If the attribution method is expected, by design, to fail under expected conditions,\r\n * such as a majority threshold not being reached,\r\n * this method should raise an AttributionFailure error.\r\n *\r\n * @param votes The ballots.\r\n * @param rest This parameter, ignored in most cases,\r\n * forces the attributions taking more required or optional parameters\r\n * to take them as an options object.\r\n * Attribution transforms (functions that take an attribution method and return another one)\r\n * should generally let that parameter pass through\r\n * so as to avoid breaking features in the wrapped attributions.\r\n * Attributions should avoid taking other parameters, for the same reason.\r\n *\r\n * @returns The attribution of seats to the parties based upon the votes.\r\n * It is a Counter mapping each party to the number of seats it won.\r\n * The return value's total() should be equal to the nSeats attributes - if any.\r\n */\r\nexport interface Attribution<Party, B extends Ballots<Party>> {\r\n (votes: B, rest?: Record<string, any>): Counter<Party>;\r\n}\r\n// TODO: for all attributions where it can make sense to start with some apportionned seats,\r\n// add an option to pass some.\r\n\r\n/**\r\n * Most attributions should generally implement this interface,\r\n * which reflects that the number of seats they allocate is always the same.\r\n *\r\n * However, some attributions may yield variable numbers of seats,\r\n * for instance the pre-2024 federal German system.\r\n */\r\nexport interface HasNSeats {\r\n readonly nSeats: number;\r\n}\r\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAYO,IAAM,qBAAN,cAAiC,MAAM;AAAE;","names":[]}