normalizeNonEmptyRecord
Normalizes a non‑empty record‑like value into a real non‑empty record or returns null.
A non‑empty record is defined as an object whose prototype is either Object.prototype or null, containing at least one own enumerable string key. This helper never throws and never mutates input. It returns a new object whose values have been normalized using the provided value‑normalizer.
Signature
function normalizeNonEmptyRecord<T>(
value: unknown,
normalizeValue: (v: unknown) => T | null,
): Record<string, T> | null
Parameters
| Name | Data type | Description |
|---|---|---|
| value | unknown |
The value to normalize. Must be a non‑empty plain‑object‑like value. |
| normalizeValue | (v: unknown) => T \| null |
A value‑normalizer applied to each property. If any value normalizes to null, the entire record is rejected. |
Returns
One of:
Record<string, T>: With normalized values.null: If the input cannot be interpreted as a non‑empty record.
Behavior
- Accepts objects whose prototype is
Object.prototypeornull. - Rejects arrays, functions, class instances, and exotic objects.
- Rejects empty objects.
- Rejects the entire record if any value fails normalization.
- Returns a new object.
- Never throws and never mutates input.
Examples
normalizeNonEmptyRecord({ a: 1 }, v => typeof v === 'number' ? v : null);
// → { a: 1 }
normalizeNonEmptyRecord({ a: "1" }, v => Number(v));
// → { a: 1 }
normalizeNonEmptyRecord({}, v => v);
// → null (empty)
normalizeNonEmptyRecord({ a: 1, b: "x" }, v => typeof v === 'number' ? v : null);
// → null (value "x" failed normalization)
normalizeNonEmptyRecord("not an object", v => v);
// → null
Notes
- This helper is stricter than normalizePlainObject because it enforces non‑emptiness.
- It is intentionally broader than isNonEmptyRecord because it allows value‑level normalization.
- Use this helper when you need a predictable, validated record before further processing.