@vnejs/plugins.vars 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 ADDED
@@ -0,0 +1,10 @@
1
+ export const TYPES = { FLAG: "flag", COUNTER: "counter" };
2
+
3
+ export const FLAG_LINE_REG = /^(?<name>[a-zA-Z0-9_\-]+)( (?<action>set|unset))?( global)?$/;
4
+ export const FLAG_ACTIONS = { SET: "set", UNSET: "unset" };
5
+
6
+ export const COUNTER_MODULE_LINE_REG =
7
+ /^(?<name>[a-zA-Z0-9_\-]+) (?<action>add|dec|\+\+)( (?<value>[\-]?[0-9]+))?( global)?$/;
8
+ export const COUNTER_BLOCK_LINE_REG = /^(?<name>[a-zA-Z0-9_\-]+) (?<action>\>|===|\<) (?<value>[\-]?[0-9]+)( global)?$/;
9
+ export const COUNTER_ACTIONS = { ADD: "add", DEC: "dec", PLUS: "++" };
10
+ export const COUNTER_COMPARE_ACTIONS = { MORE: ">", EQ: "===", LESS: "<" };
@@ -0,0 +1,4 @@
1
+ export const SUBSCRIBE_EVENTS = {
2
+ SET: "vne:vars:set",
3
+ INJECT: "vne:vars:inject",
4
+ };
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 { Vars } from "./modules/vars";
7
+
8
+ regPlugin("VARS", { constants, events: SUBSCRIBE_EVENTS }, [Vars]);
@@ -0,0 +1,40 @@
1
+ import { Module } from "@vnejs/module";
2
+
3
+ export class Vars extends Module {
4
+ name = "vars";
5
+
6
+ subscribe = () => {
7
+ this.on(this.EVENTS.VARS.SET, this.onVarSet);
8
+ this.on(this.EVENTS.VARS.INJECT, this.onVarInject);
9
+
10
+ this.on(this.EVENTS.STATE.SET, this.setStateFromGlobalState);
11
+ this.on(this.EVENTS.STATE.CLEAR, this.setDefaultState);
12
+ };
13
+
14
+ init = async () => {
15
+ const vars = await this.emitOne(this.EVENTS.STORAGE.GET_ALL, { prefix: this.name });
16
+
17
+ this.shared.vars = vars || {};
18
+ };
19
+
20
+ onVarSet = async ({ name, type, value, isGlobal = false }) => {
21
+ const varName = `${type}:${name}`;
22
+
23
+ (isGlobal ? this.shared.vars : this.state)[varName] = value;
24
+ isGlobal && this.emit(this.EVENTS.STORAGE.SET, { prefix: this.name, key: varName, value });
25
+ };
26
+
27
+ onVarInject = async ({ text = "" }) => this.injectsVars(this.injectsVars(text.slice(), this.shared.vars, "%%"), this.state, "%");
28
+
29
+ injectsVars = (text, vars, symbol) => {
30
+ let newText = text.slice();
31
+ Object.keys(vars).forEach((key) => {
32
+ const varKey = `${symbol}${key}${symbol}`;
33
+ while (newText.includes(varKey)) newText = newText.replace(varKey, vars[key]);
34
+ });
35
+
36
+ return newText;
37
+ };
38
+
39
+ getDefaultState = () => ({});
40
+ }
package/package.json ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "name": "@vnejs/plugins.vars",
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
+ }