UNPKG

@ndn/naming-convention2

Version:

NDNts: Naming Convention rev2 and rev3

71 lines (57 loc) 2.81 kB
import { Keyword, Version, Segment, Timestamp, AltUri } from "@ndn/naming-convention2"; // other imports for examples import { Name } from "@ndn/packet"; import assert from "node:assert/strict"; // convention.create() returns a Component. let name = new Name(["A", Keyword.create("metadata")]); assert.equal(name.toString(), "/8=A/32=metadata"); // name.append() has an overload for convention component. name = name.append(Version, 3); assert.equal(name.toString(), "/8=A/32=metadata/54=%03"); name = name.append(Segment, 0); assert.equal(name.toString(), "/8=A/32=metadata/54=%03/50=%00"); // convention.match() checks whether a Component follows the convention. assert.equal(Segment.match(name.at(-1)), true); assert.equal(Version.match(name.at(-1)), false); // Or you can use component.is(). assert.equal(name.at(-1).is(Segment), true); assert.equal(name.at(-1).is(Version), false); // convention.parse() extracts the value from a Component. assert.equal(Keyword.parse(name.at(-3)), "metadata"); assert.equal(Version.parse(name.at(-2)), 3); assert.equal(Segment.parse(name.at(-1)), 0); // Or you can use component.as(). assert.equal(name.at(-3).as(Keyword), "metadata"); assert.equal(name.at(-2).as(Version), 3); assert.equal(name.at(-1).as(Segment), 0); // Use AltUri.ofName() and AltUri.ofComponent() to print as alternate URI syntax. assert.equal(AltUri.ofName(name), "/A/32=metadata/v=3/seg=0"); assert.equal(AltUri.ofComponent(name.at(2)), "v=3"); // Use AltUri.parseName() and AltUri.parseComponent() to parse from alternate URI syntax. assert.ok(AltUri.parseName("/A/32=metadata/v=3/seg=0").equals(name)); assert.ok(AltUri.parseComponent("v=3").equals(name.at(2))); // Creating from number, interpreted as microseconds since Unix epoch: const tsA = Timestamp.us.create(819170640000000); // Creating from number, interpreted as milliseconds since Unix epoch: const tsB = Timestamp.ms.create(819170640000); // Creating from Date object: const tsC = Timestamp.create(new Date("1995-12-17T03:24:00Z")); // They shall create the same name component: assert.ok(tsA.equals(tsB)); assert.ok(tsA.equals(tsC)); // Parsing into number as microseconds from Unix epoch: assert.equal(Timestamp.us.parse(tsB), 819170640000000); // Parsing into number as milliseconds from Unix epoch: assert.equal(Timestamp.ms.parse(tsC), 819170640000); // Call the Date constructor if you want a Date object: assert.deepEqual(new Date(Timestamp.ms.parse(tsA)), new Date("1995-12-17T03:24:00Z")); // Creating from number: const verA = Version.create(7); // Creating from bigint: const verB = Version.create(7n); // They shall create the same name component: assert.ok(verA.equals(verB)); // Parsing into number: assert.equal(Version.parse(verB), 7); // Parsing into bigint: assert.equal(Version.big.parse(verB), 7n);