MatrixAI / js-id

ID generation for JavaScript & TypeScript Applications
https://polykey.com
Apache License 2.0
10 stars 1 forks source link

Add encoding and decoding methods to `IdInternal` #11

Closed tegefaulkes closed 2 years ago

tegefaulkes commented 2 years ago

Description

This adds some QOL features to the Id and IdInternal.

Issues Fixed

Tasks

  1. [x] Add type conversion methods to InternalId
  2. [x] add support for working with and converting to opaque types of Id
  3. [x] expand testing to test new features.

Final checklist

tegefaulkes commented 2 years ago

I can't quite get the encode/decode feature to work nicely so we're stuck with to/fromMultibase and wrapping them.

But I have added this neat feature were we can create an Opaque type directly when converting by doing


// where
type NodeId = Opaque<'nodeId', Id>;

// we can do
const nodeId = internalId.fromBuffer<NodeId>(buffer);
// or
const nodeid2: NodeId = InternalId.fromBuffer(buffer);

Any method that returns an Id type can support this. so the generators like IdRandom can return our opaque type directly aswell. This just saves us having to use id as OpaqueId.

tegefaulkes commented 2 years ago

This is still a WIP. I'm still making changes.

tegefaulkes commented 2 years ago

It may be possible to extend InternalId and set a Multibase format with;

type NodeId = InternalNodeid & number;
class internalNodeId extends InternalId {
    public readonly base: MultibaseFormats = 'base58btc';
}

The encode and decode functions can be built into InternalNodeId that way. Unfortunately I can't use the class generic types for the static methods so we'd still have to do InternalNodeId.fromBuffer<NodeId> to get it to return a NodeId. or we'd have to override all of the static methods to return a NodeId.

The alternative is to just have the utility functions for each domain to encode/decode.

CMCDragonkai commented 2 years ago

It's not necessary, it would just be a nice to have. We can proceed to merge this with just the relevant instance and static methods added in here.

We keep using nodesUtils and vaultsUtils for encoding/decoding relevant to the domains.

CMCDragonkai commented 2 years ago

@tegefaulkes Changed PR title to be more clearer. You should try to be more descriptive on those PR names.

tegefaulkes commented 2 years ago

Examples of new features.

All of the type coverters are in one place now in the IdInternal class.

IdInternal.fromBuffer();
IdInternal.fromString();
IdInternal.fromUUID();
IdInternal.fromMultibase();

const id = IdInternal.create();

id.toBuffer();
id.toString();
id.toUUID();
id.toMultibase('someRandomBase');

Any constructors for a Id can create an opaque type directly now by using

type OpaqueId<'opaqueId', Id>
const opaqueId = IdInternal.create<OpaqueId>();
const opaqueId: OpaqueId = idInternal.create();

// Same for conversions from other types
const opaqueId = IdInternal.fromBuffer<OpaqueId>();
const opaqueId: OpaqueId = IdInternal.fromString();

// The original way still works 
const id = IdInternal.create() as OpaqueId;

The id generators can do the same thing.

type OpaqueId = Opaque<'opaque', Id>;
const idGen = new IdRandom<OpaqueId>();

// ids has type OpaqueId[]
 const ids = [...utils.take(idGen, 10)];
tegefaulkes commented 2 years ago

All review comments are resolved. should be good to merge.