Mapper

About

Mapper is very simple JavaScript library which is used to get/set data from forms or any other container. It's entirely written in TypeScript, without any external dependencies. Primary motivation for building tool like this was:


How to use

This page shows only examples, to check out full documentation, please visit project's GitHub page.


Examples

Example 1 - simple mapping
Example 2 - nested mapping
Example 3 - simple array mapping

Example 4 - complex array mapping

Each input has map property like map="rolesAsJsonMap[userId=1].department". This will map input value (if checked) to element which matches provided userId condition.

Department 1 Department 2 Department 3
User 1
User 2
User 3
Example 5 - complex array mapping

Each input element in first row has map property in map="rolesAsJsonMap[roleId=1].userId[]" form. This will add input value (if checked) to element which matches role=1 condition.

User 1 User 2 User 3
Role 1
Role 2
Role 3
Example 6 - complex array mapping - finding array element by index

Mapping elements by array index in case finding elements by key is not possible

Example 7

Mapping elements to array by order of appearance

Example 8 - Using data-attribute instead of value

Components like datepickers might not use value attribute to store value in required format. Easiest way is to store data in wanted format to data-value attribute. Name of this attribute can be changed.

To always set/get date in ISO format, all that needs to be done in following example is to set data-value="" attribute and set event onchange="this.setAttribute('data-value', this.valueAsDate.toISOString().substr(0, 10))".

Example 9 - Mapping raw JSON data

Additional parsers can be specified (please see project page for more info) and used to map data. Json parser sets JSON data as string and gets it back as JSON.

Example 10 - Fetching forms as FormData