-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
52 lines (47 loc) · 1.29 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
/**
* copy object values to another object with a key mapping based on a schema.
* A new Object is created if target is not specified
* If value is already on the target object for a given key, it will be overwritten.
* Only keys in the schema will be mapped.
* @example
*
* var schema = {
* prop: '_prop',
* otherProp: '_otherProp',
* mayExist: '_mayExist'
* };
*
* var object = {
* prop: 'value',
* otherProp: 'otherValue',
* notInSchema: 'error'
* };
*
* var output = mapKeys(object, schema);
*
* assert.equal(output._prop, 'value');
* assert.equal(output._otherProp, 'otherValue');
* assert(output.notInSchema === undefined);
* assert(output.prop === undefined);
* assert(output.otherProp === undefined);
* assert(output.mayExist === undefined);
* assert(output._mayExist === undefined);
*
* @param {Object} source - the source object
* @param {Object} schema - the schema to apply
* @param {Object} [target] - a target object
* @returns {Object} the output object
*/
function mapKeys(source, schema, target) {
var obj = target || {};
var schemaKeys = Object.keys(schema);
schemaKeys.forEach(function (skey) {
if (source[skey]) {
obj[schema[skey]] = source[skey];
}
});
return obj;
}
module.exports = {
mapKeys: mapKeys
};