Skip to content

Commit

Permalink
Merge pull request #3 from raygesualdo/refactor_normalization
Browse files Browse the repository at this point in the history
Refactor normalization
  • Loading branch information
zackify committed Dec 9, 2016
2 parents 0547274 + 14775d0 commit 006c593
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 74 deletions.
4 changes: 2 additions & 2 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"presets": [
"es2015"
"latest"
],
"plugins": [
"transform-async-to-generator",
"transform-object-rest-spread",
]
}
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
dist
dist
node_modules
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
12 changes: 0 additions & 12 deletions src/utilities/find-line.js

This file was deleted.

81 changes: 40 additions & 41 deletions src/utilities/normalize.js
Original file line number Diff line number Diff line change
@@ -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
}
}
}
}
16 changes: 0 additions & 16 deletions tests/unit/find-line.js

This file was deleted.

0 comments on commit 006c593

Please sign in to comment.