UNPKG

2.36 kBJavaScriptView Raw
1(function() {
2 var _, cloudinary_config, isNestedKey, putNestedValue;
3
4 _ = require("lodash");
5
6 cloudinary_config = void 0;
7
8 isNestedKey = function(key) {
9 return key.match(/\w+\[\w+\]/);
10 };
11
12
13 /***
14 * Assign a value to a nested object
15 * @function putNestedValue
16 * @param params the parent object - this argument will be modified!
17 * @param key key in the form nested[innerkey]
18 * @param value the value to assign
19 * @return the modified params object
20 */
21
22 putNestedValue = function(params, key, value) {
23 var chain, inner, innerKey, j, lastKey, len, outer;
24 chain = key.split(/[\[\]]+/).filter((function(_this) {
25 return function(i) {
26 return i.length;
27 };
28 })(this));
29 outer = params;
30 lastKey = chain.pop();
31 for (j = 0, len = chain.length; j < len; j++) {
32 innerKey = chain[j];
33 inner = outer[innerKey];
34 if (inner == null) {
35 inner = {};
36 outer[innerKey] = inner;
37 }
38 outer = inner;
39 }
40 return outer[lastKey] = value;
41 };
42
43 module.exports = function(new_config, new_value) {
44 var cloudinary_url, k, ref, uri, v;
45 if ((cloudinary_config == null) || new_config === true) {
46 cloudinary_url = process.env.CLOUDINARY_URL;
47 if (cloudinary_url != null) {
48 uri = require('url').parse(cloudinary_url, true);
49 cloudinary_config = {
50 cloud_name: uri.host,
51 api_key: uri.auth && uri.auth.split(":")[0],
52 api_secret: uri.auth && uri.auth.split(":")[1],
53 private_cdn: uri.pathname != null,
54 secure_distribution: uri.pathname && uri.pathname.substring(1)
55 };
56 if (uri.query != null) {
57 ref = uri.query;
58 for (k in ref) {
59 v = ref[k];
60 if (isNestedKey(k)) {
61 putNestedValue(cloudinary_config, k, v);
62 } else {
63 cloudinary_config[k] = v;
64 }
65 }
66 }
67 } else {
68 cloudinary_config = {};
69 }
70 }
71 if (!_.isUndefined(new_value)) {
72 cloudinary_config[new_config] = new_value;
73 } else if (_.isString(new_config)) {
74 return cloudinary_config[new_config];
75 } else if (_.isObject(new_config)) {
76 _.extend(cloudinary_config, new_config);
77 }
78 return cloudinary_config;
79 };
80
81}).call(this);
82
83//# sourceMappingURL=config.js.map