@vnejs/plugins.vars.counters 0.1.2 → 0.1.4

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 CHANGED
@@ -1,6 +1,5 @@
1
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: "<" };
2
+ export const EXEC_NAME = "counter";
3
+
4
+ export const CHANGE_ACTIONS = { ADD: "+=", SUB: "-=", INC: "++", DEC: "--", SET: "=" };
5
+ export const COMPARE_ACTIONS = { MORE: ">", MORE_EQ: ">=", EQ: "===", EQ_NOT: "!==", LESS: "<", LESS_EQ: "<=" };
package/const/events.js CHANGED
@@ -1,3 +1,8 @@
1
1
  export const SUBSCRIBE_EVENTS = {
2
- EMIT: "vne:vars_counter:emit",
2
+ SET: "vne:vars_counter:set",
3
+ GET: "vne:vars_counter:get",
4
+ CHANGE: "vne:vars_counter:change",
5
+ COMPARE: "vne:vars_counter:compare",
6
+ EXEC_INFO: "vne:vars_counter:exec_info",
7
+ EXEC_CHECK: "vne:vars_counter:exec_check",
3
8
  };
package/index.js CHANGED
@@ -4,5 +4,6 @@ import * as constants from "./const/const";
4
4
  import { SUBSCRIBE_EVENTS } from "./const/events";
5
5
 
6
6
  import { Counters } from "./modules/counters";
7
+ import { CountersExec } from "./modules/exec";
7
8
 
8
- regPlugin("VARS_COUNTERS", { constants, events: SUBSCRIBE_EVENTS }, [Counters]);
9
+ regPlugin("VARS_COUNTERS", { constants, events: SUBSCRIBE_EVENTS }, [Counters, CountersExec]);
@@ -3,65 +3,45 @@ import { Module } from "@vnejs/module";
3
3
  export class Counters extends Module {
4
4
  name = "vars.counters";
5
5
 
6
- execName = "counter";
7
-
8
6
  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,
7
+ [this.CONST.VARS_COUNTERS.CHANGE_ACTIONS.SET]: (oldValue = 0, value = 0) => value,
8
+ [this.CONST.VARS_COUNTERS.CHANGE_ACTIONS.INC]: (oldValue = 0) => oldValue + 1,
9
+ [this.CONST.VARS_COUNTERS.CHANGE_ACTIONS.DEC]: (oldValue = 0) => oldValue - 1,
10
+ [this.CONST.VARS_COUNTERS.CHANGE_ACTIONS.ADD]: (oldValue = 0, value = 0) => oldValue + value,
11
+ [this.CONST.VARS_COUNTERS.CHANGE_ACTIONS.SUB]: (oldValue = 0, value = 0) => oldValue - value,
12
12
  };
13
13
  varsCompareHandlers = {
14
14
  [this.CONST.VARS_COUNTERS.COMPARE_ACTIONS.MORE]: (oldValue = 0, value) => oldValue > value,
15
+ [this.CONST.VARS_COUNTERS.COMPARE_ACTIONS.MORE_EQ]: (oldValue = 0, value) => oldValue >= value,
15
16
  [this.CONST.VARS_COUNTERS.COMPARE_ACTIONS.EQ]: (oldValue = 0, value) => oldValue === value,
17
+ [this.CONST.VARS_COUNTERS.COMPARE_ACTIONS.EQ_NOT]: (oldValue = 0, value) => oldValue !== value,
16
18
  [this.CONST.VARS_COUNTERS.COMPARE_ACTIONS.LESS]: (oldValue = 0, value) => oldValue < value,
19
+ [this.CONST.VARS_COUNTERS.COMPARE_ACTIONS.LESS_EQ]: (oldValue = 0, value) => oldValue <= value,
17
20
  };
18
21
 
19
22
  subscribe = () => {
20
- this.on(this.EVENTS.VARS_COUNTERS.SET, this.onCounter);
23
+ this.on(this.EVENTS.VARS_COUNTERS.SET, this.onCounterSet);
24
+ this.on(this.EVENTS.VARS_COUNTERS.GET, this.onCounterGet);
25
+ this.on(this.EVENTS.VARS_COUNTERS.CHANGE, this.onCounterChange);
26
+ this.on(this.EVENTS.VARS_COUNTERS.COMPARE, this.onCounterCompare);
27
+ this.on(this.EVENTS.VARS_COUNTERS.EXEC_INFO, this.onCounterExecInfo);
28
+ this.on(this.EVENTS.VARS_COUNTERS.EXEC_CHECK, this.onCounterExecCheck);
21
29
  };
22
30
 
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 });
31
+ onCounterSet = ({ name, isGlobal = false, value = 0 }) => this.emit(this.EVENTS.VARS.SET, { name, type: this.CONST.VARS_COUNTERS.TYPE, value, isGlobal });
32
+ onCounterGet = ({ name = "", isGlobal = false } = {}) => this.emitOne(this.EVENTS.VARS.GET, { name, type: this.CONST.VARS_COUNTERS.TYPE, isGlobal });
33
+ onCounterChange = ({ value = 0, execValue = 0, action = "" } = {}) => this.varsChangeHandlers[action](value, execValue);
34
+ onCounterCompare = ({ value = 0, execValue = 0, action = "" } = {}) => this.varsCompareHandlers[action](value, execValue);
35
+ onCounterExecInfo = async ({ line = "", keywords = [], withValue = true } = {}) => {
36
+ const [name = "", action = "", execValue = "0"] = line.split(" ");
37
+ const isGlobal = keywords.includes("global");
38
+ const result = { name, action, isGlobal, execValue: Number(execValue) };
38
39
 
39
- this.emit(this.EVENTS.SCENARIO.NEXT, { module: this.name });
40
+ return withValue ? { ...result, value: await this.emitOne(this.EVENTS.VARS_COUNTERS.GET, { name, isGlobal }) } : result;
40
41
  };
42
+ onCounterExecCheck = async ({ line = "", keywords = [] } = {}) => {
43
+ const { action = "", execValue = 0, value = 0 } = await this.emitOne(this.EVENTS.VARS_COUNTERS.EXEC_INFO, { line, keywords });
41
44
 
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 });
45
+ return this.emitOne(this.EVENTS.VARS_COUNTERS.COMPARE, { value, execValue, action });
62
46
  };
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
47
  }
@@ -0,0 +1,30 @@
1
+ import { Module } from "@vnejs/module";
2
+
3
+ export class CountersExec extends Module {
4
+ name = "vars.counters.exec";
5
+
6
+ init = () =>
7
+ Promise.all([
8
+ this.emit(this.EVENTS.SCENARIO.LINE_EXEC_REG, { block: this.CONST.VARS_COUNTERS.EXEC_NAME, handler: this.onLineExecBlock }),
9
+ this.emit(this.EVENTS.SCENARIO.LINE_EXEC_REG, { module: this.CONST.VARS_COUNTERS.EXEC_NAME, handler: this.onLineExecModule }),
10
+ ]);
11
+
12
+ onLineExecModule = async ({ line = "", keywords = [] } = {}) => {
13
+ const { name = "", action = "", execValue = 0, value = 0, isGlobal = false } = await this.emitOne(this.EVENTS.VARS_COUNTERS.EXEC_INFO, { line, keywords });
14
+
15
+ const newValue = await this.emitOne(this.EVENTS.VARS_COUNTERS.CHANGE, { value, execValue, action });
16
+
17
+ await this.emit(this.EVENTS.VARS_COUNTERS.SET, { name, isGlobal, value: newValue });
18
+
19
+ this.emit(this.EVENTS.SCENARIO.NEXT, { module: this.name });
20
+ };
21
+ onLineExecBlock = async ({ line = "", keywords = [] } = {}) => {
22
+ const { action = "", execValue = 0, value = 0 } = await this.emitOne(this.EVENTS.VARS_COUNTERS.EXEC_INFO, { line, keywords });
23
+
24
+ const shouldGoInside = await this.emitOne(this.EVENTS.VARS_COUNTERS.COMPARE, { value, execValue, action });
25
+
26
+ await this.emit(this.EVENTS.VARS.SCENARIO, { shouldGoInside });
27
+
28
+ this.emit(this.EVENTS.SCENARIO.NEXT, { module: this.name });
29
+ };
30
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vnejs/plugins.vars.counters",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1",