forked from NatLabRockies/nodehaystack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHStr.js
More file actions
144 lines (130 loc) · 2.99 KB
/
HStr.js
File metadata and controls
144 lines (130 loc) · 2.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
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
//
// 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');
/**
* HStr wraps a java.lang.String as a tag value.
* @see {@link http://project-haystack.org/doc/TagModel#tagKinds|Project Haystack}
*
* @constructor
* @extends {HVal}
* @param {string{ val
*/
function HStr(val) {
// ensure singleton usage
if (val==="" && arguments.callee._emptySingletonInstance) return arguments.callee._emptySingletonInstance;
if (val==="") arguments.callee._emptySingletonInstance = this;
this.val = val;
}
HStr.prototype = Object.create(HVal.prototype);
module.exports = HStr;
/**
* Encode using double quotes and back slash escapes
* @return {string}
*/
HStr.prototype.toZinc = function() {
return HStr.toCode(this.val);
};
/**
* Encode using "n:" with back slash escapes
* @return {string}
*/
HStr.prototype.toJSON = function() {
return "s:" + HStr.parseCode(this.val);
};
/**
* Equals is based on reference
* @return {boolean}
*/
HStr.prototype.equals = function(that) {
return that instanceof HStr && this.val === that.val;
};
/**
* String format is for human consumption only
* @return {string}
*/
HStr.prototype.toString = function() {
return this.val;
};
/**
* Singleton value for empty string ""
* @static
* @return {HStr}
*/
HStr.EMPTY = new HStr("");
/**
* Construct from String value
* @static
* @param {string} val
* @return {HStr}
*/
HStr.make = function(val) {
if (typeof(val) === 'undefined' || val === null) return val;
if (val.length === 0) return HStr.EMPTY;
return new HStr(val);
};
/**
* Encode using double quotes and back slash escapes
* @param {string} val
* @return {string}
*/
HStr.toCode = function(val) {
var s = '"';
s += HStr.parseCode(val);
s += '"';
return s;
};
HStr.parseCode = function(val) {
var s = "";
for (var i = 0; i < val.length; ++i) {
var c = HVal.cc(val.charAt(i));
if (c < HVal.cc(' ') || c === HVal.cc('"') || c === HVal.cc('\\')) {
s += '\\';
switch (c) {
case HVal.cc('\n'):
s += 'n';
break;
case HVal.cc('\r'):
s += 'r';
break;
case HVal.cc('\t'):
s += 't';
break;
case HVal.cc('"'):
s += '"';
break;
case HVal.cc('\\'):
s += '\\';
break;
default:
s += 'u00';
if (c <= 0xf) s += '0';
s += c.toString(16);
}
} else {
s += String.fromCharCode(c);
}
}
return s;
}
/**
* Custom split routine to maintain compatibility with Java Haystack
* @param {string} str
* @param {string} sep
* @param {boolean} trim
* @return {string[]}
*/
HStr.split = function(str, sep, trim) {
var s = str.split(sep);
if (trim) {
for (var i = 0; i < s.length; i++)
s[i] = s[i].trim();
}
return s;
};