-
Notifications
You must be signed in to change notification settings - Fork 5
/
HVal.ts
269 lines (239 loc) · 5.99 KB
/
HVal.ts
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
* Copyright (c) 2020, J2 Innovations. All Rights Reserved
*/
import { Kind } from './Kind'
import { HaysonVal } from './hayson'
import { HFilter } from '../filter/HFilter'
import { Node } from '../filter/Node'
import { HList } from './HList'
import { HGrid } from './HGrid'
import { HDict } from './HDict'
import { EvalContext } from '../filter/EvalContext'
import { JsonV3Val } from './jsonv3'
/**
* Default error message when a zinc value is not supported in the filter.
*/
export const NOT_SUPPORTED_IN_FILTER_MSG = 'Not supported in haystack filter'
/**
* Default error message when attempting to change immutable values.
*/
export const CANNOT_CHANGE_READONLY_VALUE = 'Cannot change readonly value'
/**
* Return true if the value is a haystack value.
*
* @param val The haystack value.
* @returns True if the value is an haystack value.
*/
export function isHVal(val: unknown): val is HVal {
return !!(val && typeof (val as HVal).isKind === 'function')
}
/**
* Return true if the kind matches.
*
* @param value The value to test.
* @param kind The kind to test.
* @returns True if the kind matches.
*/
export function valueIsKind<Value extends OptionalHVal>(
value: unknown,
kind: Kind
): value is Value {
return isHVal(value) && value.getKind() === kind
}
/**
* Dump the value to the local console output.
*
* This method acts as a default implementation for most haystack value types.
*
* @param message An optional message to display before the value.
* @returns The value instance.
*/
export function valueInspect<Value extends HVal>(
value: Value,
message?: string
): Value {
if (message) {
console.log(String(message))
}
console.log(String(value))
return value
}
/**
* A value comparison that handles null or undefined values.
*
* @param val0 The first value to compare.
* @param val1 The second value to compare.
*/
export function valueEquals(
val0: HVal | undefined | null,
val1: HVal | undefined | null
): boolean {
if (!val0 || !val1) {
return val0 === val1
} else {
return val0.equals(val1)
}
}
/**
* Return true if the haystack filter matches the value.
*
* This method acts as a default implementation for most haystack value types.
*
* @param value The haystack value to test on.
* @param filter The haystack filter or node.
* @param cx Optional haystack filter evaluation context.
* @returns True if there's a match.
*/
export function valueMatches(
value: HVal,
filter: string | Node,
cx?: Partial<EvalContext>
): boolean {
return new HFilter(filter).eval({
dict: HDict.make({
item: value,
it: value,
}),
namespace: cx?.namespace,
resolve: cx?.resolve,
})
}
/**
* The Zinc symbol for null.
*/
export const ZINC_NULL = 'N'
/**
* Returns a Zinc encoded value.
*
* @param val The value to encode.
* @returns The Zinc encoded value.
*/
export function valueToZinc(val: HVal | null): string {
return val?.toZinc() ?? ZINC_NULL
}
/**
* The Axon symbol for null
*/
export const AXON_NULL = 'null'
/**
* Returns an Axon encoded value.
*
* @param val The value to encode.
* @returns The Axon encoded value.
*/
export function valueToAxon(val: HVal | null): string {
return val?.toAxon() ?? AXON_NULL
}
/**
* An HVal or null type.
*/
export type OptionalHVal<Value extends HVal | null = HVal | null> = Value | null
/**
* The interface for a haystack value.
*
* Each haystack encoded value should declare a static factory 'make' method
* used for creating values. All constructors should be kept private (or protected).
*/
export interface HVal {
/**
* @return The value's kind.
*/
getKind(): Kind
/**
* Compares the value's kind.
*
* @param kind The kind to compare against.
* @returns True if the kind matches.
*/
isKind(kind: Kind): boolean
/**
* Value equality check.
*
* @param value The value used to check for equality.
* @returns True if the value is the same.
*/
equals(value: unknown): boolean
/**
* Compares two numbers.
*
* @param value The value to compare against.
* @returns The sort order as negative, 0, or positive
*/
compareTo(value: unknown): number
/**
* Encodes to an encoded zinc value that can be used
* in a haystack filter string.
*
* The encoding for a haystack filter is mostly zinc but contains
* some exceptions.
*
* @returns The encoded value that can be used in a haystack filter.
*/
toFilter(): string
/**
* Encodes to an encoding zinc value.
*
* @param nested An optional flag used to indiciate whether the
* value being encoded is nested.
* @returns The encoded zinc string.
*/
toZinc(nested?: boolean): string
/**
* @returns A JSON reprentation of the object.
*/
toJSON(): HaysonVal
/**
* Encode the value using the older Haystack version 3 JSON encoding.
* Please note, this has been supplanted by the newer `Hayson` JSON
* format (use `toJSON`).
*
* https://project-haystack.org/doc/docHaystack/Json
*
* @returns A JSON v3 representation of the object.
*/
toJSONv3(): JsonV3Val
/**
* @returns A string representation of the value.
*/
toString(): string
/**
* @returns An Axon encoded representation of the value.
*/
toAxon(): string
/**
* Returns true if the haystack filter matches the value.
*
* @param filter The filter to test.
* @param cx Optional haystack filter evaluation context.
* @returns True if the filter matches ok.
*/
matches(filter: string | Node, cx?: Partial<EvalContext>): boolean
/**
* Dump the value to the local console output.
*
* @param message An optional message to display before the value.
* @returns The value instance.
*/
inspect(message?: string): this
/**
* Returns a new copy of the underlying value.
*
* If the value is immutable, the value will simply return itself.
* If mutable, the value will return a brand new copy of the value.
*
* @returns A value.
*/
newCopy(): HVal
/**
* @returns The value as a grid.
*/
toGrid(): HGrid
/**
* @returns The value as a list.
*/
toList(): HList
/**
* @returns The value as a dict.
*/
toDict(): HDict
}