Parakeet Mapper
Simple data conversion library
npm install --save parakeet-mapper
For more options see installation
What is this?
It's a small collection of utility functions and types that help with mapping (transforming) data.
You can find it useful if:
- You work with complex data objects and tired of it
- You need to quickly and efficiently transform objects or tuples regularly
- Your data models need additional functionality
- You need a type-safe way of initializing a class from an object
- You need to convert data types back and forth multiple times
- You don't like your backend's API and
GraphQL
is not an option 😁
Installation
Install as dependency
npm install --save parakeet-mapper# or yarn add parakeet-mapper
Import and use
ES
CommonJS
const mapTypes mapFactory = ;
Script tag
...
API
parakeet-mapper
exposes several helpers to deal with type conversions.
All of them use the TypeMap interface to communicate.
name | overloads | description |
---|---|---|
mapFactory | 3 | Accepts a TypeMap. Returns a function that accepts one input and returns an output converted using rules defined in the TypeMap. |
mapTypes | 3 | Same as mapFactory, but instead of returning a function, accepts input as its first argument and returns an output right away. |
Convertable | 2 | Class mixin. Allows creation of convertable classes. |
TypeMap
object
It is a set of rules that define how the output type is made from the input type.\
The rules are simple:\
- Each key corresponds to a key in the output.
- Each value tells what to assign to that key from the input.
true
= the value is assigned from the same key in the input.- A string = the value is assigned from this string key in the input.
- An object = the value is assigned from this object's first key in the input and is processed using the value as converter.
- An array of a single element = the first element in the array is used as converter for the input value by the output key.
- A function = the value is mapped using this function from the input.
Examples
; ; /* output */
object/array shorthand with the same key
new in
v2.1.2
It's not necessary to specify the correct key in the conversion object:
; ; /* output */
It only works if the input has the same property key as the output.
Can also be written using the array (tuple) syntax:
; ; /* output */
mapFactory
function
A factory function that produces a converter from a TypeMap:
; ; ; /* { transferred: 'foo', outputRenamed: 'bar', convertedNumber: 42, // Number('42') mappedSum: 4, mappedPlusConverted: 46 // 4 + 42 // notice the absence of `ommited` property} */
Overloads
This function has 3 overloads, all of which are needed for type safety and type inference (TypeScript).
There are basically 3 typed use-cases of using this function (hence 3 overloads):
- Input and output types are known, and TypeMap needs to convert them precisely.declare ;;// output is OutputType now
- Input type is known, output needs to be inferred from the TypeMap.
The first call without arguments is a noop. Reasons: first, second.declare ; // The empty braces are here due to TS issues.; - Input and output types are known, but the output type needs to be modified slightly.
The first call without arguments is a noop. Reasons: first, second.declare ; // The empty braces are here due to TS issues.;
mapTypes
function
Basically, a mapFactory, called in-place.
First Argument | Second Argument |
---|---|
The input object | Corresponding TypeMap |
; ; /* { transferred: 'foo', outputRenamed: 'bar', convertedNumber: 42, // Number('42') mappedSum: 4, mappedPlusConverted: 46 // 4 + 42 // notice the absence of `ommited` property} */ // Same as// const output = mapFactory(TypeMap)(input);
Overloads
There are 3. All are semantically the same as the overloads of mapFactory, including the noop call:
/* 1 */ mapTypesinput, TypeMap;/* 2 */ mapTypesinput, TypeMap;/* 3 */ mapTypesinput, TypeMap;
Wait
function
new inv2.1
A complementary function to mapFactory
that helps to flatten out promises
It produces a converter that returns a flat promise from a converter that returns an object with promises.
In a typical situation, when some convertations are asyncronous, you'd end up with this:
; // Imagine that this is requesting something from an API and returns a promise; ; ; ;// Result:/* { a: Promise<['a']>, b: 42, c: Promise<'b'>} */// Not very comfortable to await every single value after this
Now with wait
:
; ; ;// Result:/* Promise<{ a: ['a'], b: 42, c: 'b'}> */// Much more useful now
flattenPromises
function
new inv2.1
Internally used in wait
, flattens top-level promises in an object:
; ; ;// Result/* Promise<{ a: ['a'], b: 42, c: 'b'}> */
Convertable
function
new inv2.0
Allows to create classes from converters.
This makes possible adding extra functionality, including reverse convertations.
; ; ; console.logoutput; /*> Output { transferred: 'foo', outputRenamed: 'bar', convertedNumber: 42, mappedSum: 4, mappedPlusConverted: 46 }*/
Accepts a function that returns a converter as its only argument.
Returns a Convertable class with all the required functionality.
Can also infer arguments from its converter factory:
; ; ;console.logoutputWithFoo; /*> Output { zoo: 'foo', zar: 'bar' }*/ ;console.logoutputWithoutFoo; /*> Output { zar: 'foo' }*/
And accept a reverse converter:
; ; ; ;console.logoutput; /*> Output { zoo: 'foo', zar: 'bar' }*/ // Convert back to input type;console.lognewInput; /*> { foo: 'foo', bar: 'bar' }*/
Convertable class
constructor
Accepts an input as its first argument and converter factory parameters as other spread arguments.
toInput
Available only if reverse converter was passed into the Convertable.
Accepts a spread of reverse converter arguments
Convertable.createConverter
static
A converter factory, passed to the Convertable.
Convertable.reverseConverter
static
A reverse converter factory, passed to the Convertable.