-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeepProcessor.ts
More file actions
161 lines (139 loc) 路 3.66 KB
/
Copy pathDeepProcessor.ts
File metadata and controls
161 lines (139 loc) 路 3.66 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
export interface ProcessingRule {
name: string
condition: (data: unknown) => boolean
action: (data: unknown) => unknown
priority: number
}
export interface ProcessingContext {
rules: ProcessingRule[]
maxIterations: number
stopOnError: boolean
metadata: Record<string, unknown>
}
export type ProcessingResult = {
success: boolean
data: unknown
iterations: number
errors: string[]
appliedRules: string[]
}
export class DeepProcessor {
private context: ProcessingContext
private isProcessing: boolean = false
constructor(context: Partial<ProcessingContext> = {}) {
this.context = {
rules: [],
maxIterations: 10,
stopOnError: true,
metadata: {},
...context
}
}
addRule(rule: ProcessingRule): void {
this.context.rules.push(rule)
this.sortRulesByPriority()
}
removeRule(ruleName: string): boolean {
const index = this.context.rules.findIndex(rule => rule.name === ruleName)
if (index !== -1) {
this.context.rules.splice(index, 1)
return true
}
return false
}
process(data: unknown): ProcessingResult {
if (this.isProcessing) {
throw new Error('Processor is already running')
}
this.isProcessing = true
const result: ProcessingResult = {
success: true,
data,
iterations: 0,
errors: [],
appliedRules: []
}
try {
let currentData = data
let hasChanges = true
while (hasChanges && result.iterations < this.context.maxIterations) {
hasChanges = false
result.iterations++
for (const rule of this.context.rules) {
try {
if (rule.condition(currentData)) {
const newData = rule.action(currentData)
if (newData !== currentData) {
currentData = newData
hasChanges = true
result.appliedRules.push(rule.name)
}
}
} catch (error) {
const errorMessage = `Rule ${rule.name} failed: ${error}`
result.errors.push(errorMessage)
if (this.context.stopOnError) {
result.success = false
break
}
}
}
}
result.data = currentData
} catch (error) {
result.success = false
result.errors.push(`Processing failed: ${error}`)
} finally {
this.isProcessing = false
}
return result
}
processAsync(data: unknown): Promise<ProcessingResult> {
return new Promise((resolve) => {
setTimeout(() => {
resolve(this.process(data))
}, 0)
})
}
getRules(): ProcessingRule[] {
return [...this.context.rules]
}
getContext(): ProcessingContext {
return { ...this.context }
}
updateContext(updates: Partial<ProcessingContext>): void {
this.context = { ...this.context, ...updates }
if (updates.rules) {
this.sortRulesByPriority()
}
}
clearRules(): void {
this.context.rules = []
}
private sortRulesByPriority(): void {
this.context.rules.sort((a, b) => b.priority - a.priority)
}
}
export function createDeepProcessor(context?: Partial<ProcessingContext>): DeepProcessor {
return new DeepProcessor(context)
}
export const DEFAULT_RULES: ProcessingRule[] = [
{
name: 'null-check',
condition: (data) => data === null,
action: (data) => data,
priority: 100
},
{
name: 'string-trim',
condition: (data) => typeof data === 'string',
action: (data) => (data as string).trim(),
priority: 50
},
{
name: 'number-round',
condition: (data) => typeof data === 'number',
action: (data) => Math.round(data as number),
priority: 25
}
]