1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | 'use strict';
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | function deepMergeAssignments(objectName, properties) {
|
17 | var assignments = [];
|
18 | collectAssignmentsInto(assignments, [], properties);
|
19 | var jsAssignments = assignments.map(function (_ref) {
|
20 | var path = _ref.path,
|
21 | value = _ref.value;
|
22 | return formatJSAssignment(objectName, path, value);
|
23 | });
|
24 | return jsAssignments.length === 0 ? '' : jsAssignments.join('\n');
|
25 | }
|
26 |
|
27 |
|
28 | function collectAssignmentsInto(assignments, parentPath, parentValue) {
|
29 |
|
30 | forEach(parentValue, function (value, key) {
|
31 |
|
32 | var path = parentPath.concat(key);
|
33 |
|
34 | if (typeof value === 'object' && value !== null) {
|
35 | collectAssignmentsInto(assignments, path, value);
|
36 | } else {
|
37 | assignments.push({
|
38 | path: path,
|
39 | value: value
|
40 | });
|
41 | }
|
42 | });
|
43 | }
|
44 |
|
45 |
|
46 | function formatJSAssignment(objectName, path, value) {
|
47 | var assignmentPath = path.map(function (p) {
|
48 | return typeof p === 'string' ? ".".concat(p) : "[".concat(p, "]");
|
49 | }).join('');
|
50 | var jsValue = value === undefined ? 'undefined' : JSON.stringify(value);
|
51 | return "".concat(objectName).concat(assignmentPath, " = ").concat(jsValue, ";");
|
52 | }
|
53 |
|
54 |
|
55 | function forEach(value, fn) {
|
56 | if (Array.isArray(value)) {
|
57 | for (var i = 0; i < value.length; i++) {
|
58 | fn(value[i], i);
|
59 | }
|
60 | } else {
|
61 | for (var k in value) {
|
62 | if (value.hasOwnProperty(k)) {
|
63 | fn(value[k], k);
|
64 | }
|
65 | }
|
66 | }
|
67 | }
|
68 |
|
69 | module.exports = deepMergeAssignments; |
\ | No newline at end of file |