Wednesday, 14 February, 2018 UTC


Summary

After a quick search I didn’t find any nodejs solution to validate tracking number, it was easy decision to just build one!
getting started:
  • clone this template so we can use es6+
  • cd folder && npm i
  • we will add the code to the src folder
How to validate tracking number ? a quick search return regex format for the big couriers.
**File structure: **
- config -> store the regex format for each courier - helpers -> functions: getCourier, isValid - dist -> client code - main index.js
for best practice I tried to write modular
**getCourier**
getCourier(tr) { for (const key in courier_info) { const courier = courier_info[key] if (courier.test(tr)) { return key; } } return null; }
**isValid**
isValid(tr, type) { for (const key in courier_info) { const courier = courier_info[key] if (courier.test(tr) && key === type.toLowerCase()) { return true; } } return false; }
Final steps:
add some examples (demo)
edit readme file and fill the details of the package in the package.json
run yarn build && npm publish
check it out [GITHUB ](https://github.com/niradler/tracking-number-validation) [NPM](https://www.npmjs.com/package/tracking-number-validation)