sailrish / shipit

shipit node module
MIT License
87 stars 65 forks source link

guessTracking #16

Closed Russ93 closed 9 years ago

Russ93 commented 9 years ago

Another UPS tracking regex /^K\d{10}$/i

it would be nice to be able to add custom objects to the array i.e. {name: 'ups', regex:/^K\d{10}$/i }

I am unsure how you would like to but if the array of carrier regex could be exposed somewhere would be nice

here is one way to do it src/guessCarrier.coffee line 108

module.exports = (trk) ->
  carriers = []
  trk = _preprocess trk
  @CARRIERS = CARRIERS

  CARRIERS.every (c) ->
    if trk.match(c.regex)
      if c.confirm?
        [good, stop] = c.confirm(trk)
        carriers.push c.name if good
        return !stop
      carriers.push c.name
      return true
    true

  carriers

I can just go

guessCarrier.CARRIERS.push 'name: 'ups', regex:/^K\d{10}$/i 
sailrish commented 9 years ago

Sorry for the late response @Russ93 . Wouldn't it just be better to just incorporate the /^K\d{10}$/1 regex for UPS in shipit? What's the reasoning behind making the regex array dynamic and user-modifiable? Do you expect people to be using "custom" carriers, where "UPS" means one thing for one app, and means something totally different for another app? I intend for shipit to work with well recognized carriers, and if there's a format of tracking number that can and should be recognized, that shipit doesn't recognize, then we should just updated's shipit's in-built regexes.

Also, /^K\d{10}$/ is way too generic, without any checksum or other secondary validation. It is a difficult balance between trying to get all possible carrier matches, and minimizing false-positives. But whenever possible, I think shipit should only recognize carriers when it is almost certain it is correct. DHL is usually the most difficult one, because it uses 10 or 7 digit tracking numbers, with little or no checksum validation, or mod-digits. That's the reason guessCarrier currently doesn't return 'dhl' even though DHL is a recognized and supported carrier.