-
Notifications
You must be signed in to change notification settings - Fork 0
/
isSSN.js
105 lines (88 loc) · 2.95 KB
/
isSSN.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/* eslint-disable */
const {isSSN} = require("../functions/isSSN");
const fs = require("fs");
function generateImageBadgeURL(label, value, passed) {
return `https://img.shields.io/badge/${encodeURIComponent(label)}-${value}-${passed === 0 ? '13b285' : 'ff69b4'}`;}
function generateTestBadge(functionName, numberOfPassed, numberOfFailed) {
const url = generateImageBadgeURL(functionName + '()', encodeURIComponent(numberOfPassed + ' passed, ' + numberOfFailed + ' failed.'), numberOfFailed);
return '\n\n### Tests for ' + functionName + '\n\n' + '![' + functionName + '](' + url + ')';}
function addToReadme(str) {fs.appendFile('./README.md', str, function (err) {if (err) throw err;console.log('String added to the file');});}/**
* Test the isSSN() function for various SSN formats, case-insensitive matches, and SSNs with special characters such as hyphens.
* Extreme cases to handle are SSNs with non-standard formats.
*/
function testIsSSN() {
let number_of_tests_passed = 0;
let number_of_tests_failed = 0;
const name_of_function = "isSSN";
// Valid SSN format
try {
const validSSN = isSSN("123-45-6789");
if (validSSN) {
number_of_tests_passed++;
} else {
number_of_tests_failed++;
}
} catch (err) {
number_of_tests_failed++;
}
// Invalid SSN format
try {
const invalidSSN = isSSN("1234-56789");
if (!invalidSSN) {
number_of_tests_passed++;
} else {
number_of_tests_failed++;
}
} catch (err) {
number_of_tests_failed++;
}
// Another invalid SSN format
try {
const invalidFormatSSN = isSSN("123-4-6789");
if (!invalidFormatSSN) {
number_of_tests_passed++;
} else {
number_of_tests_failed++;
}
} catch (err) {
number_of_tests_failed++;
}
// Special characters in SSN
try {
const specialCharSSN = isSSN("123#45$6789");
if (!specialCharSSN) {
number_of_tests_passed++;
} else {
number_of_tests_failed++;
}
} catch (err) {
number_of_tests_failed++;
}
// Empty string as input
try {
const emptyStringSSN = isSSN("");
if (!emptyStringSSN) {
number_of_tests_passed++;
} else {
number_of_tests_failed++;
}
} catch (err) {
number_of_tests_failed++;
}
// Whitespace input
try {
const whitespaceSSN = isSSN(" ");
if (!whitespaceSSN) {
number_of_tests_passed++;
} else {
number_of_tests_failed++;
}
} catch (err) {
number_of_tests_failed++;
}
// Calling the addToReadme function with the generated test badge
addToReadme(generateTestBadge(name_of_function, number_of_tests_passed, number_of_tests_failed));
}
module.exports = {
testIsSSN
};