This page contains code samples, configuration options and general idea behind mapper. To see live exmaples of mapper in action please go to Mapper GitHub pages.
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:
- using .Net Core and Razor pages is very simple and fast way to build apps. In order to communicate with APIs, I needed simple way to map data to and from forms.
- didn't want to use libraries which introduce observables as I only wanted to map data on specific events (API data received, user clicked save, ...)
- wanted to have library which is written using vanilla typescript
- didn't want to introduce other libraries which by themselves have other dependencies which by themselves have other ...
- to modify it to automatically work with other components I usually use in projects like bootstrap datepicker, select2, ...
- to have my first public project on GitHub
Before doing anythin, either compile Typescript files in src
folder or include mapper.js
found in dist
folder. Mapper will soon be available on npm.
HTML
<form id="myForm">
<input type="text" map="username" value="[email protected]" />
<input type="text" map="password" value="1234567" />
</form>
To get data from this form:
// using Mapper instance for single container element
const containerElement = document.getElementById("myForm");
const mapper = new Mapper(containerElement); //reusable instance
const data = mapper.getData();
or
// using Mapper static methods
const containerElement = document.getElementById("myForm");
const data = Mapper.getData(containerElement);
data
object will contain data found on elements with map
attribute. Mapper works with any container element (HtmlDivElement
, ...). Data returned would be:
{
"username": "[email protected]",
"password": "1234567"
}
To set data to elements in specified container element:
const mapper = new Mapper();
const containerElement = document.getElementById("myForm");
const dataToSet = {
"username": "[email protected]",
"password": "1234567"
}
const data = mapper.setData(containerElement, dataToSet);
HTML
<form id="myForm">
<input type="text" map="userData.firstName" value="John" />
<input type="number" map="userData.age" value="42" />
<input type="checkbox" value="role1" map="contactData.userRoles[]" />
<input type="checkbox" value="role2" map="contactData.userRoles[]" />
<input type="text" map="contactData.phone[0]" value="zzz-xxx1" />
<input type="text" map="contactData.phone[1]" value="zzz-xxx2" />
<input type="text" map="claims[claimType=role].value" value="Type1" />
<input type="text" map="claims[claimType=age].value" value="23" />
</form>
map="userData.firstName"
- in resulting object find propertyuserData
and assign input value to propertyfirstName
.map="userData.age"
- in resulting object find propertyuserData
and assign numeric input value to propertyage
map="contactData.userRoles[]"
- add checkbox value to arraycontactData.userRoles
. Value is added only if checkbox is checked. Multiple elements with same map property can be added to build arraymap="contactData.phone[0]"
in array atcontactData.phone
, add value at index 0map="contactData.phone[1]"
in array atcontactData.phone
, add value at index 1map="claims[claimType=role].value"
in array assigned to propertyclaims
, find object with keyclaimType
equal to stringrole
. In matching object (create it if it doesn't exist), assign value to propertyvalue
Procedure is same as above, just instead of assigning values to object - values are assigned to DomElements.
- input:
containerElement
- any HTMLElement which containes other elements - returns: Mapper instance
In case of simple forms, this method will add required map attributes to all elements inside containerElement
. Map attributes will be defined as element's name. E.g.: <input name="FirstName" />
will after running this method look like <input name="FirstName" map='firstName'/>
.
- input:
containerElement
- returns: Object containing mapped data
Instead of creating Mapper instance to get data, this method can be used. It will make new Mapper instance and return mapped data.
- input:
containerElement
- input:
dataToMap
- object containing data to map
Instead of creating Mapper instance to set data, this method can be used. It will make new Mapper instance and set data contained in dataToMap
object.
Check out my LinkedIn profile