assignUUID()
Implementation of assignUUID.
Call Signature
assignUUID<
T>(array):T&object[]
Defined in: utils/array/assignUUID.ts:48
Assigns a UUID to each element in the array.
- If an object already has a string
idproperty, thatidis preserved. - If an object lacks an
id, the object is spread and a newidis 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 }(ifTis an object),- or
{ id: string; value: T }(ifTis a primitive).
Examples
// 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' } ]// 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: utils/array/assignUUID.ts:51
Assigns a UUID to each element in the array.
- If an object already has a string
idproperty, thatidis preserved. - If an object lacks an
id, the object is spread and a newidis 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 }(ifTis an object),- or
{ id: string; value: T }(ifTis a primitive).
Examples
// 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' } ]// 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 } ]