@meith/polls 0.31.0 → 0.33.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@meith/polls",
3
- "version": "0.31.0",
3
+ "version": "0.33.0",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -10,6 +10,10 @@
10
10
  "type": "module",
11
11
  "main": "./src/index.ts",
12
12
  "types": "./src/index.ts",
13
+ "exports": {
14
+ ".": "./src/index.ts",
15
+ "./limits": "./src/limits.ts"
16
+ },
13
17
  "files": [
14
18
  "src",
15
19
  "!src/**/*.test.*",
@@ -19,7 +23,7 @@
19
23
  "access": "public"
20
24
  },
21
25
  "dependencies": {
22
- "@meith/core": "0.31.0",
23
- "@meith/i18n": "0.31.0"
26
+ "@meith/core": "0.33.0",
27
+ "@meith/i18n": "0.33.0"
24
28
  }
25
29
  }
package/src/index.ts CHANGED
@@ -4,8 +4,11 @@ export {
4
4
  POLL_OPTION_MAX,
5
5
  POLL_QUESTION_MAX,
6
6
  POLL_VOTERS_SHOWN_MAX,
7
+ } from './limits'
8
+ export {
7
9
  PollService,
8
10
  planPollEdit,
11
+ pollOptionShares,
9
12
  ThreadRatingService,
10
13
  validatePoll,
11
14
  } from './service'
package/src/limits.ts ADDED
@@ -0,0 +1,5 @@
1
+ export const POLL_QUESTION_MAX = 250
2
+ export const POLL_OPTION_MAX = 20
3
+ export const POLL_OPTION_LENGTH_MAX = 200
4
+ export const POLL_CHOICES_UNLIMITED = 0
5
+ export const POLL_VOTERS_SHOWN_MAX = 50
package/src/service.ts CHANGED
@@ -1,6 +1,12 @@
1
1
  import { ForbiddenError, ValidationError } from '@meith/core'
2
2
  import { msg } from '@meith/i18n'
3
3
 
4
+ import {
5
+ POLL_CHOICES_UNLIMITED,
6
+ POLL_OPTION_LENGTH_MAX,
7
+ POLL_OPTION_MAX,
8
+ POLL_QUESTION_MAX,
9
+ } from './limits'
4
10
  import type {
5
11
  NewPoll,
6
12
  Poll,
@@ -15,11 +21,24 @@ import type {
15
21
  ValidatedPoll,
16
22
  } from './types'
17
23
 
18
- export const POLL_QUESTION_MAX = 250
19
- export const POLL_OPTION_MAX = 20
20
- export const POLL_OPTION_LENGTH_MAX = 200
21
- export const POLL_CHOICES_UNLIMITED = 0
22
- export const POLL_VOTERS_SHOWN_MAX = 50
24
+ export function pollOptionShares(votes: readonly number[]): readonly number[] {
25
+ const total = votes.reduce((sum, count) => sum + count, 0)
26
+ if (total === 0) return votes.map(() => 0)
27
+
28
+ const shares = votes.map((count) => Math.floor((count / total) * 100))
29
+ const byRemainder = votes
30
+ .map((count, index) => ({ index, remainder: ((count / total) * 100) % 1 }))
31
+ .sort((a, b) => b.remainder - a.remainder)
32
+
33
+ let short = 100 - shares.reduce((sum, share) => sum + share, 0)
34
+ for (const { index } of byRemainder) {
35
+ if (short <= 0) break
36
+ shares[index] = (shares[index] ?? 0) + 1
37
+ short -= 1
38
+ }
39
+
40
+ return shares
41
+ }
23
42
 
24
43
  function trimmedQuestion(question: string): string {
25
44
  const trimmed = question.trim()