@lhncbc/ucum-lhc 5.0.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.
@@ -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
- } // In es6 this should be Number.isInteger(dimSetting). But Babel
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
- if (dimSetting < 0 || dimSetting >= UC.Ucum.dimLen_) {
65
- throw new Error('Parameter error, invalid element number specified for ' + 'Dimension constructor');
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,UAAU,GAAG,CAAb,IAAkBA,UAAU,IAAIL,EAAE,CAACM,IA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G/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,EAAE,CAACM,IAAI,CAACC,OAAO,EAAE;QACzC,MAAM,IAAIC,KAAK,CA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AC,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
- _config = prepareConfig(config);
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
@@ -1 +1 @@
1
- {"version":3,"sources":["../source/jsonArrayPack.js"],"names":["pushFn","Array","prototype","push","isObject","value","Object","toString","call","createConfig","refObj","keys","reduce","config","key","apply","map","keyTail","concat","prepareConfig","isArray","packItem","item","join","Error","keyArr","place","forEach","undefined","unpackItem","result","i","length","packArray","arr","_config","data","bind","unpackArray","obj"],"mappings":";;;;;;;;AAAA;;;AAGA,MAAMA,MAAM,GAAGC,KAAK,CAACC,SAAN,CAAgBC,IAA/B;;AAEA,SAASC,QAAT,CAAkBC,KAAlB,EAAyB;AACvB,SAAOC,MAAM,CAACJ,SAAP,CAAiBK,QAAjB,CAA0BC,IAA1B,CAA+BH,KAA/B,MAA0C,iBAAjD;AACD;AAED;;;;;;;AAKA,SAASI,YAAT,CAAsBC,MAAtB,EAA8B;AAC5B,SAAOJ,MAAM,CAACK,IAAP,CAAYD,MAAZ,EAAoBE,MAApB,CAA2B,CAACC,MAAD,EAASC,GAAT,KAAiB;AACjD,QAAIV,QAAQ,CAACM,MAAM,CAACI,GAAD,CAAP,CAAZ,EAA2B;AACzBd,MAAAA,MAAM,CAACe,KAAP,CAAaF,MAAb,EAAqBJ,YAAY,CAACC,MAAM,CAACI,GAAD,CAAP,CAAZ,CAA0BE,GAA1B,CAA8BC,OAAO,IAAI,CAACH,GAAD,EAAM,GAAG,GAAGI,MAAH,CAAUD,OAAV,CAAT,CAAzC,CAArB;AACD,KAFD,MAEO;AACLJ,MAAAA,MAAM,CAACV,IAAP,CAAYW,GAAZ;AACD;;AACD,WAAOD,MAAP;AACD,GAPM,EAOJ,EAPI,CAAP;AAQD;AAED;;;;;;;AAKA,SAASM,aAAT,CAAuBN,MAAvB,EAA+B;AAC7B,SAAOA,MAAM,CAACG,GAAP,CAAWF,GAAG,IAAIb,KAAK,CAACmB,OAAN,CAAcN,GAAd,IAAqBA,GAArB,GAA2B,CAACA,GAAD,CAA7C,CAAP;AACD;AAED;;;;;;;;AAMA,SAASO,QAAT,CAAkBR,MAAlB,EAA0BS,IAA1B,EAAgC;AAC9B,MAAIT,MAAM,CAACU,IAAP,OAAkBJ,aAAa,CAACV,YAAY,CAACa,IAAD,CAAb,CAAb,CAAkCC,IAAlC,E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,MAAD,CADzB;;AAGA,QAAIA,MAAM,CAACmB,MAAX,EAAmB;AACjB,aAAO;AACLnB,QAAAA,MAAM,EAAEA,MADH;AAELuB,QAAAA,IAAI,EAAEF,GAAG,CAAClB,GAAJ,CAAQK,QAAQ,CAACgB,IAAT,CAAc,IAAd,EAAoBF,OAApB,CAAR;AAFD,OAAP;AAID;AACF;;AAED,SAAO;AACLtB,IAAAA,MAAM,EAAE,EADH;AAELuB,IAAAA,IAAI,EAAEF;AAFD,GAAP;AAID;AAED;;;;;;;AAKO,SAASI,WAAT,CAAqBC,GAArB,EAA0B;AAC/B,QAAM1B,MAAM,GAAG0B,GAAG,IAAIA,GAAG,CAAC1B,MAA1B;;AAEA,MAAIA,MAAJ,EAAY;AACV,QAAIA,MAAM,CAACmB,MAAP,IAAiBO,GAAG,CAACH,IAAzB,EAA+B;AAC7B,YAAMD,OAAO,GAAGhB,aAAa,CAACN,MAAD,CAA7B;;AAEA,aAAO0B,GAAG,CAACH,IAAJ,CAASpB,GAAT,CAAaa,UAAU,CAACQ,IAAX,CAAgB,IAAhB,EAAsBF,OAAtB,CAAb,CAAP;AACD,KAJD,MAIO;AACL,aAAOI,GAAG,CAACH,IAAX;AACD;AACF;;AAED,SAAOG,GAAP;AACD","sourcesContent":["/**\n * This file provides functions to reduce the size of an array of objects of the same structure in JSON.\n */\nconst pushFn = Array.prototype.push;\n\nfunction isObject(value) {\n return Object.prototype.toString.call(value) === '[object Object]';\n}\n\n/**\n * Makes human readable config used to pack/unpack array of objects of the same structure to store with packed data.\n * @param {Object} refObj - reference item of array of objects of the same structure\n * @returns {Array}\n */\nfunction createConfig(refObj) {\n return Object.keys(refObj).reduce((config, key) => {\n if (isObject(refObj[key])) {\n pushFn.apply(config, createConfig(refObj[key]).map(keyTail => [key, ...[].concat(keyTail)]));\n } else {\n config.push(key);\n }\n return config;\n }, [])\n}\n\n/**\n * Prepares config created with createConfig function to use in packItem/unpackItem functions.\n * @param {Array} config\n * @returns {Array}\n */\nfunction prepareConfig(config) {\n return config.map(key => Array.isArray(key) ? key : [key]);\n}\n\n/**\n * Converts an object to an array of values in the order of keys from configuration array.\n * @param {Array} config - configuration array\n * @param {Object} item - input object\n * @returns {Array}\n */\nfunction packItem(config, item) {\n if (config.join() !== prepareConfig(createConfig(item)).join()) {\n throw new Error('Object of unusual structure')\n }\n\n return config.map(keyArr => {\n let place = item;\n keyArr.forEach(key => {\n place = place[key];\n if (place === undefined) {\n throw new Error('Object of unusual structure')\n }\n });\n return place;\n });\n}\n\n/**\n * Performs the reverse of packItem function.\n * @param {Array} config - configuration array\n * @param {Array} item - input object\n * @returns {Object}\n */\nfunction unpackItem(config, item) {\n let result = {};\n\n config.forEach((keyArr, i) => {\n let place = result;\n for (let i = 0; i < keyArr.length - 1; i++) {\n place = place[keyArr[i]] = place[keyArr[i]] || {};\n }\n place[keyArr[keyArr.length - 1]] = item[i];\n });\n\n return result;\n}\n\n/**\n * Reduces size of an array of objects of the same structure before serialize it to JSON\n * @param {Array} arr\n * @returns {Object}\n */\nexport function packArray(arr) {\n if (arr && arr.length) {\n const config = createConfig(arr[0]),\n _config = prepareConfig(config);\n\n if (config.length) {\n return {\n config: config,\n data: arr.map(packItem.bind(null, _config))\n };\n }\n }\n\n return {\n config: [],\n data: arr\n };\n}\n\n/**\n * Restores an array of objects of the same structure after deserializing this object from JSON\n * @param {Object} obj\n * @returns {Array}\n */\nexport function unpackArray(obj) {\n const config = obj && obj.config;\n\n if (config) {\n if (config.length && obj.data) {\n const _config = prepareConfig(config);\n\n return obj.data.map(unpackItem.bind(null, _config));\n } else {\n return obj.data;\n }\n }\n\n return obj;\n}"],"file":"jsonArrayPack.js"}
1
+ {"version":3,"file":"jsonArrayPack.js","names":["pushFn","Array","prototype","push","isObject","value","Object","toString","call","createConfig","refObj","keys","reduce","config","key","apply","map","keyTail","concat","prepareConfig","isArray","packItem","item","join","Error","keyArr","place","forEach","undefined","unpackItem","result","i","length","packArray","arr","_config","data","bind","unpackArray","obj"],"sources":["../source/jsonArrayPack.js"],"sourcesContent":["/**\n * This file provides functions to reduce the size of an array of objects of the same structure in JSON.\n */\nconst pushFn = Array.prototype.push;\n\nfunction isObject(value) {\n return Object.prototype.toString.call(value) === '[object Object]';\n}\n\n/**\n * Makes human readable config used to pack/unpack array of objects of the same structure to store with packed data.\n * @param {Object} refObj - reference item of array of objects of the same structure\n * @returns {Array}\n */\nfunction createConfig(refObj) {\n return Object.keys(refObj).reduce((config, key) => {\n if (isObject(refObj[key])) {\n pushFn.apply(config, createConfig(refObj[key]).map(keyTail => [key, ...[].concat(keyTail)]));\n } else {\n config.push(key);\n }\n return config;\n }, [])\n}\n\n/**\n * Prepares config created with createConfig function to use in packItem/unpackItem functions.\n * @param {Array} config\n * @returns {Array}\n */\nfunction prepareConfig(config) {\n return config.map(key => Array.isArray(key) ? key : [key]);\n}\n\n/**\n * Converts an object to an array of values in the order of keys from configuration array.\n * @param {Array} config - configuration array\n * @param {Object} item - input object\n * @returns {Array}\n */\nfunction packItem(config, item) {\n if (config.join() !== prepareConfig(createConfig(item)).join()) {\n throw new Error('Object of unusual structure')\n }\n\n return config.map(keyArr => {\n let place = item;\n keyArr.forEach(key => {\n place = place[key];\n if (place === undefined) {\n throw new Error('Object of unusual structure')\n }\n });\n return place;\n });\n}\n\n/**\n * Performs the reverse of packItem function.\n * @param {Array} config - configuration array\n * @param {Array} item - input object\n * @returns {Object}\n */\nfunction unpackItem(config, item) {\n let result = {};\n\n config.forEach((keyArr, i) => {\n let place = result;\n for (let i = 0; i < keyArr.length - 1; i++) {\n place = place[keyArr[i]] = place[keyArr[i]] || {};\n }\n place[keyArr[keyArr.length - 1]] = item[i];\n });\n\n return result;\n}\n\n/**\n * Reduces size of an array of objects of the same structure before serialize it to JSON\n * @param {Array} arr\n * @returns {Object}\n */\nexport function packArray(arr) {\n if (arr && arr.length) {\n const config = createConfig(arr[0]),\n _config = prepareConfig(config);\n\n if (config.length) {\n return {\n config: config,\n data: arr.map(packItem.bind(null, _config))\n };\n }\n }\n\n return {\n config: [],\n data: arr\n };\n}\n\n/**\n * Restores an array of objects of the same structure after deserializing this object from JSON\n * @param {Object} obj\n * @returns {Array}\n */\nexport function unpackArray(obj) {\n const config = obj && obj.config;\n\n if (config) {\n if (config.length && obj.data) {\n const _config = prepareConfig(config);\n\n return obj.data.map(unpackItem.bind(null, _config));\n } else {\n return obj.data;\n }\n }\n\n return obj;\n}"],"mappings":";;;;;;;AAAA;AACA;AACA;AACA,MAAMA,MAAM,GAAGC,KAAK,CAACC,SAAS,CAACC,IAAI;AAEnC,SAASC,QAAQA,CAACC,KAAK,EAAE;EACvB,OAAOC,MAAM,CAACJ,SAAS,CAACK,QAAQ,CAACC,IAAI,CAACH,KAAK,CAAC,KAAK,iBAAiB;AACpE;;AAEA;AACA;AACA;AACA;AACA;AACA,SAASI,YAAYA,CAACC,MAAM,EAAE;EAC5B,OAAOJ,MAAM,CAACK,IAAI,CAACD,MAAM,CAAC,CAACE,MAAM,CAAC,CAACC,MAAM,EAAEC,GAAG,KAAK;IACjD,IAAIV,QAAQ,CAACM,MAAM,CAACI,GAAG,CAAC,CAAC,EAAE;MACzBd,MAAM,CAACe,KAAK,CAACF,MAAM,EAAEJ,YAAY,CAACC,MAAM,CAACI,GAAG,CAAC,CAAC,CAACE,GAAG,CAACC,OAAO,IAAI,CAACH,GAAG,EAAE,GAAG,EAAE,CAACI,MAAM,CAACD,OAAO,CAAC,CAAC,CAAC,CAAC;IAC9F,CAAC,MAAM;MACLJ,MAAM,CAACV,IAAI,CAACW,GAAG,CAAC;IAClB;IACA,OAAOD,MAAM;EACf,CAAC,EAAE,EAAE,CAAC;AACR;;AAEA;AACA;AACA;AACA;AACA;AACA,SAASM,aAAaA,CAACN,MAAM,EAAE;EAC7B,OAAOA,MAAM,CAACG,GAAG,CAACF,GAAG,IAAIb,KAAK,CAACmB,OAAO,CAACN,GAAG,CAAC,GAAGA,GAAG,GAAG,CAACA,GAAG,CAAC,CAAC;AAC5D;;AAEA;AAC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