|
1 |
| -import stampit from 'stampit'; |
2 |
| -import { propEq } from 'ramda'; |
3 | 1 | import { isNotUndefined, isString } from 'ramda-adjunct';
|
4 | 2 |
|
5 |
| -import { ReferenceSet as IReferenceSet } from './types'; |
6 | 3 | import type Reference from './Reference';
|
7 | 4 |
|
8 |
| -const ReferenceSet: stampit.Stamp<IReferenceSet> = stampit({ |
9 |
| - props: { |
10 |
| - rootRef: null, |
11 |
| - refs: [], |
12 |
| - circular: false, |
13 |
| - }, |
14 |
| - init({ refs = [] } = {}) { |
| 5 | +export interface ReferenceSetOptions { |
| 6 | + readonly refs?: Reference[]; |
| 7 | + readonly circular?: boolean; |
| 8 | +} |
| 9 | + |
| 10 | +class ReferenceSet { |
| 11 | + public rootRef?: Reference; |
| 12 | + |
| 13 | + public readonly refs: Reference[]; |
| 14 | + |
| 15 | + public readonly circular: boolean; |
| 16 | + |
| 17 | + constructor({ refs = [], circular = false }: ReferenceSetOptions = {}) { |
15 | 18 | this.refs = [];
|
16 |
| - refs.forEach((ref: Reference) => this.add(ref)); |
17 |
| - }, |
18 |
| - methods: { |
19 |
| - get size(): number { |
20 |
| - // @ts-ignore |
21 |
| - return this.refs.length; |
22 |
| - }, |
23 |
| - |
24 |
| - add(reference: Reference): IReferenceSet { |
25 |
| - if (!this.has(reference)) { |
26 |
| - this.refs.push(reference); |
27 |
| - this.rootRef = this.rootRef === null ? reference : this.rootRef; |
28 |
| - reference.refSet = this; // eslint-disable-line no-param-reassign |
29 |
| - } |
30 |
| - return this; |
31 |
| - }, |
32 |
| - |
33 |
| - merge(anotherRefSet: IReferenceSet): IReferenceSet { |
34 |
| - for (const reference of anotherRefSet.values()) { |
35 |
| - this.add(reference); |
36 |
| - } |
37 |
| - return this; |
38 |
| - }, |
39 |
| - |
40 |
| - has(thing: string | Reference): boolean { |
41 |
| - const uri = isString(thing) ? thing : thing.uri; |
42 |
| - return isNotUndefined(this.find(propEq(uri, 'uri'))); |
43 |
| - }, |
44 |
| - |
45 |
| - find(callback): Reference | undefined { |
46 |
| - return this.refs.find(callback); |
47 |
| - }, |
48 |
| - |
49 |
| - *values() { |
50 |
| - yield* this.refs; |
51 |
| - }, |
52 |
| - |
53 |
| - clean() { |
54 |
| - this.refs.forEach((ref: Reference) => { |
55 |
| - ref.refSet = undefined; // eslint-disable-line no-param-reassign |
56 |
| - }); |
57 |
| - this.rootRef = null; |
58 |
| - this.refs = []; |
59 |
| - }, |
60 |
| - }, |
61 |
| -}); |
| 19 | + this.circular = circular; |
| 20 | + refs.forEach(this.add.bind(this)); |
| 21 | + } |
| 22 | + |
| 23 | + get size(): number { |
| 24 | + return this.refs.length; |
| 25 | + } |
| 26 | + |
| 27 | + add(reference: Reference): this { |
| 28 | + if (!this.has(reference)) { |
| 29 | + this.refs.push(reference); |
| 30 | + this.rootRef = this.rootRef === undefined ? reference : this.rootRef; |
| 31 | + reference.refSet = this; // eslint-disable-line no-param-reassign |
| 32 | + } |
| 33 | + return this; |
| 34 | + } |
| 35 | + |
| 36 | + merge(anotherRefSet: this): this { |
| 37 | + for (const reference of anotherRefSet.values()) { |
| 38 | + this.add(reference); |
| 39 | + } |
| 40 | + return this; |
| 41 | + } |
| 42 | + |
| 43 | + has(thing: string | Reference): boolean { |
| 44 | + const uri = isString(thing) ? thing : thing.uri; |
| 45 | + return isNotUndefined(this.find((ref: Reference) => ref.uri === uri)); |
| 46 | + } |
| 47 | + |
| 48 | + find( |
| 49 | + predicate: (value: Reference, index: number, obj: Reference[]) => boolean, |
| 50 | + ): Reference | undefined { |
| 51 | + return this.refs.find(predicate); |
| 52 | + } |
| 53 | + |
| 54 | + *values() { |
| 55 | + yield* this.refs; |
| 56 | + } |
| 57 | + |
| 58 | + clean() { |
| 59 | + this.refs.forEach((ref: Reference) => { |
| 60 | + ref.refSet = undefined; // eslint-disable-line no-param-reassign |
| 61 | + }); |
| 62 | + this.rootRef = undefined; |
| 63 | + this.refs.length = 0; |
| 64 | + } |
| 65 | +} |
62 | 66 |
|
63 | 67 | export default ReferenceSet;
|
0 commit comments