From 5abfa808690795ea75c972b8e28687ea395d6dfd Mon Sep 17 00:00:00 2001 From: Cedric Hoelzl Date: Thu, 2 Jul 2020 14:13:39 +0200 Subject: [PATCH] kk --- tracking.js | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 tracking.js diff --git a/tracking.js b/tracking.js new file mode 100644 index 0000000..75a86a4 --- /dev/null +++ b/tracking.js @@ -0,0 +1,60 @@ +const courier_info = { + ups: { + patterns: [new RegExp(/\b(1Z ?[0-9A-Z]{3} ?[0-9A-Z]{3} ?[0-9A-Z]{2} ?[0-9A-Z]{4} ?[0-9A-Z]{3} ?[0-9A-Z]|T\d{3} ?\d{4} ?\d{3})\b/i)], + tracking_url: "http://wwwapps.ups.com/WebTracking/processInputRequest?TypeOfInquiryNumber=T&Inq" + + "uiryNumber1=" + }, + usps: { + patterns: [ + new RegExp(/\b((420 ?\d{5} ?)?(91|92|93|94|95|01|03|04|70|23|13)\d{2} ?\d{4} ?\d{4} ?\d{4} ?\d{4}( ?\d{2,6})?)\b/i), new RegExp(/\b((M|P[A-Z]?|D[C-Z]|LK|E[A-C]|V[A-Z]|R[A-Z]|CP|CJ|LC|LJ) ?\d{3} ?\d{3} ?\d{3} ?[A-Z]?[A-Z]?)\b/i), new RegExp(/\b(82 ?\d{3} ?\d{3} ?\d{2})\b/i) + ], + tracking_url: "https://tools.usps.com/go/TrackConfirmAction?qtc_tLabels1=" + }, + ontrac: { + patterns: [new RegExp(/\b(C\d{14})\b/i)], + tracking_url: "http://www.ontrac.com/trackres.asp?tracking_number=" + }, + dhl: { + patterns: [new RegExp(/\b(\d{4}[- ]?\d{4}[- ]?\d{2}|\d{3}[- ]?\d{8}|[A-Z]{3}\d{7})\b/i)], + tracking_url: "http://www.dhl.com/content/g0/en/express/tracking.shtml?brand=DHL&AWB=" + }, + + fedex: { + patterns: [new RegExp(/\b(((96\d\d|6\d)\d{3} ?\d{4}|96\d{2}|\d{4}) ?\d{4} ?\d{4}( ?\d{3})?)\b/i)], + tracking_url: "http://www.fedex.com/Tracking?language=english&cntry_code=us&tracknumbers=" + }, + swisspost: { + patterns: [new RegExp(/\b(99(\d{16}))\b/i)], + tracking_url: "https://service.post.ch/ekp-web/ui/entry/search/" + } +}; + +const getCourier = tr => Object + .keys(courier_info) + .filter(c => courier_info[c].patterns.filter(p => p.test(tr)).length > 0); + +const getCourierOne = tr => getCourier(tr)[0]; + +const isCourier = (tr, courier) => getCourier(tr).indexOf(courier.toLowerCase()) > -1; + +const getTrackingUrl = (tr, courier) => courier + ? courier_info[courier.toLowerCase()] && courier_info[courier.toLowerCase()].tracking_url + tr + : courier_info[getCourier(tr)[0]].tracking_url + tr; + +const injectPatterns = (courier, patt) => !courier || !courier_info[courier.toLowerCase()] + ? false + : courier_info[courier.toLowerCase()] + .patterns + .push(new RegExp(patt)); + +const isValid = (tr) => getCourier(tr).length > 0; + + + module.exports = { + getCourier, + getCourierOne, + isCourier, + getTrackingUrl, + injectPatterns, + isValid +};