Skip to content

Commit

Permalink
Split out ijruAverage to avoid pulling in FS into speed bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
andrejpk committed Sep 30, 2024
1 parent d2df5d5 commit 887e855
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 36 deletions.
22 changes: 22 additions & 0 deletions lib/models/competition-events/ijru.common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export const ijruAverage = (scores: number[]): number => {
// sort ascending
scores.sort(function (a, b) {
return a - b
})

if (scores.length >= 4) {
scores.pop()
scores.shift()

const score = scores.reduce((a, b) => a + b)
return score / scores.length
} else if (scores.length === 3) {
const closest = scores[1] - scores[0] < scores[2] - scores[1] ? scores[1] + scores[0] : scores[2] + scores[1]
return closest / 2
} else if (scores.length === 2) {
const score = scores.reduce((a, b) => a + b)
return score / scores.length
} else {
return scores[0]
}
}
2 changes: 1 addition & 1 deletion lib/models/competition-events/ijru.freestyle@2.0.0.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { RSRWrongJudgeTypeError } from '../../errors'
import { filterTally, formatFactor, matchMeta, roundTo, roundToCurry, simpleCalculateTallyFactory } from '../../helpers'
import { type JudgeTypeGetter, type JudgeFieldDefinition, type TableDefinition, type CompetitionEventModel } from '../types'
import { ijruAverage } from './ijru.freestyle@3.0.0'
import { ijruAverage } from './ijru.common'

type Option = 'noMusicality' | 'discipline' | 'interactions'

Expand Down
15 changes: 8 additions & 7 deletions lib/models/competition-events/ijru.freestyle@3.0.0.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import assert from 'node:assert'
import test from 'node:test'
import * as mod from './ijru.freestyle@3.0.0.js'
import { ijruAverage } from './ijru.common.js'
import { type JudgeResult, type EntryMeta, type JudgeMeta } from '../types.js'
import { RSRWrongJudgeTypeError } from '../../errors.js'

Expand All @@ -26,31 +27,31 @@ void test('ijru.freestyle@3.0.0', async t => {

await t.test('ijruAverage', async t => {
await t.test('Should return single number', () => {
assert.strictEqual(mod.ijruAverage([1]), 1)
assert.strictEqual(ijruAverage([1]), 1)
})

await t.test('Should average two numbers', () => {
assert.strictEqual(mod.ijruAverage([1, 3]), 2)
assert.strictEqual(ijruAverage([1, 3]), 2)
})

await t.test('Should average the closest two of three numbers, when the lower two are closest', () => {
assert.strictEqual(mod.ijruAverage([1, 10, 3]), 2)
assert.strictEqual(ijruAverage([1, 10, 3]), 2)
})

await t.test('Should average the closest two of three numbers, when the higher two are closest', () => {
assert.strictEqual(mod.ijruAverage([1, 10, 8]), 9)
assert.strictEqual(ijruAverage([1, 10, 8]), 9)
})

await t.test('Should average the highest two of three numbers, when the numbers are equidistant', () => {
assert.strictEqual(mod.ijruAverage([1, 1 + 3, 1 + 3 + 3]), 5.5)
assert.strictEqual(ijruAverage([1, 1 + 3, 1 + 3 + 3]), 5.5)
})

await t.test('Should average all except highest and lowest for four numbers', () => {
assert.strictEqual(mod.ijruAverage([119, 114, 111, 118]), 116)
assert.strictEqual(ijruAverage([119, 114, 111, 118]), 116)
})

await t.test('Should average all except highest and lowest for five numbers', () => {
assert.strictEqual(mod.ijruAverage([119, 114, 131, 111, 118]), 117)
assert.strictEqual(ijruAverage([119, 114, 131, 111, 118]), 117)
})
})

Expand Down
23 changes: 1 addition & 22 deletions lib/models/competition-events/ijru.freestyle@3.0.0.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { RSRWrongJudgeTypeError } from '../../errors.js'
import { filterTally, formatFactor, matchMeta, roundTo, roundToCurry, simpleCalculateTallyFactory } from '../../helpers.js'
import type { CompetitionEventModel, JudgeFieldDefinition, JudgeTypeGetter, TableDefinition } from '../types.js'
import { ijruAverage } from './ijru.common.js'

type Option = 'noMusicality' | 'discipline' | 'interactions'

Expand All @@ -20,28 +21,6 @@ export function L (l: number): number {
return roundTo(0.1 * Math.pow(1.5, l), 2)
}

export const ijruAverage = (scores: number[]): number => {
// sort ascending
scores.sort(function (a, b) {
return a - b
})

if (scores.length >= 4) {
scores.pop()
scores.shift()

const score = scores.reduce((a, b) => a + b)
return score / scores.length
} else if (scores.length === 3) {
const closest = scores[1] - scores[0] < scores[2] - scores[1] ? scores[1] + scores[0] : scores[2] + scores[1]
return closest / 2
} else if (scores.length === 2) {
const score = scores.reduce((a, b) => a + b)
return score / scores.length
} else {
return scores[0]
}
}

Check failure on line 24 in lib/models/competition-events/ijru.freestyle@3.0.0.ts

View workflow job for this annotation

GitHub Actions / lint

More than 1 blank line not allowed

Check failure on line 24 in lib/models/competition-events/ijru.freestyle@3.0.0.ts

View workflow job for this annotation

GitHub Actions / lint

More than 1 blank line not allowed
// ======
// JUDGES
Expand Down
2 changes: 1 addition & 1 deletion lib/models/competition-events/ijru.speed@1.0.0.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { RSRWrongJudgeTypeError } from '../../errors.js'
import { filterTally, matchMeta, roundTo, simpleCalculateTallyFactory } from '../../helpers.js'
import type { CompetitionEventModel, JudgeTypeGetter, TableDefinition } from '../types.js'
import { ijruAverage } from './ijru.freestyle@3.0.0.js'
import { ijruAverage } from './ijru.common.js'

type Option = 'falseSwitches'

Expand Down
2 changes: 1 addition & 1 deletion lib/models/competition-events/svgf-rh.freestyle@2020.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { RSRWrongJudgeTypeError } from '../../errors'
import { clampNumber, filterMarkStream, filterTally, matchMeta, roundTo, roundToCurry, simpleCalculateTallyFactory } from '../../helpers'
import { type ScoreTally, type JudgeTypeGetter, type TableDefinition, type CompetitionEventModel } from '../types'
import { ijruAverage } from './ijru.freestyle@3.0.0'
import { ijruAverage } from './ijru.common'

type Option = 'discipline'

Expand Down
15 changes: 11 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,18 @@
"default": "./dist/esm/lib/index.js"
}
},
"./models/competition-events/*": {
"types": "./dist/esm/lib/models/competition-events/*.d.ts",
"default": "./dist/esm/lib/models/competition-events/*.js"
"./models/*": {
"require": {
"types": "./dist/cjs/lib/models/*.d.ts",
"default": "./dist/cjs/lib/models/*.js"
},
"import": {
"types": "./dist/esm/lib/models/*.d.ts",
"default": "./dist/esm/lib/models/*.js"
}
}
},
"sideEffects": false,
"files": [
"dist/"
],
Expand Down Expand Up @@ -52,4 +59,4 @@
"tsx": "^4.18.0",
"typescript": "5.5"
}
}
}

0 comments on commit 887e855

Please sign in to comment.