Fix naming of set-theory utils to match convention (#7343)
This commit is contained in:
parent
908e938996
commit
fcc4939075
7 changed files with 39 additions and 39 deletions
|
@ -200,21 +200,21 @@ export function arrayDiff<T>(a: T[], b: T[]): { added: T[], removed: T[] } {
|
|||
}
|
||||
|
||||
/**
|
||||
* Returns the union of two arrays.
|
||||
* Returns the intersection of two arrays.
|
||||
* @param a The first array. Must be defined.
|
||||
* @param b The second array. Must be defined.
|
||||
* @returns The union of the arrays.
|
||||
* @returns The intersection of the arrays.
|
||||
*/
|
||||
export function arrayUnion<T>(a: T[], b: T[]): T[] {
|
||||
export function arrayIntersection<T>(a: T[], b: T[]): T[] {
|
||||
return a.filter(i => b.includes(i));
|
||||
}
|
||||
|
||||
/**
|
||||
* Merges arrays, deduping contents using a Set.
|
||||
* Unions arrays, deduping contents using a Set.
|
||||
* @param a The arrays to merge.
|
||||
* @returns The merged array.
|
||||
* @returns The union of all given arrays.
|
||||
*/
|
||||
export function arrayMerge<T>(...a: T[][]): T[] {
|
||||
export function arrayUnion<T>(...a: T[][]): T[] {
|
||||
return Array.from(a.reduce((c, v) => {
|
||||
v.forEach(i => c.add(i));
|
||||
return c;
|
||||
|
|
|
@ -14,16 +14,16 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { arrayDiff, arrayMerge, arrayUnion } from "./arrays";
|
||||
|
||||
export function iterableMerge<T>(a: Iterable<T>, b: Iterable<T>): Iterable<T> {
|
||||
return arrayMerge(Array.from(a), Array.from(b));
|
||||
}
|
||||
import { arrayDiff, arrayUnion, arrayIntersection } from "./arrays";
|
||||
|
||||
export function iterableUnion<T>(a: Iterable<T>, b: Iterable<T>): Iterable<T> {
|
||||
return arrayUnion(Array.from(a), Array.from(b));
|
||||
}
|
||||
|
||||
export function iterableIntersection<T>(a: Iterable<T>, b: Iterable<T>): Iterable<T> {
|
||||
return arrayIntersection(Array.from(a), Array.from(b));
|
||||
}
|
||||
|
||||
export function iterableDiff<T>(a: Iterable<T>, b: Iterable<T>): { added: Iterable<T>, removed: Iterable<T> } {
|
||||
return arrayDiff(Array.from(a), Array.from(b));
|
||||
}
|
||||
|
|
|
@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
|
|||
limitations under the License.
|
||||
*/
|
||||
|
||||
import { arrayDiff, arrayMerge, arrayUnion } from "./arrays";
|
||||
import { arrayDiff, arrayUnion, arrayIntersection } from "./arrays";
|
||||
|
||||
/**
|
||||
* Determines the keys added, changed, and removed between two Maps.
|
||||
|
@ -27,7 +27,7 @@ export function mapDiff<K, V>(a: Map<K, V>, b: Map<K, V>): { changed: K[], added
|
|||
const aKeys = [...a.keys()];
|
||||
const bKeys = [...b.keys()];
|
||||
const keyDiff = arrayDiff(aKeys, bKeys);
|
||||
const possibleChanges = arrayUnion(aKeys, bKeys);
|
||||
const possibleChanges = arrayIntersection(aKeys, bKeys);
|
||||
const changes = possibleChanges.filter(k => a.get(k) !== b.get(k));
|
||||
|
||||
return { changed: changes, added: keyDiff.added, removed: keyDiff.removed };
|
||||
|
@ -42,7 +42,7 @@ export function mapDiff<K, V>(a: Map<K, V>, b: Map<K, V>): { changed: K[], added
|
|||
*/
|
||||
export function mapKeyChanges<K, V>(a: Map<K, V>, b: Map<K, V>): K[] {
|
||||
const diff = mapDiff(a, b);
|
||||
return arrayMerge(diff.removed, diff.added, diff.changed);
|
||||
return arrayUnion(diff.removed, diff.added, diff.changed);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
|
|||
limitations under the License.
|
||||
*/
|
||||
|
||||
import { arrayDiff, arrayMerge, arrayUnion } from "./arrays";
|
||||
import { arrayDiff, arrayUnion, arrayIntersection } from "./arrays";
|
||||
|
||||
type ObjectExcluding<O extends {}, P extends (keyof O)[]> = {[k in Exclude<keyof O, P[number]>]: O[k]};
|
||||
|
||||
|
@ -90,7 +90,7 @@ export function objectHasDiff<O extends {}>(a: O, b: O): boolean {
|
|||
const aKeys = Object.keys(a);
|
||||
const bKeys = Object.keys(b);
|
||||
if (aKeys.length !== bKeys.length) return true;
|
||||
const possibleChanges = arrayUnion(aKeys, bKeys);
|
||||
const possibleChanges = arrayIntersection(aKeys, bKeys);
|
||||
// if the amalgamation of both sets of keys has the a different length to the inputs then there must be a change
|
||||
if (possibleChanges.length !== aKeys.length) return true;
|
||||
|
||||
|
@ -111,7 +111,7 @@ export function objectDiff<O extends {}>(a: O, b: O): Diff<keyof O> {
|
|||
const aKeys = Object.keys(a) as (keyof O)[];
|
||||
const bKeys = Object.keys(b) as (keyof O)[];
|
||||
const keyDiff = arrayDiff(aKeys, bKeys);
|
||||
const possibleChanges = arrayUnion(aKeys, bKeys);
|
||||
const possibleChanges = arrayIntersection(aKeys, bKeys);
|
||||
const changes = possibleChanges.filter(k => a[k] !== b[k]);
|
||||
|
||||
return { changed: changes, added: keyDiff.added, removed: keyDiff.removed };
|
||||
|
@ -128,7 +128,7 @@ export function objectDiff<O extends {}>(a: O, b: O): Diff<keyof O> {
|
|||
*/
|
||||
export function objectKeyChanges<O extends {}>(a: O, b: O): (keyof O)[] {
|
||||
const diff = objectDiff(a, b);
|
||||
return arrayMerge(diff.removed, diff.added, diff.changed);
|
||||
return arrayUnion(diff.removed, diff.added, diff.changed);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue