Skip to content

Commit 8808b84

Browse files
authored
0.1.4. (#1)
1 parent 2e4e7b4 commit 8808b84

File tree

6 files changed

+201
-35
lines changed

6 files changed

+201
-35
lines changed

.github/workflows/main.yml

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: main
2+
on:
3+
pull_request:
4+
branches:
5+
- main
6+
push:
7+
branches:
8+
- main
9+
jobs:
10+
build:
11+
name: Build
12+
runs-on: ${{matrix.os}}
13+
strategy:
14+
matrix:
15+
os: [ubuntu-latest]
16+
steps:
17+
- name: Checkout Repo
18+
uses: actions/checkout@v2
19+
- name: Setup Node
20+
uses: actions/setup-node@v2
21+
with:
22+
node-version: 18
23+
- name: Install
24+
run: yarn install
25+
- name: Build
26+
run: yarn build
27+
- name: Prettier
28+
run: yarn prettier
29+
- name: Tests
30+
run: yarn test:single

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
## 0.1.4
2+
3+
* Added the `forEach()` method to the `DefinitionWalker` class.
4+
* The `getParents()` method of the `DefinitionWalker` class accepts a `stepId` argument.

package.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "sequential-workflow-model",
33
"description": "Extendable data model of sequential workflow.",
4-
"version": "0.1.3",
4+
"version": "0.1.4",
55
"homepage": "https://nocode-js.com/",
66
"author": {
77
"name": "NoCode JS",
@@ -43,11 +43,11 @@
4343
},
4444
"devDependencies": {
4545
"typescript": "^4.9.5",
46-
"rollup-plugin-dts": "^5.2.0",
47-
"rollup-plugin-typescript2": "^0.34.1",
48-
"rollup": "^3.18.0",
46+
"rollup-plugin-dts": "^5.3.0",
47+
"rollup-plugin-typescript2": "^0.35.0",
48+
"rollup": "^3.25.3",
4949
"prettier": "^2.8.8",
50-
"@types/jest": "^29.5.1",
50+
"@types/jest": "^29.5.2",
5151
"jest": "^29.5.0",
5252
"ts-jest": "^29.1.0"
5353
},

src/definition-walker/definition-walker.spec.ts

+74-5
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ describe('DefinitionWalker', () => {
2727
}
2828

2929
const taskFoo = createTask('foo');
30-
const ifAlfa = createIf('beta', taskFoo);
31-
const ifBeta = createIf('alfa', ifAlfa);
30+
const ifAlfa = createIf('alfa', taskFoo);
31+
const ifBeta = createIf('beta', ifAlfa);
3232
const loop = {
3333
componentType: 'container',
3434
id: 'loop',
@@ -98,6 +98,13 @@ describe('DefinitionWalker', () => {
9898
const parents = walker.getParents(definition, definition.sequence);
9999
expect(parents.length).toEqual(0);
100100
});
101+
102+
it('returns parents when passed stepId', () => {
103+
const parents = walker.getParents(definition, 'ifbeta');
104+
expect(parents.length).toEqual(2);
105+
expect(parents[0]).toBe(loop);
106+
expect(parents[1]).toBe(ifBeta);
107+
});
101108
});
102109

103110
describe('findById', () => {
@@ -108,17 +115,79 @@ describe('DefinitionWalker', () => {
108115

109116
it('returns task step', () => {
110117
const found = walker.findById(definition, taskFoo.id);
111-
expect(found).toEqual(taskFoo);
118+
expect(found).toBe(taskFoo);
112119
});
113120

114121
it('returns container step', () => {
115122
const found = walker.findById(definition, loop.id);
116-
expect(found).toEqual(loop);
123+
expect(found).toBe(loop);
117124
});
118125

119126
it('returns switch step', () => {
120127
const found = walker.findById(definition, ifBeta.id);
121-
expect(found).toEqual(ifBeta);
128+
expect(found).toBe(ifBeta);
129+
});
130+
});
131+
132+
describe('forEach', () => {
133+
it('iterates over defition', () => {
134+
let count = 0;
135+
136+
walker.forEach(definition, (step, index, parent) => {
137+
switch (count) {
138+
case 0:
139+
expect(step).toBe(definition.sequence[0]);
140+
expect(index).toBe(0);
141+
expect(parent).toBe(definition.sequence);
142+
break;
143+
case 1:
144+
expect(step).toBe((definition.sequence[0] as BranchedStep).branches['false'][0]);
145+
break;
146+
case 2:
147+
expect(step).toBe(loop);
148+
break;
149+
case 3:
150+
expect(step).toBe(ifBeta);
151+
expect(index).toBe(0);
152+
expect(parent).toBe(loop.sequence);
153+
break;
154+
case 4:
155+
expect(step).toBe(ifAlfa);
156+
break;
157+
case 5:
158+
expect(step).toBe(taskFoo);
159+
break;
160+
}
161+
count++;
162+
});
163+
164+
expect(count).toEqual(6);
165+
});
166+
167+
it('iterates over sequence', () => {
168+
const steps: Step[] = [];
169+
170+
walker.forEach(loop.sequence, step => {
171+
steps.push(step);
172+
});
173+
174+
expect(steps.length).toEqual(3);
175+
expect(steps[0]).toBe(ifBeta);
176+
expect(steps[1]).toBe(ifAlfa);
177+
expect(steps[2]).toBe(taskFoo);
178+
});
179+
180+
it('stops when callback returns false', () => {
181+
let count = 0;
182+
183+
walker.forEach(definition, () => {
184+
count++;
185+
if (count === 2) {
186+
return false;
187+
}
188+
});
189+
190+
expect(count).toEqual(2);
122191
});
123192
});
124193
});

src/definition-walker/definition-walker.ts

+67-4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ type StepWithParentSequenceOrName = StepWithParentSequence | string;
2626

2727
export type StepChildrenResolver = (step: Step) => StepChildren | null;
2828

29+
export type StepForEachCallback = (step: Step, index: number, parentSequence: Sequence) => void | boolean;
30+
2931
export class DefinitionWalker {
3032
private readonly resolvers: StepChildrenResolver[];
3133

@@ -34,7 +36,8 @@ export class DefinitionWalker {
3436
}
3537

3638
/**
37-
* Returns children of the step.
39+
* Returns children of the step. If the step doesn't have children, returns null.
40+
* @param step The step.
3841
*/
3942
public getChildren(step: Step): StepChildren | null {
4043
const count = this.resolvers.length;
@@ -47,10 +50,24 @@ export class DefinitionWalker {
4750
return null;
4851
}
4952

50-
public getParents(definition: Definition, needle: Sequence | Step): StepOrName[] {
53+
/**
54+
* Returns the parents of the step or the sequence.
55+
* @param definition The definition.
56+
* @param needle The step, stepId or sequence to find.
57+
* @returns The parents of the step or the sequence.
58+
*/
59+
public getParents(definition: Definition, needle: Sequence | Step | string): StepOrName[] {
5160
const result: StepWithParentSequenceOrName[] = [];
52-
const searchSequence = Array.isArray(needle) ? needle : null;
53-
const searchStepId = !searchSequence ? (needle as Step).id : null;
61+
62+
let searchSequence: Sequence | null = null;
63+
let searchStepId: string | null = null;
64+
if (Array.isArray(needle)) {
65+
searchSequence = needle;
66+
} else if (typeof needle === 'string') {
67+
searchStepId = needle;
68+
} else {
69+
searchStepId = needle.id;
70+
}
5471

5572
if (this.find(definition.sequence, searchSequence, searchStepId, result)) {
5673
result.reverse();
@@ -87,6 +104,11 @@ export class DefinitionWalker {
87104
return this.getParentSequence(definition, stepId).step;
88105
}
89106

107+
public forEach(sequenceOrDefinition: Sequence | Definition, callback: StepForEachCallback) {
108+
const sequence = Array.isArray(sequenceOrDefinition) ? sequenceOrDefinition : sequenceOrDefinition.sequence;
109+
this.iterate(sequence, callback);
110+
}
111+
90112
private find(
91113
sequence: Sequence,
92114
needSequence: Sequence | null,
@@ -139,4 +161,45 @@ export class DefinitionWalker {
139161
}
140162
return false;
141163
}
164+
165+
private iterate(sequence: Sequence, callback: StepForEachCallback): boolean {
166+
const count = sequence.length;
167+
for (let index = 0; index < count; index++) {
168+
const step = sequence[index];
169+
if (callback(step, index, sequence) === false) {
170+
return false;
171+
}
172+
173+
const children = this.getChildren(step);
174+
if (children) {
175+
switch (children.type) {
176+
case StepChildrenType.sequence:
177+
{
178+
const childSequence = children.items as Sequence;
179+
if (this.iterate(childSequence, callback) === false) {
180+
return false;
181+
}
182+
}
183+
break;
184+
185+
case StepChildrenType.branches:
186+
{
187+
const branches = children.items as Branches;
188+
const branchNames = Object.keys(branches);
189+
for (const branchName of branchNames) {
190+
const parentSequence = branches[branchName];
191+
if (this.iterate(parentSequence, callback) === false) {
192+
return false;
193+
}
194+
}
195+
}
196+
break;
197+
198+
default:
199+
throw new Error(`Step children type ${children.type} is not supported`);
200+
}
201+
}
202+
}
203+
return true;
204+
}
142205
}

yarn.lock

+21-21
Original file line numberDiff line numberDiff line change
@@ -637,10 +637,10 @@
637637
dependencies:
638638
"@types/istanbul-lib-report" "*"
639639

640-
"@types/jest@^29.5.1":
641-
version "29.5.1"
642-
resolved "https://registry.yarnpkg.com/@types/jest/-/jest-29.5.1.tgz#83c818aa9a87da27d6da85d3378e5a34d2f31a47"
643-
integrity sha512-tEuVcHrpaixS36w7hpsfLBLpjtMRJUE09/MHXn923LOVojDwyC14cWcfc0rDs0VEfUyYmt/+iX1kxxp+gZMcaQ==
640+
"@types/jest@^29.5.2":
641+
version "29.5.2"
642+
resolved "https://registry.yarnpkg.com/@types/jest/-/jest-29.5.2.tgz#86b4afc86e3a8f3005b297ed8a72494f89e6395b"
643+
integrity sha512-mSoZVJF5YzGVCk+FsDxzDuH7s+SCkzrgKZzf0Z0T2WudhBUPoF6ktoTPC4R0ZoCPCV5xUvuU6ias5NvxcBcMMg==
644644
dependencies:
645645
expect "^29.0.0"
646646
pretty-format "^29.0.0"
@@ -1734,10 +1734,10 @@ lru-cache@^6.0.0:
17341734
dependencies:
17351735
yallist "^4.0.0"
17361736

1737-
magic-string@^0.29.0:
1738-
version "0.29.0"
1739-
resolved "https://registry.yarnpkg.com/magic-string/-/magic-string-0.29.0.tgz#f034f79f8c43dba4ae1730ffb5e8c4e084b16cf3"
1740-
integrity sha512-WcfidHrDjMY+eLjlU+8OvwREqHwpgCeKVBUpQ3OhYYuvfaYCUgcbuBzappNzZvg/v8onU3oQj+BYpkOJe9Iw4Q==
1737+
magic-string@^0.30.0:
1738+
version "0.30.0"
1739+
resolved "https://registry.yarnpkg.com/magic-string/-/magic-string-0.30.0.tgz#fd58a4748c5c4547338a424e90fa5dd17f4de529"
1740+
integrity sha512-LA+31JYDJLs82r2ScLrlz1GjSgu66ZV518eyWT+S8VhyQn/JL0u9MeBOvQMGYiPk1DBiSN9DDMOcXvigJZaViQ==
17411741
dependencies:
17421742
"@jridgewell/sourcemap-codec" "^1.4.13"
17431743

@@ -1972,30 +1972,30 @@ resolve@^1.20.0:
19721972
path-parse "^1.0.7"
19731973
supports-preserve-symlinks-flag "^1.0.0"
19741974

1975-
rollup-plugin-dts@^5.2.0:
1976-
version "5.2.0"
1977-
resolved "https://registry.yarnpkg.com/rollup-plugin-dts/-/rollup-plugin-dts-5.2.0.tgz#1970edb69cfa3ead285ff4289d8fe273b87a5af3"
1978-
integrity sha512-B68T/haEu2MKcz4kNUhXB8/h5sq4gpplHAJIYNHbh8cp4ZkvzDvNca/11KQdFrB9ZeKucegQIotzo5T0JUtM8w==
1975+
rollup-plugin-dts@^5.3.0:
1976+
version "5.3.0"
1977+
resolved "https://registry.yarnpkg.com/rollup-plugin-dts/-/rollup-plugin-dts-5.3.0.tgz#80a95988002f188e376f6db3b7e2f53679168957"
1978+
integrity sha512-8FXp0ZkyZj1iU5klkIJYLjIq/YZSwBoERu33QBDxm/1yw5UU4txrEtcmMkrq+ZiKu3Q4qvPCNqc3ovX6rjqzbQ==
19791979
dependencies:
1980-
magic-string "^0.29.0"
1980+
magic-string "^0.30.0"
19811981
optionalDependencies:
19821982
"@babel/code-frame" "^7.18.6"
19831983

1984-
rollup-plugin-typescript2@^0.34.1:
1985-
version "0.34.1"
1986-
resolved "https://registry.yarnpkg.com/rollup-plugin-typescript2/-/rollup-plugin-typescript2-0.34.1.tgz#c457f155a71d133c142689213fce78694e30d0be"
1987-
integrity sha512-P4cHLtGikESmqi1CA+tdMDUv8WbQV48mzPYt77TSTOPJpERyZ9TXdDgjSDix8Fkqce6soYz3+fa4lrC93IEkcw==
1984+
rollup-plugin-typescript2@^0.35.0:
1985+
version "0.35.0"
1986+
resolved "https://registry.yarnpkg.com/rollup-plugin-typescript2/-/rollup-plugin-typescript2-0.35.0.tgz#a84fb4e802b919613f31552c69c3415101b547c1"
1987+
integrity sha512-szcIO9hPUx3PhQl91u4pfNAH2EKbtrXaES+m163xQVE5O1CC0ea6YZV/5woiDDW3CR9jF2CszPrKN+AFiND0bg==
19881988
dependencies:
19891989
"@rollup/pluginutils" "^4.1.2"
19901990
find-cache-dir "^3.3.2"
19911991
fs-extra "^10.0.0"
19921992
semver "^7.3.7"
19931993
tslib "^2.4.0"
19941994

1995-
rollup@^3.18.0:
1996-
version "3.18.0"
1997-
resolved "https://registry.yarnpkg.com/rollup/-/rollup-3.18.0.tgz#2354ba63ba66d6a09c652c3ea0dbcd9dad72bbde"
1998-
integrity sha512-J8C6VfEBjkvYPESMQYxKHxNOh4A5a3FlP+0BETGo34HEcE4eTlgCrO2+eWzlu2a/sHs2QUkZco+wscH7jhhgWg==
1995+
rollup@^3.25.3:
1996+
version "3.25.3"
1997+
resolved "https://registry.yarnpkg.com/rollup/-/rollup-3.25.3.tgz#f9a8986f0f244bcfde2208da91ba46b8fd252551"
1998+
integrity sha512-ZT279hx8gszBj9uy5FfhoG4bZx8c+0A1sbqtr7Q3KNWIizpTdDEPZbV2xcbvHsnFp4MavCQYZyzApJ+virB8Yw==
19991999
optionalDependencies:
20002000
fsevents "~2.3.2"
20012001

0 commit comments

Comments
 (0)