Skip to content

Commit

Permalink
feat(lexicons): add iterable capabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
matthieusieben committed May 15, 2024
1 parent 0a1e434 commit 7575c17
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions packages/lexicon/src/lexicons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,33 @@ import * as ComplexValidators from './validators/complex'
/**
* A collection of compiled lexicons.
*/
export class Lexicons {
export class Lexicons implements Iterable<LexiconDoc> {
docs: Map<string, LexiconDoc> = new Map()
defs: Map<string, LexUserType> = new Map()

constructor(docs?: LexiconDoc[]) {
if (docs?.length) {
constructor(docs?: Iterable<LexiconDoc>) {
if (docs) {
for (const doc of docs) {
this.add(doc)
}
}
}

/**
* @example clone a lexicon:
* ```ts
* const clone = new Lexicons(originalLexicon)
* ```
*
* @example get docs array:
* ```ts
* const docs = Array.from(lexicons)
* ```
*/
[Symbol.iterator](): Iterator<LexiconDoc> {
return this.docs.values()
}

/**
* Add a lexicon doc.
*/
Expand Down

0 comments on commit 7575c17

Please sign in to comment.