A tool for mapping a JavaScript object from a data schema to another data schema
Install with NPM
npm install --save jul-data-mapper
Use this tool to map a JavaScript object from a data schema to another data schema
Use with parsed JSON responses or any native JavaScript object structures.
Code example:
'use strict';
const {mapper} = require('jul-data-mapper');
const oSrc = {
server: 'express',
items: [
{id: 101, name: 'Ana'},
{id: 102, name: 'Bell'},
{id: 103, name: 'Kevin'}
],
pref: {perPage: 25, filter: false},
grid: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
};
const oDest = {version: '1.0.0'};
const oMap = {
'server': 'result.source',
'items[$u].id': 'result.entries[$u].uid',
'items[$u].name': 'result.entries[$u].fullName',
'grid[$i][$j]': 'map[$i][$j].value',
pref: 'result.show'
};
// source -> destination
console.info(
mapper(oDest, oSrc, oMap)
);
// destination -> source
const oReverseMap = Object.fromEntries(Object.entries(oMap).map(aItem =>aItem.reverse()));
console.info(
mapper({}, oDest, oReverseMap)
);
// using a more compact form of mapping
const oCompactMap = {
'server': 'result.source',
'items[$u]': {
_mapTo: 'result.entries[$u]',
id: 'uid',
name: 'fullName'
},
'grid[$i][$j]': 'map[$i][$j].value',
pref: 'result.show'
};
console.info(
mapper({}, oSrc, oCompactMap, {prefixProp: '_mapTo'})
);
Version:
- 1.1.3
Author:
Source:
Converts an object to another using a namespace path mapping.
Isomorphic: works both in backends and in frontends via e.g. webpack
Source:
Compacts a mapping (key:value) object into a tree-like structure
oMap
Object
Key-value object to compact
Source:
Performs a mapping with a given object from a data schema to another data schema
oDest
Object
The destination object where the mapped values are applied over
oSrc
Object
The source object
oMap
Object
A key-value hash (mapping) between namespace paths in the source and those in the destination
oConfig
Object
Optional configuration object with any of the following options:
uint {RegExp|String}
- regular expression or string to match an array index placeholder e.g.'$n'
.
Defaults to /\$[a-z]/
prefixProp {String}
- name of a property of the mapping the will be used as a prefix when computing the destination namespace for the current siblings.
Defaults to '_mapToPrefix'
strict {Boolean}
- performs checkings of not overwriting descendant values that are already mapped.
Defaults to false
Source:
Licensed under GNU GPLv2 or later and under GNU LGPLv3 or later. See enclosed 'licenses' folder.
This utility is based on JUL, which is a set of tools for managing configuration trees.