-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathHRef.js
More file actions
136 lines (119 loc) · 3.1 KB
/
HRef.js
File metadata and controls
136 lines (119 loc) · 3.1 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
//
// 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');
/**
* HRef wraps a string reference identifier and optional display name.
* @see {@link http://project-haystack.org/doc/TagModel#tagKinds|Project Haystack}
*
* @constructor
* @extends {HVal}
* @param {string} val
* @param {string} dis
*/
function HRef(val, dis) {
/** String identifier for reference */
this.val = val;
/** Display name for reference or null */
this.display = dis;
}
HRef.prototype = Object.create(HVal.prototype);
module.exports = HRef;
var HStr = require('./HStr');
/**
* Encode as "@id <dis>"
* @return {string}
*/
HRef.prototype.toZinc = function() {
var s = "@" + this.val;
if (typeof(this.display) !== 'undefined' && this.display !== null)
s += " " + HStr.toCode(this.display);
return s;
};
/**
* Encode as "r:id <dis>"
* @returns string
*/
HRef.prototype.toJSON = function() {
var s = "r:" + this.val;
if (typeof(this.display) !== 'undefined' && this.display !== null)
s += " " + HStr.parseCode(this.display);
return s;
};
/**
* Equals is based on val field only
* @param {HRef}
* @return {boolean}
*/
HRef.prototype.equals = function(that) {
return that instanceof HRef && this.val === that.val;
};
/**
* Return the val string
* @return {string}
*/
HRef.prototype.toString = function() {
return this.val;
};
/**
* Encode as "@id"
* @return {string}
*/
HRef.prototype.toCode = function() {
return "@" + this.val;
};
/**
* Return display string which is dis field if non-null, val field otherwise
* @return {string}
*/
HRef.prototype.dis = function() {
return typeof(this.display) === 'undefined' || this.display === null ? this.val : this.display;
};
/**
* Construct for string identifier and optional display
* @static
* @param {string} val
* @param {string} dis
*/
HRef.make = function(val, dis) {
if (typeof(val) === 'undefined' || val === null || !HRef.isId(val))
throw new Error("Invalid id val: \"" + val + "\"");
return new HRef(val, dis);
};
/**
* Return if the given string is a valid id for a reference
* @param {string} id
* @return {boolean}
*/
HRef.isId = function(id) {
if (id.length === 0) return false;
for (var i = 0; i < id.length; ++i) {
if (!HRef.isIdChar(HVal.cc(id.charAt(i))))
return false;
}
return true;
};
var idChars = [];
for (var i = 0; i < 127; i++) idChars[i] = false;
for (var i = HVal.cc('a'); i <= HVal.cc('z'); ++i) idChars[i] = true;
for (var i = HVal.cc('A'); i <= HVal.cc('Z'); ++i) idChars[i] = true;
for (var i = HVal.cc('0'); i <= HVal.cc('9'); ++i) idChars[i] = true;
idChars[HVal.cc('_')] = true;
idChars[HVal.cc(':')] = true;
idChars[HVal.cc('-')] = true;
idChars[HVal.cc('.')] = true;
idChars[HVal.cc('~')] = true;
/**
* Is the given character valid in the identifier part
* @param {int} ch
* @return {boolean}
*/
HRef.isIdChar = function(ch) {
return ch >= 0 && ch < idChars.length && idChars[ch];
};