Skip to content

Commit

Permalink
Always return distinct object to avoid modification.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ramirisu committed Feb 5, 2024
1 parent d3bb45d commit 52e8bab
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 53 deletions.
4 changes: 1 addition & 3 deletions src/data/ideology.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const IDEOLOGIES = [
export const getIdeologies = () => [
{
id: 'anarcho_communism',
weight: {
Expand Down Expand Up @@ -477,5 +477,3 @@ const IDEOLOGIES = [
},
},
]

export default IDEOLOGIES
6 changes: 4 additions & 2 deletions src/data/ideology_tag.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import MULTIPLIER from '../utils/multiplier'

export const IDEOLOGY_TAGS = [
export const getIdeologyTags = () => [
{
id: 'roc_unification',
predicate: (choices) => choices['q1103'] > MULTIPLIER.n,
Expand Down Expand Up @@ -56,4 +56,6 @@ export const IDEOLOGY_TAGS = [
]

export const getMatchedIdeologyTags = (choices) =>
IDEOLOGY_TAGS.filter((value) => value.predicate(choices)).map((value) => value.id)
getIdeologyTags()
.filter((value) => value.predicate(choices))
.map((value) => value.id)
4 changes: 1 addition & 3 deletions src/data/political_party.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const POLITICAL_PARTIES = [
export const getPoliticalParties = () => [
{
id: 'kmt',
icon: 'https://upload.wikimedia.org/wikipedia/commons/d/dc/Blue_Sky_White_Sun.png',
Expand Down Expand Up @@ -120,5 +120,3 @@ const POLITICAL_PARTIES = [
},
},
]

export default POLITICAL_PARTIES
4 changes: 1 addition & 3 deletions src/data/question.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const WEIGHTS = {
p050: 8,
}

const QUESTIONS = [
export const getQuestions = () => [
{
id: 'q0000',
weight: {
Expand Down Expand Up @@ -405,5 +405,3 @@ const QUESTIONS = [
},
},
]

export default QUESTIONS
4 changes: 2 additions & 2 deletions src/pages/Quiz.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useTranslation } from 'react-i18next'
import { useNavigate, createSearchParams } from 'react-router-dom'
import shuffle from '../utils/shuffle'
import useBreakpoint from '../utils/useBreakpoint'
import QUESTIONS from '../data/question'
import { getQuestions } from '../data/question'
import { getMatchedIdeologyTags } from '../data/ideology_tag'
import MULTIPLIER from '../utils/multiplier'
import { API_VERSION_KEY, API_VERSION_VALUE } from '../utils/apiVersion'
Expand Down Expand Up @@ -42,7 +42,7 @@ const Quiz = () => {
})

const questions = useMemo(() => {
return shuffle(QUESTIONS)
return shuffle(getQuestions())
}, [])

const [currentSelectedQuestionIndex, setCurrentSelectedQuestionIndex] = useState(0)
Expand Down
4 changes: 2 additions & 2 deletions src/pages/Result.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useSearchParams } from 'react-router-dom'
import { Card, Typography, Image, Button, Flex, Switch, Row, Col } from 'antd'
import { useTranslation } from 'react-i18next'
import ValueCard from '../components/ValueCard'
import { IDEOLOGY_TAGS } from '../data/ideology_tag'
import { getIdeologyTags } from '../data/ideology_tag'
import { getIdeologyMatchScores, getPoliticalPartyMatchScores } from '../utils/match'
import { API_VERSION_KEY, API_VERSION_VALUE } from '../utils/apiVersion'

Expand Down Expand Up @@ -340,7 +340,7 @@ const Result = () => {
/>
}
>
{getMatchTags(IDEOLOGY_TAGS, expandTags).map((value) => {
{getMatchTags(getIdeologyTags(), expandTags).map((value) => {
const name = t(`quiz.result.tags.data.${value.id}.name`)
const description = t(`quiz.result.tags.data.${value.id}.description`)
const link = t(`quiz.result.tags.data.${value.id}.link`)
Expand Down
63 changes: 35 additions & 28 deletions src/utils/match.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import IDEOLOGIES from '../data/ideology'
import POLITICAL_PARTIES from '../data/political_party'
import { getIdeologies } from '../data/ideology'
import { getPoliticalParties } from '../data/political_party'

export const getValueScores = (questions, choices) => {
const getScore = (props) => {
Expand Down Expand Up @@ -39,37 +39,44 @@ const getMatchRate = (distance, lower, upper) => {
}

export const getIdeologyMatchScores = (weights) => {
const ideologyScores = IDEOLOGIES.map((value) => {
let distance = 0.0
distance += Math.pow(Math.abs(value.weight.economic - weights.economic), 2)
distance += Math.pow(Math.abs(value.weight.diplomatic - weights.diplomatic), 2)
distance += Math.pow(Math.abs(value.weight.civil - weights.civil), 2)
distance += Math.pow(Math.abs(value.weight.societal - (0.25 * weights.environmental + 0.75 * weights.societal)), 2)
return {
id: value.id,
distance: distance,
rate: getMatchRate(distance, 4 * 10 * 10, 4 * 50 * 50),
}
}).sort((lhs, rhs) => (lhs.distance < rhs.distance ? -1 : lhs.distance > rhs.distance ? 1 : 0))
const ideologyScores = getIdeologies()
.map((value) => {
let distance = 0.0
distance += Math.pow(Math.abs(value.weight.economic - weights.economic), 2)
distance += Math.pow(Math.abs(value.weight.diplomatic - weights.diplomatic), 2)
distance += Math.pow(Math.abs(value.weight.civil - weights.civil), 2)
distance += Math.pow(
Math.abs(value.weight.societal - (0.25 * weights.environmental + 0.75 * weights.societal)),
2,
)
return {
id: value.id,
distance: distance,
rate: getMatchRate(distance, 4 * 10 * 10, 4 * 50 * 50),
}
})
.sort((lhs, rhs) => (lhs.distance < rhs.distance ? -1 : lhs.distance > rhs.distance ? 1 : 0))

return ideologyScores
}

export const getPoliticalPartyMatchScores = (weights) => {
const politicalScores = POLITICAL_PARTIES.map((value) => {
let distance = 0.0
distance += Math.pow(Math.abs(value.weight.economic - weights.economic), 2)
distance += Math.pow(Math.abs(value.weight.civil - weights.civil), 2)
distance += Math.pow(Math.abs(value.weight.environmental - weights.environmental), 2)
distance += Math.pow(Math.abs(value.weight.societal - weights.societal), 2)
distance += Math.pow(Math.abs(value.weight.sovereignty - weights.sovereignty), 2)
distance += Math.pow(Math.abs(value.weight.us_vs_china - weights.us_vs_china), 2)
return {
...value,
distance: distance,
rate: getMatchRate(distance, 6 * 10 * 10, 6 * 50 * 50),
}
}).sort((lhs, rhs) => (lhs.distance < rhs.distance ? -1 : lhs.distance > rhs.distance ? 1 : 0))
const politicalScores = getPoliticalParties()
.map((value) => {
let distance = 0.0
distance += Math.pow(Math.abs(value.weight.economic - weights.economic), 2)
distance += Math.pow(Math.abs(value.weight.civil - weights.civil), 2)
distance += Math.pow(Math.abs(value.weight.environmental - weights.environmental), 2)
distance += Math.pow(Math.abs(value.weight.societal - weights.societal), 2)
distance += Math.pow(Math.abs(value.weight.sovereignty - weights.sovereignty), 2)
distance += Math.pow(Math.abs(value.weight.us_vs_china - weights.us_vs_china), 2)
return {
...value,
distance: distance,
rate: getMatchRate(distance, 6 * 10 * 10, 6 * 50 * 50),
}
})
.sort((lhs, rhs) => (lhs.distance < rhs.distance ? -1 : lhs.distance > rhs.distance ? 1 : 0))

return politicalScores
}
20 changes: 10 additions & 10 deletions src/utils/match.test.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { expect, test } from 'vitest'
import { getValueScores, getPoliticalPartyMatchScores } from './match'
import QUESTIONS from '../data/question'
import POLITICAL_PARTIES from '../data/political_party'
import { getQuestions } from '../data/question'
import { getPoliticalParties } from '../data/political_party'
import MULTIPLIER from './multiplier'

const checkWeights = (weights, partyId) => {
const getParty = (id) => POLITICAL_PARTIES.filter((value) => value.id == id)[0]
const getParty = (id) => getPoliticalParties().filter((value) => value.id == id)[0]
const party = getParty(partyId)
const threshold = 3
expect(weights.economic).toBeGreaterThanOrEqual(party.weight.economic - threshold)
Expand Down Expand Up @@ -90,7 +90,7 @@ test('kmt', () => {
q1301: MULTIPLIER.n,
}

const weights = getValueScores(QUESTIONS, choices)
const weights = getValueScores(getQuestions(), choices)
checkWeights(weights, 'kmt')
const party = getPoliticalPartyMatchScores(weights).at(0)
expect(party.id).toEqual('kmt')
Expand Down Expand Up @@ -165,7 +165,7 @@ test('lp', () => {
q1301: MULTIPLIER.a,
}

const weights = getValueScores(QUESTIONS, choices)
const weights = getValueScores(getQuestions(), choices)
checkWeights(weights, 'lp')
const party = getPoliticalPartyMatchScores(weights).at(0)
expect(party.id).toEqual('lp')
Expand Down Expand Up @@ -240,7 +240,7 @@ test('dpp', () => {
q1301: MULTIPLIER.d,
}

const weights = getValueScores(QUESTIONS, choices)
const weights = getValueScores(getQuestions(), choices)
checkWeights(weights, 'dpp')
const party = getPoliticalPartyMatchScores(weights).at(0)
expect(party.id).toEqual('dpp')
Expand Down Expand Up @@ -315,7 +315,7 @@ test('np', () => {
q1301: MULTIPLIER.n,
}

const weights = getValueScores(QUESTIONS, choices)
const weights = getValueScores(getQuestions(), choices)
checkWeights(weights, 'np')
const party = getPoliticalPartyMatchScores(weights).at(0)
expect(party.id).toEqual('np')
Expand Down Expand Up @@ -390,7 +390,7 @@ test('npp', () => {
q1301: MULTIPLIER.d,
}

const weights = getValueScores(QUESTIONS, choices)
const weights = getValueScores(getQuestions(), choices)
checkWeights(weights, 'npp')
const party = getPoliticalPartyMatchScores(weights).at(0)
expect(party.id).toEqual('npp')
Expand Down Expand Up @@ -465,7 +465,7 @@ test('tpp', () => {
q1301: MULTIPLIER.n,
}

const weights = getValueScores(QUESTIONS, choices)
const weights = getValueScores(getQuestions(), choices)
checkWeights(weights, 'tpp')
const party = getPoliticalPartyMatchScores(weights).at(0)
expect(party.id).toEqual('tpp')
Expand Down Expand Up @@ -540,7 +540,7 @@ test('gpt', () => {
q1301: MULTIPLIER.d,
}

const weights = getValueScores(QUESTIONS, choices)
const weights = getValueScores(getQuestions(), choices)
checkWeights(weights, 'gpt')
const party = getPoliticalPartyMatchScores(weights).at(0)
expect(party.id).toEqual('gpt')
Expand Down

0 comments on commit 52e8bab

Please sign in to comment.