Why would you ever want a UUID in the form of a number array?
saltz opened this issue Β· 31 comments
In the latest release: version 2.0.0, the type signature of the return parameter in the v4 UUID function changed from string
to string | number[]
. Why? I'm curious what use case would require the UUID to be represented as a number array? As the change in the return signature is breaking for typescript projects that depend on the typed return signature. (which could be interpreted from the major release bump)
Hey @saltz idea behind number[] was to make it backward compatible with the ArrayBuffer
and a choice went to going with number[]
as a simplified version of bytes array that was used in the original version of uuid.
I'm working on a new native implementation right now, so I'd love to know more about your use case.
I get the point about that string | number[]
is confusing. I'll probably refactor those into two separate functions too.
This signature totally breaks my usage of UUIDs. A lot of places expect UUIDs to be a string, a common one is <Component key={uuid} />
which is now broken.
This is going to be a part of 2.0.2
Thanks!
@eugenehp is there any update on when 2.0.2
will be released?
Ok, have a fresh version refactored! Going to do some more tests this week with a planned release for the weekend.
@eugenehp any update on this or any way that I can help assist in testing?
Thanks @katiawheeler
I've a drafty new version ready, I'll circle back for your feedback in the next few days.
I've also found a new way to optimize and ensure cryptographic stability, but that will come later in a next major release π
alright, good news is that we've made some progress, bad news is that it's new branch, and not the current release.
I plan to get a little bit more time next week to work on this, and will try to do a pre-release and could use some help to do the testing.
@eugenehp happy to provide assistance where I can!
I was curious to see what kind of benefit string | number[]
would provide. It'd be nice to see this in the README Doc.
@eugenehp Sure! How could I help? I'm an entry level dev so I'm not super experienced but I'd be happy to help.
@eugenehp Hi! Is there any progress on this? Is the current recommended usage to cast uuid.v4()
to string?
@eugenehp That sounds great! Of course, just ping me and I will assist with what I can.
Any update on this?
If anyone is still stuck on this, I just switched to using uuid
, @types/uuid
, and react-native-get-random-values
polyfill which works as expected.
With a little function overloading, the return type can be inferred from the arguments.
let result1: string = uuid.v4()
let result2: number[] = uuid.v4(undefined, [1, 1], undefined)
I made a mock example at the typescript playground.
Here's my workaround for manipulating uuid's string:
const uniqueId = uuid.v4()
const editedUuid = typeof uniqueId === 'string' ? uniqueId.replace(/-/g, '') : ''
If someone has a better idea, a flag or something, please tell me
upping this case @eugenehp, I can see that there is 2 years of discussion on the subject π Is it still in work or do some help are still required on this subject ?
Also a simple workaround could be to break it into two function, one for string and one for array number, as we want to avoid to have any params to provide
Surprised and confused to stumble upon this problem and GitHub issue. A uuid is uncontroversially a string. This problem seems to have arisen from poor dev decisions, but it is shocking that it has been unresolved for so long.
@eugenehp Do you have any response as to if/when this will be fixed?
@eugenehp any update on this?
If you use patch-package you can use this patch which uses the work @levabala did to patch this package
diff --git a/node_modules/react-native-uuid/dist/index.d.ts b/node_modules/react-native-uuid/dist/index.d.ts
index a70f091..6b2c511 100644
--- a/node_modules/react-native-uuid/dist/index.d.ts
+++ b/node_modules/react-native-uuid/dist/index.d.ts
@@ -1,4 +1,5 @@
export type { GenerateUUID } from './v35';
+import { v4 } from './v4';
declare const _default: {
parse: (s: string, buf?: number[] | undefined, offset?: number | undefined) => number[];
unparse: (buf: number[], offset?: number | undefined) => string;
@@ -13,10 +14,7 @@ declare const _default: {
rng: () => number[];
} | undefined, buf?: Uint8Array | undefined, offset?: number) => string | Uint8Array;
v3: import("./v35").GenerateUUID;
- v4: (options?: string | {
- random: number[];
- rng?: (() => number[]) | undefined;
- } | undefined, buf?: number[] | undefined, offset?: number | undefined) => string | number[];
+ v4: typeof v4;
v5: import("./v35").GenerateUUID;
NIL: string;
DNS: string;
diff --git a/node_modules/react-native-uuid/dist/v4.d.ts b/node_modules/react-native-uuid/dist/v4.d.ts
index 5c5e838..5238094 100644
--- a/node_modules/react-native-uuid/dist/v4.d.ts
+++ b/node_modules/react-native-uuid/dist/v4.d.ts
@@ -2,5 +2,6 @@ declare type V4Options = {
random: number[];
rng?: () => number[];
};
-export declare const v4: (options?: string | V4Options | undefined, buf?: number[] | undefined, offset?: number | undefined) => string | number[];
+export declare function v4(options?: V4Options | string, buf?: undefined, offset?: number): string;
+export declare function v4(options: V4Options | string | undefined, buf: Array<number>, offset?: number): number[];
export {};