Skip to content

assignUUID()

Implementation of assignUUID.

Call Signature

assignUUID<T>(array): T & object[]

Defined in: src/utils/arrayUtils.ts:129

Assigns a UUID to each element in the array.

  • If an object already has a string id property, that id is preserved.
  • If an object lacks an id, the object is spread and a new id is added.
  • For primitive values, each element is wrapped into { id: string; value: T }.

Note: When falling back (Next.js server side) to the Math.random–based UUID generator, the IDs are not cryptographically secure. This utility is intended for small arrays, loop indexes, UI‑keys, or other non‑critical identifiers. Avoid using it for large volumes of data or any security‑sensitive contexts.

Type Parameters

T

T extends object

The type of the array elements.

Parameters

array

T[]

The input array of items to which UUIDs will be assigned.

Returns

T & object[]

An array of the same length where each element is either:

  • T & { id: string } (if T is an object),
  • or { id: string; value: T } (if T is a primitive).

Examples

typescript
// Objects with and without existing IDs:
const objs = [{ id: 'abc', name: 'foo' }, { name: 'bar' }];
const withIds = assignUUID(objs);
// → [ { id: 'abc', name: 'foo' }, { id: '550e8400-e29b-41d4-a716-446655440000', name: 'bar' } ]
typescript
// Array of numbers:
const nums = [42, 7];
const wrappedNums = assignUUID(nums);
// → [ { id: '550e8400-e29b-41d4-a716-446655440001', value: 42 },
//     { id: '550e8400-e29b-41d4-a716-446655440002', value: 7 } ]

Call Signature

assignUUID<T>(array): object[]

Defined in: src/utils/arrayUtils.ts:132

Assigns a UUID to each element in the array.

  • If an object already has a string id property, that id is preserved.
  • If an object lacks an id, the object is spread and a new id is added.
  • For primitive values, each element is wrapped into { id: string; value: T }.

Note: When falling back (Next.js server side) to the Math.random–based UUID generator, the IDs are not cryptographically secure. This utility is intended for small arrays, loop indexes, UI‑keys, or other non‑critical identifiers. Avoid using it for large volumes of data or any security‑sensitive contexts.

Type Parameters

T

T

The type of the array elements.

Parameters

array

T[]

The input array of items to which UUIDs will be assigned.

Returns

object[]

An array of the same length where each element is either:

  • T & { id: string } (if T is an object),
  • or { id: string; value: T } (if T is a primitive).

Examples

typescript
// Objects with and without existing IDs:
const objs = [{ id: 'abc', name: 'foo' }, { name: 'bar' }];
const withIds = assignUUID(objs);
// → [ { id: 'abc', name: 'foo' }, { id: '550e8400-e29b-41d4-a716-446655440000', name: 'bar' } ]
typescript
// Array of numbers:
const nums = [42, 7];
const wrappedNums = assignUUID(nums);
// → [ { id: '550e8400-e29b-41d4-a716-446655440001', value: 42 },
//     { id: '550e8400-e29b-41d4-a716-446655440002', value: 7 } ]

Released under the MIT License.