UNPKG

1.45 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3/*@internal*/
4function iterator() {
5 const ids = [];
6 let inAnnex = false;
7 let currentLevel = 0;
8 return {
9 next(level, annex) {
10 if (inAnnex && !annex)
11 throw new Error('Clauses cannot follow annexes');
12 if (level - currentLevel > 1)
13 throw new Error('Skipped clause');
14 const nextNum = annex ? nextAnnexNum : nextClauseNum;
15 if (level === currentLevel) {
16 ids[currentLevel] = nextNum(level);
17 }
18 else if (level > currentLevel) {
19 ids.push(nextNum(level));
20 }
21 else {
22 ids.length = level + 1;
23 ids[level] = nextNum(level);
24 }
25 currentLevel = level;
26 return { value: ids.join('.'), done: false };
27 },
28 };
29 function nextAnnexNum(level) {
30 if (!inAnnex) {
31 if (level > 0)
32 throw new Error('First annex must be at depth 0');
33 inAnnex = true;
34 return 'A';
35 }
36 if (level === 0) {
37 return String.fromCharCode(ids[0].charCodeAt(0) + 1);
38 }
39 return nextClauseNum(level);
40 }
41 function nextClauseNum(level) {
42 if (ids[level] === undefined)
43 return 1;
44 return ids[level] + 1;
45 }
46}
47exports.default = iterator;