@meith/polls 0.30.1 → 0.32.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 +7 -3
- package/src/index.ts +3 -0
- package/src/limits.ts +5 -0
- package/src/service.ts +24 -5
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@meith/polls",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.32.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.
|
|
23
|
-
"@meith/i18n": "0.
|
|
26
|
+
"@meith/core": "0.32.0",
|
|
27
|
+
"@meith/i18n": "0.32.0"
|
|
24
28
|
}
|
|
25
29
|
}
|
package/src/index.ts
CHANGED
package/src/limits.ts
ADDED
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
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
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()
|