jshttp / fresh

HTTP request freshness testing
MIT License
161 stars 28 forks source link

May i ask a question? #30

Closed sanriqing closed 5 years ago

sanriqing commented 5 years ago

hi,Why do you write this function like this? What's the difference between ascill and normal characters?

function parseTokenList (str) {
  var end = 0
  var list = []
  var start = 0

  // gather tokens
  for (var i = 0, len = str.length; i < len; i++) {
    switch (str.charCodeAt(i)) {
      case 0x20: /*   */
        if (start === end) {
          start = end = i + 1
        }
        break
      case 0x2c: /* , */
        list.push(str.substring(start, end))
        start = end = i + 1
        break
      default:
        end = i + 1
        break
    }
  }

  // final token
  list.push(str.substring(start, end))

  return list
}

emmmm,

str.replace(/\s/g,'').split(',');
dougwilson commented 5 years ago

Hi @sanriqing thanks for being curious! So your code at the bottom does not work in the same way as the function; for example here is an example of a different result:

> parseTokenList('fo o,bar')
[ 'fo o', 'bar' ]
> 'fo o,bar'.replace(/\s/g,'').split(',')
[ 'foo', 'bar' ]

Notice that your .replace is removing whitespace characters that occur anywhere, even in the middle of a value.