Skip to content

Instantly share code, notes, and snippets.

@hmps
Created February 4, 2019 12:02
Show Gist options
  • Save hmps/238b6dcc9b47b004d2f2cba87a9e8192 to your computer and use it in GitHub Desktop.
Save hmps/238b6dcc9b47b004d2f2cba87a9e8192 to your computer and use it in GitHub Desktop.
Random SSN
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
var age = Math.floor(Math.random() * Math.floor(70));
var year = new Date().getFullYear() - age + '';
var month = Math.floor(Math.random() * Math.floor(12)) + 1; // 1-12
var monthWithZero = month < 10 ? '0' + month : month.toString();
var daysInMonth = new Date(year, month, 0).getDate();
var day = Math.floor(Math.random() * Math.floor(daysInMonth)) + 1 + '';
var dayWithZero = day < 10 ? '0' + day : day.toString();
var firstThree = Math.floor(Math.random() * (999 - 100 + 1) + 100) + '';
var luhn = [2, 1, 2, 1, 2, 1, 2, 1, 2];
var ssnSum = [].concat(_toConsumableArray(year.slice(2, 4).split('')), _toConsumableArray(monthWithZero.split('')), _toConsumableArray(dayWithZero.split('')), _toConsumableArray(firstThree.split(''))).map(function (s) {
return Number(s);
}).map(function (n, i) {
return n * luhn[i];
}).reduce(function (prev, x) {
return prev.concat.apply(prev, _toConsumableArray(x.toString().split('')));
}, []).reduce(function (prev, x) {
return prev + Number(x);
}, 0);
var lastDigit = 10 - ssnSum % 10;
var ssn = year + monthWithZero + dayWithZero + '-' + firstThree + lastDigit;
const age = Math.floor(Math.random() * Math.floor(70));
const year = new Date().getFullYear() - age + '';
const month = Math.floor(Math.random() * Math.floor(12)) + 1; // 1-12
const monthWithZero = month < 10 ? '0' + month : month.toString();
const daysInMonth = new Date(year, month, 0).getDate();
const day = Math.floor(Math.random() * Math.floor(daysInMonth)) + 1 + '';
const dayWithZero = day < 10 ? '0' + day : day.toString();
const firstThree = Math.floor(Math.random() * (999 - 100 + 1) + 100) + '';
const luhn = [2,1,2,1,2,1,2,1,2];
const ssnSum = [
...year.slice(2, 4).split(''),
...monthWithZero.split(''),
...dayWithZero.split(''),
...firstThree.split('')
]
.map(s => Number(s))
.map((n, i) => n * luhn[i])
.reduce((prev, x) => prev.concat(...x.toString().split('')), [])
.reduce((prev, x) => prev + Number(x), 0)
const lastDigit = 10 - (ssnSum % 10);
const ssn = year + monthWithZero + dayWithZero + '-' + firstThree + lastDigit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment