diff options
author | Ivan Chen <ivan@tagg.id> | 2021-06-09 15:40:08 -0400 |
---|---|---|
committer | Ivan Chen <ivan@tagg.id> | 2021-06-09 15:40:08 -0400 |
commit | cd6e9ba609cfdbcad1365c8589e2c98d755752ad (patch) | |
tree | 98b1e947f4ae4e306f8289e26354fb783c5ee5b5 /src/utils/common.ts | |
parent | 9d7e900a89f343f7752457956f8e1d205774b910 (diff) | |
parent | 946b1be53189487e860f37e1b422c69bb44cf0c8 (diff) |
Merge branch 'master' into tma872-purple-indicator
# Conflicts:
# src/constants/constants.ts
# src/utils/common.ts
Diffstat (limited to 'src/utils/common.ts')
-rw-r--r-- | src/utils/common.ts | 42 |
1 files changed, 39 insertions, 3 deletions
diff --git a/src/utils/common.ts b/src/utils/common.ts index cb0bd62d..645f229a 100644 --- a/src/utils/common.ts +++ b/src/utils/common.ts @@ -1,11 +1,17 @@ import AsyncStorage from '@react-native-community/async-storage'; import moment from 'moment'; -import {Linking} from 'react-native'; +import {ImageSourcePropType, Linking} from 'react-native'; import {getAll} from 'react-native-contacts'; -import {BROWSABLE_SOCIAL_URLS, TOGGLE_BUTTON_TYPE} from '../constants'; +import { + BADGE_DATA, + BROWSABLE_SOCIAL_URLS, + TOGGLE_BUTTON_TYPE, +} from '../constants'; import { ContactType, NotificationType, + UniversityBadge, + UniversityBadgeDisplayType, UniversityBadgeType, UniversityType, } from './../types/types'; @@ -198,7 +204,37 @@ export const validateImageLink = async (url: string | undefined) => { }); }; +/** + * Turns a list of badges into display badges (just a badge with img) by + * looking up the img source from our badge asset lookup constant. + * + * WARNING: Assumes a small list of badges, complexity goes up exponentially. + * + * @param badges list of university badges + * @param university university of which all the badges belong + * @returns list of display badges + */ +export const badgesToDisplayBadges = ( + badges: UniversityBadge[], + university: UniversityType, +) => { + const badgeSet: Set<string> = new Set(badges.map((b) => b.category + b.name)); + const badgeToImgMap: Record<string, ImageSourcePropType> = {}; + BADGE_DATA[university].forEach((category) => { + category.data.forEach((badgeInfo) => { + const key = category.title + badgeInfo.badgeName; + if (badgeSet.has(key)) { + badgeToImgMap[key] = badgeInfo.badgeImage; + } + }); + }); + return <UniversityBadgeDisplayType[]>badges.map((b) => ({ + ...b, + img: badgeToImgMap[b.category + b.name], + })); +}; + // Documentation: https://stackoverflow.com/questions/2901102/how-to-print-a-number-with-commas-as-thousands-separators-in-javascript export const numberWithCommas = (digits: number) => { return digits.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ','); -}; +
\ No newline at end of file |