Skip to content

Commit 6838225

Browse files
author
Amir Tocker
committed
Don't replace the internal config object
1 parent 5810cc5 commit 6838225

File tree

2 files changed

+49
-26
lines changed

2 files changed

+49
-26
lines changed

lib/config.js

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
"use strict";
22

3+
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }
4+
5+
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }
6+
7+
function _iterableToArrayLimit(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
8+
9+
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
10+
311
/***
412
* Assign a value to a nested object
513
* @function putNestedValue
@@ -48,31 +56,44 @@ function putNestedValue(params, key, value) {
4856

4957
module.exports = function (new_config, new_value) {
5058
if (cloudinary_config == null || new_config === true) {
59+
if (cloudinary_config == null) {
60+
cloudinary_config = {};
61+
} else {
62+
Object.keys(cloudinary_config).forEach(function (key) {
63+
return delete cloudinary_config[key];
64+
});
65+
}
66+
5167
var cloudinary_url = process.env.CLOUDINARY_URL;
5268

5369
if (cloudinary_url != null) {
5470
var uri = url.parse(cloudinary_url, true);
55-
cloudinary_config = {
71+
var parsedConfig = {
5672
cloud_name: uri.host,
5773
api_key: uri.auth && uri.auth.split(":")[0],
5874
api_secret: uri.auth && uri.auth.split(":")[1],
5975
private_cdn: uri.pathname != null,
6076
secure_distribution: uri.pathname && uri.pathname.substring(1)
6177
};
78+
Object.entries(parsedConfig).forEach(function (_ref) {
79+
var _ref2 = _slicedToArray(_ref, 2),
80+
key = _ref2[0],
81+
value = _ref2[1];
6282

63-
if (uri.query != null) {
64-
for (var k in uri.query) {
65-
var v = uri.query[k];
66-
67-
if (isNestedKey(k)) {
68-
putNestedValue(cloudinary_config, k, v);
69-
} else {
70-
cloudinary_config[k] = v;
71-
}
83+
if (value !== undefined) {
84+
cloudinary_config[key] = value;
7285
}
86+
});
87+
88+
if (uri.query != null) {
89+
Object.entries(uri.query).forEach(function (_ref3) {
90+
var _ref4 = _slicedToArray(_ref3, 2),
91+
key = _ref4[0],
92+
value = _ref4[1];
93+
94+
return putNestedValue(cloudinary_config, key, value);
95+
});
7396
}
74-
} else {
75-
cloudinary_config = {};
7697
}
7798
}
7899

src/config.js

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ function isNestedKey(key) {
1919
}
2020

2121
function putNestedValue(params, key, value) {
22-
let chain = key.split(/[\[\]]+/).filter((i) => {
23-
return i.length;
24-
});
22+
let chain = key.split(/[\[\]]+/).filter((i) => i.length);
2523
let outer = params;
2624
let lastKey = chain.pop();
2725
for (let j = 0; j < chain.length; j++) {
@@ -35,31 +33,35 @@ function putNestedValue(params, key, value) {
3533
}
3634
return outer[lastKey] = value;
3735
}
36+
3837
module.exports = function(new_config, new_value) {
3938
if ((cloudinary_config == null) || new_config === true) {
39+
if (cloudinary_config == null){
40+
cloudinary_config = {};
41+
} else {
42+
Object.keys(cloudinary_config).forEach(key=> delete cloudinary_config[key]);
43+
}
44+
45+
4046
let cloudinary_url = process.env.CLOUDINARY_URL;
4147
if (cloudinary_url != null) {
4248

4349
let uri = url.parse(cloudinary_url, true);
44-
cloudinary_config = {
50+
let parsedConfig = {
4551
cloud_name: uri.host,
4652
api_key: uri.auth && uri.auth.split(":")[0],
4753
api_secret: uri.auth && uri.auth.split(":")[1],
4854
private_cdn: uri.pathname != null,
4955
secure_distribution: uri.pathname && uri.pathname.substring(1)
5056
};
51-
if (uri.query != null) {
52-
for (let k in uri.query) {
53-
let v = uri.query[k];
54-
if (isNestedKey(k)) {
55-
putNestedValue(cloudinary_config, k, v);
56-
} else {
57-
cloudinary_config[k] = v;
58-
}
57+
Object.entries(parsedConfig).forEach(([key, value])=> {
58+
if(value !== undefined) {
59+
cloudinary_config[key] = value;
5960
}
61+
});
62+
if (uri.query != null) {
63+
Object.entries(uri.query).forEach( ([key, value])=> putNestedValue(cloudinary_config, key, value));
6064
}
61-
} else {
62-
cloudinary_config = {};
6365
}
6466
}
6567
if (!isUndefined(new_value)) {

0 commit comments

Comments
 (0)