generated from bcgov/quickstart-openshift
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
50 changed files
with
999 additions
and
720 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { Nullable } from "../types/common"; | ||
|
||
/** | ||
* Check if two nullable values are different. | ||
* @param val1 First nullable value to be compared | ||
* @param val2 Second nullable value to be compared | ||
* @returns true when only one of the values are empty, or both are non-empty and different, false otherwise | ||
*/ | ||
export const areValuesDifferent = <T>( | ||
val1?: Nullable<T>, | ||
val2?: Nullable<T>, | ||
): boolean => { | ||
if (!val1 && !val2) return false; // Both empty implicitly means that values are the same | ||
|
||
if ((val1 && !val2) || (!val1 && val2) || (val1 && val2 && val1 !== val2)) { | ||
return true; // Only one empty, or both are non-empty but different means that values are different | ||
} | ||
|
||
return false; // Values are considered equal otherwise | ||
}; | ||
|
||
/** | ||
* Determine whether or not two arrays, each with only unique primitive values, have the same values. | ||
* @param arr1 First array consisting of only non-duplicate primitive values | ||
* @param arr2 Second array consisting of only non-duplicate primitive values | ||
* @returns Whether or not the two arrays have the same values | ||
*/ | ||
export const doUniqueArraysHaveSameItems = <T extends (number | string)>( | ||
arr1: T[], | ||
arr2: T[], | ||
) => { | ||
const set1 = new Set(arr1); | ||
const set2 = new Set(arr2); | ||
|
||
for (const val of set1) { | ||
if (!set2.has(val)) return false; | ||
} | ||
|
||
for (const val of set2) { | ||
if (!set1.has(val)) return false; | ||
} | ||
|
||
return true; | ||
}; | ||
|
||
/** | ||
* Determine whether or not two arrays, each with objects of a certain type identifiable by keys, | ||
* have the same objects. | ||
* @param arr1 First array consisting of identifiable objects | ||
* @param arr2 Second array consisting of identifiable objects | ||
* @param key Function that returns the identifier of an object of the given type | ||
* @param equalFn Function that compares equality of two objects of the given type | ||
* @returns Whether or not the two arrays have the same objects | ||
*/ | ||
export const doUniqueArraysHaveSameObjects = <T, K extends (number | string)>( | ||
arr1: T[], | ||
arr2: T[], | ||
key: (item: T) => K, | ||
equalFn: (item1: T, item2: T) => boolean, | ||
) => { | ||
const map1 = new Map<K, T>(arr1.map(item => [key(item), item])); | ||
const map2 = new Map<K, T>(arr2.map(item => [key(item), item])); | ||
|
||
for (const [key, item] of map1) { | ||
const itemInOtherMapWithSameKey = map2.get(key); | ||
if (!itemInOtherMapWithSameKey || !equalFn(item, itemInOtherMapWithSameKey)) | ||
return false; | ||
} | ||
|
||
for (const [key, item] of map2) { | ||
const itemInOtherMapWithSameKey = map1.get(key); | ||
if (!itemInOtherMapWithSameKey || !equalFn(item, itemInOtherMapWithSameKey)) | ||
return false; | ||
} | ||
|
||
return true; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { useEffect, useState } from "react"; | ||
import { doUniqueArraysHaveSameObjects } from "../helpers/equality"; | ||
|
||
/** | ||
* Hook that memoizes an array of objects. | ||
* The memoized array only changes when the items in the array change. | ||
* eg. If items === [{a: 1}, {a: 2}], and later [{a: 2}, {a: 1}] is passed in, | ||
* the hook returns the same items [{a: 1}, {a: 2}]. | ||
* @param items Array of objects | ||
* @param key Function that returns identifier for each object | ||
* @param equalFn Function that determines whether or not two objects are equal | ||
* @returns Memoized array of objects | ||
*/ | ||
export const useMemoizedArray = <T, K extends (number | string)>( | ||
items: T[], | ||
key: (item: T) => K, | ||
equalFn: (item1: T, item2: T) => boolean, | ||
) => { | ||
const [arrayItems, setArrayItems] = useState<T[]>(items); | ||
|
||
useEffect(() => { | ||
if (!doUniqueArraysHaveSameObjects( | ||
arrayItems, | ||
items, | ||
key, | ||
equalFn, | ||
)) { | ||
setArrayItems(items); | ||
} | ||
}, [items]); | ||
|
||
return arrayItems; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { useEffect, useState } from "react"; | ||
|
||
/** | ||
* Hook that memoizes an object. | ||
* The memoized object only changes when its contents change. | ||
* eg. If obj === {a: 1, b: 2}, and later {b: 2, a: 1} is passed in, | ||
* the hook returns the same obj {a: 1, b: 2}. | ||
* @param obj An object | ||
* @param equalFn Function that determines whether or not two objects are equal | ||
* @returns Memoized object | ||
*/ | ||
export const useMemoizedObject = <T>( | ||
obj: T, | ||
equalFn: (obj1: T, obj2: T) => boolean, | ||
) => { | ||
const [memoizedObj, setMemoizedObj] = useState<T>(obj); | ||
|
||
useEffect(() => { | ||
if (!equalFn(memoizedObj, obj)) { | ||
setMemoizedObj(obj); | ||
} | ||
}, [obj]); | ||
|
||
return memoizedObj; | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.