Skip to content

Commit cdf98d3

Browse files
authored
Merge pull request #28 from TillaTheHun0/master
Format README.md
2 parents 40e2681 + 7c7d053 commit cdf98d3

1 file changed

Lines changed: 70 additions & 57 deletions

File tree

README.md

Lines changed: 70 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
##About##
66

7-
This module provides a way to copy properties from one object to another based
8-
on a separate object which defines how the properties should be mapped.
7+
Copy properties from one `Object` to another based
8+
on a separate `Object`, which defines how the properties should be mapped.
99

1010
##Installation##
1111

@@ -15,36 +15,36 @@ $ npm install --save object-mapper
1515

1616
##Usage##
1717

18-
The mapping object is a simple object, where the `key` is the **source** and the `value` is the **destination**.
18+
A mapping object `key` is the **source** `key` and the `value` is the `key` on the **destination** object the `value` is mapped to.
1919

2020
###Source###
2121

22-
The source can be specified as a simple string like:
22+
The source `key` can be specified as a simple string:
2323

2424
```javascript
2525
{
26-
"foo": "bar"
26+
"foo": "bar" //map src.foo to dest.bar
2727
}
2828
```
2929

30-
You may specify properties deep within the source object to be copied to
31-
properties deep within the destination object by using dot notation in the
32-
mapping like:
30+
You may specify properties deep within the source `Object` to be copied to
31+
properties deep within the destination `Object` by using dot notation in the
32+
mapping `key`:
3333

3434
```javascript
3535
{
36-
"foo": "bar.baz"
37-
, "bar.foo": "baz"
36+
"foo": "bar.baz", //map src.foo to dest.bar.baz
37+
"bar.foo": "baz" //map src.bar.foo to dest.baz
3838
}
3939
```
4040

41-
You may also specify array lookups within the source object to be copied to properties deep within the destination object by using `[]` notation in the mapping link:
41+
You may also specify `Array` lookups within the source `Object` to be copied to properties deep within the destination object by using `[]` notation in the mapping:
4242

4343
```javascript
4444
{
45-
"[].foo": "bar[]"
46-
, "foo[].bar": "[]"
47-
, "foo[0].bar": "baz"
45+
"[].foo": "bar[]",
46+
"foo[].bar": "[]",
47+
"foo[0].bar": "baz"
4848
}
4949
```
5050

@@ -57,67 +57,80 @@ You may specify the destination as:
5757

5858
####String####
5959

60-
When using string as destination you shall use as described above.
60+
When using a `String` as the destination, use the method described above.
6161

62-
In order to utilize a source field more than once, utilize the key-transform syntax in the mapping link:
62+
To utilize a source field more than once, utilize the key-transform syntax in the mapping link:
6363

6464
```javascript
65-
{
65+
var objectMapper = require('object-mapper');
66+
67+
var map = {
6668
"foo": [
6769
{
6870
key: "foo",
69-
transform: function (value) {
71+
transform: function (value) {
7072
return val + "_foo";
7173
}
7274
},
7375
{
7476
key: "baz",
7577
transform: function (value) {
76-
return val + "_baz"
78+
return val + "_baz";
7779
}
7880
}
7981
],
8082
"bar": "bar"
81-
}
83+
};
84+
85+
var src = {
86+
foo: 'blah',
87+
bar: 'something'
88+
};
89+
90+
var dest = objectMapper(src, map);
91+
92+
// dest.foo: 'blah_foo'
93+
// dest.baz: 'blah_baz'
94+
// dest.bar: 'something'
8295
```
8396

8497
####Object####
8598

86-
When using object as destination you need can use the following object:
99+
Using an `Object` as the destination:
87100

88101
```javascript
89102
{
90-
"key": (String)
91-
, "transform": (Function())
92-
, "default": (Function()|String|Number)
103+
"key": (String),
104+
"transform": (Function()),
105+
"default": (Function()|String|Number)
93106
}
94107
```
95108

96109
#####Methods#####
97110

98111
###### transform(sourceValue, sourceObject, destinationObject, destinationKey);
99112

100-
This function let you handle the **sourceValue** as you need;
113+
Specify the mapping of a **sourceValue** as you need;
101114

102115
###### default(sourceObject, sourceKey, destinationObject, destinationKey);
103116

104-
This function let you return a _default_ value when the **sourceValue** is `undefined` or `null`.
117+
Specify a _default_ return value when the **sourceValue** is `undefined` or `null`.
105118

106119
####Array####
107120

108-
When using arrays as destination you can pass a string, object or another array (shorthand for object):
121+
When using an `Array` as the destination you can pass a `String`, an `Object` or another `Array` (shorthand for `Object`):
109122

