@umituz/react-native-uuid 1.2.7 → 1.2.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@umituz/react-native-uuid",
3
- "version": "1.2.7",
3
+ "version": "1.2.9",
4
4
  "description": "Cross-platform UUID generation utility for React Native apps using expo-crypto",
5
5
  "main": "./src/index.ts",
6
6
  "types": "./src/index.ts",
package/src/index.ts CHANGED
@@ -8,5 +8,8 @@ export {
8
8
  generateUUID,
9
9
  generateUUID as uuidv4,
10
10
  generateUUID as generateCreationId,
11
+ isValidUUID,
12
+ getUUIDVersion,
11
13
  } from "./infrastructure/utils/UUIDUtils";
12
14
  export type { UUID } from "./types/UUID";
15
+ export { UUIDVersion, UUID_CONSTANTS } from "./types/UUID";
@@ -7,6 +7,7 @@
7
7
 
8
8
  import * as Crypto from 'expo-crypto';
9
9
  import type { UUID } from '../../types/UUID';
10
+ import { UUID_CONSTANTS } from '../../types/UUID';
10
11
 
11
12
  /**
12
13
  * Generate a v4 UUID
@@ -23,6 +24,52 @@ import type { UUID } from '../../types/UUID';
23
24
  * ```
24
25
  */
25
26
  export const generateUUID = (): UUID => {
26
- return Crypto.randomUUID();
27
+ return Crypto.randomUUID() as UUID;
28
+ };
29
+
30
+ /**
31
+ * Validate UUID format
32
+ * Checks if a string is a valid v4 UUID
33
+ *
34
+ * @param value - The value to validate
35
+ * @returns True if the value is a valid v4 UUID
36
+ *
37
+ * @example
38
+ * ```typescript
39
+ * import { isValidUUID } from '@umituz/react-native-uuid';
40
+ *
41
+ * isValidUUID('550e8400-e29b-41d4-a716-446655440000'); // true
42
+ * isValidUUID('invalid-uuid'); // false
43
+ * ```
44
+ */
45
+ export const isValidUUID = (value: string): value is UUID => {
46
+ return UUID_CONSTANTS.PATTERN.test(value);
47
+ };
48
+
49
+ /**
50
+ * Get version from UUID string
51
+ * Returns the UUID version number (1-5) or null for NIL/invalid
52
+ *
53
+ * @param value - The UUID string
54
+ * @returns UUID version number or null
55
+ *
56
+ * @example
57
+ * ```typescript
58
+ * import { getUUIDVersion } from '@umituz/react-native-uuid';
59
+ *
60
+ * getUUIDVersion('550e8400-e29b-41d4-a716-446655440000'); // 4
61
+ * getUUIDVersion('00000000-0000-0000-0000-000000000000'); // 0 (NIL)
62
+ * getUUIDVersion('invalid'); // null
63
+ * ```
64
+ */
65
+ export const getUUIDVersion = (value: string): number | null => {
66
+ if (value === UUID_CONSTANTS.NIL) {
67
+ return 0;
68
+ }
69
+
70
+ const versionChar = value.charAt(14);
71
+ const version = parseInt(versionChar, 10);
72
+
73
+ return (version >= 1 && version <= 5) ? version : null;
27
74
  };
28
75
 
package/src/types/UUID.ts CHANGED
@@ -3,7 +3,34 @@
3
3
  */
4
4
 
5
5
  /**
6
- * UUID v4 string type
6
+ * UUID brand type for type safety
7
7
  * Format: xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
8
8
  */
9
- export type UUID = string;
9
+ export type UUID = string & { readonly __brand: unique symbol };
10
+
11
+ /**
12
+ * UUID Version enum
13
+ */
14
+ export enum UUIDVersion {
15
+ NIL = 'nil',
16
+ V1 = 'v1',
17
+ V2 = 'v2',
18
+ V3 = 'v3',
19
+ V4 = 'v4',
20
+ V5 = 'v5',
21
+ }
22
+
23
+ /**
24
+ * UUID constants
25
+ */
26
+ export const UUID_CONSTANTS = {
27
+ /**
28
+ * NIL UUID (00000000-0000-0000-0000-000000000000)
29
+ */
30
+ NIL: '00000000-0000-0000-0000-000000000000' as UUID,
31
+
32
+ /**
33
+ * UUID v4 regex pattern
34
+ */
35
+ PATTERN: /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i,
36
+ } as const;