@vnejs/plugins.vars.counters 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/const/const.js +6 -0
- package/const/events.js +3 -0
- package/index.js +8 -0
- package/modules/counters.js +67 -0
- package/package.json +17 -0
package/const/const.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export const TYPE = "counter";
|
|
2
|
+
export const MODULE_LINE_REG = /^(?<name>[a-zA-Z0-9_\-]+) (?<action>add|dec|\+\+)( (?<value>[\-]?[0-9]+))?( global)?$/;
|
|
3
|
+
export const BLOCK_LINE_REG = /^(?<name>[a-zA-Z0-9_\-]+) (?<action>\>|===|\<) (?<value>[\-]?[0-9]+)( global)?$/;
|
|
4
|
+
export const CHOOSE_LINE_REG = /^(?<name>[a-zA-Z0-9_\-]+)( global)?$/;
|
|
5
|
+
export const ACTIONS = { ADD: "add", DEC: "dec", PLUS: "++" };
|
|
6
|
+
export const COMPARE_ACTIONS = { MORE: ">", EQ: "===", LESS: "<" };
|
package/const/events.js
ADDED
package/index.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { regPlugin } from "@vnejs/shared";
|
|
2
|
+
|
|
3
|
+
import * as constants from "./const/const";
|
|
4
|
+
import { SUBSCRIBE_EVENTS } from "./const/events";
|
|
5
|
+
|
|
6
|
+
import { Counters } from "./modules/counters";
|
|
7
|
+
|
|
8
|
+
regPlugin("VARS_COUNTERS", { constants, events: SUBSCRIBE_EVENTS }, [Counters]);
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { Module } from "@vnejs/module";
|
|
2
|
+
|
|
3
|
+
export class Counters extends Module {
|
|
4
|
+
name = "vars.counters";
|
|
5
|
+
|
|
6
|
+
execName = "counter";
|
|
7
|
+
|
|
8
|
+
varsChangeHandlers = {
|
|
9
|
+
[this.CONST.VARS_COUNTERS.ACTIONS.PLUS]: (oldValue = 0) => oldValue + 1,
|
|
10
|
+
[this.CONST.VARS_COUNTERS.ACTIONS.ADD]: (oldValue = 0, value) => oldValue + value,
|
|
11
|
+
[this.CONST.VARS_COUNTERS.ACTIONS.DEC]: (oldValue = 0, value) => oldValue - value,
|
|
12
|
+
};
|
|
13
|
+
varsCompareHandlers = {
|
|
14
|
+
[this.CONST.VARS_COUNTERS.COMPARE_ACTIONS.MORE]: (oldValue = 0, value) => oldValue > value,
|
|
15
|
+
[this.CONST.VARS_COUNTERS.COMPARE_ACTIONS.EQ]: (oldValue = 0, value) => oldValue === value,
|
|
16
|
+
[this.CONST.VARS_COUNTERS.COMPARE_ACTIONS.LESS]: (oldValue = 0, value) => oldValue < value,
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
subscribe = () => {
|
|
20
|
+
this.on(this.EVENTS.VARS_COUNTERS.SET, this.onCounter);
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
init = () =>
|
|
24
|
+
Promise.all([
|
|
25
|
+
this.emit(this.EVENTS.SCENARIO.LINE_EXEC_REG, { block: this.execName, handler: this.execHandlerBlock }),
|
|
26
|
+
this.emit(this.EVENTS.SCENARIO.LINE_EXEC_REG, { module: this.execName, handler: this.execHandlerModule }),
|
|
27
|
+
]);
|
|
28
|
+
|
|
29
|
+
execHandlerModule = async (line = "") => {
|
|
30
|
+
const defaultActions = this.CONST.VARS_COUNTERS.ACTIONS.PLUS;
|
|
31
|
+
const { name = "", action = defaultActions, value = 0 } = line.match(this.CONST.VARS_COUNTERS.MODULE_LINE_REG).groups;
|
|
32
|
+
const isGlobal = line.endsWith(" global");
|
|
33
|
+
|
|
34
|
+
const oldValue = this.getValue(name, isGlobal);
|
|
35
|
+
const newValue = this.varsChangeHandlers[action](oldValue, Number(value));
|
|
36
|
+
|
|
37
|
+
await this.emit(this.EVENTS.VARS_COUNTERS.SET, { name, value: newValue, isGlobal });
|
|
38
|
+
|
|
39
|
+
this.emit(this.EVENTS.SCENARIO.NEXT, { module: this.name });
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
execHandlerBlock = async (line, args) => {
|
|
43
|
+
const { curLine = 0, label } = this.globalState.scenario;
|
|
44
|
+
const defaultActions = this.CONST.VARS_COUNTERS.ACTIONS.PLUS;
|
|
45
|
+
const { name = "", action = defaultActions, value = 0 } = line.match(this.CONST.VARS_COUNTERS.BLOCK_LINE_REG).groups;
|
|
46
|
+
const oldValue = this.getValue(name, line.endsWith(" global"));
|
|
47
|
+
|
|
48
|
+
if (this.varsCompareHandlers[action](oldValue, Number(value))) return this.increaseCurLineAndGoNext(1);
|
|
49
|
+
|
|
50
|
+
const lines = await this.emitOne(this.EVENTS.SCENARIO.LINES_GET, { label });
|
|
51
|
+
|
|
52
|
+
let indexShift = 1;
|
|
53
|
+
|
|
54
|
+
while (lines[curLine + indexShift] && lines[curLine + indexShift].indent > lines[curLine].indent) indexShift++;
|
|
55
|
+
|
|
56
|
+
return this.increaseCurLineAndGoNext(indexShift);
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
increaseCurLineAndGoNext = (indexShift) => {
|
|
60
|
+
this.globalState.scenario.curLine += indexShift;
|
|
61
|
+
this.emit(this.EVENTS.SCENARIO.NEXT, { module: this.name });
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
onCounter = ({ name, isGlobal = false, value = 0 }) => this.emit(this.EVENTS.VARS.SET, { name, type: this.CONST.VARS.TYPES.COUNTER, value, isGlobal });
|
|
65
|
+
|
|
66
|
+
getValue = (name, isGlobal) => (isGlobal ? this.shared : this.globalState).vars[`${this.CONST.VARS.TYPES.COUNTER}:${name}`];
|
|
67
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@vnejs/plugins.vars.counters",
|
|
3
|
+
"version": "0.1.2",
|
|
4
|
+
"main": "index.js",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
7
|
+
"publish:major:plugin": "npm run publish:major",
|
|
8
|
+
"publish:minor:plugin": "npm run publish:minor",
|
|
9
|
+
"publish:patch:plugin": "npm run publish:patch",
|
|
10
|
+
"publish:major": "npm version major && npm publish --access public",
|
|
11
|
+
"publish:minor": "npm version minor && npm publish --access public",
|
|
12
|
+
"publish:patch": "npm version patch && npm publish --access public"
|
|
13
|
+
},
|
|
14
|
+
"author": "",
|
|
15
|
+
"license": "ISC",
|
|
16
|
+
"description": ""
|
|
17
|
+
}
|