qw.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. 'use strict'
  2. module.exports = qw
  3. function appendLast (arr, str) {
  4. var last = arr.length - 1
  5. if (last < 0) {
  6. arr.push(str)
  7. } else {
  8. var lastValue = String(arr[last])
  9. return arr[last] = lastValue + String(str)
  10. }
  11. }
  12. function qw () {
  13. const args = Object.assign([], arguments[0])
  14. const values = [].slice.call(arguments, 1)
  15. const words = []
  16. let lastWordWasValue = false
  17. while (args.length) {
  18. const arg = args.shift()
  19. const concatValue = arg.length === 0 || arg.slice(-1) !== ' '
  20. if (arg.trim() !== '') {
  21. const theseWords = arg.replace(/^\s+|\s+$/g, '').replace(/\s+/g, ' ').split(/ /)
  22. if (lastWordWasValue && arg[0] !== ' ') {
  23. appendLast(words, theseWords.shift())
  24. }
  25. words.push.apply(words, theseWords)
  26. }
  27. if (values.length) {
  28. const val = values.shift()
  29. if (concatValue) {
  30. appendLast(words, val)
  31. } else {
  32. words.push(val)
  33. }
  34. lastWordWasValue = true
  35. } else {
  36. lastWordWasValue = false
  37. }
  38. }
  39. return words
  40. }