manyfest 1.0.4 → 1.0.5

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "manyfest",
3
- "version": "1.0.4",
3
+ "version": "1.0.5",
4
4
  "description": "JSON Object Manifest for Data Description and Parsing",
5
5
  "main": "source/Manyfest.js",
6
6
  "scripts": {
@@ -183,15 +183,7 @@ class Manyfest
183
183
 
184
184
  getDescriptorByHash(pHash)
185
185
  {
186
- if (this.elementHashes.hasOwnProperty(pHash) || this.hashTranslations.translationTable.hasOwnProperty(pHash))
187
- {
188
- return this.getDescriptor(this.elementHashes[this.hashTranslations.translate(pHash)]);
189
- }
190
- else
191
- {
192
- this.logError(`(${this.scope}) Error in getDescriptorByHash; the Hash ${pHash} doesn't exist in the schema.`);
193
- return undefined;
194
- }
186
+ return this.getDescriptor(this.resolveHashAddress(pHash));
195
187
  }
196
188
 
197
189
  getDescriptor(pAddress)
@@ -205,15 +197,7 @@ class Manyfest
205
197
  // Check if an element exists by its hash
206
198
  checkAddressExistsByHash (pObject, pHash)
207
199
  {
208
- if (this.elementHashes.hasOwnProperty(pHash) || this.hashTranslations.translationTable.hasOwnProperty(pHash))
209
- {
210
- return this.checkAddressExists(pObject, this.elementHashes[this.hashTranslations.translate(pHash)]);
211
- }
212
- else
213
- {
214
- this.logError(`(${this.scope}) Error in checkAddressExistsByHash; the Hash ${pHash} doesn't exist in the schema.`);
215
- return undefined;
216
- }
200
+ return this.checkAddressExists(pObject,this.resolveHashAddress(pHash));
217
201
  }
218
202
 
219
203
  // Check if an element exists at an address
@@ -222,19 +206,43 @@ class Manyfest
222
206
  return this.objectAddressResolver.checkAddressExists(pObject, pAddress);
223
207
  }
224
208
 
225
-
226
- // Get the value of an element by its hash
227
- getValueByHash (pObject, pHash)
209
+ // Turn a hash into an address, factoring in the translation table.
210
+ resolveHashAddress(pHash)
228
211
  {
229
- if (this.elementHashes.hasOwnProperty(pHash) || this.hashTranslations.translationTable.hasOwnProperty(pHash))
212
+ let tmpAddress = undefined;
213
+
214
+ let tmpInElementHashTable = this.elementHashes.hasOwnProperty(pHash);
215
+ let tmpInTranslationTable = this.hashTranslations.translationTable.hasOwnProperty(pHash);
216
+
217
+ // The most straightforward: the hash exists, no translations.
218
+ if (tmpInElementHashTable && !tmpInTranslationTable)
230
219
  {
231
- return this.getValueAtAddress(pObject, this.elementHashes[this.hashTranslations.translate(pHash)]);
220
+ tmpAddress = this.elementHashes[pHash];
232
221
  }
222
+ // There is a translation from one hash to another, and, the elementHashes contains the pointer end
223
+ else if (tmpInTranslationTable && this.elementHashes.hasOwnProperty(this.hashTranslations.translate(pHash)))
224
+ {
225
+ tmpAddress = this.elementHashes[this.hashTranslations.translate(pHash)];
226
+ }
227
+ // Use the level of indirection only in the Translation Table
228
+ else if (tmpInTranslationTable)
229
+ {
230
+ tmpAddress = this.hashTranslations.translate(pHash);
231
+ }
232
+ // Just treat the hash as an address.
233
+ // TODO: Discuss this ... it is magic but controversial
233
234
  else
234
235
  {
235
- this.logError(`(${this.scope}) Error in getValueByHash; the Hash ${pHash} doesn't exist in the schema.`);
236
- return undefined;
236
+ tmpAddress = pHash;
237
237
  }
238
+
239
+ return tmpAddress;
240
+ }
241
+
242
+ // Get the value of an element by its hash
243
+ getValueByHash (pObject, pHash)
244
+ {
245
+ return this.getValueAtAddress(pObject, this.resolveHashAddress(pHash));
238
246
  }
239
247
 
240
248
  // Get the value of an element at an address
@@ -246,15 +254,7 @@ class Manyfest
246
254
  // Set the value of an element by its hash
247
255
  setValueByHash(pObject, pHash, pValue)
248
256
  {
249
- if (this.elementHashes.hasOwnProperty(pHash) || this.hashTranslations.translationTable.hasOwnProperty(pHash))
250
- {
251
- return this.setValueAtAddress(pObject, this.elementHashes[this.hashTranslations.translate(pHash)], pValue);
252
- }
253
- else
254
- {
255
- this.logError(`(${this.scope}) Error in setValueByHash; the Hash ${pHash} doesn't exist in the schema. Value ${pValue} will not be written!`);
256
- return undefined;
257
- }
257
+ return this.setValueAtAddress(pObject, this.resolveHashAddress(pHash), pValue);
258
258
  }
259
259
 
260
260
 
@@ -147,6 +147,22 @@ suite
147
147
  }
148
148
  );
149
149
  test
150
+ (
151
+ 'Translate to a value not in the hashes, falling back to address.',
152
+ (fTestComplete)=>
153
+ {
154
+ let _Manyfest = new libManyfest({ Scope:'Archive.org', Descriptors: {'metadata.creator': {Name:'Creator', Hash:'Creator'}}});
155
+ // Create a translation between "Creator" and "metadata.identifier", which isn't in the manifest in any way
156
+ _Manyfest.hashTranslations.addTranslation({"Creator":"metadata.identifier"});
157
+ // This address is not in the descriptor address list or the hash list
158
+ Expect(_Manyfest.getValueAtAddress(_SampleDataArchiveOrgFrankenberry, 'metadata.identifier')).to.equal('FrankenberryCountChoculaTevevisionCommercial1971');
159
+ // But now we've pointed the Creator hash to it!
160
+ Expect(_Manyfest.getValueByHash(_SampleDataArchiveOrgFrankenberry, 'Creator')).to.equal('FrankenberryCountChoculaTevevisionCommercial1971');
161
+
162
+ fTestComplete();
163
+ }
164
+ );
165
+ test
150
166
  (
151
167
  'Add a bogus translation.',
152
168
  (fTestComplete)=>