Skip to content

Commit

Permalink
No pc in random (#941)
Browse files Browse the repository at this point in the history
Resolves #774

---------

Co-authored-by: Luke Brody <[email protected]>
  • Loading branch information
kavigupta and lukebrody authored Feb 13, 2025
1 parent 9dd849a commit 73b6f1a
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 15 deletions.
2 changes: 1 addition & 1 deletion react/eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export default tseslint.config(
'newlines-between': 'always',
}],
'import/no-unassigned-import': ['error', {
allow: ['**/*.css', './unit/util/fetch'],
allow: ['**/*.css', './unit/util/*'],
}],
'import/no-named-as-default-member': 'off',
'import/namespace': 'off',
Expand Down
2 changes: 1 addition & 1 deletion react/src/navigation/PageDescriptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ export async function loadPageDescriptor(newDescriptor: PageDescriptor, settings
let longname: string
switch (newDescriptor.sampleby) {
case 'uniform':
longname = await uniform()
longname = (await uniform())()
break
case 'population':
longname = await byPopulation(newDescriptor.us_only)
Expand Down
40 changes: 29 additions & 11 deletions react/src/navigation/random.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import '../style.css'
import '../common.css'

import { loadJSON, loadProtobuf } from '../load_json'
import { Settings } from '../page_template/settings'
import { isHistoricalCD } from '../utils/is_historical'
Expand All @@ -27,7 +24,7 @@ export async function byPopulation(domesticOnly: boolean): Promise<string> {
}
}

if (!Settings.shared.get('show_historical_cds') && isHistoricalCD(x!)) {
if (!valid(x!)) {
continue
}

Expand All @@ -41,14 +38,35 @@ export async function byPopulation(domesticOnly: boolean): Promise<string> {
}
}

export async function uniform(): Promise<string> {
export async function uniform(): Promise<() => string> {
const values = (await loadProtobuf('/index/pages.gz', 'StringList')).elements
while (true) {
const randomIndex = Math.floor(Math.random() * values.length)
const x = values[randomIndex]
if (!Settings.shared.get('show_historical_cds') && isHistoricalCD(x)) {
continue
return () => {
while (true) {
const randomIndex = Math.floor(Math.random() * values.length)
const x = values[randomIndex]
if (!valid(x)) {
continue
}
return x
}
}
}

function valid(x: string): boolean {
if (!Settings.shared.get('show_historical_cds') && isHistoricalCD(x)) {
return false
}
if (isPopulationCircle(x)) {
return false
}
return true
}

function isPopulationCircle(x: string): boolean {
for (const mpcOrBPC of ['5MPC', '10MPC', '20MPC', '50MPC', '100MPC', '200MPC', '500MPC', '1BPC']) {
if (x.includes(`${mpcOrBPC}, `)) {
return true
}
return x
}
return false
}
4 changes: 2 additions & 2 deletions react/src/page_template/template.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,11 @@ function TemplateFooter(): ReactNode {
}

function Version(): ReactNode {
return <span id="current-version">23.8.0</span>
return <span id="current-version">23.8.1</span>
}

function LastUpdated(): ReactNode {
return <span id="last-updated">2025-02-09</span>
return <span id="last-updated">2025-02-12</span>
}

function MainCredits(): ReactNode {
Expand Down
1 change: 1 addition & 0 deletions react/test/random_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ async function assertNoSetUniverse(t: TestController): Promise<void> {
async function assertNoSpecials(t: TestController): Promise<void> {
const location = await getLocation()
await t.expect(location).notMatch(/.*Historical\+Congressional.*/)
await t.expect(location).notMatch(/.*PC%2C.*/)
}

async function assertCorrect(t: TestController): Promise<void> {
Expand Down
22 changes: 22 additions & 0 deletions react/unit/random.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { test } from 'uvu'
import * as assert from 'uvu/assert'

import './util/fetch'
import './util/localStorage'
import { uniform } from '../src/navigation/random'

function assertNoSpecials(article: string): void {
assert.not.match(article, /.*Historical\+Congressional.*/)
assert.not.match(article, /.*PC%2C.*/)
}

const repeats = 100000

test('uniform', async () => {
const getArticle = await uniform()
for (let count = 0; count < repeats; count++) {
assertNoSpecials(getArticle())
}
})

test.run()
9 changes: 9 additions & 0 deletions react/unit/util/localStorage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
global.localStorage = {
get: () => null,
length: 0,
clear: () => undefined,
getItem: () => null,
removeItem: () => undefined,
key: () => null,
setItem: () => undefined,
}

0 comments on commit 73b6f1a

Please sign in to comment.