@vnejs/plugins.feature.date 0.0.1

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 @@
1
+ export const ACTIONS = { ADD: "add", SET: "set" };
@@ -0,0 +1,5 @@
1
+ export const SUBSCRIBE_EVENTS = {
2
+ SET: "vne:date:set",
3
+ ADD: "vne:date:add",
4
+ CHANGED: "vne:date:changed",
5
+ };
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 { DateModule } from "./modules/date";
7
+
8
+ regPlugin("DATE", { constants, events: SUBSCRIBE_EVENTS }, [DateModule]);
@@ -0,0 +1,49 @@
1
+ import { Module } from "@vnejs/module";
2
+
3
+ export class DateModule extends Module {
4
+ name = "date";
5
+
6
+ subscribe = () => {
7
+ this.on(this.EVENTS.DATE.SET, this.onDateSet);
8
+ this.on(this.EVENTS.DATE.ADD, this.onDateAdd);
9
+
10
+ this.on(this.EVENTS.STATE.SET, this.setStateFromGlobalState);
11
+ this.on(this.EVENTS.STATE.CLEAR, this.setDefaultState);
12
+ };
13
+
14
+ init = () => this.emit(this.EVENTS.SCENARIO.EXEC_REG, { module: this.name, handler: this.execHandler });
15
+
16
+ execHandler = async (line) => {
17
+ const [action, ...values] = line.split(" ");
18
+
19
+ const days = Number(values[0] || 0) || 0;
20
+ const months = Number(values[1] || 0) || 0;
21
+ const years = Number(values[2] || 0) || 0;
22
+
23
+ switch (action) {
24
+ case this.CONST.DATE.ACTIONS.ADD:
25
+ await this.emit(this.EVENTS.DATE.ADD, { days, months, years });
26
+ break;
27
+ case this.CONST.DATE.ACTIONS.SET:
28
+ await this.emit(this.EVENTS.DATE.SET, { days, months, years });
29
+ }
30
+
31
+ this.emit(this.EVENTS.SCENARIO.NEXT, { module: this.name });
32
+ };
33
+
34
+ onDateSet = ({ days = 0, months = 0, years = 0 } = {}) => this.changeDate(days + 1, months, years);
35
+ onDateAdd = ({ days = 0, months = 0, years = 0 } = {}) => this.changeDate(this.state.days + days, this.state.months + months, this.state.years + years);
36
+
37
+ changeDate = (days = 0, months = 0, years = 0) => {
38
+ const w = new Date();
39
+ w.setDate(days);
40
+ w.setMonth(months);
41
+ w.setFullYear(years);
42
+
43
+ this.setState({ days: w.getDate(), months: w.getMonth(), years: w.getFullYear() });
44
+
45
+ this.emit(this.EVENTS.DATE.CHANGED);
46
+ };
47
+
48
+ getDefaultState = () => ({ days: 0, months: 0, years: 0 });
49
+ }
package/package.json ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "name": "@vnejs/plugins.feature.date",
3
+ "version": "0.0.1",
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
+ }