Skip to content

A tool for mapping a JavaScript object from a data schema to another data schema

License

Notifications You must be signed in to change notification settings

zonebuilder/jul-data-mapper

Repository files navigation

jul-data-mapper

A tool for mapping a JavaScript object from a data schema to another data schema

Install

Install with NPM

npm install --save jul-data-mapper

Usage

mapper.js

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:

API

jul-data-mapper

Converts an object to another using a namespace path mapping.

Isomorphic: works both in backends and in frontends via e.g. webpack

Source:

Methods

compact

(inner) compact(oMap)→ {Object}

Compacts a mapping (key:value) object into a tree-like structure

Parameters:

oMapObject Key-value object to compact

Source:

mapper

(inner) mapper(oDest, oSrc, oMap, oConfigopt)→ {Object}

Performs a mapping with a given object from a data schema to another data schema

Parameters:

oDestObject The destination object where the mapped values are applied over

oSrcObject The source object

oMapObject A key-value hash (mapping) between namespace paths in the source and those in the destination

oConfigObject 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:

License

Licensed under GNU GPLv2 or later and under GNU LGPLv3 or later. See enclosed 'licenses' folder.

About

This utility is based on JUL, which is a set of tools for managing configuration trees.

Resources

About

A tool for mapping a JavaScript object from a data schema to another data schema

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages