A modern JavaScript library for validating and generating Swedish Personal Numbers (Social Security Numbers).
- 🔍 Validate Swedish Personal Numbers
- 🎲 Generate valid random SSNs
- 👥 Generate SSNs with specific gender
- 📅 Support for both YY and YYYY date formats
- 0️⃣ Zero dependencies
- 🌐 Works in Node.js and browsers
- 📦 Modern ES modules and UMD builds
# Using npm
npm install swedish-ssn-tool
# Using yarn
yarn add swedish-ssn-tool
# Using pnpm
pnpm add swedish-ssn-tool
import SwedishSSN from 'swedish-ssn-tool';
// Validate an SSN
const isValid = SwedishSSN.validate('870430-2713');
console.log(isValid); // true
// Generate a random SSN
const randomSSN = SwedishSSN.generateRandomSSN();
console.log(randomSSN); // e.g., '901224-1234'
// Generate SSN for specific gender and date
const femaleSSN = SwedishSSN.generateSSNWithParameters(
new Date('1990-12-24'),
'female'
);
<script src="https://unpkg.com/swedish-ssn-tool/dist/swedish-ssn.min.js"></script>
<script>
// Library is available as global SwedishSSN
const isValid = SwedishSSN.validate('20870430-2713');
console.log(isValid); // true
</script>
Validates a Swedish SSN. Returns true
if valid, false
otherwise.
SwedishSSN.validate('870430-2713'); // true
SwedishSSN.validate('20870430-2713'); // true (YYYY format)
SwedishSSN.validate('invalid'); // false
Generates a random valid Swedish SSN.
const ssn = SwedishSSN.generateRandomSSN();
// Returns formatted SSN like '870430-2713'
Generates a valid SSN with specified parameters.
// Generate female SSN
const femaleSSN = SwedishSSN.generateSSNWithParameters(
new Date('1987-04-30'),
'female'
);
// Generate male SSN
const maleSSN = SwedishSSN.generateSSNWithParameters(
new Date('1987-04-30'),
'male'
);
// Generate random gender SSN
const randomSSN = SwedishSSN.generateSSNWithParameters(
new Date('1987-04-30')
);
# Install dependencies
npm install
# Run tests
npm test
# Build distribution files
npm run build
# Lint code
npm run lint
# Format code
npm run format
Swedish Personal Numbers follow this format:
- YYMMDD-XXXX (10 digits)
- YYYYMMDD-XXXX (12 digits)
Where:
- YY/YYYY = Year
- MM = Month
- DD = Day
- XXXX = Four digit sequence where last digit is a checksum
- The second to last digit indicates gender (even for female, odd for male)
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
teaddict - GitHub
- Update package and simplify build process
- Modernized build system with Rollup
- Added ESM module support
- Improved testing setup
- Added TypeScript types
- Enhanced documentation
- Bug fixes and improvements
- Initial release