-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathHDictBuilder.js
More file actions
131 lines (118 loc) · 3.38 KB
/
HDictBuilder.js
File metadata and controls
131 lines (118 loc) · 3.38 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
//
// 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
//
/**
* HDictBuilder is used to construct an immutable HDict instance.
* @see {@link http://project-haystack.org/doc/TagModel#tags|Project Haystack}
*
* @constructor
*/
function HDictBuilder() {
this.map = {};
}
module.exports = HDictBuilder;
var HBool = require('./HBool'),
HDict = require('./HDict'),
HMarker = require('./HMarker'),
HNum = require('./HNum'),
HStr = require('./HStr');
/**
* Return if size is zero
* @return {boolean}
*/
HDictBuilder.prototype.isEmpty = function() {
return this.size() === 0;
};
/**
* Return if the given tag is present
* @param {string} name
* @return {boolean}
*/
HDictBuilder.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}
*/
HDictBuilder.prototype.missing = function(name) {
var t = this.get(name, false);
return typeof(t) === 'undefined' || t === null;
};
/**
* Return number of tag name/value pairs
* @return {int}
*/
HDictBuilder.prototype.size = function() {
return Object.keys(this.map).length;
};
/**
* Get a tag by name. If not found and checked is false then
* return null, otherwise throw Error
* @param {string} name
* @param {boolean} checked
* @return {HVal}
*/
HDictBuilder.prototype.get = function(name, checked) {
var _checked = checked;
if (typeof(_checked) === 'undefined') _checked = false;
var val = this.map[name];
if (typeof(val) !== 'undefined' && val !== null)
return val;
if (!_checked) return null;
throw new Error("Unknown Name: " + name);
};
/**
* Convert current state to an immutable HDict instance
* @return {HDict}
*/
HDictBuilder.prototype.toDict = function() {
if (typeof(this.map) === 'undefined' || this.map === null || this.isEmpty())
return HDict.EMPTY;
var dict = new HDict.MapImpl(this.map);
this.map = null;
return dict;
};
/**
* Add to this and return this
* @param {HDict|string} name
* - HDict: Add all the name/value pairs in given HDict.
* - string: Add tag name and value
* @param {boolean|number|string|HVal} val - not required for HDict and HMarkers
* @param {string} unit - only used with numbers
* @returns {HDictBuilder}
*/
HDictBuilder.prototype.add = function(name, val, unit) {
if (typeof(val) === 'undefined') {
if (name instanceof HDict) {
for (var it = name.iterator(); it.hasNext();) {
var entry = it.next();
this.add(entry.getKey(), entry.getValue());
}
return this;
} else {
return this.add(name, HMarker.VAL);
}
} else {
if (!HDict.isTagName(name)) throw new Error("Invalid tag name: " + name);
// handle primitives
if (typeof(val) === 'number' || val instanceof Number)
return this.add(name, HNum.make(val, unit));
if (typeof(val) === 'string' || val instanceof String)
return this.add(name, HStr.make(val));
if (typeof(val) === 'boolean' || val instanceof Boolean)
return this.add(name, HBool.make(val));
if (typeof(this.map) === 'undefined' || this.map === null)
this.map = {};
this.map[name] = val;
return this;
}
};