binhend 1.0.8 → 1.0.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/example/config.js CHANGED
@@ -1,4 +1,13 @@
1
1
 
2
2
  module.exports = function(loader) {
3
- loader.object({ PORT: 1400 });
3
+ var sample = {
4
+ ABC: 123,
5
+ Text: 'Hello',
6
+ NAME: 'MyApp',
7
+ PORT: 1400
8
+ };
9
+
10
+ var { filter, object } = loader;
11
+
12
+ filter(object, sample, ['PORT', 'NAME']);
4
13
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "binhend",
3
- "version": "1.0.8",
3
+ "version": "1.0.10",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "author": "Nguyen Duc Binh",
package/src/binh.js CHANGED
@@ -43,7 +43,21 @@ function Binh() {
43
43
  };
44
44
 
45
45
  binh.config = function(key) {
46
- return typeof key === 'string' ? config[key] : config;
46
+ if (typeof key !== 'string') return config;
47
+
48
+ if (config.hasOwnProperty(key)) return config[key];
49
+
50
+ var pointer = config, keys = key.split('.'), length = keys.length;
51
+
52
+ try {
53
+ for (var i = 0; i < length; i++) {
54
+ pointer = pointer[keys[i]];
55
+ }
56
+ return pointer;
57
+ }
58
+ catch (e) {
59
+ return undefined;
60
+ }
47
61
  };
48
62
 
49
63
  binh.config.reload = function() {
@@ -122,7 +136,7 @@ function Binh() {
122
136
  this.port.value = port;
123
137
  }
124
138
  else if (typeof port === 'string') {
125
- this.port.value = config[port];
139
+ this.port.value = binh.config(port);
126
140
  }
127
141
  return this;
128
142
  };
@@ -5,6 +5,8 @@ var fs = require('fs');
5
5
  function ConfigLoader(config_object) {
6
6
  const configs = config_object || {};
7
7
 
8
+ var deferred = [];
9
+
8
10
  this.getConfigs = function() {
9
11
  return configs;
10
12
  };
@@ -13,10 +15,34 @@ function ConfigLoader(config_object) {
13
15
  return key == undefined ? configs : (configs[key] = configs[key] || {});
14
16
  };
15
17
 
18
+ this.filter = function(method, input, filters, key) {
19
+ if (!(filters instanceof Array) || !filters.length) return {};
20
+
21
+ if (method && method.fetch instanceof Function) {
22
+ var filtered = filter(method.fetch(input), filters);
23
+ return this.object(filtered, key);
24
+ }
25
+
26
+ if (method && method.is_promise === true) {
27
+ var _this = this;
28
+
29
+ var promise = new Promise(callback).then(function(loaded_config) {
30
+ var filtered = filter(loaded_config, filters);
31
+ _this.object(filtered, key);
32
+ });
33
+
34
+ deferred.push(promise);
35
+
36
+ return this;
37
+ }
38
+
39
+ return filter(input, filters);
40
+ };
41
+
16
42
  this.object = function(object, key) {
17
43
  try {
18
44
  var config = getConfigPosition(key);
19
- Object.assign(config, JSON.parse(JSON.stringify(object)));
45
+ Object.assign(config, JSON.parse(JSON.stringify({ ...object })));
20
46
  }
21
47
  catch(e) {
22
48
  return failed(e, object);
@@ -36,14 +62,15 @@ function ConfigLoader(config_object) {
36
62
  return this.object(file(path, encoding), key);
37
63
  };
38
64
 
39
- var deferred = [];
40
-
41
- this.loadAsync = function(callback, key) {
65
+ this.promise = function(callback, key) {
42
66
  var _this = this;
43
- var promise = loadAsync(callback).then(function(loaded_config) {
67
+
68
+ var promise = new Promise(callback).then(function(loaded_config) {
44
69
  _this.object(loaded_config, key);
45
70
  });
71
+
46
72
  deferred.push(promise);
73
+
47
74
  return this;
48
75
  };
49
76
 
@@ -63,8 +90,34 @@ function ConfigLoader(config_object) {
63
90
  this.target = function(config_object) {
64
91
  return new ConfigLoader(config_object);
65
92
  };
93
+
94
+ for (var key in this) {
95
+ var method = this[key];
96
+ if (method instanceof Function) {
97
+ this[key] = method.bind(this);
98
+ }
99
+ }
100
+
101
+ this.json.fetch = json;
102
+ this.cli.fetch = cli;
103
+ this.file.fetch = file;
104
+ this.object.fetch = function(object) { return object; };
105
+ this.promise.is_promise = true;
66
106
  }
67
107
 
108
+ function filter(object, filters) {
109
+ if (!(object instanceof Object) || !(filters instanceof Array)) return {};
110
+
111
+ var filtered = {}, length = filters.length;
112
+
113
+ for (var i = 0; i < length; i++) {
114
+ var field = filters[i];
115
+ filtered[field] = object[field];
116
+ }
117
+
118
+ return filtered;
119
+ };
120
+
68
121
  function json(filepath) {
69
122
  try {
70
123
  return require.main.require(filepath);
@@ -125,21 +178,17 @@ function parseKeyValue(string) {
125
178
  return { key, value, success: true };
126
179
  }
127
180
 
128
- function loadAsync(callback) {
129
- return new Promise(callback);
130
- }
131
-
132
181
  function config(config_object) {
133
182
  return new ConfigLoader(config_object);
134
183
  }
135
184
 
136
185
  function failed(error, source) {
137
- console.log(`Failed loading config from: ${source}`);
138
- console.log('Details:', error);
186
+ console.log(`[BINHEND] Failed loading config from: ${source}`);
187
+ console.log('[BINHEND] Details:', error);
139
188
  return {};
140
189
  };
141
190
 
142
191
  module.exports = {
143
192
  ConfigLoader, config,
144
- json, cli, file, loadAsync
193
+ json, cli, file
145
194
  };