-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changing to Id derivative of Uint8Array in order to provide operator overloading #6
Conversation
Currently the implementation for binary string encoding is: function toString(id: Id): string {
const b = Buffer.from(id.buffer, id.byteOffset, id.byteLength);
return b.toString('binary');
}
function fromString(idString: string): Id | undefined {
const b = Buffer.from(idString, 'binary');
return IdInternal.create(b.buffer, b.byteOffset, b.byteLength);
} We need to try to get this working without the help of Node buffers. Need to search how to convert |
I might want to disambiguate |
Seems like we could do something like:
|
For function toString(id: Id): string {
return String.fromCharCode(...id);
} However for function fromString(idString: string): Id | undefined {
if (idString.length !== 16) return;
const id = IdInternal.create(16);
for (let i = 0; i < 16; i++) {
id[i] = idString.charCodeAt(i);
}
return id;
} The above should work. Except now we have another TS type error originating from the hack of using The problem is that When they conflict, the resulting inference is that I've tried a few different techniques like with (https://stackoverflow.com/questions/51465182/how-to-remove-index-signature-using-mapped-types): type RemoveIndex<T> = {
[ K in keyof T as string extends K ? never : number extends K ? never : K ] : T[K]
};
type RemoveIndex2<T> = {
[K in keyof T as {} extends Record<K, 1> ? never : K]: T[K]
}
type X = IdInternal & RemoveIndex<string>; But those don't work on the primitive string. They do work on the The only primitive type that does work is |
Ok it might work if I use The interface Number {
/**
* Returns a string representation of an object.
* @param radix Specifies a radix for converting numeric values to strings. This value is only used for numbers.
*/
toString(radix?: number): string;
/**
* Returns a string representing a number in fixed-point notation.
* @param fractionDigits Number of digits after the decimal point. Must be in the range 0 - 20, inclusive.
*/
toFixed(fractionDigits?: number): string;
/**
* Returns a string containing a number represented in exponential notation.
* @param fractionDigits Number of digits after the decimal point. Must be in the range 0 - 20, inclusive.
*/
toExponential(fractionDigits?: number): string;
/**
* Returns a string containing a number represented either in exponential or fixed-point notation with a specified number of digits.
* @param precision Number of significant digits. Must be in the range 1 - 21, inclusive.
*/
toPrecision(precision?: number): string;
/** Returns the primitive value of the specified object. */
valueOf(): number;
} So tricking TS into thinking the Note that |
I just realised that bigints are not 64bit, they are arbitrary sized. In that sense perhaps we can say that it's a Unfortunately the |
I wanted to compare our binary encoding to what's used in multibase with their identity encoding. Internally they are using |
e9b90c5
to
bd8fe29
Compare
New tests were added for string equality, comparison and also record index usage. |
Description
@tegefaulkes
Changes the id type from
ArrayBuffer
toId
. TheId
is an extension ofUint8Array
, but with additional TS hack making TS think it's also like a string.This enables things like:
Because
Id
is aUint8Array
and allUint8Array
areArrayBuffer
, whereverArrayBuffer
is needed, theId
can be substituted. This makesId
a more "flexible" type.Beware that TS thinks
id
is also a string, but it's not really. So limit automatic typecasting to the above situations.Also these utility functions changed:
Right now the primitive string from is a "binary string" as described by https://developer.mozilla.org/en-US/docs/Web/API/DOMString/Binary. This is probably the least expensive encoding compared to uuid or multibase. It's very raw.
I will need to do something like:
In the utils so that way it will use those conversions too.
Issues Fixed
Tasks
IdInternal
class andId
typeIdSortable
,IdDeterministic
,IdRandom
Id
)Final checklist