-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
76 lines (58 loc) · 2.08 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
(function () {
var Functions = require('./lib/Functions'),
fieldmap = Functions.fieldmap,
DynamicIndexMapper = require('./lib/mappers/DynamicIndexMapper'),
FixedIndexMapper = require('./lib/mappers/FixedIndexMapper');
var Razorback = function (params) {
var seperator = '_';
var defaultToCustomMappings = {};
var customToDefaultMappings = {};
var mappers;
var mapperType;
var exclusions;
function applyCustomMappingsToFlatObject(obj, mappings) {
fieldmap(function (source, k) {
if (mappings[k]) {
var temp = source;
delete source;
obj[mappings[k]] = temp;
}
}, obj);
}
if (!params) {
params = {}
}
if (params.mappings) {
params.mappings.map(function (mapping) {
defaultToCustomMappings[mapping.from] = mapping.to;
customToDefaultMappings[mapping.to] = mapping.from;
});
}
if (params.exclusions) {
exclusions = params.exclusions;
}
mapperType = params.useMapper ? params.useMapper : 'dynamicIndex';
if (params.seperator) {
seperator = params.seperator
} else {
seperator = '_';
}
mappers = {
dynamicIndex: new DynamicIndexMapper(seperator, exclusions),
fixedIndex: new FixedIndexMapper(seperator)
};
Razorback.prototype.denormalize = function (obj) {
var mapper = mappers[mapperType];
var out = mapper.seedValue();
mapper.flatten(obj, out, '');
applyCustomMappingsToFlatObject(out, defaultToCustomMappings);
return out;
};
Razorback.prototype.normalize = function (obj) {
applyCustomMappingsToFlatObject(obj, customToDefaultMappings);
var mapper = mappers[mapperType];
return mapper.unflatten(obj);
};
};
module.exports = Razorback;
})();