1exports.replaceDollarWithPercentPair = replaceDollarWithPercentPair
2exports.convertToSetCommand = convertToSetCommand
3exports.convertToSetCommands = convertToSetCommands
4
5function convertToSetCommand (key, value) {
6  var line = ''
7  key = key || ''
8  key = key.trim()
9  value = value || ''
10  value = value.trim()
11  if (key && value && value.length > 0) {
12    line = '@SET ' + key + '=' + replaceDollarWithPercentPair(value) + '\r\n'
13  }
14  return line
15}
16
17function extractVariableValuePairs (declarations) {
18  var pairs = {}
19  declarations.map(function (declaration) {
20    var split = declaration.split('=')
21    pairs[split[0]] = split[1]
22  })
23  return pairs
24}
25
26function convertToSetCommands (variableString) {
27  var variableValuePairs = extractVariableValuePairs(variableString.split(' '))
28  var variableDeclarationsAsBatch = ''
29  Object.keys(variableValuePairs).forEach(function (key) {
30    variableDeclarationsAsBatch += convertToSetCommand(key, variableValuePairs[key])
31  })
32  return variableDeclarationsAsBatch
33}
34
35function replaceDollarWithPercentPair (value) {
36  var dollarExpressions = /\$\{?([^$@#?\- \t{}:]+)\}?/g
37  var result = ''
38  var startIndex = 0
39  do {
40    var match = dollarExpressions.exec(value)
41    if (match) {
42      var betweenMatches = value.substring(startIndex, match.index) || ''
43      result += betweenMatches + '%' + match[1] + '%'
44      startIndex = dollarExpressions.lastIndex
45    }
46  } while (dollarExpressions.lastIndex > 0)
47  result += value.slice(startIndex)
48  return result
49}
50