Remap Object Fields
Transforms an object's structure by mapping properties to new names according to a provided field mapping.
Import
import { remapFields } from 'nhb-toolbox';
import { remapObjectFields } from 'nhb-toolbox';
Function Signature
function remapFields<Source extends GenericObject, Target extends Record<string, keyof Source>>(
source: Source,
fieldMap: Target
): { [K in keyof Target]: Source[Target[K]] }
Usage Examples
Basic Field Renaming
const user = {
userId: 123,
fullName: 'John Doe',
emailAddress: 'john@example.com'
};
const mappedUser = remapFields(user, {
id: 'userId',
name: 'fullName',
email: 'emailAddress'
});
// Returns { id: 123, name: 'John Doe', email: 'john@example.com' }
Partial Remapping
const product = {
prodId: 'P100',
productName: 'Laptop',
price: 999,
inStock: true
};
const inventoryItem = remapFields(product, {
id: 'prodId',
name: 'productName'
});
// Returns { id: 'P100', name: 'Laptop' }
API Reference
Type Parameters
Name | Description |
---|---|
Source | Type of source object |
Target | Object type mapping target keys to source keys |
Parameters
Name | Type | Description |
---|---|---|
source | Source | Source object to remap |
fieldMap | Target | Mapping of target keys to source keys |
Returns
An object with the same keys as fieldMap
and corresponding value types from source
Key Features
- Type Safe: Preserves value types from source object
- Selective Mapping: Only includes fields specified in fieldMap
- Non-Destructive: Creates new object without modifying original
- Flexible: Works with partial mappings
- Simple Types: No complex generic type parameters needed
Aliases
This function is also exported as:
remapObjectFields
Limitations
- Shallow Only: Doesn't handle nested object remapping
- No Transformation: Only renames fields, doesn't transform values
- No Defaults: Missing fields become undefined, optional field's type will be
type
orundefined
Recommended Use Cases
- API response reshaping
- Data normalization
- Interface adaptation
- Field name standardization
- Data transfer object (DTO) conversion