Small refactoring. (#44)

Basically I like how when using Object.entries you can destructure and
name the value. It feels a lot more explicit to give the value a name
rather than refer to it as obj[k]. The syntax is not super pretty though.
This commit is contained in:
Sergio Díaz 2018-08-05 00:00:54 -06:00 committed by GitHub
parent f2b9cad662
commit d5da7d87e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 34 additions and 80 deletions

View File

@ -36,18 +36,6 @@ Beau {
], ],
}, },
"requests": RequestList { "requests": RequestList {
"PLUGINS": Plugins {
"autoload": Array [
"std",
],
"context": Object {},
"registry": Object {
"dynamicValues": Array [],
"postRequestModifiers": Array [],
"preRequestModifiers": Array [],
},
},
"REQUESTS": Array [],
"cache": RequestCache { "cache": RequestCache {
"$cache": Object { "$cache": Object {
"env": Object {}, "env": Object {},
@ -60,34 +48,6 @@ Beau {
exports[`Beau's config Loader. should load the request list using the configuration 1`] = ` exports[`Beau's config Loader. should load the request list using the configuration 1`] = `
RequestList { RequestList {
"PLUGINS": Plugins {
"autoload": Array [
"std",
],
"context": Object {},
"registry": Object {
"dynamicValues": Array [],
"postRequestModifiers": Array [],
"preRequestModifiers": Array [],
},
},
"REQUESTS": Array [
Object {
"ALIAS": "get-post",
"COOKIEJAR": false,
"ENDPOINT": "http://example.com",
"REQUEST": "GET /posts/1",
},
Object {
"ALIAS": "user",
"COOKIEJAR": false,
"ENDPOINT": "http://example.com",
"HEADERS": Object {
"hello": "world",
},
"REQUEST": "GET /user",
},
],
"cache": RequestCache { "cache": RequestCache {
"$cache": Object { "$cache": Object {
"env": Object {}, "env": Object {},

View File

@ -52,13 +52,13 @@ class Config {
} }
loadRequests(host, settings) { loadRequests(host, settings) {
let requests = Object.keys(host) let requests = Object.entries(host)
.filter(key => requestRegex.test(key)) .filter(([key]) => requestRegex.test(key))
.map(key => { .map(([key, rDefinition]) => {
let requestDefinitionIsString = typeof host[key] === 'string'; let requestDefinitionIsString = typeof rDefinition === 'string';
let originalRequest = requestDefinitionIsString let originalRequest = requestDefinitionIsString
? { ALIAS: host[key] } ? { ALIAS: rDefinition }
: host[key]; : rDefinition;
let request = UpperCaseKeys(originalRequest); let request = UpperCaseKeys(originalRequest);
@ -81,9 +81,9 @@ class Config {
loadConfig(host) { loadConfig(host) {
let config = {}; let config = {};
Object.keys(host) Object.entries(host)
.filter(k => this.configKeys.includes(k.toUpperCase())) .filter(([key]) => this.configKeys.includes(key.toUpperCase()))
.forEach(k => (config[k.toUpperCase()] = host[k])); .forEach(([key, value]) => (config[key.toUpperCase()] = value));
return config; return config;
} }

View File

@ -41,11 +41,11 @@ class Request {
findDependencies(request, set = new Set()) { findDependencies(request, set = new Set()) {
let type = typeof request; let type = typeof request;
if (type === 'object') { if (type === 'object' && request !== null) {
Object.keys(request) Object.entries(request)
.filter(key => key !== 'ALIAS') .filter(([key]) => key !== 'ALIAS')
.forEach(key => { .forEach(([key, value]) => {
set = this.findDependencies(request[key], set); set = this.findDependencies(value, set);
}); });
} else if (type === 'string') { } else if (type === 'string') {
const matches = []; const matches = [];

View File

@ -3,10 +3,7 @@ const RequestCache = require('./requestCache');
class RequestList { class RequestList {
constructor(config = { REQUESTS: [] }) { constructor(config = { REQUESTS: [] }) {
this.PLUGINS = config.PLUGINS; this.list = this.loadRequests(config.REQUESTS, config.PLUGINS);
this.REQUESTS = config.REQUESTS;
this.list = this.loadRequests();
this.cache = new RequestCache(); this.cache = new RequestCache();
this.cache.add(`env`, config.ENVIRONMENT); this.cache.add(`env`, config.ENVIRONMENT);
@ -42,11 +39,11 @@ class RequestList {
return this.cache; return this.cache;
} }
loadRequests() { loadRequests(REQUESTS, PLUGINS) {
let requests = []; let requests = [];
this.REQUESTS.forEach(request => { REQUESTS.forEach(request => {
try { try {
requests.push(new Request(request, this.PLUGINS)); requests.push(new Request(request, PLUGINS));
} catch (e) { } catch (e) {
throw new Error(`${request.request} was ignored: ${e}`); throw new Error(`${request.request} was ignored: ${e}`);
} }

View File

@ -16,7 +16,7 @@ const dynamicValueRegex = /\$\[(\w+\((?:.|[\n\r])*?\))\]/g;
const UpperCaseKeys = function(obj) { const UpperCaseKeys = function(obj) {
let result = {}; let result = {};
Object.keys(obj).forEach(k => (result[k.toUpperCase()] = obj[k])); Object.entries(obj).forEach(([k, v]) => (result[k.toUpperCase()] = v));
return result; return result;
}; };
@ -26,12 +26,12 @@ const isEmptyObject = obj =>
const removeOptionalKeys = function(obj, optionalValues) { const removeOptionalKeys = function(obj, optionalValues) {
let result = {}; let result = {};
Object.keys(obj).forEach(key => { Object.entries(obj).forEach(([key, value]) => {
if (optionalValues.includes(key) && isEmptyObject(obj[key])) { if (optionalValues.includes(key) && isEmptyObject(value)) {
return; return;
} }
result[key] = obj[key]; result[key] = value;
}); });
return result; return result;
@ -49,22 +49,19 @@ const replaceInObject = function(obj, fn) {
return null; return null;
} }
let type = typeof obj; switch (typeof obj) {
case 'undefined':
if (type === 'undefined') { return {};
return {}; case 'string':
return fn(obj);
case 'object':
obj = Object.assign({}, obj);
Object.entries(obj).forEach(
([key, value]) => (obj[key] = replaceInObject(value, fn))
);
default:
return obj;
} }
if (type === 'string') {
return fn(obj);
}
if (type === 'object') {
obj = Object.assign({}, obj);
Object.keys(obj).forEach(k => (obj[k] = replaceInObject(obj[k], fn)));
}
return obj;
}; };
const moduleVersion = () => parseInt(require('../package.json').version, 10); const moduleVersion = () => parseInt(require('../package.json').version, 10);