summaryrefslogtreecommitdiff
path: root/deps/node/deps/npm/node_modules/qw/qw.js
blob: 239e5a565ef94e5bffa30377404232e2380933ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
'use strict'
module.exports = qw

function appendLast (arr, str) {
  var last = arr.length - 1
  if (last < 0) {
    arr.push(str)
  } else {
    var lastValue = String(arr[last])
    return arr[last] = lastValue + String(str)
  }
}

function qw () {
  const args = Object.assign([], arguments[0])
  const values = [].slice.call(arguments, 1)
  const words = []
  let lastWordWasValue = false
  while (args.length) {
    const arg = args.shift()
    const concatValue = arg.length === 0 || arg.slice(-1) !== ' '
    if (arg.trim() !== '') {
      const theseWords = arg.replace(/^\s+|\s+$/g, '').replace(/\s+/g, ' ').split(/ /)
      if (lastWordWasValue && arg[0] !== ' ') {
        appendLast(words, theseWords.shift())
      }
      words.push.apply(words, theseWords)
    }

    if (values.length) {
      const val = values.shift()
      if (concatValue) {
        appendLast(words, val)
      } else {
        words.push(val)
      }
      lastWordWasValue = true
    } else {
      lastWordWasValue = false
    }
  }
  return words
}