Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/CommonValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,33 @@ module.exports = {
let checkSum = line.substr(68, 1);
return numberRegExp.test(checkSum);
},
checkCheckSum: function(line) {
let result = {isValid: true};

let providedCheckSum = Number(line.slice(-1)); //extract the checksum
line = line.slice(0, -1); //remove checksum from line
let checksum = 0;
line.split('').forEach(function(element) {
if (element === '-') {
checksum++;
}
if (!isNaN(element)) {
checksum += Number(element);
}
});

checksum = checksum % 10;
if (checksum === providedCheckSum) {

} else {
result = {
isValid: false,
calculatedCheckSum: checksum
};
}

return result;
},
validSatNo: function(line) {
let result = false;
let satNo = line.substr(2, 5);
Expand Down
6 changes: 3 additions & 3 deletions src/LineOneValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ module.exports = {
let result = false;
let firstChar = line1.substr(33, 1);
let secondChar = line1.substr(34, 1);
if (CommonValidator.isPosNegOrZero(firstChar) && CommonValidator.isPeriod(secondChar)) {
if ((CommonValidator.isPosNegOrZero(firstChar) || CommonValidator.isSpace(line1, 33) ) && CommonValidator.isPeriod(secondChar)) {
let firstMeanMotionFraction = line1.substr(35, 8);
if (eightDigitsRegExp.test(firstMeanMotionFraction)) {
result = true;
Expand All @@ -64,7 +64,7 @@ module.exports = {
let preDashNums = line1.substr(45, 5);
let hyphen = line1.substr(50, 1);
let lastChar = line1.substr(51, 1);
if (CommonValidator.isSpace(line1, 44)) {
if (CommonValidator.isPosNegOrZero(line1.substr(44, 1)) || CommonValidator.isSpace(line1, 44)) {
if (fiveDigitsRegExp.test(preDashNums)) {
if (CommonValidator.isHyphen(hyphen)) {
if (!isNaN(lastChar)) {
Expand All @@ -82,7 +82,7 @@ module.exports = {
let preDashNums = line1.substr(54, 5);
let hyphen = line1.substr(59, 1);
let lastChar = line1.substr(60, 1);
if (CommonValidator.isPosNegOrZero(firstChar)) {
if (CommonValidator.isPosNegOrZero(firstChar) || CommonValidator.isSpace(line1, 53)) {
if (fiveDigitsRegExp.test(preDashNums)) {
if (CommonValidator.isHyphen(hyphen)) {
if (!isNaN(lastChar)) {
Expand Down
18 changes: 18 additions & 0 deletions src/TLEValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@ module.exports = {
let result = {
isValid: lineOneRegExp.test(line1)
};
if (result.isValid) {
let checkSumResult = CommonValidator.checkCheckSum(line1);
if (checkSumResult.isValid === false) {
result = {
invalidSubStr: [68, 1],
message: "Incorrect Checksum, expected: "+checkSumResult.calculatedCheckSum
};
}
}
if (!result.isValid) {
if (!LineOneValidator.validLineOneNumber(line1)) {
result = {
Expand Down Expand Up @@ -188,6 +197,15 @@ module.exports = {
let result = {
isValid: lineTwoRegExp.test(line2)
};
if (result.isValid) {
let checkSumResult = CommonValidator.checkCheckSum(line2);
if (checkSumResult.isValid === false) {
result = {
invalidSubStr: [68, 1],
message: "Incorrect Checksum, expected: "+checkSumResult.calculatedCheckSum
};
}
}
if (!result.isValid) {
if (!LineTwoValidator.validLineTwoNumber(line2)) {
result = {
Expand Down
12 changes: 12 additions & 0 deletions test/unit/CommonValidator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ test('validates invalid checksum', () => {
expect(CommonValidator.validCheckSum("1 25544U 98067777 08264.51782528 -.00002182 00000-0 -00000-0 0 0001 ")).toBe(false);
});

test('validates checksum calculation', () => {
expect(CommonValidator.checkCheckSum("1 41868U 16061G 20139.90454403 +.00034898 +87835-5 +88966-4 0 9994")).toEqual({
isValid: true,
});
});
test('validates invalid checksum calculation', () => {
expect(CommonValidator.checkCheckSum("1 41868U 16061G 20139.90454403 +.00034898 +87835-5 +88966-4 0 9995")).toEqual({
isValid: false,
calculatedCheckSum: 4
});
});

test('validates valid satNo', () => {
const validLineOne = "1 25544 ";
expect(CommonValidator.validSatNo(validLineOne)).toBe(true);
Expand Down
16 changes: 16 additions & 0 deletions test/unit/TLEValidator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,14 @@ test('line1 invalid checksum', () => {
});
});

test('line1 incorrect checksum', () => {
expect(TLEValidator.validateLineOneWithMessage("1 25544U 98067A 08264.51782528 -.00002182 00000-0 -11606-4 0 2928")).toEqual({

invalidSubStr: [68, 1],
message: "Incorrect Checksum, expected: 7"
});
});

test('line1 invalid space', () => {
expect(TLEValidator.validateLineOneWithMessage("1a25544U 98067A 08264.51782528 -.00002182 00000-0 -11606-4 0 2927")).toEqual({

Expand Down Expand Up @@ -230,6 +238,14 @@ test('line2 invalid Checksum', () => {
});
});

test('line2 incorrect Checksum', () => {
expect(TLEValidator.validateLineTwoWithMessage("2 25544 51.6416 247.4627 0006703 130.5360 325.0288 15.72125391563538")).toEqual({

invalidSubStr: [68, 1],
message: "Incorrect Checksum, expected: 7"
});
});

test('line2 invalid space', () => {
expect(TLEValidator.validateLineTwoWithMessage("2.25544 51.6416 247.4627 0006703 130.5360 325.0288 15.7212539156353a")).toEqual({
invalidSubStr: [1, 1],
Expand Down