fix: optional return for null cases

This commit is contained in:
0xdevant 2024-07-22 18:17:18 +08:00
parent 950b4164a0
commit abc5e3cd4d
No known key found for this signature in database
GPG key ID: 3B0072B3F5B415CB
2 changed files with 23 additions and 20 deletions

View file

@ -1,5 +1,5 @@
<script lang="ts" setup> <script lang="ts" setup>
import type { Project } from '~/types' import type { Project, ProjectIndexable } from '~/types'
const props = defineProps<{ const props = defineProps<{
project: Project project: Project
@ -21,34 +21,33 @@ const availableSupport = computed(() => {
- team: anon / public - team: anon / public
- audit: yes / no - audit: yes / no
*/ */
const calculateScore: number = computed(() => { const calculateScore = computed(() => {
const criterias: { value: string; key?: string }[] = [ const criterias: { value: string; key: string }[] = [
{ value: 'product_readiness' }, { value: 'product_readiness', key: '' },
{ value: 'github', key: 'links' }, { value: 'github', key: 'links' },
{ value: 'docs', key: 'links' }, { value: 'docs', key: 'links' },
{ value: 'team' }, { value: 'team', key: '' },
{ value: 'audits' }, { value: 'audits', key: '' },
] ]
let matched = 0 let matched = 0
for (let i = 0; i < criterias.length; i++) { for (let i = 0; i < criterias.length; i++) {
let value let value
if (criterias[i].key) { // value = ((criterias[i].key ?? props.project[criterias[i].value as keyof typeof props.project]) ?? null === null) ? null : (props.project as ProjectIndexable)[criterias[i].key][criterias[i].value]
if (props.project[criterias[i].key] === null || props.project[criterias[i].key] === undefined)
const indexableProject = (props.project as ProjectIndexable)
if (criterias[i].key !== '')
value = indexableProject?.[criterias[i].key]?.[criterias[i].value]
else
value = indexableProject?.[criterias[i].value]
// console.log(props.project?.links?.github);
// console.log(Object.keys(props.indexableProject["team"]).length);
if (value === null || value === undefined)
continue continue
value = props.project[criterias[i].key][criterias[i].value]
}
else {
if (props.project[criterias[i].value] === null || props.project[criterias[i].value] === undefined)
continue
value = props.project[criterias[i].value]
}
// console.log(value);
// console.log(Object.keys(props.project["team"]).length);
if (fulfilled(value)) if (fulfilled(value))
matched++ matched++
// console.log('matched', matched);
} }
return 100 / criterias.length * matched return 100 / criterias.length * matched
@ -65,10 +64,10 @@ function fulfilled(value: any): boolean {
if (Object.keys(value).length > 0) if (Object.keys(value).length > 0)
return true return true
break break
default: default:
return false return false
} }
return false
} }
const logo = props.project?.logos?.at(0)?.url const logo = props.project?.logos?.at(0)?.url

View file

@ -143,3 +143,7 @@ export interface ProjectShallow {
support?: number | undefined support?: number | undefined
anonymity?: boolean | undefined anonymity?: boolean | undefined
} }
export interface ProjectIndexable extends Project {
[key: string]: any
}