From 3f62d68babc4b090127903ef69471f690a720b9b Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Sat, 9 Jan 2021 16:33:56 +0100 Subject: [PATCH] union, intersect, subtract --- implementations/javascript/src/flex.ts | 22 ++++++++++++++++++++-- implementations/javascript/src/values.ts | 4 ++-- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/implementations/javascript/src/flex.ts b/implementations/javascript/src/flex.ts index 1d098d5..ed4a7c9 100644 --- a/implementations/javascript/src/flex.ts +++ b/implementations/javascript/src/flex.ts @@ -97,7 +97,7 @@ export class FlexMap implements Map { return this.items.values(); } - [Symbol.toStringTag] = 'FlexMap'; + get [Symbol.toStringTag]() { return 'FlexMap'; } equals(other: any, eqv: Equivalence = (v1, v2) => v1 === v2): boolean { if (!('size' in other && 'has' in other && 'get' in other)) return false; @@ -206,7 +206,7 @@ export class FlexSet implements Set { return this.items.values(); } - [Symbol.toStringTag] = 'FlexSet'; + get [Symbol.toStringTag]() { return 'FlexSet'; } equals(other: any): boolean { if (!('size' in other && 'has' in other)) return false; @@ -220,4 +220,22 @@ export class FlexSet implements Set { canonicalValues(): IterableIterator { return this.items.keys(); } + + union(other: Set): FlexSet { + const result = new FlexSet(this.canonicalizer, this); + for (let k of other) result.add(k); + return result; + } + + intersect(other: Set): FlexSet { + const result = new FlexSet(this.canonicalizer); + for (let k of this) if (other.has(k)) result.add(k); + return result; + } + + subtract(other: Set): FlexSet { + const result = new FlexSet(this.canonicalizer); + for (let k of this) if (!other.has(k)) result.add(k); + return result; + } } diff --git a/implementations/javascript/src/values.ts b/implementations/javascript/src/values.ts index 090d081..70f790d 100644 --- a/implementations/javascript/src/values.ts +++ b/implementations/javascript/src/values.ts @@ -598,7 +598,7 @@ export class Dictionary extends FlexMap { return this.asPreservesText(); } - [Symbol.toStringTag] = 'Dictionary'; + get [Symbol.toStringTag]() { return 'Dictionary'; } [PreserveOn](encoder: Encoder) { if (encoder.canonical) { @@ -643,7 +643,7 @@ export class Set extends FlexSet { return new Set(this); } - [Symbol.toStringTag] = 'Set'; + get [Symbol.toStringTag]() { return 'Set'; } [PreserveOn](encoder: Encoder) { if (encoder.canonical) {