-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathHBool.js
More file actions
91 lines (80 loc) · 1.99 KB
/
HBool.js
File metadata and controls
91 lines (80 loc) · 1.99 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
//
// 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
//
var HVal = require('./HVal');
/**
* HBool defines singletons for true/false tag values.
* @see {@link http://project-haystack.org/doc/TagModel#tagKinds|Project Haystack}
*
* @constructor
* @private
* @extends {HVal}
* @param {boolean} val - Boolean value
*/
function HBool(val) {
// ensure singleton usage
if (val && arguments.callee._trueSingletonInstance) return arguments.callee._trueSingletonInstance;
if (!val && arguments.callee._falseSingletonInstance) return arguments.callee._falseSingletonInstance;
if (val) arguments.callee._trueSingletonInstance = this;
else arguments.callee._falseSingletonInstance = this;
this.val = val;
}
HBool.prototype = Object.create(HVal.prototype);
module.exports = HBool;
/**
* Construct from boolean value
* @param {boolean} val
* @return {HBool}
*/
HBool.make = function(val) {
if (!HVal.typeis(val, 'boolean', Boolean))
throw new Error("Invalid boolean val: \"" + val + "\"");
return val ? HBool.TRUE : HBool.FALSE;
};
/**
* Encode as T/F
* @return {string}
*/
HBool.prototype.toZinc = function() {
return this.val ? "T" : "F";
};
/**
* Return val as string
* @returns string
*/
HBool.prototype.toJSON = function() {
return this.toString();
};
/**
* Equals is based on reference
* @param {HBool} that - object to be compared to
* @return {boolean}
*/
HBool.prototype.equals = function(that) {
return that instanceof HBool && this === that;
};
/**
* Encode as "true" or "false"
* @return {string}
*/
HBool.prototype.toString = function() {
return this.val ? "true" : "false";
};
/**
* Singleton value for true
* @static
* @return {HBool}
*/
HBool.TRUE = new HBool(true);
/**
* Singleton value for false
* @static
* @return {HBool}
*/
HBool.FALSE = new HBool(false);