In Name constructor, allow the argument to be another Name.
In Name.add, return this to allow chaining, and allow the argument to be another Name.
Added Name.addSegment.
diff --git a/js/Name.js b/js/Name.js
index 4d8a64c..833f383 100644
--- a/js/Name.js
+++ b/js/Name.js
@@ -6,8 +6,10 @@
/*
* Create a new Name from _components.
- * If _components is a string, parse it as a URI. Otherwise it is an array of components
- * where each is a string, byte array, ArrayBuffer or Uint8Array.
+ * If _components is a string, parse it as a URI.
+ * If _components is a Name, add a deep copy of its components.
+ * Otherwise it is an array of components where each is a string, byte array, ArrayBuffer, Uint8Array
+ * or Name.
* Convert and store as an array of Uint8Array.
* If a component is a string, encode as utf8.
*/
@@ -17,10 +19,13 @@
this.components = Name.createNameArray(_components);
}
else if(typeof _components === 'object'){
- if(LOG>4)console.log('Content Name Array '+_components);
this.components = [];
- for (var i = 0; i < _components.length; ++i)
- this.add(_components[i]);
+ if (_components instanceof Name)
+ this.add(_components);
+ else {
+ for (var i = 0; i < _components.length; ++i)
+ this.add(_components[i]);
+ }
}
else if(_components==null)
this.components =[];
@@ -114,10 +119,10 @@
};
/*
- * component is a string, byte array, ArrayBuffer or Uint8Array.
+ * component is a string, byte array, ArrayBuffer, Uint8Array or Name.
* Convert to Uint8Array and add to this Name.
* If a component is a string, encode as utf8.
- * Return the converted value.
+ * Return this Name object to allow chaining calls to add.
*/
Name.prototype.add = function(component){
var result;
@@ -130,6 +135,18 @@
result = new Uint8Array(new ArrayBuffer(component.byteLength));
result.set(new Uint8Array(component));
}
+ else if (typeof component == 'object' && component instanceof Name) {
+ var components;
+ if (component == this)
+ // special case, when we need to create a copy
+ components = this.components.slice(0, this.components.length);
+ else
+ components = component.components;
+
+ for (var i = 0; i < components.length; ++i)
+ this.components.push(new Uint8Array(components[i]));
+ return this;
+ }
else if(typeof component == 'object')
// Assume component is a byte array. We can't check instanceof Array because
// this doesn't work in JavaScript if the array comes from a different module.
@@ -138,7 +155,8 @@
throw new Error("Cannot add Name element at index " + this.components.length +
": Invalid type");
- return this.components.push(result);
+ this.components.push(result);
+ return this;
};
// Return the escaped name string according to "CCNx URI Scheme".
@@ -154,6 +172,26 @@
return result;
};
+/**
+* @brief Add component that represents a segment number
+*
+* @param number Segment number (integer is expected)
+*
+* This component has a special format handling:
+* - if number is zero, then %00 is added
+* - if number is between 1 and 255, %00%01 .. %00%FF is added
+* - ...
+*/
+Name.prototype.addSegment = function(number) {
+ var segmentNumberBigEndian = DataUtils.nonNegativeIntToBigEndian(number);
+ // Put a 0 byte in front.
+ var segmentNumberComponent = new Uint8Array(segmentNumberBigEndian.length + 1);
+ segmentNumberComponent.set(segmentNumberBigEndian, 1);
+
+ this.components.push(segmentNumberComponent);
+ return this;
+};
+
/*
* Return a new Name with the first nComponents components of this Name.
*/