Package Exports
- parakeet-mapper
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (parakeet-mapper) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Parakeet Mapper
Simple data converter
npm install --save parakeet-mapperFor more options see installation
Table of contents
Features
The main feature is a possibility to specify a set of rules to convert one type structure to another.
Additional features:
- It's small
- It just works
Installation
Install as dependency
npm install --save parakeet-mapper
# or
yarn add parakeet-mapperImport and use
ES
import { mapTypes, mapFactory } from 'parakeet-mapper'CommonJS
const { mapTypes, , mapFactory } = require('parakeet-mapper');Script tag
<script src="https://unpkg.com/parakeet-mapper"></script>
...
<script>
// global variable parakeetMapper
const { mapTypes, mapFactory } = parakeetMapper;
</script>Simple example
Let's say you got some object from API:
const podguznikFromServer = {
title: 'Podguznik',
productId: 54213,
url: '/podguznik-54213',
image: 'tygjhjkqw89786dtsugyh',
compressedImage: 'bjghftryutyiguhkjhgjh',
isFavorite: true,
rating: 4.5,
ordersQuantity: 83,
fullPrice: 1500,
sellPrice: 1300
};and you want to make it suitable for your needs. It is very simple, just describe some rules and you are ready to go! (NO WAY!)
let result = mapTypes(podguznikFromServer, {
title: true,
url: true,
isFavorite: true,
fullPrice: true,
purchasePrice: 'sellPrice',
id: 'productId',
images: v => [v.image],
ratingInfo: v => ({
ordersQuantity: v.ordersQuantity,
rating: String(v.rating)
})
});Resulted object will be:
{
fullPrice: 1500,
id: 54213,
images: ["tygjhjkqw89786dtsugyh"],
isFavorite: true,
purchasePrice: 1300,
ratingInfo: [object Object] {
ordersQuantity: 83,
rating: "4.5"
},
title: "Podguznik",
url: "/podguznik-54213"
}Now let's take apart this example.
Set of rules are represented as an object, where keys - are resulted keys, and values - well, the RULES themselves.
Rules can be one of the following types: boolean, string, function.
boolean rule tells the mapper to just forward this field to the resulted object. Can be only true, because false is equals to not specifying this field at all and it will be skipped.
string rule tells the mapper to use different name for this field. Would be very handy if you just need to rename some fields in object.
And last, but not least - function rule. The most advanced and technologically progressive rule of all time (it's realy not). It does only one thing - returns a field value.
API
mapTypes params
| argument | type | description |
|---|---|---|
| input | Object | Input object to modify |
| FieldMap | Object | Set of rules for a mapper |
function rule params
| argument | type | description |
|---|---|---|
| inputObject | Object | Input object |
Factory mode
new in v1.1.0
It's also possible to create mappers using mapFactory:
import { mapFactory } from 'parakeet-mapper';
const productMapper = mapFactory({
title: true,
url: true,
isFavorite: true,
fullPrice: true,
purchasePrice: 'sellPrice',
id: 'productId',
images: v => [v.image],
ratingInfo: v => ({
ordersQuantity: v.ordersQuantity,
rating: String(v.rating)
})
});const result = productMapper(podguznikFromServer);
// results in:
{
fullPrice: 1500,
id: 54213,
images: ["tygjhjkqw89786dtsugyh"],
isFavorite: true,
purchasePrice: 1300,
ratingInfo: [object Object] {
ordersQuantity: 83,
rating: "4.5"
},
title: "Podguznik",
url: "/podguznik-54213"
}