You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
23 lines
807 B
JavaScript
23 lines
807 B
JavaScript
2 years ago
|
"use strict";
|
||
|
|
||
|
const execAll = require("fix-esm").require("execall").default;
|
||
|
|
||
|
const regexPossibleModelNumber = /(?:^|\s)[A-Z0-9-]{3,}(?:\s|$)/g;
|
||
|
const regexNumber = /[0-9]/;
|
||
|
const regexLetter = /[A-Z]/;
|
||
|
|
||
|
// To be considered a 'model number', a space-delimited substring of all-caps/numeric must contain at least one letter and one number.
|
||
|
|
||
|
module.exports = function extractModelNumber(string) {
|
||
|
let candidates = execAll(regexPossibleModelNumber, string)
|
||
|
.filter(({ match }) => regexNumber.test(match) && regexLetter.test(match));
|
||
|
|
||
|
if (candidates.length === 1) {
|
||
|
return candidates[0].match;
|
||
|
} else if (candidates.length === 0) {
|
||
|
throw new Error(`No model number found in string`);
|
||
|
} else {
|
||
|
throw new Error(`More than one potential model number found in string: ${candidates.join(", ")}`);
|
||
|
}
|
||
|
};
|