pathchain 1.1.26 → 1.1.28

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.
Files changed (4) hide show
  1. package/getter.js +51 -13
  2. package/index.js +13 -0
  3. package/maker.js +3 -1
  4. package/package.json +1 -1
package/getter.js CHANGED
@@ -201,6 +201,40 @@ function getObj(xaddress) {
201
201
  }
202
202
  }
203
203
 
204
+ /**
205
+ * Retrieves and returns the proto type that belongs the address object from a file.
206
+ * @param {string} xaddress - The address string.
207
+ * @returns {Object|string} The decoded object or an error message.
208
+ */
209
+ function getType(xaddress) {
210
+ var splitted_address = xaddress.split("/");
211
+ console.log("Splitted address: ", splitted_address);
212
+ var obj_type = splitted_address[splitted_address.length - 2];
213
+ console.log("Retrieving object of type: ", obj_type);
214
+
215
+ try {
216
+ switch (obj_type) {
217
+ case "entities":
218
+ return "entity";
219
+ case "labels":
220
+ return "label";
221
+ case "links":
222
+ return "link";
223
+ case "moments":
224
+ return "moment";
225
+ case "nodes":
226
+ return "node";
227
+ case "paths":
228
+ return "path";
229
+ case "secrets":
230
+ return "secret";
231
+ }
232
+ }
233
+ catch (err) {
234
+ return err.code === 'ENOENT' ? "Element not found" : err;
235
+ }
236
+ }
237
+
204
238
  /**
205
239
  * Retrieves and decodes a path object from a file.
206
240
  * @param {string} xpath - The path hash.
@@ -240,21 +274,24 @@ function getPathchainObj(xpath, xauthor = '') {
240
274
  const fileContents = fs.readFileSync(filePath);
241
275
  var pathObj = pathProto.path.decode(fileContents);
242
276
 
243
- var current_link = this.getObj(pathObj.head);
244
- console.log("Head link", current_link);
277
+ var head_link = this.getObj(pathObj.head);
278
+ console.log("Head link", head_link);
245
279
 
246
- var current_obj = this.getObj(current_link.target);
247
- console.log("Head target object", current_obj);
280
+ var head_obj = this.getObj(head_link.target);
281
+ console.log("Head target object", head_obj);
248
282
 
249
- /*let obj_list = [];
250
- // append targets to obj_list until the link's prev property points to itself
251
- while (current_link.prev !== current_link.target) {
252
- obj_list.push(current_obj);
253
- current_link = this.getObj(current_link.prev);
254
- current_obj = this.getObj(current_link.target);
255
- }*/
256
- // Get link -> get link.target
257
- return current_obj;
283
+ let obj_list = [];
284
+ let current_link = head_link;
285
+
286
+ // As the head is the last inserted element, we go backwards till there's no more linked elements
287
+ while (current_link.prev != current_link.tag) {
288
+ obj_list.push(this.getObj(current_link.target));
289
+ current_link = current_link.prev;
290
+ }
291
+ // Push the last element
292
+ obj_list.push(this.getObj(current_link.target));
293
+
294
+ return obj_list;
258
295
  } catch (err) {
259
296
  return err.code === 'ENOENT' ? "Path not found" : err;
260
297
  }
@@ -271,6 +308,7 @@ module.exports = {
271
308
  getPathObj,
272
309
  getLabelObj,
273
310
  getObj,
311
+ getType,
274
312
  getPatheadObj,
275
313
  getPathchainObj
276
314
  };
package/index.js CHANGED
@@ -287,6 +287,19 @@ exports.getObj = (xaddress) => {
287
287
  return pathchain_object;
288
288
  }
289
289
 
290
+ /** getType
291
+ * [Function that recieves a path hash and returns the path object]
292
+ *
293
+
294
+ * Retrieves and decodes a address object from a file.
295
+ * @param {string} xaddress - The address string.
296
+ * @returns {Object|string} The decoded object or an error message.
297
+ */
298
+ exports.getType = (xaddress) => {
299
+ const pathchain_type = getter.getType(xaddress);
300
+ return pathchain_type;
301
+ }
302
+
290
303
  /** getPatheadObj
291
304
  * [Function that recieves a path hash and returns the path object]
292
305
  *
package/maker.js CHANGED
@@ -237,10 +237,12 @@ function path(text, elements, author, ancestor, format = 'MM DD YYYY HH:mm:SSS [
237
237
  const current_link = link(elements[i], "", "", author, "");
238
238
  // only if this is not the first element
239
239
  if (i > 0) {
240
+ // For the previous element, set the current element as its next element
240
241
  updater.setLinkNext(prev_link, current_link);
242
+ // For the current element, set the previous element as its previous element
241
243
  updater.setLinkPrev(current_link, prev_link);
242
244
  }
243
- if(i==0) head_link = current_link;
245
+ head_link = current_link;
244
246
  // for the next iteration, the previous link is set as the current link
245
247
  prev_link = current_link;
246
248
  }
package/package.json CHANGED
@@ -34,5 +34,5 @@
34
34
  "deprecated": false,
35
35
  "description": "![image](https://user-images.githubusercontent.com/104391124/282268780-cbbc1ee6-8d97-4d80-b896-66ae3eb723dd.png)",
36
36
  "name": "pathchain",
37
- "version": "1.1.26"
37
+ "version": "1.1.28"
38
38
  }