-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
137 lines (132 loc) · 4.65 KB
/
index.js
File metadata and controls
137 lines (132 loc) · 4.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/**
* Case switcher
**/
'use strict';
const assert = require('assert');
const debug = require('debug')('switch-case.Switcher');
const extend = require('extend');
const BREAK = 'BREAK';
class Switcher {
static get BREAK() { return BREAK; }
constructor(proxy) {
debug('construct');
const defaultProxy = extend(true, {}, DEFAULT_PROXY);
this.proxy = extend(true, defaultProxy, proxy);
this._caseList = [];
this._defaultCaseList = [];
this._indexTable = {};
}
async dispatch(targetCondition) {
return await this.switch(targetCondition);
}
async switch(targetCondition) {
debug('switch');
let caseList = this._searchIndexCaseList(targetCondition)
.map(({ condition, result }) => { return { condition, result, context: {}}; })
.filter(({ condition, context }) => this._match(targetCondition, condition, context))
.map(({ result, context }) => { return { result, context }; });
caseList = caseList.length === 0 ?
this._defaultCaseList.map(result => { return { result, context: {}}; }) : caseList;
return await this._execute(targetCondition, caseList);
}
case(...conditions) {
const result = conditions.pop();
debug('set case');
assert(conditions.length > 0, 'No condition for Switcher.case');
let condition = null;
for (condition of conditions) {
this._addCase(condition, result);
}
}
default(result) {
debug('set default case');
this._validateResult(result);
this._defaultCaseList.push(result);
}
_addCase(condition, result) {
debug('add case');
this._validateCondition(condition);
this._validateResult(result);
this._caseList.push({ condition, result });
this._createIndex(condition, this._caseList.length - 1);
}
_validateCondition(condition) {
debug('validate condition');
if (this.proxy.validateCondition instanceof Function &&
!this.proxy.validateCondition(condition)) {
throw new Error('Invalid case condition');
}
}
_validateResult(result) {
debug('validate result');
if (this.proxy.validateResult instanceof Function &&
!this.proxy.validateResult(result)) {
throw new Error('Invalid case condition');
}
}
_createIndex(condition, index) {
debug('create index');
if (!(this.proxy.createIndex instanceof Function)) {
return;
}
this.proxy.createIndex(condition, index, this._indexTable);
}
_searchIndexCaseList(targetCondition) {
debug('search index');
if (!(this.proxy.searchIndex instanceof Function)) {
return this._caseList;
}
const caseList = new Set();
let indexList = this.proxy.searchIndex(targetCondition, this._indexTable);
if (Array.isArray(indexList)) {
indexList.forEach(index => caseList.add(this._caseList[index]));
return [...caseList.values()];
}
return this._caseList;
}
_match(targetCondition, condition, context) {
debug('testing match');
let matched = false;
if (!(this.proxy.match instanceof Function)) {
matched = targetCondition === condition;
}
else {
matched = this.proxy.match(targetCondition, condition, context);
}
debug(matched ? 'matched' : 'not matched');
return matched;
}
async _execute(targetCondition, caseList, index = 0) {
if (index >= caseList.length) {
return;
}
debug('executing');
let result = null;
let caseResult = caseList[index].result;
let caseContext = caseList[index].context;
if (!(this.proxy.execute instanceof Function)) {
assert(caseResult instanceof Function, 'Case action should be a function');
result = await caseResult(targetCondition);
}
else {
result = await this.proxy.execute(caseResult, targetCondition, caseContext);
}
if (result === Switcher.BREAK) {
return;
}
return (await this._execute(targetCondition, caseList, index + 1)) || result;
}
}
const DEFAULT_PROXY = {
match(targetCondition, condition) {
return targetCondition === condition;
},
createIndex(condition, index, indexTable) {
indexTable[condition] = indexTable[condition] || [];
indexTable[condition].push(index);
},
searchIndex(condition, indexTable) {
return indexTable[condition];
}
};
module.exports = Switcher;