From e5878a99633898e80d032713b729cd566f081245 Mon Sep 17 00:00:00 2001 From: Ray Gesualdo Date: Fri, 9 Dec 2016 14:18:41 -0500 Subject: [PATCH 1/4] added node_modules to .gitignore --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 53c37a1..de4d1f0 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -dist \ No newline at end of file +dist +node_modules From 09d40e0f2a732b7ffa7bd001fbc74a307e2521dc Mon Sep 17 00:00:00 2001 From: Ray Gesualdo Date: Fri, 9 Dec 2016 14:19:23 -0500 Subject: [PATCH 2/4] updated babel dependencies --- .babelrc | 4 ++-- package.json | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.babelrc b/.babelrc index a6f200f..ee8c159 100644 --- a/.babelrc +++ b/.babelrc @@ -1,8 +1,8 @@ { "presets": [ - "es2015" + "latest" ], "plugins": [ - "transform-async-to-generator", + "transform-object-rest-spread", ] } diff --git a/package.json b/package.json index 90b5b31..c6311a5 100644 --- a/package.json +++ b/package.json @@ -23,9 +23,9 @@ }, "homepage": "https://github.com/zackify/legible#readme", "devDependencies": { - "babel-plugin-transform-async-to-generator": "^6.16.0", + "babel-plugin-transform-object-rest-spread": "^6.20.2", "babel-polyfill": "^6.20.0", - "babel-preset-es2015": "^6.16.0", + "babel-preset-latest": "^6.16.0", "babel-register": "^6.18.0", "chai": "^3.5.0", "mocha": "^3.2.0" From f6012f99a047c0e1b5ebcb30f923530a508570f5 Mon Sep 17 00:00:00 2001 From: Ray Gesualdo Date: Fri, 9 Dec 2016 14:19:56 -0500 Subject: [PATCH 3/4] don't need find-line.js anymore --- src/utilities/find-line.js | 12 ------------ tests/unit/find-line.js | 16 ---------------- 2 files changed, 28 deletions(-) delete mode 100644 src/utilities/find-line.js delete mode 100644 tests/unit/find-line.js diff --git a/src/utilities/find-line.js b/src/utilities/find-line.js deleted file mode 100644 index a78e2c4..0000000 --- a/src/utilities/find-line.js +++ /dev/null @@ -1,12 +0,0 @@ -/* - Take in a title, search through lines and return it - ex: an array with "url: blah.com" -> findLine('url', lines) -> blah.com -*/ -export default (title, lines) => { - let line = lines.find((line, index) => line.match(`${title}:`)) - if(!line) return null - - return line - .replace(/ /g, '') - .replace(`${title}:`, '') -} \ No newline at end of file diff --git a/tests/unit/find-line.js b/tests/unit/find-line.js deleted file mode 100644 index 9288e5b..0000000 --- a/tests/unit/find-line.js +++ /dev/null @@ -1,16 +0,0 @@ -import findLine from '../../src/utilities/find-line' - -const passToNormalize = (strings, ...vars) => normalize(strings, vars) - -describe('fineLine', () => { - it('returns line value for url', () => { - let lines = [ - 'blah: true', - 'url: https://google.com' - ] - - expect(findLine('url', lines)).to.equal('https://google.com') - }) - - -}) \ No newline at end of file From 14775d04dd8317d486b4b54950ca05b01dfc6a9d Mon Sep 17 00:00:00 2001 From: Ray Gesualdo Date: Fri, 9 Dec 2016 14:20:31 -0500 Subject: [PATCH 4/4] rewrote how variables are extracted from the template string --- src/utilities/normalize.js | 81 +++++++++++++++++++------------------- 1 file changed, 40 insertions(+), 41 deletions(-) diff --git a/src/utilities/normalize.js b/src/utilities/normalize.js index e455fe1..ed0cc67 100644 --- a/src/utilities/normalize.js +++ b/src/utilities/normalize.js @@ -1,53 +1,52 @@ -/* - Take in raw query string and +/* + Take in raw query string and return a fetch api compatible object */ -import findLine from './find-line' - - -const getVars = (lines, vars, variableMap) => { - let body = lines.findIndex(line => line.match(/body/)) - let headers = lines.findIndex(line => line.match(/headers/)) - if(headers === -1) variableMap.body -= 1 - - if(body !== -1 && headers !== -1 && body < headers) { - variableMap.headers += 1 - variableMap.body -= 1 - } - - return { - body: body || body === 0 ? JSON.stringify(vars[variableMap.body]) : null, - headers: headers || headers === 0 ? vars[variableMap.headers] : null - } -} - -export default (strings, vars) => { - let variableMap = { - headers: 0, - body: 1, - } - - let lines = strings +const buildObjectFromTag = (strings, vars) => { + const namespace = 'legible-request-var-' + return strings + // First, add namespaced placeholders to elements in `strings` + // for each element in `vars` + .map((str, index) => str + (vars[index] ? `${namespace}${index}` : '')) + // Join the elements into a single string .join('') + // Split them back out by linebreak .split('\n') + // Remove empty elements .filter(i => i) + // Trim each element + .map(s => s.trim()) + // Split each element at `:` + .map(s => s.split(':').map(s => s.trim())) + // Strings from above step with multiple `:` in them will get split + // more than once, meaning `values` is an array of strings instead of + // a single string. Let's fix that. + .map(([key, ...values]) => [key, values.join(':')]) + // If `value` is a reference, replace it with respective element in `vars` + .map(([key, value]) => { + // Ignore non-namespaced value + if (!value.startsWith(namespace)) return [key, value] + // Get the index at the end of the namespaced string + const index = parseInt(value.replace(namespace, ''), 10) + // Return an array of the object key and replaced value from `vars` + return [key, vars[index]] + }) + // Convert to object + .reduce((obj, [key, value]) => { + return {...obj, [key]: value} + }, {}) +} - let url = findLine('url', lines) - - if(!url) { - url = vars[0] - variableMap = { headers: 1, body: 2 } - } - - let { headers, body } = getVars(lines, vars, variableMap) +export default (strings, vars) => { + const { url, method, body, ...options } = buildObjectFromTag(strings, vars) return { - url: findLine('url', lines) || vars[0], + url, options: { - method: findLine('method', lines) || 'GET', - headers, - body, + method: method || 'GET', + body: body ? JSON.stringify(body) : null, + ...options } } -} \ No newline at end of file +}