1 | "use strict";
|
2 |
|
3 | function filter (template, obj) {
|
4 |
|
5 | if (typeof template === 'undefined') {
|
6 | return undefined;
|
7 | } else if ( typeof template === 'object' ){
|
8 | if (typeof obj === 'object') {
|
9 | var result = {};
|
10 | Object.keys(template).forEach( function (key) {
|
11 | var tmp = filter(template[key], obj[key]);
|
12 |
|
13 | if (typeof tmp !== 'undefined') {
|
14 | result[key] = tmp
|
15 | }
|
16 | });
|
17 | return result;
|
18 | }
|
19 | } else if ( typeof template === 'function' ) {
|
20 | return template(obj);
|
21 | } else {
|
22 | return obj;
|
23 | }
|
24 | }
|
25 |
|
26 | function merge (template, obj) {
|
27 |
|
28 | if (typeof template === 'undefined') {
|
29 | return undefined;
|
30 | } else if ( typeof template === 'object' ){
|
31 | if (typeof obj === 'object') {
|
32 | var result = {};
|
33 | Object.keys(template).forEach( function (key) {
|
34 | var ret = merge(template[key], obj[key]);
|
35 |
|
36 | if (typeof ret !== 'undefined') {
|
37 | result[key] = ret;
|
38 | } else if (typeof template[key] !== 'undefined') {
|
39 | result[key] = template[key];
|
40 | }
|
41 | });
|
42 | return result;
|
43 | }
|
44 | } else if ( typeof template === 'function' ) {
|
45 | return template(obj);
|
46 | } else {
|
47 | if (typeof obj === 'undefined') {
|
48 | return template;
|
49 | } else {
|
50 | return obj;
|
51 | }
|
52 | }
|
53 | }
|
54 |
|
55 | module.exports = filter;
|
56 | module.exports.merge = merge;
|