Closed CMCDragonkai closed 2 years ago
@tegefaulkes @joshuakarp. This is ready to be used and will be released as 3.3.0
. You can replace any equality comparison in the src code with equals
instead of converting to binary string.
Furthermore, the toJSON
may not be used for now because you're using NodeIdEncoded
, but this gives us the opportunity to change if we need to.
Some fixes for the previous PR too.
fromString
doesn't return undefined
, any binary string is valid Id
fromBuffer
doesn't return undefined
, any buffer is valid Id
This means some !
assertions may not be needed.
@tegefaulkes
Description
This adds the methods of
toJSON
,fromJSON
andequals
.The JSON methods matches the behaviour of Node's buffer. It allows us to marshal
IdInternal
into JSON and back with the appropriatereviver
.Notice that
JSON.stringify
does not need a specialreplacer
. This is already done by having atoJSON
method in theIdInternal
class.In the case of
JSON.parse
, when it will convert any{ type: 'IdInternal', data: [ ... ] }
into theIdInternal
object.This is important as in some cases, when storing Ids where JSON is expected, this allow us to unmarshal back to the canonical binary ID representation rather than always encoding it as a string.
The above reviver will default if it's not an valid
IdInternal
object.Additionally the
equals
method provides an efficient way to check forIdInternal
equality.Issues Fixed
Tasks
toJSON
andfromJSON
and testsequals
and testsfromJSON
can be used inside the reviver ofJSON.parse
Final checklist