forked from NatLabRockies/nodehaystack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHBin.js
More file actions
77 lines (69 loc) · 1.62 KB
/
HBin.js
File metadata and controls
77 lines (69 loc) · 1.62 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
//
// 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');
/**
* HBin models a binary file with a MIME type.
* @see {@link http://project-haystack.org/doc/TagModel#tagKinds|Project Haystack}
*
* @constructor
* @private
* @extends {HVal}
* @param {string} mime - MIME type for binary file
*/
function HBin(mime) {
this.mime = mime;
}
HBin.prototype = Object.create(HVal.prototype);
module.exports = HBin;
/**
* Construct for MIME type
* @param {string} mime
* @return {HBin}
*/
HBin.make = function(mime) {
if (typeof(mime) === 'undefined' || mime.length === 0 || mime.indexOf('/') < 0)
throw new Error("Invalid mime val: \"" + mime + "\"");
return new HBin(mime);
};
/**
* Encode as "Bin(<mime>)"
* @returns string
*/
HBin.prototype.toZinc = function() {
var s = "Bin(";
s += parse(this.mime);
s += ")";
return s;
};
/**
* Encode as "b:<mime>"
* @returns string
*/
HBin.prototype.toJSON = function() {
return "b:" + parse(this.mime);
};
function parse(mime) {
var s = "";
for (var i = 0; i < mime.length; ++i) {
var c = mime.charAt(i);
if (HVal.cc(c) > 127 || c === ')')
throw new Error("Invalid mime, char='" + c + "'");
s += c;
}
return s;
}
/**
* Equals is based on mime field
* @param {HBin} that - object to be compared to
* @returns boolean
*/
HBin.prototype.equals = function(that) {
return that instanceof HBin && this.mime === that.mime;
};