remap-js
TypeScript icon, indicating that this package has built-in type declarations

0.1.0 • Public • Published

Remap JS

Give your objects a different shape

Basic usage

Remap an object by passing its structure as a mapping, and its values is a dot-notation string path, or a function.

 
const remap = require('remap').remap;
 
const source = {
  id: 'abc123',
  fname: 'John',
  lname: 'Doe',
  age: 20,
  cellNumber: '555-5555',
  homeNumber: '555-5556',
  very: { deep: { path: 'foo' } },
};
 
remap({
  id: '', // no change
  firstName: 'fname', // change key from fname to firstName
  lastName: 'lname',
  ageDescription: (obj) => obj.age + ' years old',
  phone: {
    cell: 'cellNumber',
    home: 'homeNumber',
  },
  noSuchKey: 'bar',
  flattenedValue: 'very.deep.path',
}, source);
 

Output:

 
{
  id: 'abc123',
  firstName: 'John',
  lastName: 'Doe',
  ageDescription: '20 years old',
  phone: {
    cell: '555-5555',
    home: '555-5556'
  },
  noSuchKey: undefined,
  flattenedValue: 'foo'
}
 

Advanced Remapping

There are functions for common operations

 
const remap = require('remap-js').remap;
const remapArray = require('remap-js').remapArray;
const remapFrom = require('remap-js').remapFrom;
const getOr = require('remap-js').getOr;
 
const source = {
  friend: {
    list: [
      { fname: 'Bob', lname: 'Builder' },
      { fname: 'Dora', lname: 'Explorer' },
    ],
  },
  myProfile: {
    profilePicture: 'picture.png',
    nickname: 'nicky',
  }
  myStatus: 'busy',
}
 
remap({
  friends: remapArray('friend.list', {
    id: (item, index) => `${item.firstName.toLowerCase()}-${index}`,
    firstName: 'fname',
    lastName: 'lname',
  }),
  profile: remapFrom('myProfile', {
    avatar: 'profilePicture',
    alias: 'nickname'
  }),
  status: getOr('myStatus', 'available'),
  messages: getOr('myMessages', [])
}, source);
 

Output

 
{
  friends: [
    { id: 'bob-1', firstName: 'Bob', lastName: 'Builder' },
    { id: 'dora-2', firstName: 'Dora', lastName: 'Explorer' }
  ],
  profile: {
    avatar: 'picture.png',
    alias: 'nicky'
  },
  status: 'available',
  messages: []
}
 

Package Sidebar

Install

npm i remap-js

Weekly Downloads

0

Version

0.1.0

License

MIT

Last publish

Collaborators

  • alanthai