110123
```javascript
111124
{
112-
"foo": ["bar", "baz"]
113-
, "bar": [{
125+
"foo": ["bar", "baz"],
126+
"bar": [{
114127
"key": "foo"
115-
}]
116-
, "baz": [["bar", null, "foo"]]
128+
}],
129+
"baz": [["bar", null, "foo"]]
117130
}
118131
```
119132

120-
If you want to append items to an existing array, include a '+' after the []
133+
If you want to append items to an existing `Array`, append a `+` after the `[]`
121134
```javascript
122135
{
123136
"sourceArray[]":{
@@ -132,21 +145,21 @@ If you want to append items to an existing array, include a '+' after the []
132145
// Results in the destination array appending the source values
133146
{
134147
"destination":[
135-
{/*Results from mapping function applied to sourceArray */},
136-
{/*Results from mapping function applied to otherSourceArray */},
148+
{/*Results from the mapping function applied to sourceArray */},
149+
{/*Results from the mapping function applied to otherSourceArray */},
137150
]
138151
}
139152
```
140153

141-
The array shorthand for object is defined like:
154+
The `Array` shorthand for an `Object`:
142155

143156
```javascript
144157
[(Key(String))), (Transform(Function())), (Default(String|Number|Function()))]
145158
```
146159

147160
###Null Values###
148161

149-
By default any source object null value is not mapped. If you want to allow this you may do so explicitly by including the post fix operator '?' to any destination key.
162+
By default `null` values on the source `Object` is not mapped. You can override this by including the post fix operator '?' to any destination `key`.
150163

151164
```javascript
152165
var original = {
@@ -180,7 +193,7 @@ This function is also exported directly from `require('object-mapper')` (ie: `va
180193

181194
### .getKeyValue(sourceObject, key);
182195

183-
Get the key value within **sourceObject**, going deep within the object if necessary.
196+
Get the `key` value within **sourceObject**, going deep within the object if necessary.
184197
This method is used internally but is exposed because it may be of use elsewhere
185198
with other projects.
186199

@@ -189,7 +202,7 @@ with other projects.
189202

190203
### .setKeyValue(destinationObject, key, value);
191204

192-
Set the key value within **destinationObject**, going deep within the object if necessary.This
205+
Set the `key` value within **destinationObject**, going deep within the object if necessary.This
193206
method is used internally but is exposed because it may be of use elsewhere with
194207
other projects.
195208

@@ -202,31 +215,31 @@ other projects.
202215
```javascript
203216
var objectMapper = require('object-mapper');
204217

205-
var obj = {
206-
"sku" : "12345"
207-
, "upc" : "99999912345X"
208-
, "title" : "Test Item"
209-
, "description" : "Description of test item"
210-
, "length" : 5
211-
, "width" : 2
212-
, "height" : 8
213-
, "inventory" : {
218+
var src = {
219+
"sku" : "12345",
220+
"upc" : "99999912345X",
221+
"title" : "Test Item",
222+
"description" : "Description of test item",
223+
"length" : 5,
224+
"width" : 2,
225+
"height" : 8,
226+
"inventory" : {
214227
"onHandQty" : 12
215228
}
216229
};
217230

218231
var map = {
219-
"sku": "Envelope.Request.Item.SKU"
220-
, "upc": "Envelope.Request.Item.UPC"
221-
, "title": "Envelope.Request.Item.ShortTitle"
222-
, "description": "Envelope.Request.Item.ShortDescription"
223-
, "length": "Envelope.Request.Item.Dimensions.Length"
224-
, "width": "Envelope.Request.Item.Dimensions.Width"
225-
, "height": "Envelope.Request.Item.Dimensions.Height"
226-
, "inventory.onHandQty": "Envelope.Request.Item.Inventory"
232+
"sku": "Envelope.Request.Item.SKU",
233+
"upc": "Envelope.Request.Item.UPC",
234+
"title": "Envelope.Request.Item.ShortTitle",
235+
"description": "Envelope.Request.Item.ShortDescription",
236+
"length": "Envelope.Request.Item.Dimensions.Length",
237+
"width": "Envelope.Request.Item.Dimensions.Width",
238+
"height": "Envelope.Request.Item.Dimensions.Height",
239+
"inventory.onHandQty": "Envelope.Request.Item.Inventory"
227240
};
228241

229-
var result = objectMapper(obj, map);
242+
var dest = objectMapper(obj, map);
230243

231244
/*
232245
{
@@ -252,7 +265,7 @@ var result = objectMapper(obj, map);
252265

253266
##Use case##
254267

255-
I use **object-mapper's** `merge()` method to map values from records
268+
I use the **object-mapper's** `merge()` method to map values from records
256269
returned from a database into horribly complex objects that will be eventually
257270
turned in to XML.
258271

0 commit comments

Comments
 (0)