@lhncbc/ucum-lhc 4.2.0 → 5.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +50 -12
- package/browser-dist/ucum-lhc.js +841 -1325
- package/data/ucumDefs.min.json +1 -1
- package/package.json +1 -1
- package/source/ucumLhcUtils.js +100 -66
- package/source/ucumXmlDocument.js +5 -2
- package/source/unit.js +2 -1
- package/source/unitString.js +159 -140
- package/source-cjs/config.js +1 -12
- package/source-cjs/config.js.map +1 -1
- package/source-cjs/dimension.js +20 -63
- package/source-cjs/dimension.js.map +1 -1
- package/source-cjs/jsonArrayPack.js +7 -25
- package/source-cjs/jsonArrayPack.js.map +1 -1
- package/source-cjs/prefix.js +12 -26
- package/source-cjs/prefix.js.map +1 -1
- package/source-cjs/prefixTables.js +10 -24
- package/source-cjs/prefixTables.js.map +1 -1
- package/source-cjs/ucumFunctions.js +35 -32
- package/source-cjs/ucumFunctions.js.map +1 -1
- package/source-cjs/ucumInternalUtils.js +5 -13
- package/source-cjs/ucumInternalUtils.js.map +1 -1
- package/source-cjs/ucumJsonDefs.js +1 -16
- package/source-cjs/ucumJsonDefs.js.map +1 -1
- package/source-cjs/ucumLhcUtils.js +117 -138
- package/source-cjs/ucumLhcUtils.js.map +1 -1
- package/source-cjs/ucumPkg.js +1 -6
- package/source-cjs/ucumPkg.js.map +1 -1
- package/source-cjs/ucumXmlDocument.js +162 -184
- package/source-cjs/ucumXmlDocument.js.map +1 -1
- package/source-cjs/unit.js +97 -181
- package/source-cjs/unit.js.map +1 -1
- package/source-cjs/unitString.js +537 -618
- package/source-cjs/unitString.js.map +1 -1
- package/source-cjs/unitTables.js +33 -139
- package/source-cjs/unitTables.js.map +1 -1
package/source-cjs/config.js
CHANGED
|
@@ -4,7 +4,6 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.Ucum = void 0;
|
|
7
|
-
|
|
8
7
|
/*
|
|
9
8
|
* This defines the namespace for the UCUM classes and provides
|
|
10
9
|
* a place for the definition of global variables and constants.
|
|
@@ -12,6 +11,7 @@ exports.Ucum = void 0;
|
|
|
12
11
|
* The javascript for this UCUM implementation uses syntax as
|
|
13
12
|
* defined by the ECMAScript 6 standard
|
|
14
13
|
*/
|
|
14
|
+
|
|
15
15
|
var Ucum = {
|
|
16
16
|
/**
|
|
17
17
|
* Flag indicating whether or not we're using case sensitive labels
|
|
@@ -28,13 +28,11 @@ var Ucum = {
|
|
|
28
28
|
* set from that.
|
|
29
29
|
*/
|
|
30
30
|
dimLen_: 7,
|
|
31
|
-
|
|
32
31
|
/**
|
|
33
32
|
* The characters used as valid operators in a UCUM unit expression,
|
|
34
33
|
* where '.' is for multiplication and '/' is for division.
|
|
35
34
|
*/
|
|
36
35
|
validOps_: ['.', '/'],
|
|
37
|
-
|
|
38
36
|
/**
|
|
39
37
|
* The string used to separate a unit code and unit name when they
|
|
40
38
|
* are displayed together
|
|
@@ -45,48 +43,41 @@ var Ucum = {
|
|
|
45
43
|
valMsgEnd_: '?',
|
|
46
44
|
cnvMsgStart_: 'We assumed you meant ',
|
|
47
45
|
cnvMsgEnd_: '.',
|
|
48
|
-
|
|
49
46
|
/**
|
|
50
47
|
* Default opening string used to emphasize portions of error messages.
|
|
51
48
|
* Used when NOT displaying messages on a web site, i.e., for output
|
|
52
49
|
* from the library methods or to a file.
|
|
53
50
|
*/
|
|
54
51
|
openEmph_: ' ->',
|
|
55
|
-
|
|
56
52
|
/**
|
|
57
53
|
* Default closing string used to emphasize portions of error messages.
|
|
58
54
|
* Used when NOT displaying messages on a web site, i.e., for output
|
|
59
55
|
* from the library methods or to a file.
|
|
60
56
|
*/
|
|
61
57
|
closeEmph_: '<- ',
|
|
62
|
-
|
|
63
58
|
/**
|
|
64
59
|
* Opening HTML used to emphasize portions of error messages. Used when
|
|
65
60
|
* displaying messages on a web site; should be blank when output is
|
|
66
61
|
* to a file.
|
|
67
62
|
*/
|
|
68
63
|
openEmphHTML_: ' <span class="emphSpan">',
|
|
69
|
-
|
|
70
64
|
/**
|
|
71
65
|
* Closing HTML used to emphasize portions of error messages. Used when
|
|
72
66
|
* displaying messages on a web site; should be blank when output is
|
|
73
67
|
* to a file.
|
|
74
68
|
*/
|
|
75
69
|
closeEmphHTML_: '</span> ',
|
|
76
|
-
|
|
77
70
|
/**
|
|
78
71
|
* Message that is displayed when annotations are included in a unit
|
|
79
72
|
* string, to let the user know how they are interpreted.
|
|
80
73
|
*/
|
|
81
74
|
bracesMsg_: 'FYI - annotations (text in curly braces {}) are ignored, ' + 'except that an annotation without a leading symbol implies ' + 'the default unit 1 (the unity).',
|
|
82
|
-
|
|
83
75
|
/**
|
|
84
76
|
* Message that is displayed or returned when a conversion is requested
|
|
85
77
|
* for two units where (only) a mass<->moles conversion is appropriate
|
|
86
78
|
* but no molecular weight was specified.
|
|
87
79
|
*/
|
|
88
80
|
needMoleWeightMsg_: 'Did you wish to convert between mass and moles? The ' + 'molecular weight of the substance represented by the ' + 'units is required to perform the conversion.',
|
|
89
|
-
|
|
90
81
|
/**
|
|
91
82
|
* Hash that matches unit column names to names used in the csv file
|
|
92
83
|
* that is submitted to the data updater.
|
|
@@ -100,12 +91,10 @@ var Ucum = {
|
|
|
100
91
|
'category': 'category_',
|
|
101
92
|
'Guidance': 'guidance_'
|
|
102
93
|
},
|
|
103
|
-
|
|
104
94
|
/**
|
|
105
95
|
* Name of the column in the csv file that serves as the key
|
|
106
96
|
*/
|
|
107
97
|
inputKey_: 'case-sensitive code',
|
|
108
|
-
|
|
109
98
|
/**
|
|
110
99
|
* Special codes that contain operators within brackets. The operator
|
|
111
100
|
* within these codes causes them to parse incorrectly if they are preceded
|
package/source-cjs/config.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"file":"config.js","names":["Ucum","dimLen_","validOps_","codeSep_","valMsgStart_","valMsgEnd_","cnvMsgStart_","cnvMsgEnd_","openEmph_","closeEmph_","openEmphHTML_","closeEmphHTML_","bracesMsg_","needMoleWeightMsg_","csvCols_","inputKey_","specUnits_","exports"],"sources":["../source/config.js"],"sourcesContent":["/*\n * This defines the namespace for the UCUM classes and provides\n * a place for the definition of global variables and constants.\n *\n * The javascript for this UCUM implementation uses syntax as\n * defined by the ECMAScript 6 standard\n */\n\nexport var Ucum = {\n\n /**\n * Flag indicating whether or not we're using case sensitive labels\n * I don't think we need this. I think we're just going with\n * case sensitive, per Clem. Gunther's code has this flag, but I\n * am removing it, at least for now. lm, 6/2016\n */\n //caseSensitive_: true ,\n\n /**\n * The number of elements in a Dimension array. Currently this\n * is set as a configuration variable, but when we get to the point\n * of loading the unit definitions from a file, this value will be\n * set from that.\n */\n dimLen_: 7,\n\n\n /**\n * The characters used as valid operators in a UCUM unit expression,\n * where '.' is for multiplication and '/' is for division.\n */\n validOps_: ['.', '/'],\n\n\n /**\n * The string used to separate a unit code and unit name when they\n * are displayed together\n */\n codeSep_ : ': ',\n\n // Message text variations for validation methods and conversion methods\n valMsgStart_ : 'Did you mean ',\n valMsgEnd_ : '?' ,\n cnvMsgStart_ : 'We assumed you meant ',\n cnvMsgEnd_ : '.',\n\n\n/**\n * Default opening string used to emphasize portions of error messages.\n * Used when NOT displaying messages on a web site, i.e., for output\n * from the library methods or to a file.\n */\n openEmph_ : ' ->',\n\n /**\n * Default closing string used to emphasize portions of error messages.\n * Used when NOT displaying messages on a web site, i.e., for output\n * from the library methods or to a file.\n */\n closeEmph_ : '<- ' ,\n\n /**\n * Opening HTML used to emphasize portions of error messages. Used when\n * displaying messages on a web site; should be blank when output is\n * to a file.\n */\n openEmphHTML_ : ' <span class=\"emphSpan\">',\n\n /**\n * Closing HTML used to emphasize portions of error messages. Used when\n * displaying messages on a web site; should be blank when output is\n * to a file.\n */\n closeEmphHTML_ : '</span> ' ,\n\n /**\n * Message that is displayed when annotations are included in a unit\n * string, to let the user know how they are interpreted.\n */\n bracesMsg_ : 'FYI - annotations (text in curly braces {}) are ignored, ' +\n 'except that an annotation without a leading symbol implies ' +\n 'the default unit 1 (the unity).',\n\n /**\n * Message that is displayed or returned when a conversion is requested\n * for two units where (only) a mass<->moles conversion is appropriate\n * but no molecular weight was specified.\n */\n needMoleWeightMsg_ : 'Did you wish to convert between mass and moles? The ' +\n 'molecular weight of the substance represented by the ' +\n 'units is required to perform the conversion.',\n\n /**\n * Hash that matches unit column names to names used in the csv file\n * that is submitted to the data updater.\n */\n csvCols_ : {\n 'case-sensitive code' : 'csCode_',\n 'LOINC property' : 'loincProperty_',\n 'name (display)' : 'name_',\n 'synonyms' : 'synonyms_',\n 'source' : 'source_',\n 'category' : 'category_',\n 'Guidance' : 'guidance_'\n } ,\n\n /**\n * Name of the column in the csv file that serves as the key\n */\n inputKey_ : 'case-sensitive code' ,\n\n /**\n * Special codes that contain operators within brackets. The operator\n * within these codes causes them to parse incorrectly if they are preceded\n * by a prefix, because the parsing algorithm splits them up on the operator.\n * So we use this object to identify them and substitute placeholders to\n * avoid that.\n */\n specUnits_ : { 'B[10.nV]' : 'specialUnitOne',\n '[m/s2/Hz^(1/2)]' : 'specialUnitTwo'}\n} ;\n\n\n"],"mappings":";;;;;;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEO,IAAIA,IAAI,GAAG;EAEhB;AACF;AACA;AACA;AACA;AACA;EACE;;EAEA;AACF;AACA;AACA;AACA;AACA;EACEC,OAAO,EAAE,CAAC;EAGV;AACF;AACA;AACA;EACEC,SAAS,EAAE,CAAC,GAAG,EAAE,GAAG,CAAC;EAGrB;AACF;AACA;AACA;EACEC,QAAQ,EAAG,IAAI;EAEf;EACAC,YAAY,EAAG,eAAe;EAC9BC,UAAU,EAAG,GAAG;EAChBC,YAAY,EAAG,uBAAuB;EACtCC,UAAU,EAAG,GAAG;EAGlB;AACA;AACA;AACA;AACA;EACEC,SAAS,EAAG,KAAK;EAEjB;AACF;AACA;AACA;AACA;EACEC,UAAU,EAAG,KAAK;EAElB;AACF;AACA;AACA;AACA;EACEC,aAAa,EAAG,0BAA0B;EAE1C;AACF;AACA;AACA;AACA;EACEC,cAAc,EAAG,UAAU;EAE3B;AACF;AACA;AACA;EACEC,UAAU,EAAG,2DAA2D,GAC3D,6DAA6D,GAC7D,iCAAiC;EAE9C;AACF;AACA;AACA;AACA;EACEC,kBAAkB,EAAG,uDAAuD,GACvD,uDAAuD,GACvD,8CAA8C;EAEnE;AACF;AACA;AACA;EACEC,QAAQ,EAAG;IACT,qBAAqB,EAAG,SAAS;IACjC,gBAAgB,EAAG,gBAAgB;IACnC,gBAAgB,EAAG,OAAO;IAC1B,UAAU,EAAG,WAAW;IACxB,QAAQ,EAAG,SAAS;IACpB,UAAU,EAAG,WAAW;IACxB,UAAU,EAAG;EACf,CAAC;EAED;AACF;AACA;EACEC,SAAS,EAAG,qBAAqB;EAEjC;AACF;AACA;AACA;AACA;AACA;AACA;EACGC,UAAU,EAAG;IAAE,UAAU,EAAG,gBAAgB;IAC7B,iBAAiB,EAAG;EAAgB;AACtD,CAAC;AAAEC,OAAA,CAAAjB,IAAA,GAAAA,IAAA"}
|
package/source-cjs/dimension.js
CHANGED
|
@@ -4,7 +4,6 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.Dimension = void 0;
|
|
7
|
-
|
|
8
7
|
/**
|
|
9
8
|
* This class implements an object containing the vector of exponents for
|
|
10
9
|
* a unit and its operations for addition, subtraction, and multiplication
|
|
@@ -18,9 +17,7 @@ exports.Dimension = void 0;
|
|
|
18
17
|
* @author Lee Mericle, based on java version by Gunther Schadow
|
|
19
18
|
*/
|
|
20
19
|
var UC = require('./config.js');
|
|
21
|
-
|
|
22
20
|
var isInteger = require("is-integer");
|
|
23
|
-
|
|
24
21
|
class Dimension {
|
|
25
22
|
/**
|
|
26
23
|
* Constructor.
|
|
@@ -46,28 +43,26 @@ class Dimension {
|
|
|
46
43
|
if (UC.Ucum.dimLen_ === 0) {
|
|
47
44
|
throw new Error('Dimension.setDimensionLen must be called before ' + 'Dimension constructor');
|
|
48
45
|
}
|
|
49
|
-
|
|
50
46
|
if (dimSetting === undefined || dimSetting === null) {
|
|
51
47
|
this.assignZero();
|
|
52
48
|
} else if (dimSetting instanceof Array) {
|
|
53
49
|
if (dimSetting.length !== UC.Ucum.dimLen_) {
|
|
54
50
|
throw new Error('Parameter error, incorrect length of vector passed to ' + `Dimension constructor, vector = ${JSON.stringify(dimSetting)}`);
|
|
55
51
|
}
|
|
56
|
-
|
|
57
52
|
this.dimVec_ = [];
|
|
58
|
-
|
|
59
53
|
for (let d = 0; d < UC.Ucum.dimLen_; d++) this.dimVec_.push(dimSetting[d]);
|
|
60
|
-
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// In es6 this should be Number.isInteger(dimSetting). But Babel
|
|
61
57
|
// doesn't transpile that correctly, so we need to use the isInteger
|
|
62
58
|
// module. :0
|
|
63
59
|
else if (isInteger(dimSetting)) {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
this.assignZero();
|
|
69
|
-
this.dimVec_[dimSetting] = 1;
|
|
60
|
+
if (dimSetting < 0 || dimSetting >= UC.Ucum.dimLen_) {
|
|
61
|
+
throw new Error('Parameter error, invalid element number specified for ' + 'Dimension constructor');
|
|
70
62
|
}
|
|
63
|
+
this.assignZero();
|
|
64
|
+
this.dimVec_[dimSetting] = 1;
|
|
65
|
+
}
|
|
71
66
|
} // end constructor
|
|
72
67
|
|
|
73
68
|
/**
|
|
@@ -81,20 +76,17 @@ class Dimension {
|
|
|
81
76
|
* @throws an exception if the specified position is invalid, i.e., not a
|
|
82
77
|
* number or is less than 0 or greater than Ucum.dimLen_
|
|
83
78
|
**/
|
|
84
|
-
|
|
85
|
-
|
|
86
79
|
setElementAt(indexPos, value) {
|
|
87
80
|
if (!isInteger(indexPos) || indexPos < 0 || indexPos >= UC.Ucum.dimLen_) {
|
|
88
81
|
throw new Error(`Dimension.setElementAt called with an invalid index ` + `position (${indexPos})`);
|
|
89
82
|
}
|
|
90
|
-
|
|
91
83
|
if (!this.dimVec_) {
|
|
92
84
|
this.assignZero();
|
|
93
85
|
}
|
|
94
|
-
|
|
95
86
|
if (value === undefined || value === null) value = 1;
|
|
96
87
|
this.dimVec_[indexPos] = value;
|
|
97
88
|
}
|
|
89
|
+
|
|
98
90
|
/**
|
|
99
91
|
* Gets the value of the element at the specified position
|
|
100
92
|
*
|
|
@@ -104,17 +96,15 @@ class Dimension {
|
|
|
104
96
|
* @throws an exception if the specified position is invalid, i.e., not a
|
|
105
97
|
* number or is less than 0 or greater than Ucum.dimLen_
|
|
106
98
|
**/
|
|
107
|
-
|
|
108
|
-
|
|
109
99
|
getElementAt(indexPos) {
|
|
110
100
|
if (!isInteger(indexPos) || indexPos < 0 || indexPos >= UC.Ucum.dimLen_) {
|
|
111
101
|
throw new Error(`Dimension.getElementAt called with an invalid index ` + `position (${indexPos})`);
|
|
112
102
|
}
|
|
113
|
-
|
|
114
103
|
let ret = null;
|
|
115
104
|
if (this.dimVec_) ret = this.dimVec_[indexPos];
|
|
116
105
|
return ret;
|
|
117
106
|
}
|
|
107
|
+
|
|
118
108
|
/**
|
|
119
109
|
* This returns the value of the property named by the parameter
|
|
120
110
|
* passed in. Although we currently only have one property, dimVec_,
|
|
@@ -127,8 +117,6 @@ class Dimension {
|
|
|
127
117
|
* @return the requested property, if found for this Dimension
|
|
128
118
|
* @throws an error if the property is not found for this Dimension
|
|
129
119
|
*/
|
|
130
|
-
|
|
131
|
-
|
|
132
120
|
getProperty(propertyName) {
|
|
133
121
|
let uProp = propertyName.charAt(propertyName.length - 1) === '_' ? propertyName : propertyName + '_';
|
|
134
122
|
return this[uProp];
|
|
@@ -142,13 +130,12 @@ class Dimension {
|
|
|
142
130
|
* values are enclosed in square brackets, each separated
|
|
143
131
|
* by a comma and a space
|
|
144
132
|
**/
|
|
145
|
-
|
|
146
|
-
|
|
147
133
|
toString() {
|
|
148
134
|
let ret = null;
|
|
149
135
|
if (this.dimVec_) ret = '[' + this.dimVec_.join(', ') + ']';
|
|
150
136
|
return ret;
|
|
151
137
|
}
|
|
138
|
+
|
|
152
139
|
/**
|
|
153
140
|
* Adds the vector of the dimension object passed in to this
|
|
154
141
|
* dimension object's vector. This object's vector is changed.
|
|
@@ -159,19 +146,16 @@ class Dimension {
|
|
|
159
146
|
* @return this object
|
|
160
147
|
* @throws an exception if dim2 is not a Dimension object
|
|
161
148
|
**/
|
|
162
|
-
|
|
163
|
-
|
|
164
149
|
add(dim2) {
|
|
165
150
|
if (!dim2 instanceof Dimension) {
|
|
166
151
|
throw new Error(`Dimension.add called with an invalid parameter - ` + `${typeof dim2} instead of a Dimension object`);
|
|
167
152
|
}
|
|
168
|
-
|
|
169
153
|
if (this.dimVec_ && dim2.dimVec_) {
|
|
170
154
|
for (let i = 0; i < UC.Ucum.dimLen_; i++) this.dimVec_[i] += dim2.dimVec_[i];
|
|
171
155
|
}
|
|
172
|
-
|
|
173
156
|
return this;
|
|
174
157
|
}
|
|
158
|
+
|
|
175
159
|
/**
|
|
176
160
|
* Subtracts the vector of the dimension object passed in from this
|
|
177
161
|
* dimension object's vector. This object's vector is changed.
|
|
@@ -181,19 +165,16 @@ class Dimension {
|
|
|
181
165
|
* @return this object
|
|
182
166
|
* @throws an exception if dim2 is not a Dimension object
|
|
183
167
|
**/
|
|
184
|
-
|
|
185
|
-
|
|
186
168
|
sub(dim2) {
|
|
187
169
|
if (!dim2 instanceof Dimension) {
|
|
188
170
|
throw new Error(`Dimension.sub called with an invalid parameter - ` + `${typeof dim2} instead of a Dimension object`);
|
|
189
171
|
}
|
|
190
|
-
|
|
191
172
|
if (this.dimVec_ && dim2.dimVec_) {
|
|
192
173
|
for (let i = 0; i < UC.Ucum.dimLen_; i++) this.dimVec_[i] -= dim2.dimVec_[i];
|
|
193
174
|
}
|
|
194
|
-
|
|
195
175
|
return this;
|
|
196
176
|
}
|
|
177
|
+
|
|
197
178
|
/**
|
|
198
179
|
* Inverts this dimension object's vector (by multiplying each element
|
|
199
180
|
* by negative 1). This object's vector is changed - unless it is null,
|
|
@@ -201,15 +182,13 @@ class Dimension {
|
|
|
201
182
|
*
|
|
202
183
|
* @return this object
|
|
203
184
|
**/
|
|
204
|
-
|
|
205
|
-
|
|
206
185
|
minus() {
|
|
207
186
|
if (this.dimVec_) {
|
|
208
187
|
for (let i = 0; i < UC.Ucum.dimLen_; i++) this.dimVec_[i] = -this.dimVec_[i];
|
|
209
188
|
}
|
|
210
|
-
|
|
211
189
|
return this;
|
|
212
190
|
}
|
|
191
|
+
|
|
213
192
|
/**
|
|
214
193
|
* Multiplies this dimension object's vector with a scalar. This is used
|
|
215
194
|
* when a unit is raised to a power. This object's vector is changed unless
|
|
@@ -219,19 +198,16 @@ class Dimension {
|
|
|
219
198
|
* @return this object
|
|
220
199
|
* @throws an exception if s is not a number
|
|
221
200
|
*/
|
|
222
|
-
|
|
223
|
-
|
|
224
201
|
mul(s) {
|
|
225
202
|
if (!isInteger(s)) {
|
|
226
203
|
throw new Error(`Dimension.sub called with an invalid parameter - ` + `${typeof dim2} instead of a number`);
|
|
227
204
|
}
|
|
228
|
-
|
|
229
205
|
if (this.dimVec_) {
|
|
230
206
|
for (let i = 0; i < UC.Ucum.dimLen_; i++) this.dimVec_[i] *= s;
|
|
231
207
|
}
|
|
232
|
-
|
|
233
208
|
return this;
|
|
234
209
|
}
|
|
210
|
+
|
|
235
211
|
/**
|
|
236
212
|
* Tests for equality of this dimension object's vector and that of
|
|
237
213
|
* the dimension object passed in. If the dimension vector for one of
|
|
@@ -242,24 +218,20 @@ class Dimension {
|
|
|
242
218
|
* @return true if the two vectors are equal; false otherwise.
|
|
243
219
|
* @throws an exception if dim2 is not a Dimension object
|
|
244
220
|
*/
|
|
245
|
-
|
|
246
|
-
|
|
247
221
|
equals(dim2) {
|
|
248
222
|
if (!dim2 instanceof Dimension) {
|
|
249
223
|
throw new Error(`Dimension.equals called with an invalid parameter - ` + `${typeof dim2} instead of a Dimension object`);
|
|
250
224
|
}
|
|
251
|
-
|
|
252
225
|
let isEqual = true;
|
|
253
226
|
let dimVec2 = dim2.dimVec_;
|
|
254
|
-
|
|
255
227
|
if (this.dimVec_ && dimVec2) {
|
|
256
228
|
for (let i = 0; isEqual && i < UC.Ucum.dimLen_; i++) isEqual = this.dimVec_[i] === dimVec2[i];
|
|
257
229
|
} else {
|
|
258
230
|
isEqual = this.dimVec_ === null && dimVec2 === null;
|
|
259
231
|
}
|
|
260
|
-
|
|
261
232
|
return isEqual;
|
|
262
233
|
}
|
|
234
|
+
|
|
263
235
|
/**
|
|
264
236
|
* Assigns the contents of the vector belonging to the dimension object
|
|
265
237
|
* passed in to this dimension's vector. If this dimension vector is null
|
|
@@ -272,39 +244,33 @@ class Dimension {
|
|
|
272
244
|
* @return this object (not sure why)
|
|
273
245
|
* @throws an exception if dim2 is not a Dimension object
|
|
274
246
|
*/
|
|
275
|
-
|
|
276
|
-
|
|
277
247
|
assignDim(dim2) {
|
|
278
248
|
if (!dim2 instanceof Dimension) {
|
|
279
249
|
throw new Error(`Dimension.assignDim called with an invalid parameter - ` + `${typeof dim2} instead of a Dimension object`);
|
|
280
250
|
}
|
|
281
|
-
|
|
282
251
|
if (dim2.dimVec_ === null) this.dimVec_ = null;else {
|
|
283
252
|
if (this.dimVec_ === null) {
|
|
284
253
|
this.dimVec_ = [];
|
|
285
254
|
}
|
|
286
|
-
|
|
287
255
|
for (let i = 0; i < UC.Ucum.dimLen_; i++) this.dimVec_[i] = dim2.dimVec_[i];
|
|
288
256
|
}
|
|
289
257
|
return this;
|
|
290
258
|
}
|
|
259
|
+
|
|
291
260
|
/**
|
|
292
261
|
* Sets all elements of this dimension object's vector to zero.
|
|
293
262
|
* If this object's vector is null, it is created as a zero-filled vector.
|
|
294
263
|
*
|
|
295
264
|
* @return this object (not sure why)
|
|
296
265
|
*/
|
|
297
|
-
|
|
298
|
-
|
|
299
266
|
assignZero() {
|
|
300
267
|
if (this.dimVec_ === null || this.dimVec_ === undefined) this.dimVec_ = [];
|
|
301
|
-
|
|
302
268
|
for (let i = 0; i < UC.Ucum.dimLen_; i++) {
|
|
303
269
|
this.dimVec_.push(0);
|
|
304
270
|
}
|
|
305
|
-
|
|
306
271
|
return this;
|
|
307
272
|
}
|
|
273
|
+
|
|
308
274
|
/**
|
|
309
275
|
* Tests for a dimension vector set to all zeroes.
|
|
310
276
|
*
|
|
@@ -312,43 +278,34 @@ class Dimension {
|
|
|
312
278
|
* zero; false otherwise (including if the current vector is null).
|
|
313
279
|
*
|
|
314
280
|
*/
|
|
315
|
-
|
|
316
|
-
|
|
317
281
|
isZero() {
|
|
318
282
|
let allZero = this.dimVec_ !== null;
|
|
319
|
-
|
|
320
283
|
if (this.dimVec_) {
|
|
321
284
|
for (let i = 0; allZero && i < UC.Ucum.dimLen_; i++) allZero = this.dimVec_[i] === 0;
|
|
322
285
|
}
|
|
323
|
-
|
|
324
286
|
return allZero;
|
|
325
287
|
}
|
|
288
|
+
|
|
326
289
|
/**
|
|
327
290
|
* Tests for a Dimension object with no dimension vector (dimVec_ is null).
|
|
328
291
|
*
|
|
329
292
|
* @return true the dimension vector is null; false if it is not
|
|
330
293
|
*
|
|
331
294
|
*/
|
|
332
|
-
|
|
333
|
-
|
|
334
295
|
isNull() {
|
|
335
296
|
return this.dimVec_ === null;
|
|
336
297
|
}
|
|
298
|
+
|
|
337
299
|
/**
|
|
338
300
|
* Creates and returns a clone of this Dimension object
|
|
339
301
|
*
|
|
340
302
|
* @return the clone
|
|
341
303
|
*/
|
|
342
|
-
|
|
343
|
-
|
|
344
304
|
clone() {
|
|
345
305
|
let that = new Dimension();
|
|
346
306
|
that.assignDim(this);
|
|
347
307
|
return that;
|
|
348
308
|
}
|
|
349
|
-
|
|
350
309
|
} // end Dimension class
|
|
351
|
-
|
|
352
|
-
|
|
353
310
|
exports.Dimension = Dimension;
|
|
354
311
|
//# sourceMappingURL=dimension.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../source/dimension.js"],"names":["UC","require","isInteger","Dimension","constructor","dimSetting","Ucum","dimLen_","Error","undefined","assignZero","Array","length","JSON","stringify","dimVec_","d","push","setElementAt","indexPos","value","getElementAt","ret","getProperty","propertyName","uProp","charAt","toString","join","add","dim2","i","sub","minus","mul","s","equals","isEqual","dimVec2","assignDim","isZero","allZero","isNull","clone","that"],"mappings":";;;;;;;AAAA;;;;;;;;;;;;AAYA,IAAIA,EAAE,GAAGC,OAAO,CAAC,aAAD,CAAhB;;AACA,IAAIC,SAAS,GAAGD,OAAO,CAAC,YAAD,CAAvB;;AACO,MAAME,SAAN,CAAgB;AAErB;;;;;;;;;;;;;;;;;;;;AAqBAC,EAAAA,WAAW,CAACC,UAAD,EAAa;AAEtB,QAAIL,EAAE,CAACM,IAAH,CAAQC,OAAR,KAAoB,CAAxB,EAA2B;AACzB,YAAM,IAAIC,KAAJ,CAAU,qDAChB,uBADM,CAAN;AAED;;AACD,QAAIH,UAAU,KAAKI,SAAf,IAA4BJ,UAAU,KAAK,IAA/C,EAAqD;AACnD,WAAKK,UAAL;AACD,KAFD,MAGK,IAAIL,UAAU,YAAYM,KAA1B,EAAiC;AACpC,UAAIN,UAAU,CAACO,MAAX,KAAsBZ,EAAE,CAACM,IAAH,CAAQC,OAAlC,EAA2C;AACzC,cAAM,IAAIC,KAAJ,CAAU,2DACX,mCAAkCK,IAAI,CAACC,SAAL,CAAeT,UAAf,CAA2B,EAD5D,CAAN;AAED;;AACD,WAAKU,OAAL,GAAe,EAAf;;AACA,WAAK,IAAIC,CAAC,GAAG,CAAb,EAAgBA,CAAC,GAAGhB,EAAE,CAACM,IAAH,CAAQC,OAA5B,EAAqCS,CAAC,EAAtC,EACE,KAAKD,OAAL,CAAaE,IAAb,CAAkBZ,UAAU,CAACW,CAAD,CAA5B;AACH,KARI,CAUL;AACA;AACA;AAZK,SAaA,IAAId,SAAS,CAACG,UAAD,CAAb,EAA2B;AAC9B,YAAIA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AIgB,CAAC,GAAG,CAAb,EAAgBU,OAAO,IAAIV,CAAC,GAAG/B,EAAE,CAACM,IAAH,CAAQC,OAAvC,EAAgDwB,CAAC,EAAjD,EACEU,OAAO,GAAG,KAAK1B,OAAL,CAAagB,CAAb,MAAoB,CAA9B;AACH;;AACD,WAAOU,OAAP;AACD;AAGD;;;;;;;;AAMAC,EAAAA,MAAM,GAAG;AACP,WAAQ,KAAK3B,OAAL,KAAiB,IAAzB;AACD;AAGD;;;;;;;AAKA4B,EAAAA,KAAK,GAAG;AACN,QAAIC,IAAI,GAAG,IAAIzC,SAAJ,EAAX;AACAyC,IAAAA,IAAI,CAACL,SAAL,CAAe,IAAf;AACA,WAAOK,IAAP;AACD;;AAnVoB,C,CAqVrB","sourcesContent":["/**\n * This class implements an object containing the vector of exponents for\n * a unit and its operations for addition, subtraction, and multiplication\n * with a scalar.\n *\n * This object should exist for each unit that can be expressed as a\n * vector of numbers. This excludes arbitrary units, e.g., (10*23), and\n * units that are not numbers but are an expression based solely on numbers,\n * e.g., mol (mole) which is based on 10*23.\n *\n * @author Lee Mericle, based on java version by Gunther Schadow\n */\nvar UC = require('./config.js');\nvar isInteger = require(\"is-integer\");\nexport class Dimension {\n\n /**\n * Constructor.\n *\n * @param dimSetting an optional parameter that may be:\n * null, which means that the dimVec_ attribute for this object will be null; or\n * an array, which must be the length defined by Ucum.dimLen_, and\n * whose contents will be copied to this new object's vector; or\n * an integer, which must be between 0 and 1 less than the vector length\n * defined by Ucum.dimLen_. This new object's vector will be\n * initialized to zero for all elements except the one whose index\n * matches the number passed in. That element will be set to one.\n\n * @throws an error if the dimSetting parameter does not meet the types\n * listed above.\n * An error will also be thrown if Ucum.dimLen_ has not been set yet,\n * i.e., is still zero. Currently that won't happen, because the\n * value is set in the config.js file. But further down the road\n * the setting will come from a definitions input file, so we check\n * here anyway.\n *\n */\n constructor(dimSetting) {\n\n if (UC.Ucum.dimLen_ === 0) {\n throw(new Error('Dimension.setDimensionLen must be called before ' +\n 'Dimension constructor'));\n }\n if (dimSetting === undefined || dimSetting === null) {\n this.assignZero() ;\n }\n else if (dimSetting instanceof Array) {\n if (dimSetting.length !== UC.Ucum.dimLen_) {\n throw(new Error('Parameter error, incorrect length of vector passed to ' +\n `Dimension constructor, vector = ${JSON.stringify(dimSetting)}`));\n }\n this.dimVec_ = [];\n for (let d = 0; d < UC.Ucum.dimLen_; d++)\n this.dimVec_.push(dimSetting[d]);\n }\n\n // In es6 this should be Number.isInteger(dimSetting). But Babel\n // doesn't transpile that correctly, so we need to use the isInteger\n // module. :0\n else if (isInteger(dimSetting)) {\n if (dimSetting < 0 || dimSetting >= UC.Ucum.dimLen_) {\n throw(new Error('Parameter error, invalid element number specified for ' +\n 'Dimension constructor'));\n }\n this.assignZero() ;\n this.dimVec_[dimSetting] = 1;\n }\n } // end constructor\n\n\n /**\n * Sets the element at the specified position to a specified value. The\n * default value is 1. If the dimension vector is null when this is called\n * a zero-filled vector is created and then the indicated position is set.\n *\n * @param indexPos the index of the element to be set\n * @param value the value to assign to the specified element; optional,\n * default value is 1\n * @throws an exception if the specified position is invalid, i.e., not a\n * number or is less than 0 or greater than Ucum.dimLen_\n **/\n setElementAt(indexPos, value) {\n\n if (!isInteger(indexPos) ||\n indexPos < 0 || indexPos >= UC.Ucum.dimLen_) {\n throw(new Error(`Dimension.setElementAt called with an invalid index ` +\n `position (${indexPos})`));\n }\n\n if (!this.dimVec_) {\n this.assignZero();\n }\n if (value === undefined || value === null)\n value = 1 ;\n\n this.dimVec_[indexPos] = value;\n }\n\n\n /**\n * Gets the value of the element at the specified position\n *\n * @param indexPos the index of the element whose value is to be returned\n * @return the value of the element at indexPos, or null if the dimension\n * vector is null\n * @throws an exception if the specified position is invalid, i.e., not a\n * number or is less than 0 or greater than Ucum.dimLen_\n **/\n getElementAt(indexPos) {\n if (!isInteger(indexPos) ||\n indexPos < 0 || indexPos >= UC.Ucum.dimLen_) {\n throw(new Error(`Dimension.getElementAt called with an invalid index ` +\n `position (${indexPos})`));\n }\n let ret = null;\n if (this.dimVec_)\n ret = this.dimVec_[indexPos];\n return ret;\n }\n\n\n /**\n * This returns the value of the property named by the parameter\n * passed in. Although we currently only have one property, dimVec_,\n * that this will get, it's possible that we'll have additional\n * properties. If we don't this could just be replaced by a\n * getVector function.\n *\n * @param propertyName name of the property to be returned, with\n * or without the trailing underscore.\n * @return the requested property, if found for this Dimension\n * @throws an error if the property is not found for this Dimension\n */\n getProperty(propertyName) {\n let uProp = propertyName.charAt(propertyName.length - 1) === '_' ? propertyName : propertyName + '_';\n\n return this[uProp] ;\n\n } // end getProperty\n\n\n /**\n * Return a string that represents the dimension vector. Returns null if\n * the dimension vector is null.\n *\n * @return the string that represents the dimension vector. The\n * values are enclosed in square brackets, each separated\n * by a comma and a space\n **/\n toString() {\n let ret = null ;\n if (this.dimVec_)\n ret = '[' + this.dimVec_.join(', ') + ']';\n return ret ;\n }\n\n\n /**\n * Adds the vector of the dimension object passed in to this\n * dimension object's vector. This object's vector is changed.\n * If either dimension vector is null, no changes are made to this object.\n *\n *\n * @param dim2 the dimension whose vector is to be added to this one\n * @return this object\n * @throws an exception if dim2 is not a Dimension object\n **/\n add(dim2) {\n if (!dim2 instanceof Dimension) {\n throw(new Error(`Dimension.add called with an invalid parameter - ` +\n `${typeof dim2} instead of a Dimension object`));\n }\n if (this.dimVec_ && dim2.dimVec_) {\n for (let i = 0; i < UC.Ucum.dimLen_; i++)\n this.dimVec_[i] += dim2.dimVec_[i];\n }\n return this;\n }\n\n\n /**\n * Subtracts the vector of the dimension object passed in from this\n * dimension object's vector. This object's vector is changed.\n * If either dimension vector is null, no changes are made to this object.\n *\n * @param dim2 the dimension whose vector is to be subtracted from this one\n * @return this object\n * @throws an exception if dim2 is not a Dimension object\n **/\n sub(dim2) {\n if (!dim2 instanceof Dimension) {\n throw(new Error(`Dimension.sub called with an invalid parameter - ` +\n `${typeof dim2} instead of a Dimension object`));\n }\n if (this.dimVec_ && dim2.dimVec_) {\n for (let i = 0; i < UC.Ucum.dimLen_; i++)\n this.dimVec_[i] -= dim2.dimVec_[i];\n }\n return this;\n }\n\n\n /**\n * Inverts this dimension object's vector (by multiplying each element\n * by negative 1). This object's vector is changed - unless it is null,\n * in which case it stays that way.\n *\n * @return this object\n **/\n minus() {\n if (this.dimVec_) {\n for (let i = 0; i < UC.Ucum.dimLen_; i++)\n this.dimVec_[i] = -this.dimVec_[i];\n }\n return this;\n }\n\n\n /**\n * Multiplies this dimension object's vector with a scalar. This is used\n * when a unit is raised to a power. This object's vector is changed unless\n * the vector is null, in which case it stays that way.\n *\n * @param s the scalar to use\n * @return this object\n * @throws an exception if s is not a number\n */\n mul(s) {\n if (!isInteger(s)) {\n throw(new Error(`Dimension.sub called with an invalid parameter - ` +\n `${typeof dim2} instead of a number`));\n }\n if (this.dimVec_) {\n for (let i = 0; i < UC.Ucum.dimLen_; i++)\n this.dimVec_[i] *= s;\n }\n return this;\n }\n\n\n /**\n * Tests for equality of this dimension object's vector and that of\n * the dimension object passed in. If the dimension vector for one of\n * the objects is null, the dimension vector for the other object must\n * also be null for the two to be equal. (I know - duh. still)\n *\n * @param dim2 the dimension object whose vector is to be compared to this one\n * @return true if the two vectors are equal; false otherwise.\n * @throws an exception if dim2 is not a Dimension object\n */\n equals(dim2) {\n if (!dim2 instanceof Dimension) {\n throw(new Error(`Dimension.equals called with an invalid parameter - ` +\n `${typeof dim2} instead of a Dimension object`));\n }\n let isEqual = true ;\n let dimVec2 = dim2.dimVec_;\n if (this.dimVec_ && dimVec2) {\n for (let i = 0; isEqual && i < UC.Ucum.dimLen_; i++)\n isEqual = (this.dimVec_[i] === dimVec2[i]);\n }\n else {\n isEqual = (this.dimVec_ === null && dimVec2 === null);\n }\n return isEqual;\n }\n\n\n /**\n * Assigns the contents of the vector belonging to the dimension object\n * passed in to this dimension's vector. If this dimension vector is null\n * and the other is not, this one will get the contents of the other. If\n * this dimension vector is not null but the one passed in is null, this\n * one will be set to null.\n *\n * @param dim2 the dimension object with the vector whose contents are\n * to be assigned to this dimension's vector\n * @return this object (not sure why)\n * @throws an exception if dim2 is not a Dimension object\n */\n assignDim(dim2) {\n\n if (!dim2 instanceof Dimension) {\n throw(new Error(`Dimension.assignDim called with an invalid parameter - ` +\n `${typeof dim2} instead of a Dimension object`));\n }\n if (dim2.dimVec_ === null)\n this.dimVec_ = null;\n else {\n if (this.dimVec_ === null) {\n this.dimVec_ = [] ;\n }\n for (let i = 0; i < UC.Ucum.dimLen_; i++)\n this.dimVec_[i] = dim2.dimVec_[i];\n }\n return this;\n }\n\n\n /**\n * Sets all elements of this dimension object's vector to zero.\n * If this object's vector is null, it is created as a zero-filled vector.\n *\n * @return this object (not sure why)\n */\n assignZero() {\n if (this.dimVec_ === null || this.dimVec_ === undefined)\n this.dimVec_ = [];\n\n for (let i = 0; i < UC.Ucum.dimLen_; i++) {\n this.dimVec_.push(0);\n }\n return this;\n }\n\n\n /**\n * Tests for a dimension vector set to all zeroes.\n *\n * @return true if exponents (elements) of this dimension's vector are all\n * zero; false otherwise (including if the current vector is null).\n *\n */\n isZero() {\n let allZero = this.dimVec_ !== null ;\n if (this.dimVec_) {\n for (let i = 0; allZero && i < UC.Ucum.dimLen_; i++)\n allZero = this.dimVec_[i] === 0;\n }\n return allZero;\n }\n\n\n /**\n * Tests for a Dimension object with no dimension vector (dimVec_ is null).\n *\n * @return true the dimension vector is null; false if it is not\n *\n */\n isNull() {\n return (this.dimVec_ === null);\n }\n\n\n /**\n * Creates and returns a clone of this Dimension object\n *\n * @return the clone\n */\n clone() {\n let that = new Dimension();\n that.assignDim(this);\n return that;\n }\n\n} // end Dimension class\n"],"file":"dimension.js"}
|
|
1
|
+
{"version":3,"file":"dimension.js","names":["UC","require","isInteger","Dimension","constructor","dimSetting","Ucum","dimLen_","Error","undefined","assignZero","Array","length","JSON","stringify","dimVec_","d","push","setElementAt","indexPos","value","getElementAt","ret","getProperty","propertyName","uProp","charAt","toString","join","add","dim2","i","sub","minus","mul","s","equals","isEqual","dimVec2","assignDim","isZero","allZero","isNull","clone","that","exports"],"sources":["../source/dimension.js"],"sourcesContent":["/**\n * This class implements an object containing the vector of exponents for\n * a unit and its operations for addition, subtraction, and multiplication\n * with a scalar.\n *\n * This object should exist for each unit that can be expressed as a\n * vector of numbers. This excludes arbitrary units, e.g., (10*23), and\n * units that are not numbers but are an expression based solely on numbers,\n * e.g., mol (mole) which is based on 10*23.\n *\n * @author Lee Mericle, based on java version by Gunther Schadow\n */\nvar UC = require('./config.js');\nvar isInteger = require(\"is-integer\");\nexport class Dimension {\n\n /**\n * Constructor.\n *\n * @param dimSetting an optional parameter that may be:\n * null, which means that the dimVec_ attribute for this object will be null; or\n * an array, which must be the length defined by Ucum.dimLen_, and\n * whose contents will be copied to this new object's vector; or\n * an integer, which must be between 0 and 1 less than the vector length\n * defined by Ucum.dimLen_. This new object's vector will be\n * initialized to zero for all elements except the one whose index\n * matches the number passed in. That element will be set to one.\n\n * @throws an error if the dimSetting parameter does not meet the types\n * listed above.\n * An error will also be thrown if Ucum.dimLen_ has not been set yet,\n * i.e., is still zero. Currently that won't happen, because the\n * value is set in the config.js file. But further down the road\n * the setting will come from a definitions input file, so we check\n * here anyway.\n *\n */\n constructor(dimSetting) {\n\n if (UC.Ucum.dimLen_ === 0) {\n throw(new Error('Dimension.setDimensionLen must be called before ' +\n 'Dimension constructor'));\n }\n if (dimSetting === undefined || dimSetting === null) {\n this.assignZero() ;\n }\n else if (dimSetting instanceof Array) {\n if (dimSetting.length !== UC.Ucum.dimLen_) {\n throw(new Error('Parameter error, incorrect length of vector passed to ' +\n `Dimension constructor, vector = ${JSON.stringify(dimSetting)}`));\n }\n this.dimVec_ = [];\n for (let d = 0; d < UC.Ucum.dimLen_; d++)\n this.dimVec_.push(dimSetting[d]);\n }\n\n // In es6 this should be Number.isInteger(dimSetting). But Babel\n // doesn't transpile that correctly, so we need to use the isInteger\n // module. :0\n else if (isInteger(dimSetting)) {\n if (dimSetting < 0 || dimSetting >= UC.Ucum.dimLen_) {\n throw(new Error('Parameter error, invalid element number specified for ' +\n 'Dimension constructor'));\n }\n this.assignZero() ;\n this.dimVec_[dimSetting] = 1;\n }\n } // end constructor\n\n\n /**\n * Sets the element at the specified position to a specified value. The\n * default value is 1. If the dimension vector is null when this is called\n * a zero-filled vector is created and then the indicated position is set.\n *\n * @param indexPos the index of the element to be set\n * @param value the value to assign to the specified element; optional,\n * default value is 1\n * @throws an exception if the specified position is invalid, i.e., not a\n * number or is less than 0 or greater than Ucum.dimLen_\n **/\n setElementAt(indexPos, value) {\n\n if (!isInteger(indexPos) ||\n indexPos < 0 || indexPos >= UC.Ucum.dimLen_) {\n throw(new Error(`Dimension.setElementAt called with an invalid index ` +\n `position (${indexPos})`));\n }\n\n if (!this.dimVec_) {\n this.assignZero();\n }\n if (value === undefined || value === null)\n value = 1 ;\n\n this.dimVec_[indexPos] = value;\n }\n\n\n /**\n * Gets the value of the element at the specified position\n *\n * @param indexPos the index of the element whose value is to be returned\n * @return the value of the element at indexPos, or null if the dimension\n * vector is null\n * @throws an exception if the specified position is invalid, i.e., not a\n * number or is less than 0 or greater than Ucum.dimLen_\n **/\n getElementAt(indexPos) {\n if (!isInteger(indexPos) ||\n indexPos < 0 || indexPos >= UC.Ucum.dimLen_) {\n throw(new Error(`Dimension.getElementAt called with an invalid index ` +\n `position (${indexPos})`));\n }\n let ret = null;\n if (this.dimVec_)\n ret = this.dimVec_[indexPos];\n return ret;\n }\n\n\n /**\n * This returns the value of the property named by the parameter\n * passed in. Although we currently only have one property, dimVec_,\n * that this will get, it's possible that we'll have additional\n * properties. If we don't this could just be replaced by a\n * getVector function.\n *\n * @param propertyName name of the property to be returned, with\n * or without the trailing underscore.\n * @return the requested property, if found for this Dimension\n * @throws an error if the property is not found for this Dimension\n */\n getProperty(propertyName) {\n let uProp = propertyName.charAt(propertyName.length - 1) === '_' ? propertyName : propertyName + '_';\n\n return this[uProp] ;\n\n } // end getProperty\n\n\n /**\n * Return a string that represents the dimension vector. Returns null if\n * the dimension vector is null.\n *\n * @return the string that represents the dimension vector. The\n * values are enclosed in square brackets, each separated\n * by a comma and a space\n **/\n toString() {\n let ret = null ;\n if (this.dimVec_)\n ret = '[' + this.dimVec_.join(', ') + ']';\n return ret ;\n }\n\n\n /**\n * Adds the vector of the dimension object passed in to this\n * dimension object's vector. This object's vector is changed.\n * If either dimension vector is null, no changes are made to this object.\n *\n *\n * @param dim2 the dimension whose vector is to be added to this one\n * @return this object\n * @throws an exception if dim2 is not a Dimension object\n **/\n add(dim2) {\n if (!dim2 instanceof Dimension) {\n throw(new Error(`Dimension.add called with an invalid parameter - ` +\n `${typeof dim2} instead of a Dimension object`));\n }\n if (this.dimVec_ && dim2.dimVec_) {\n for (let i = 0; i < UC.Ucum.dimLen_; i++)\n this.dimVec_[i] += dim2.dimVec_[i];\n }\n return this;\n }\n\n\n /**\n * Subtracts the vector of the dimension object passed in from this\n * dimension object's vector. This object's vector is changed.\n * If either dimension vector is null, no changes are made to this object.\n *\n * @param dim2 the dimension whose vector is to be subtracted from this one\n * @return this object\n * @throws an exception if dim2 is not a Dimension object\n **/\n sub(dim2) {\n if (!dim2 instanceof Dimension) {\n throw(new Error(`Dimension.sub called with an invalid parameter - ` +\n `${typeof dim2} instead of a Dimension object`));\n }\n if (this.dimVec_ && dim2.dimVec_) {\n for (let i = 0; i < UC.Ucum.dimLen_; i++)\n this.dimVec_[i] -= dim2.dimVec_[i];\n }\n return this;\n }\n\n\n /**\n * Inverts this dimension object's vector (by multiplying each element\n * by negative 1). This object's vector is changed - unless it is null,\n * in which case it stays that way.\n *\n * @return this object\n **/\n minus() {\n if (this.dimVec_) {\n for (let i = 0; i < UC.Ucum.dimLen_; i++)\n this.dimVec_[i] = -this.dimVec_[i];\n }\n return this;\n }\n\n\n /**\n * Multiplies this dimension object's vector with a scalar. This is used\n * when a unit is raised to a power. This object's vector is changed unless\n * the vector is null, in which case it stays that way.\n *\n * @param s the scalar to use\n * @return this object\n * @throws an exception if s is not a number\n */\n mul(s) {\n if (!isInteger(s)) {\n throw(new Error(`Dimension.sub called with an invalid parameter - ` +\n `${typeof dim2} instead of a number`));\n }\n if (this.dimVec_) {\n for (let i = 0; i < UC.Ucum.dimLen_; i++)\n this.dimVec_[i] *= s;\n }\n return this;\n }\n\n\n /**\n * Tests for equality of this dimension object's vector and that of\n * the dimension object passed in. If the dimension vector for one of\n * the objects is null, the dimension vector for the other object must\n * also be null for the two to be equal. (I know - duh. still)\n *\n * @param dim2 the dimension object whose vector is to be compared to this one\n * @return true if the two vectors are equal; false otherwise.\n * @throws an exception if dim2 is not a Dimension object\n */\n equals(dim2) {\n if (!dim2 instanceof Dimension) {\n throw(new Error(`Dimension.equals called with an invalid parameter - ` +\n `${typeof dim2} instead of a Dimension object`));\n }\n let isEqual = true ;\n let dimVec2 = dim2.dimVec_;\n if (this.dimVec_ && dimVec2) {\n for (let i = 0; isEqual && i < UC.Ucum.dimLen_; i++)\n isEqual = (this.dimVec_[i] === dimVec2[i]);\n }\n else {\n isEqual = (this.dimVec_ === null && dimVec2 === null);\n }\n return isEqual;\n }\n\n\n /**\n * Assigns the contents of the vector belonging to the dimension object\n * passed in to this dimension's vector. If this dimension vector is null\n * and the other is not, this one will get the contents of the other. If\n * this dimension vector is not null but the one passed in is null, this\n * one will be set to null.\n *\n * @param dim2 the dimension object with the vector whose contents are\n * to be assigned to this dimension's vector\n * @return this object (not sure why)\n * @throws an exception if dim2 is not a Dimension object\n */\n assignDim(dim2) {\n\n if (!dim2 instanceof Dimension) {\n throw(new Error(`Dimension.assignDim called with an invalid parameter - ` +\n `${typeof dim2} instead of a Dimension object`));\n }\n if (dim2.dimVec_ === null)\n this.dimVec_ = null;\n else {\n if (this.dimVec_ === null) {\n this.dimVec_ = [] ;\n }\n for (let i = 0; i < UC.Ucum.dimLen_; i++)\n this.dimVec_[i] = dim2.dimVec_[i];\n }\n return this;\n }\n\n\n /**\n * Sets all elements of this dimension object's vector to zero.\n * If this object's vector is null, it is created as a zero-filled vector.\n *\n * @return this object (not sure why)\n */\n assignZero() {\n if (this.dimVec_ === null || this.dimVec_ === undefined)\n this.dimVec_ = [];\n\n for (let i = 0; i < UC.Ucum.dimLen_; i++) {\n this.dimVec_.push(0);\n }\n return this;\n }\n\n\n /**\n * Tests for a dimension vector set to all zeroes.\n *\n * @return true if exponents (elements) of this dimension's vector are all\n * zero; false otherwise (including if the current vector is null).\n *\n */\n isZero() {\n let allZero = this.dimVec_ !== null ;\n if (this.dimVec_) {\n for (let i = 0; allZero && i < UC.Ucum.dimLen_; i++)\n allZero = this.dimVec_[i] === 0;\n }\n return allZero;\n }\n\n\n /**\n * Tests for a Dimension object with no dimension vector (dimVec_ is null).\n *\n * @return true the dimension vector is null; false if it is not\n *\n */\n isNull() {\n return (this.dimVec_ === null);\n }\n\n\n /**\n * Creates and returns a clone of this Dimension object\n *\n * @return the clone\n */\n clone() {\n let that = new Dimension();\n that.assignDim(this);\n return that;\n }\n\n} // end Dimension class\n"],"mappings":";;;;;;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,IAAIA,EAAE,GAAGC,OAAO,CAAC,aAAa,CAAC;AAC/B,IAAIC,SAAS,GAAGD,OAAO,CAAC,YAAY,CAAC;AAC9B,MAAME,SAAS,CAAC;EAErB;AACF;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;EAEEC,WAAWA,CAACC,UAAU,EAAE;IAEtB,IAAIL,EAAE,CAACM,IAAI,CAACC,OAAO,KAAK,CAAC,EAAE;MACzB,MAAM,IAAIC,KAAK,CAAC,kDAAkD,GAClE,uBAAuB,CAAC;IAC1B;IACA,IAAIH,UAAU,KAAKI,SAAS,IAAIJ,UAAU,KAAK,IAAI,EAAE;MACnD,IAAI,CAACK,UAAU,CAAC,CAAC;IACnB,CAAC,MACI,IAAIL,UAAU,YAAYM,KAAK,EAAE;MACpC,IAAIN,UAAU,CAACO,MAAM,KAAKZ,E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B,KAAKA,CAAA,EAAG;IACN,IAAIC,IAAI,GAAG,IAAIzC,SAAS,CAAC,CAAC;IAC1ByC,IAAI,CAACL,SAAS,CAAC,IAAI,CAAC;IACpB,OAAOK,IAAI;EACb;AAEF,CAAC,CAAC;AAAAC,OAAA,CAAA1C,SAAA,GAAAA,SAAA"}
|
|
@@ -5,22 +5,19 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.packArray = packArray;
|
|
7
7
|
exports.unpackArray = unpackArray;
|
|
8
|
-
|
|
9
8
|
/**
|
|
10
9
|
* This file provides functions to reduce the size of an array of objects of the same structure in JSON.
|
|
11
10
|
*/
|
|
12
11
|
const pushFn = Array.prototype.push;
|
|
13
|
-
|
|
14
12
|
function isObject(value) {
|
|
15
13
|
return Object.prototype.toString.call(value) === '[object Object]';
|
|
16
14
|
}
|
|
15
|
+
|
|
17
16
|
/**
|
|
18
17
|
* Makes human readable config used to pack/unpack array of objects of the same structure to store with packed data.
|
|
19
18
|
* @param {Object} refObj - reference item of array of objects of the same structure
|
|
20
19
|
* @returns {Array}
|
|
21
20
|
*/
|
|
22
|
-
|
|
23
|
-
|
|
24
21
|
function createConfig(refObj) {
|
|
25
22
|
return Object.keys(refObj).reduce((config, key) => {
|
|
26
23
|
if (isObject(refObj[key])) {
|
|
@@ -28,38 +25,33 @@ function createConfig(refObj) {
|
|
|
28
25
|
} else {
|
|
29
26
|
config.push(key);
|
|
30
27
|
}
|
|
31
|
-
|
|
32
28
|
return config;
|
|
33
29
|
}, []);
|
|
34
30
|
}
|
|
31
|
+
|
|
35
32
|
/**
|
|
36
33
|
* Prepares config created with createConfig function to use in packItem/unpackItem functions.
|
|
37
34
|
* @param {Array} config
|
|
38
35
|
* @returns {Array}
|
|
39
36
|
*/
|
|
40
|
-
|
|
41
|
-
|
|
42
37
|
function prepareConfig(config) {
|
|
43
38
|
return config.map(key => Array.isArray(key) ? key : [key]);
|
|
44
39
|
}
|
|
40
|
+
|
|
45
41
|
/**
|
|
46
42
|
* Converts an object to an array of values in the order of keys from configuration array.
|
|
47
43
|
* @param {Array} config - configuration array
|
|
48
44
|
* @param {Object} item - input object
|
|
49
45
|
* @returns {Array}
|
|
50
46
|
*/
|
|
51
|
-
|
|
52
|
-
|
|
53
47
|
function packItem(config, item) {
|
|
54
48
|
if (config.join() !== prepareConfig(createConfig(item)).join()) {
|
|
55
49
|
throw new Error('Object of unusual structure');
|
|
56
50
|
}
|
|
57
|
-
|
|
58
51
|
return config.map(keyArr => {
|
|
59
52
|
let place = item;
|
|
60
53
|
keyArr.forEach(key => {
|
|
61
54
|
place = place[key];
|
|
62
|
-
|
|
63
55
|
if (place === undefined) {
|
|
64
56
|
throw new Error('Object of unusual structure');
|
|
65
57
|
}
|
|
@@ -67,39 +59,34 @@ function packItem(config, item) {
|
|
|
67
59
|
return place;
|
|
68
60
|
});
|
|
69
61
|
}
|
|
62
|
+
|
|
70
63
|
/**
|
|
71
64
|
* Performs the reverse of packItem function.
|
|
72
65
|
* @param {Array} config - configuration array
|
|
73
66
|
* @param {Array} item - input object
|
|
74
67
|
* @returns {Object}
|
|
75
68
|
*/
|
|
76
|
-
|
|
77
|
-
|
|
78
69
|
function unpackItem(config, item) {
|
|
79
70
|
let result = {};
|
|
80
71
|
config.forEach((keyArr, i) => {
|
|
81
72
|
let place = result;
|
|
82
|
-
|
|
83
73
|
for (let i = 0; i < keyArr.length - 1; i++) {
|
|
84
74
|
place = place[keyArr[i]] = place[keyArr[i]] || {};
|
|
85
75
|
}
|
|
86
|
-
|
|
87
76
|
place[keyArr[keyArr.length - 1]] = item[i];
|
|
88
77
|
});
|
|
89
78
|
return result;
|
|
90
79
|
}
|
|
80
|
+
|
|
91
81
|
/**
|
|
92
82
|
* Reduces size of an array of objects of the same structure before serialize it to JSON
|
|
93
83
|
* @param {Array} arr
|
|
94
84
|
* @returns {Object}
|
|
95
85
|
*/
|
|
96
|
-
|
|
97
|
-
|
|
98
86
|
function packArray(arr) {
|
|
99
87
|
if (arr && arr.length) {
|
|
100
88
|
const config = createConfig(arr[0]),
|
|
101
|
-
|
|
102
|
-
|
|
89
|
+
_config = prepareConfig(config);
|
|
103
90
|
if (config.length) {
|
|
104
91
|
return {
|
|
105
92
|
config: config,
|
|
@@ -107,32 +94,27 @@ function packArray(arr) {
|
|
|
107
94
|
};
|
|
108
95
|
}
|
|
109
96
|
}
|
|
110
|
-
|
|
111
97
|
return {
|
|
112
98
|
config: [],
|
|
113
99
|
data: arr
|
|
114
100
|
};
|
|
115
101
|
}
|
|
102
|
+
|
|
116
103
|
/**
|
|
117
104
|
* Restores an array of objects of the same structure after deserializing this object from JSON
|
|
118
105
|
* @param {Object} obj
|
|
119
106
|
* @returns {Array}
|
|
120
107
|
*/
|
|
121
|
-
|
|
122
|
-
|
|
123
108
|
function unpackArray(obj) {
|
|
124
109
|
const config = obj && obj.config;
|
|
125
|
-
|
|
126
110
|
if (config) {
|
|
127
111
|
if (config.length && obj.data) {
|
|
128
112
|
const _config = prepareConfig(config);
|
|
129
|
-
|
|
130
113
|
return obj.data.map(unpackItem.bind(null, _config));
|
|
131
114
|
} else {
|
|
132
115
|
return obj.data;
|
|
133
116
|
}
|
|
134
117
|
}
|
|
135
|
-
|
|
136
118
|
return obj;
|
|
137
119
|
}
|
|
138
120
|
//# sourceMappingURL=jsonArrayPack.js.map
|