From 9172e52115ecb5d0809ce45af1610d9f63496a40 Mon Sep 17 00:00:00 2001 From: Sam Sciolla Date: Wed, 20 Jan 2021 13:53:37 -0500 Subject: [PATCH] Simplify return types; rename a var --- src/assets/src/changes.ts | 25 ++++++++++++------------ src/assets/src/hooks/useEntityChanges.ts | 2 +- src/mypy.ini | 6 ++++++ 3 files changed, 20 insertions(+), 13 deletions(-) create mode 100644 src/mypy.ini diff --git a/src/assets/src/changes.ts b/src/assets/src/changes.ts index f192dab0..fdbe9b05 100644 --- a/src/assets/src/changes.ts +++ b/src/assets/src/changes.ts @@ -54,7 +54,7 @@ const transformProperty = (value: string, propertyMap: HumanReadableMap) => { // Core functions function detectChanges ( - versOne: T, versTwo: T, propsToWatch: (keyof T)[], transforms: ValueTransform[]): string[] | undefined + versOne: T, versTwo: T, propsToWatch: (keyof T)[], transforms: ValueTransform[]): string[] { let changedPropMessages = []; for (const property of propsToWatch) { @@ -67,11 +67,12 @@ function detectChanges ( changedPropMessages.push(`The ${propName} changed from "${valueOne}" to "${valueTwo}".`); } } - if (changedPropMessages.length > 0) return changedPropMessages; + return changedPropMessages; } // Any new types added to ComparableEntity need to be supported in this function. + function describeEntity (entity: ComparableEntity): string[] { let entityType; let permIdent; @@ -89,18 +90,18 @@ function describeEntity (entity: ComparableEntity): string[] { // https://lodash.com/docs/4.17.15#xorWith -export function compareEntities (oldOnes: T[], newOnes: T[]): string[] | undefined +export function compareEntities (oldOnes: T[], newOnes: T[]): string[] { const symDiff = xorWith(oldOnes, newOnes, isEqual); - if (symDiff.length === 0) return; + if (symDiff.length === 0) return []; const oldIDs = oldOnes.map((value) => value.id); const newIDs = newOnes.map((value) => value.id); let changeMessages: string[] = []; - let changedIDsProcessed: number[] = []; + let processedChangedObjectIDs: number[] = []; for (const entity of symDiff) { - if (changedIDsProcessed.includes(entity.id)) continue; + if (processedChangedObjectIDs.includes(entity.id)) continue; const [entityType, permIdent] = describeEntity(entity); if (oldIDs.includes(entity.id) && !newIDs.includes(entity.id)) { changeMessages.push(`The ${entityType} with ${permIdent} was deleted.`); @@ -111,21 +112,21 @@ export function compareEntities (oldOnes: T[], newOn const [firstEntity, secondEntity] = symDiff.filter(value => value.id === entity.id); let changesDetected: string[] = []; if (isMeeting(firstEntity) && isMeeting(secondEntity)) { - const detectResult = detectChanges(firstEntity, secondEntity, meetingPropsToWatch, standardTransforms); - if (detectResult) changesDetected.push(...detectResult); + const changes = detectChanges(firstEntity, secondEntity, meetingPropsToWatch, standardTransforms); + if (changes.length > 0) changesDetected.push(...changes); // Custom check for Meeting.status, since only some status changes are relevant here. if (firstEntity.status !== secondEntity.status && secondEntity.status === MeetingStatus.STARTED) { changesDetected.push('The meeting is now in progress.'); } } else if (isQueueBase(firstEntity) && isQueueBase(secondEntity)) { - const detectResult = detectChanges(firstEntity, secondEntity, queueBasePropsToWatch, standardTransforms); - if (detectResult) changesDetected.push(...detectResult); + const changes = detectChanges(firstEntity, secondEntity, queueBasePropsToWatch, standardTransforms); + if (changes.length > 0) changesDetected.push(...changes); } if (changesDetected.length > 0) { changeMessages.push(`The ${entityType} with ${permIdent} was changed. ` + changesDetected.join(' ')); } - changedIDsProcessed.push(entity.id) + processedChangedObjectIDs.push(entity.id) } } - if (changeMessages.length > 0) return changeMessages; + return changeMessages; } diff --git a/src/assets/src/hooks/useEntityChanges.ts b/src/assets/src/hooks/useEntityChanges.ts index 198ef7db..7e6bdf7f 100644 --- a/src/assets/src/hooks/useEntityChanges.ts +++ b/src/assets/src/hooks/useEntityChanges.ts @@ -11,7 +11,7 @@ export function useEntityChanges(): const compareAndSetChangeEvents = (oldEntities: readonly T[], newEntities: readonly T[]): void => { const changeMessages = compareEntities(oldEntities.slice(), newEntities.slice()); - if (changeMessages !== undefined) { + if (changeMessages.length > 0) { let eventID = nextID; const newChangeEvents = changeMessages.map( (m) => { diff --git a/src/mypy.ini b/src/mypy.ini new file mode 100644 index 00000000..9c451dc4 --- /dev/null +++ b/src/mypy.ini @@ -0,0 +1,6 @@ +[mypy] +plugins = + mypy_django_plugin.main + +[mypy.plugins.django-stubs] +django_settings_module = "officehours.settings" \ No newline at end of file