diff --git a/src/helpers/code-builder.js b/src/helpers/code-builder.js index 335ea34c0..7f566abbc 100644 --- a/src/helpers/code-builder.js +++ b/src/helpers/code-builder.js @@ -1,6 +1,6 @@ 'use strict' -var util = require('util') +const util = require('util') /** * Helper object to format and aggragate lines of code. @@ -11,7 +11,7 @@ var util = require('util') * @param {string} indentation Desired indentation character for aggregated lines of code * @param {string} join Desired character to join each line of code */ -var CodeBuilder = function (indentation, join) { +const CodeBuilder = function (indentation, join) { this.code = [] this.indentation = indentation this.lineJoin = join || '\n' @@ -37,8 +37,8 @@ var CodeBuilder = function (indentation, join) { * // returns: 'console.log("\t\thello world")' */ CodeBuilder.prototype.buildLine = function (indentationLevel, line) { - var lineIndentation = '' - var slice = 2 + let lineIndentation = '' + let slice = 2 if (Object.prototype.toString.call(indentationLevel) === '[object String]') { slice = 1 line = indentationLevel @@ -52,7 +52,7 @@ CodeBuilder.prototype.buildLine = function (indentationLevel, line) { indentationLevel-- } - var format = Array.prototype.slice.call(arguments, slice, arguments.length) + let format = Array.prototype.slice.call(arguments, slice, arguments.length) format.unshift(lineIndentation + line) return util.format.apply(this, format) diff --git a/src/helpers/reducer.js b/src/helpers/reducer.js index bc17a0a0d..b7089da4c 100644 --- a/src/helpers/reducer.js +++ b/src/helpers/reducer.js @@ -1,13 +1,13 @@ 'use strict' -module.exports = function (obj, pair) { - if (obj[pair.name] === undefined) { +module.exports = (obj, pair) => { + if (obj[pair.name]) { obj[pair.name] = pair.value return obj } // convert to array - var arr = [ + const arr = [ obj[pair.name], pair.value ] diff --git a/src/helpers/shell.js b/src/helpers/shell.js index 12b155b1a..53364eecd 100644 --- a/src/helpers/shell.js +++ b/src/helpers/shell.js @@ -1,6 +1,6 @@ 'use strict' -var util = require('util') +const util = require('util') module.exports = { /** @@ -8,8 +8,8 @@ module.exports = { * to deal with nested single quote characters. * http://wiki.bash-hackers.org/syntax/quoting#strong_quoting */ - quote: function (value) { - var safe = /^[a-z0-9-_/.@%^=:]+$/i + quote(value) { + let safe = /^[a-z0-9-_/.@%^=:]+$/i // Unless `value` is a simple shell-safe string, quote it. if (!safe.test(value)) { @@ -19,7 +19,7 @@ module.exports = { return value }, - escape: function (value) { + escape(value) { return value.replace(/\r/g, '\\r').replace(/\n/g, '\\n') } } diff --git a/src/index.js b/src/index.js index 0c6cf1bd6..1d8a4003c 100644 --- a/src/index.js +++ b/src/index.js @@ -1,20 +1,20 @@ 'use strict' -var debug = require('debug')('httpsnippet') -var es = require('event-stream') -var MultiPartForm = require('form-data') -var qs = require('querystring') -var reducer = require('./helpers/reducer') -var targets = require('./targets') -var url = require('url') -var util = require('util') -var validate = require('har-validator/lib/async') +const debug = require('debug')('httpsnippet') +const es = require('event-stream') +const MultiPartForm = require('form-data') +const qs = require('querystring') +const reducer = require('./helpers/reducer') +const targets = require('./targets') +const url = require('url') +const util = require('util') +const validate = require('har-validator/lib/async') // constructor -var HTTPSnippet = function (data) { - var entries - var self = this - var input = util._extend({}, data) +const HTTPSnippet = function (data) { + let entries + let self = this + let input = util._extend({}, data) // prep the main container self.requests = [] @@ -28,7 +28,7 @@ var HTTPSnippet = function (data) { }] } - entries.forEach(function (entry) { + entries.forEach(entry => { // add optional properties to make validation successful entry.request.httpVersion = entry.request.httpVersion || 'HTTP/1.1' entry.request.queryString = entry.request.queryString || [] @@ -41,7 +41,7 @@ var HTTPSnippet = function (data) { entry.request.headersSize = 0 entry.request.postData.size = 0 - validate.request(entry.request, function (err, valid) { + validate.request(entry.request, (err, valid) => { if (!valid) { throw err } @@ -85,7 +85,7 @@ HTTPSnippet.prototype.prepare = function (request) { } // construct Cookie header - var cookies = request.cookies.map(function (cookie) { + let cookies = request.cookies.map(function (cookie) { return encodeURIComponent(cookie.name) + '=' + encodeURIComponent(cookie.value) }) @@ -103,7 +103,7 @@ HTTPSnippet.prototype.prepare = function (request) { request.postData.mimeType = 'multipart/form-data' if (request.postData.params) { - var form = new MultiPartForm() + let form = new MultiPartForm() // easter egg form._boundary = '---011000010111000001101001' @@ -186,17 +186,15 @@ HTTPSnippet.prototype.prepare = function (request) { return request } -HTTPSnippet.prototype.convert = function (target, client, opts) { +HTTPSnippet.prototype.convert = (target, client, opts) => { if (!opts && client) { opts = client } - var func = this._matchTarget(target, client) + let func = this._matchTarget(target, client) if (func) { - var results = this.requests.map(function (request) { - return func(request, opts) - }) + let results = this.requests.map(request => func(request, opts) return results.length === 1 ? results[0] : results } @@ -224,25 +222,22 @@ module.exports = HTTPSnippet module.exports.availableTargets = function () { return Object.keys(targets).map(function (key) { - var target = util._extend({}, targets[key].info) - var clients = Object.keys(targets[key]) + let target = util._extend({}, targets[key].info) + let clients = Object.keys(targets[key]) + + .filter(prop => !~['info', 'index'].indexOf(prop) - .filter(function (prop) { - return !~['info', 'index'].indexOf(prop) - }) - .map(function (client) { - return targets[key][client].info - }) + .map(client => targets[key][client].info if (clients.length) { target.clients = clients } return target - }) + }); } -module.exports.extname = function (target) { - return targets[target] ? targets[target].info.extname : '' -} +module.exports.extname = target => targets[target] ? targets[target].info.extname : '' + +