0.2.1 • Published 5 years ago

ob-map v0.2.1

Weekly downloads
10
License
MIT
Repository
github
Last release
5 years ago

ob-map

npm Build Status npm

Simple object mapper for creating flat javascript objects.

Description

Maps an object to a flat structure. Map object keys from one key to another with the help of a mapping object. Supports mapping of a nested object to a flat structure.

Installation

npm

npm install --save ob-map

yarn

yarn add ob-map

Usage

Flat Structure

import { map } from 'ob-map';

map(
  {
    a: 1,
    b: 2
  },
  {
    y: 'a',
    z: 'b'
  }
);
// => {y: 1, z: 2}

Nested Structure

import { map } from 'ob-map';

const obj = {
  a: {
    b: {
      c: 1
    }
  },
  d: {
    e: 2
  }
};

const mapping = {
  x: 'a.b.c',
  y: 'd.e'
};

map(obj, mapping);
// => {x: 1, y: 2}

Bulk Map

import { bulkMap } from 'ob-map';

const arr = [
  {
    a: 1,
    b: 2,
    c: 3
  },
  {
    a: 3,
    b: 4,
    c: 5
  }
];

const mapping = {
  x: 'a',
  y: 'b',
  z: 'c'
};

bulkMap(arr, mapping);
// => [{ x: 1, y: 2, z: 3 },{ x: 3, y: 4, z: 5 }]
0.2.1

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago