Skip to content

Instantly share code, notes, and snippets.

@stephenfeather
Created June 28, 2023 14:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stephenfeather/520370bfcc358a6587b9cba641efc52c to your computer and use it in GitHub Desktop.
Save stephenfeather/520370bfcc358a6587b9cba641efc52c to your computer and use it in GitHub Desktop.
A javascript function to parse a Federal Firearms License (FFL) number
/**
* Parses the FFL number and extracts relevant information.
*
* @param {string} fflNumber - The FFL number to parse.
* @returns {object} - An object containing the parsed FFL information or an error object.
* If parsing is successful, the object will have the following properties:
* - ffl_number: The full FFL number.
* - region: The region of the FFL.
* - irs_district: The IRS district of the FFL.
* - fips_code: The FIPS code of the FFL.
* - license_type: The type of license.
* - expiration_date: The expiration date of the FFL.
* - unique_code: The unique code of the FFL.
* If any information cannot be calculated, an error object will be returned.
* The error object will have the following properties:
* - error: A descriptive error message.
* - ffl_number: The full FFL number (optional, if available).
*
* @author Stephen Feather
* @copyright Copyright (c) 2023
*/
function parseFFLNumber(fflNumber) {
const regions = {
1: 'Region 1: Southeast',
3: 'Region 3: Midwest',
4: 'Region 4: Central',
5: 'Region 5: Southwest',
6: 'Region 6: North Atlantic',
8: 'Region 8: Mid Atlantic',
9: 'Region 9: Western'
};
const months = {
A: 'January',
B: 'February',
C: 'March',
D: 'April',
E: 'May',
F: 'June',
G: 'July',
H: 'August',
J: 'September',
K: 'October',
L: 'November',
M: 'December'
};
const licenseTypes = {
1: 'Type 01: Firearm Dealer/Gunsmith',
2: 'Type 02: Pawnbroker',
3: 'Type 03: Collector',
6: 'Type 06: Manufacturer of Ammunition',
7: 'Type 07: Manufacturer of Firearms',
8: 'Type 08: Importer of Firearms',
9: 'Type 09: Dealer of "Destructive Devices"',
10: 'Type 10: Manufacturer of "Destructive Devices"',
11: 'Type 11: Importer of "Destructive Devices"'
};
const fflInfo = {};
// First section: FFL Number
fflInfo.ffl_number = fflNumber;
// Second section: Region
const regionDigit = parseInt(fflNumber[0]);
if (regions[regionDigit]) {
fflInfo.region = regions[regionDigit];
} else {
return {
error: 'Invalid Region',
ffl_number: fflNumber
};
}
// Third section: IRS district
fflInfo.irs_district = parseInt(fflNumber.substr(2, 2));
// Fourth section: FIPS code
fflInfo.fips_code = fflNumber.substr(5, 3);
// Fifth section: License type
const licenseTypeDigit = parseInt(fflNumber.substr(9, 2));
if (licenseTypes[licenseTypeDigit]) {
fflInfo.license_type = licenseTypes[licenseTypeDigit];
} else {
return {
error: 'Invalid License Type',
ffl_number: fflNumber
};
}
// Sixth section: Expiration date
const yearDigit = parseInt(fflNumber[12]);
const monthLetter = fflNumber[13];
const currentYear = new Date().getFullYear();
const yearPrefix = currentYear - (currentYear % 10);
const year = yearPrefix + yearDigit;
if (months[monthLetter]) {
fflInfo.expiration_date = months[monthLetter] + ' ' + year;
} else {
return {
error: 'Invalid Expiration Month',
ffl_number: fflNumber
};
}
// Seventh section: Unique code
fflInfo.unique_code = fflNumber.substr(15);
return fflInfo;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment