Skip to content

Commit 006c593

Browse files
authored
Merge pull request #3 from raygesualdo/refactor_normalization
Refactor normalization
2 parents 0547274 + 14775d0 commit 006c593

6 files changed

Lines changed: 46 additions & 74 deletions

File tree

.babelrc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"presets": [
3-
"es2015"
3+
"latest"
44
],
55
"plugins": [
6-
"transform-async-to-generator",
6+
"transform-object-rest-spread",
77
]
88
}

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
dist
1+
dist
2+
node_modules

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
},
2424
"homepage": "https://github.com/zackify/legible#readme",
2525
"devDependencies": {
26-
"babel-plugin-transform-async-to-generator": "^6.16.0",
26+
"babel-plugin-transform-object-rest-spread": "^6.20.2",
2727
"babel-polyfill": "^6.20.0",
28-
"babel-preset-es2015": "^6.16.0",
28+
"babel-preset-latest": "^6.16.0",
2929
"babel-register": "^6.18.0",
3030
"chai": "^3.5.0",
3131
"mocha": "^3.2.0"

src/utilities/find-line.js

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/utilities/normalize.js

Lines changed: 40 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,52 @@
1-
/*
2-
Take in raw query string and
1+
/*
2+
Take in raw query string and
33
return a fetch api compatible object
44
*/
55

6-
import findLine from './find-line'
7-
8-
9-
const getVars = (lines, vars, variableMap) => {
10-
let body = lines.findIndex(line => line.match(/body/))
11-
let headers = lines.findIndex(line => line.match(/headers/))
12-
if(headers === -1) variableMap.body -= 1
13-
14-
if(body !== -1 && headers !== -1 && body < headers) {
15-
variableMap.headers += 1
16-
variableMap.body -= 1
17-
}
18-
19-
return {
20-
body: body || body === 0 ? JSON.stringify(vars[variableMap.body]) : null,
21-
headers: headers || headers === 0 ? vars[variableMap.headers] : null
22-
}
23-
}
24-
25-
export default (strings, vars) => {
26-
let variableMap = {
27-
headers: 0,
28-
body: 1,
29-
}
30-
31-
let lines = strings
6+
const buildObjectFromTag = (strings, vars) => {
7+
const namespace = 'legible-request-var-'
8+
return strings
9+
// First, add namespaced placeholders to elements in `strings`
10+
// for each element in `vars`
11+
.map((str, index) => str + (vars[index] ? `${namespace}${index}` : ''))
12+
// Join the elements into a single string
3213
.join('')
14+
// Split them back out by linebreak
3315
.split('\n')
16+
// Remove empty elements
3417
.filter(i => i)
18+
// Trim each element
19+
.map(s => s.trim())
20+
// Split each element at `:`
21+
.map(s => s.split(':').map(s => s.trim()))
22+
// Strings from above step with multiple `:` in them will get split
23+
// more than once, meaning `values` is an array of strings instead of
24+
// a single string. Let's fix that.
25+
.map(([key, ...values]) => [key, values.join(':')])
26+
// If `value` is a reference, replace it with respective element in `vars`
27+
.map(([key, value]) => {
28+
// Ignore non-namespaced value
29+
if (!value.startsWith(namespace)) return [key, value]
30+
// Get the index at the end of the namespaced string
31+
const index = parseInt(value.replace(namespace, ''), 10)
32+
// Return an array of the object key and replaced value from `vars`
33+
return [key, vars[index]]
34+
})
35+
// Convert to object
36+
.reduce((obj, [key, value]) => {
37+
return {...obj, [key]: value}
38+
}, {})
39+
}
3540

36-
let url = findLine('url', lines)
37-
38-
if(!url) {
39-
url = vars[0]
40-
variableMap = { headers: 1, body: 2 }
41-
}
42-
43-
let { headers, body } = getVars(lines, vars, variableMap)
41+
export default (strings, vars) => {
42+
const { url, method, body, ...options } = buildObjectFromTag(strings, vars)
4443

4544
return {
46-
url: findLine('url', lines) || vars[0],
45+
url,
4746
options: {
48-
method: findLine('method', lines) || 'GET',
49-
headers,
50-
body,
47+
method: method || 'GET',
48+
body: body ? JSON.stringify(body) : null,
49+
...options
5150
}
5251
}
53-
}
52+
}

tests/unit/find-line.js

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)