sebastiansandqvist/s-valid

View on GitHub
zipCodeLong.js

Summary

Maintainability
A
3 hrs
Test Coverage
'use strict';

// ----- zip code
//        --    1. input must be a string
//        --    2. check number
//        --    3. perform regex test
// ---------------------------------------
module.exports = function(str) {

    // [1] throws here if not a string
    str = str.toLowerCase();

    // [2] every zip code begins with a number, and none are < 00501
    var numeric = parseInt(str, 10);

    if (isNaN(numeric) || numeric < 501) {
        return false;
    }

    // [3]
    var regex = /^\d{5}(-\d{4})?$/;

    return regex.test(str);

};