* Generate a deduplication key for a value. * - Primitives: use the value directly * - Vertices/Edges: use their id property * - Arrays of primitives: efficient string concatenation * - Complex arrays/objects: JSON serialization
(value: unknown)
| 3886 | * - Complex arrays/objects: JSON serialization |
| 3887 | */ |
| 3888 | #getDeduplicationKey(value: unknown): unknown { |
| 3889 | // Primitives can be used directly as Set keys |
| 3890 | if (value === null || typeof value !== "object") { |
| 3891 | return value; |
| 3892 | } |
| 3893 | |
| 3894 | // Vertices and Edges have an 'id' property we can use |
| 3895 | if (value instanceof Vertex || value instanceof Edge) { |
| 3896 | return value.id; |
| 3897 | } |
| 3898 | |
| 3899 | // For arrays, check if all elements are primitives for efficient key generation |
| 3900 | if (Array.isArray(value)) { |
| 3901 | if (value.length === 0) { |
| 3902 | return "\0[]"; |
| 3903 | } |
| 3904 | // Check if all elements are primitives (string, number, boolean, null, undefined) |
| 3905 | const allPrimitives = value.every((el) => el === null || typeof el !== "object"); |
| 3906 | if (allPrimitives) { |
| 3907 | // Use type-prefixed join for primitives to avoid collisions |
| 3908 | // e.g., [1, "1"] vs ["1", 1] should have different keys |
| 3909 | return "\0[" + value.map((el) => typeof el + ":" + String(el)).join("\0") + "]"; |
| 3910 | } |
| 3911 | } |
| 3912 | |
| 3913 | // Fall back to JSON for complex objects/arrays |
| 3914 | return JSON.stringify(value); |
| 3915 | } |
| 3916 | |
| 3917 | public override clone(partial?: Partial<DedupStepConfig>) { |
| 3918 | return new DedupStep({ |