UNPKG

@google/aside

Version:

Apps Script IDE framework

88 lines (87 loc) 2.77 kB
/** * Copyright 2023 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * The result of a set comparison of two inputs. * @template T the element type. */ class SetComparison { /** * The elements that are only in the left input. * @type {T} */ get left() { return this.entries .filter(entry => entry.left && !entry.right) .map(entry => entry.element); } /** * The elements that are only in the right input. * @type {T} */ get right() { return this.entries .filter(entry => entry.right && !entry.left) .map(entry => entry.element); } /** * The set intersection of the inputs. * @type {T} */ get both() { return this.entries .filter(entry => entry.left && entry.right) .map(entry => entry.element); } /** * Creates a new comparison object. * @param {Iterable<ComparisonEntry<T>>} [entries] an optional initialization * iterable of comparison entries. */ constructor(entries = []) { this.entries = [...entries]; } /** * Creates a comparison of two input iterables. * @template T the element type of the inputs. * @param {Iterable<T>} left the first input iterable. * @param {Iterable<T>} right the second input iterable. * @returns {SetComparison<T>} the comparison. */ static create(left, right) { const leftSet = new Set(left); const rightSet = new Set(right); const union = new Set([...leftSet, ...rightSet]); const comparison = new SetComparison(); for (const element of union) { comparison.entries.push({ element, left: leftSet.has(element), right: rightSet.has(element), }); } return comparison; } } /** * Creates a comparison of two input iterables. * @template T the element type of the inputs. * @param {Iterable<T>} left the first input iterable. * @param {Iterable<T>} right the second input iterable. * @returns {SetComparison<T>} the comparison. */ export function compare(left, right) { return SetComparison.create(left, right); }