Skip to content

Commit 0c56c54

Browse files
kanongilMarsup
authored andcommitted
Handle non-integer and empty meetings
1 parent e42390e commit 0c56c54

File tree

3 files changed

+56
-15
lines changed

3 files changed

+56
-15
lines changed

lib/index.d.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,15 @@ export namespace Team {
4444
*
4545
* @default 1
4646
*/
47-
readonly meetings?: number;
47+
readonly meetings?: number | undefined;
4848

4949
/**
50-
* Throws when the team attends more than the expected number of `meetings`.
50+
* Throws when the team attends more than the expected number of `meetings`,
51+
* or if scheduling an empty meeting.
5152
*
5253
* @default false
5354
*/
54-
readonly strict?: boolean;
55+
readonly strict?: boolean | undefined;
5556
}
5657

5758
type ElementOf<T> = T extends (infer E)[] ? E : T;

lib/index.js

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,52 @@ exports.Team = class {
2929
this._reject = reject;
3030
});
3131

32-
const meetings = options.meetings || 1;
32+
const meetings = options.meetings ?? 1;
33+
const strict = !!options.strict;
34+
35+
if (!Number.isInteger(meetings) || meetings <= 0) {
36+
if (meetings === 0 && !strict) {
37+
return this._finalize(null, null);
38+
}
39+
40+
throw new Error('Invalid meetings value');
41+
}
42+
3343
this.#meetings = meetings;
3444
this.#count = meetings;
3545
this.#notes = [];
3646
this.#done = false;
37-
this.#strict = options.strict;
47+
this.#strict = strict;
48+
3849
}
3950

40-
attend(note) {
51+
_finalize(err, note) {
4152

42-
if (this.#strict && this.#done) {
43-
throw new Error('Unscheduled meeting');
53+
this.#done = true;
54+
this.#notes = null;
55+
56+
if (err) {
57+
this._reject(err);
4458
}
45-
else if (this.#done) {
59+
else {
60+
this._resolve(note);
61+
}
62+
}
63+
64+
attend(note) {
65+
66+
if (this.#done) {
67+
if (this.#strict) {
68+
throw new Error('Unscheduled meeting');
69+
}
70+
71+
// else ignore
72+
4673
return;
4774
}
4875

4976
if (note instanceof Error) {
50-
this.#done = true;
51-
this.#notes = null;
52-
return this._reject(note);
77+
return this._finalize(note);
5378
}
5479

5580
this.#notes.push(note);
@@ -58,9 +83,7 @@ exports.Team = class {
5883
return;
5984
}
6085

61-
this.#done = true;
62-
this._resolve(this.#meetings === 1 ? this.#notes[0] : [...this.#notes]);
63-
this.#notes = null;
86+
this._finalize(null, this.#meetings === 1 ? this.#notes[0] : this.#notes);
6487
}
6588

6689
async regroup(options) {

test/index.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,23 @@ describe('Team', () => {
198198
expect(Teamwork.Team._notes(team)).to.be.null();
199199
});
200200

201+
it('immediately resolves non-strict empty meetings with null', async () => {
202+
203+
const team = new Teamwork.Team({ meetings: 0, strict: false });
204+
205+
const notes = await team.work;
206+
expect(notes).to.equal(null);
207+
});
208+
209+
it('throws error on invalid meetings', () => {
210+
211+
expect(() => new Teamwork.Team({ meetings: 0, strict: true })).to.throw('Invalid meetings value');
212+
expect(() => new Teamwork.Team({ meetings: 0.5 })).to.throw('Invalid meetings value');
213+
expect(() => new Teamwork.Team({ meetings: -1 })).to.throw('Invalid meetings value');
214+
expect(() => new Teamwork.Team({ meetings: NaN })).to.throw('Invalid meetings value');
215+
expect(() => new Teamwork.Team({ meetings: '' })).to.throw('Invalid meetings value');
216+
});
217+
201218
it('regroup works after team error', async () => {
202219

203220
const team = new Teamwork.Team({ meetings: 2, strict: true });

0 commit comments

Comments
 (0)