-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(reference): change ReferenceSet from stamp to TypeScript class (#…
…4100) Refs #3481 BREAKING CHANGE: ReferenceSet from apidom-reference package became a class and requires to be instantiated with new operator.
- Loading branch information
Showing
21 changed files
with
128 additions
and
133 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,67 @@ | ||
import stampit from 'stampit'; | ||
import { propEq } from 'ramda'; | ||
import { isNotUndefined, isString } from 'ramda-adjunct'; | ||
|
||
import { ReferenceSet as IReferenceSet } from './types'; | ||
import type Reference from './Reference'; | ||
|
||
const ReferenceSet: stampit.Stamp<IReferenceSet> = stampit({ | ||
props: { | ||
rootRef: null, | ||
refs: [], | ||
circular: false, | ||
}, | ||
init({ refs = [] } = {}) { | ||
export interface ReferenceSetOptions { | ||
readonly refs?: Reference[]; | ||
readonly circular?: boolean; | ||
} | ||
|
||
class ReferenceSet { | ||
public rootRef?: Reference; | ||
|
||
public readonly refs: Reference[]; | ||
|
||
public readonly circular: boolean; | ||
|
||
constructor({ refs = [], circular = false }: ReferenceSetOptions = {}) { | ||
this.refs = []; | ||
refs.forEach((ref: Reference) => this.add(ref)); | ||
}, | ||
methods: { | ||
get size(): number { | ||
// @ts-ignore | ||
return this.refs.length; | ||
}, | ||
|
||
add(reference: Reference): IReferenceSet { | ||
if (!this.has(reference)) { | ||
this.refs.push(reference); | ||
this.rootRef = this.rootRef === null ? reference : this.rootRef; | ||
reference.refSet = this; // eslint-disable-line no-param-reassign | ||
} | ||
return this; | ||
}, | ||
|
||
merge(anotherRefSet: IReferenceSet): IReferenceSet { | ||
for (const reference of anotherRefSet.values()) { | ||
this.add(reference); | ||
} | ||
return this; | ||
}, | ||
|
||
has(thing: string | Reference): boolean { | ||
const uri = isString(thing) ? thing : thing.uri; | ||
return isNotUndefined(this.find(propEq(uri, 'uri'))); | ||
}, | ||
|
||
find(callback): Reference | undefined { | ||
return this.refs.find(callback); | ||
}, | ||
|
||
*values() { | ||
yield* this.refs; | ||
}, | ||
|
||
clean() { | ||
this.refs.forEach((ref: Reference) => { | ||
ref.refSet = undefined; // eslint-disable-line no-param-reassign | ||
}); | ||
this.rootRef = null; | ||
this.refs = []; | ||
}, | ||
}, | ||
}); | ||
this.circular = circular; | ||
refs.forEach(this.add.bind(this)); | ||
} | ||
|
||
get size(): number { | ||
return this.refs.length; | ||
} | ||
|
||
add(reference: Reference): this { | ||
if (!this.has(reference)) { | ||
this.refs.push(reference); | ||
this.rootRef = this.rootRef === undefined ? reference : this.rootRef; | ||
reference.refSet = this; // eslint-disable-line no-param-reassign | ||
} | ||
return this; | ||
} | ||
|
||
merge(anotherRefSet: this): this { | ||
for (const reference of anotherRefSet.values()) { | ||
this.add(reference); | ||
} | ||
return this; | ||
} | ||
|
||
has(thing: string | Reference): boolean { | ||
const uri = isString(thing) ? thing : thing.uri; | ||
return isNotUndefined(this.find((ref: Reference) => ref.uri === uri)); | ||
} | ||
|
||
find( | ||
predicate: (value: Reference, index: number, obj: Reference[]) => boolean, | ||
): Reference | undefined { | ||
return this.refs.find(predicate); | ||
} | ||
|
||
*values() { | ||
yield* this.refs; | ||
} | ||
|
||
clean() { | ||
this.refs.forEach((ref: Reference) => { | ||
ref.refSet = undefined; // eslint-disable-line no-param-reassign | ||
}); | ||
this.rootRef = undefined; | ||
this.refs.length = 0; | ||
} | ||
} | ||
|
||
export default ReferenceSet; |
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
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.