-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathHDict.js
More file actions
332 lines (297 loc) · 7.81 KB
/
HDict.js
File metadata and controls
332 lines (297 loc) · 7.81 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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
//
// Copyright (c) 2015, Shawn Jacobson
// Licensed under the Academic Free License version 3.0
//
// Ported from @see {@link https://bitbucket.org/brianfrank/haystack-java|Haystack Java Toolkit}
//
// History:
// 21 Mar 2015 Shawn Jacobson Creation
//
/**
* HDict is an immutable map of name/HVal pairs. Use HDictBuilder
* to construct a HDict instance.
* @see {@link http://project-haystack.org/doc/TagModel#tags|Project Haystack}
*
* @constructor
*/
function HDict() {}
module.exports = HDict;
var HMarker = require('./HMarker'),
HBool = require('./HBool'),
HNum = require('./HNum'),
HRef = require('./HRef'),
HStr = require('./HStr'),
HVal = require('./HVal');
/**
* Return if size is zero
* @return {boolean}
*/
HDict.prototype.isEmpty = function() {
return this.size() === 0;
};
/**
* Return if the given tag is present
* @param {string} name
* @return {boolean}
*/
HDict.prototype.has = function(name) {
var t = this.get(name, false);
return typeof(t) !== 'undefined' && t !== null;
};
/**
* Return if the given tag is not present
* @param {string} name
* @return {boolean}
*/
HDict.prototype.missing = function(name) {
var t = this.get(name, false);
return typeof(t) === 'undefined' || t === null;
};
/**
* Get the "id" tag as HRef.
* @return {HRef}
*/
HDict.prototype.id = function() {
return this.getRef("id");
};
/**
* Get display string for this entity:
* - dis tag
* - id tag
* @return {string}
*/
HDict.prototype.dis = function() {
var v = this.get("dis", false);
if (v instanceof HStr) return v.val;
v = this.get("id", false);
if (typeof(v) !== 'undefined' && v !== null) return v.dis();
return "????";
};
/**
* Return number of tag name/value pairs
* @abstract
* @return {int}
*/
HDict.prototype.size = function() {
throw new Error('must be implemented by subclass!');
};
/**
* Get a tag by name. If not found and checked if false then return null, otherwise throw Error
* @abstract
* @param {string} name
* @param {boolean} checked
* @return {HVa}
*/
HDict.prototype.get = function(name, checked) {
throw new Error('must be implemented by subclass!');
};
/**
* Create Map.Entry iteratator to walk each name/tag pair
* @abstract
* @return {Iterator}
*/
HDict.prototype.iterator = function() {
throw new Error('must be implemented by subclass!');
};
//////////////////////////////////////////////////////////////////////////
// Get Conveniences
//////////////////////////////////////////////////////////////////////////
/**
* Get tag as HBool or raise Error.
* @param {string} name
* @return {HBool}
*/
HDict.prototype.getBool = function(name) {
var v = this.get(name);
if (!(v instanceof HBool)) throw Error("ClassCastExcetion: " + name);
return v.val;
};
/**
* Get tag as HStr or raise Error.
* @param {string} name
* @return {HStr}
*/
HDict.prototype.getStr = function(name) {
var v = this.get(name);
if (!(v instanceof HStr)) throw Error("ClassCastExcetion: " + name);
return v.val;
};
/**
* Get tag as HRef or raise Error.
* @param {string} name
* @return {HRef}
*/
HDict.prototype.getRef = function(name) {
var v = this.get(name);
if (!(v instanceof HRef)) throw Error("ClassCastExcetion: " + name);
return v;
};
/**
* Get tag as HNum or raise Error.
* @param {string} name
* @return {int}
*/
HDict.prototype.getInt = function(name) {
var v = this.get(name);
if (!(v instanceof HNum)) throw Error("ClassCastExcetion: " + name);
return v.val;
};
/**
* Get tag as HNum or raise Error.
* @param {string} name
* @return {float}
*/
HDict.prototype.getDouble = function(name) {
var v = this.get(name);
if (!(v instanceof HNum)) throw Error("ClassCastExcetion: " + name);
return v.val;
};
//////////////////////////////////////////////////////////////////////////
// Identity
//////////////////////////////////////////////////////////////////////////
/**
* String format is always "toZinc"
* @return {string}
*/
HDict.prototype.toString = function() {
return this.toZinc();
};
/**
* Equality is tags
* @param {HDict} that
* @return {boolean}
*/
HDict.prototype.equals = function(that) {
if (!(that instanceof HDict)) return false;
if (this.size() !== that.size()) return false;
for (var it = this.iterator(); it.hasNext();) {
var entry = it.next();
var name = entry.getKey();
var val = entry.getValue();
var tval = that.get(name, false);
var neq;
try {
neq = !val.equals(tval);
} catch (err) {
neq = (val !== tval);
}
if (neq) return false;
}
return true;
};
//////////////////////////////////////////////////////////////////////////
// Encoding
//////////////////////////////////////////////////////////////////////////
function cc(c) {
return HVal.cc(c);
}
var tagChars = [];
for (var i = cc("a"); i <= cc("z"); ++i) tagChars[i] = true;
for (var i = cc("A"); i <= cc("Z"); ++i) tagChars[i] = true;
for (var i = cc("0"); i <= cc("9"); ++i) tagChars[i] = true;
tagChars[cc("_")] = true;
/**
* Return if the given string is a legal tag name. The
* first char must be ASCII lower case letter. Rest of
* chars must be ASCII letter, digit, or underbar.
* @param {string} n
* @return {boolean}
*/
HDict.isTagName = function(n) {
if (n.length === 0) return false;
var first = n.charCodeAt(0);
if (first < cc("a") || first > cc("z")) return false;
for (var i = 0; i < n.length; ++i) {
var c = n.charCodeAt(i);
if (c >= 128 || !tagChars[c]) return false;
}
return true;
};
/**
* Encode value to zinc format
* @return {string}
*/
HDict.prototype.toZinc = function() {
var s = "";
var first = true;
for (var it = this.iterator(); it.hasNext();) {
var entry = it.next();
var name = entry.getKey();
var val = entry.getValue();
if (first) first = false;
else s += " ";
s += name;
if (val !== HMarker.VAL)
s += ":" + val.toZinc();
}
return s;
};
//////////////////////////////////////////////////////////////////////////
// MapImpl
//////////////////////////////////////////////////////////////////////////
HDict.MapImpl = function(map) {
this.map = map;
this.size = function() {
return Object.keys(this.map).length;
};
this.get = function(name, checked) {
var val = this.map[name];
if (typeof(val) !== 'undefined' && val !== null) return val;
if (!checked) return null;
throw new Error("Unknown name: " + name);
};
this.iterator = function() {
var index = 0;
var map = this.map;
var keys = Object.keys(map);
keys.sort();
var length = keys.length;
return {
next: function() {
var elem;
if (!this.hasNext()) return null;
elem = keys[index];
index++;
return new HDict.MapEntry(elem, map[elem]);
},
hasNext: function() {
return index < length;
}
};
};
};
HDict.MapImpl.prototype = Object.create(HDict.prototype);
//////////////////////////////////////////////////////////////////////////
// MapEntry
//////////////////////////////////////////////////////////////////////////
/**
* Create Map.Entry for given name/value tag pair
* @param {string} key
* @param {HVal} val
* @return {HDict.MapEntry}
*/
HDict.prototype.toEntry = function(key, val) {
return new HDict.MapEntry(key, val);
};
/**
* @constructor
* @param {string} key
* @param {object} val
*/
HDict.MapEntry = function(key, val) {
this.key = key;
this.val = val;
this.getKey = function() {
return this.key;
};
this.getValue = function() {
return this.val;
};
this.equals = function(that) {
return (typeof(this.key) === 'undefined' || this.key === null ?
typeof(that.key) === 'undefined' || this.key === null : this.key === that.key) &&
(typeof(this.val) === 'undefined' || this.val === null ?
typeof(that.val) === 'undefined' || that.val === null : this.val === that.val);
};
};
HDict.EMPTY = new HDict.MapImpl({});