forked from NatLabRockies/nodehaystack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHUri.js
More file actions
97 lines (87 loc) · 1.94 KB
/
HUri.js
File metadata and controls
97 lines (87 loc) · 1.94 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
//
// 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');
/**
* HUri models a URI as a string.
* @see {@link http://project-haystack.org/doc/TagModel#tagKinds|Project Haystack}
*
* @constructor
* @extends {HVal}
* @param {string} val
*/
function HUri(val) {
// ensure singleton usage
if (val==="" && arguments.callee._emptySingletonInstance) return arguments.callee._emptySingletonInstance;
if (val==="") arguments.callee._emptySingletonInstance = this;
this.val = val;
}
HUri.prototype = Object.create(HVal.prototype);
module.exports = HUri;
/**
* Equals is based on string value
* @param {HUri}
* @return {boolean}
*/
HUri.prototype.equals = function(that) {
return that instanceof HUri && this.val === that.val;
};
/**
* String format is for human consumption only
* @return {string}
*/
HUri.prototype.toString = function() {
return this.val;
};
/**
* Encode using "`" back ticks
* @return {string}
*/
HUri.prototype.toZinc = function() {
var s = "`";
s += parse(this);
s += "`";
return s;
};
/**
* Encode as "h:hh:mm:ss.FFF"
* @return {string}
*/
HUri.prototype.toJSON = function() {
return "u:" + parse(this);
};
function parse(self) {
var s = "";
for (var i = 0; i < self.val.length; ++i) {
var c = self.val.charAt(i);
if (HVal.cc(c) < HVal.cc(" "))
throw new Error("Invalid URI char '" + self.val + "', char='" + c + "'");
if (c === "`") s += "\\";
s += c;
}
return s;
}
/**
* Singleton value for empty URI
* @static
* @return {HUri}
*/
HUri.EMPTY = new HUri("");
/**
* Construct from string value
* @static
* @param {string} val
* @return {HUri}
*/
HUri.make = function(val) {
if (val.length === 0) {
return HUri.EMPTY;
}
return new HUri(val);
};