Simple encoder and decoder for the USPS' IMB service based on:
In that spirit, I've included their comment below:
You may use this code for any purpose, with no restrictions. However, there is NO WARRANTY for this code; use it at your own risk. This work is released under the Creative Commons Zero License. To view a copy of this license, visit https://creativecommons.org/publicdomain/zero/1.0/
This is a simple and small module (6kb) designed to easily transform an IMB ready object to an IMB String and back again. It supports both synchronous and asynchronous (callback) implementations.
Synchronous
const imb = require('imb');
const imb_string = imb.encode({
delivery_pt: "",
zip: "",
plus4: "",
barcode_id: "12",
service_type: "234",
mailer_id: "567094",
serial_num: "987654321"
});
// Result
// ATTFAATTFTADFDATDDADAATTTTTTTTADFFFFFDFAFATTDAADATDDDTADAFFDFDTFT
Asynchronous
const imb = require('imb');
imb.encode({
delivery_pt: "",
zip: "",
plus4: "",
barcode_id: "12",
service_type: "234",
mailer_id: "567094",
serial_num: "987654321"
}, (err, imb_string) => {
if (err)
throw new Error(err);
// imb_string
// ATTFAATTFTADFDATDDADAATTTTTTTTADFFFFFDFAFATTDAADATDDDTADAFFDFDTFT
});
Synchronous
const imb = require('imb');
const imb_object = imb.decode("ATTFAATTFTADFDATDDADAATTTTTTTTADFFFFFDFAFATTDAADATDDDTADAFFDFDTFT");
// imb_object
// {
// barcode_id: '12',
// service_type: '234',
// mailer_id: '567094',
// serial_num: '987654321'
// }
Asynchronous
const imb = require('imb');
imb.decode("ATTFAATTFTADFDATDDADAATTTTTTTTADFFFFFDFAFATTDAADATDDDTADAFFDFDTFT", (err, imb_object) => {
if (err)
throw new Error(err);
// imb_object
// {
// barcode_id: '12',
// service_type: '234',
// mailer_id: '567094',
// serial_num: '987654321'
// }
});
I have included a basic CLI that should help easily encode or decode single records.
Option | Descriptive | Abbreviated | Meaning |
---|---|---|---|
Type | --type | -t | "encode" or "decode" |
Barcode | --barcode | -b | Automatically set type to 'decode' and decode value |
You should be able to encode an object using:
$ imb
? Would you like to encode or decode an IMB? (Use arrow keys)
❯ encode
decode
? Delivery Point 2-Characters (Optional):
? Zip (Optional):
? Zip Plus 4 (Optional):
? Barcode ID: 12
? Service Type: 234
? USPS Mailer ID: 567094
? USPS Serial Number: 987654321
Encoded output
ATTFAATTFTADFDATDDADAATTTTTTTTADFFFFFDFAFATTDAADATDDDTADAFFDFDTFT
And decoding an object is even easier:
$ imb
? Would you like to encode or decode an IMB? (Use arrow keys)
encode
❯ decode
? Barcode String: ATTFAATTFTADFDATDDADAATTTTTTTTADFFFFFDFAFATTDAADATDDDTADAFFDFDTFT
Decoded output
barcode_id: 12
service_type: 234
mailer_id: 567094
serial_num: 987654321
Alternatively, Decoding is the default behavior so you can simply run:
$ imb ATTFAATTFTADFDATDDADAATTTTTTTTADFFFFFDFAFATTDAADATDDDTADAFFDFDTFT
Decoded output
barcode_id: 12
service_type: 234
mailer_id: 567094
serial_num: 987654321
Hey,
I hope that you were able to find this module helpful. If so, let me know! Who doesn't love hearing that they made a difference, even if it's a little one. Don't worry, it's totally free - but if it really made a difference, you can send a little love using the link below 🍻!
Cheers!