states-utils
A rich library that provides various utility functions to get/check name or USPS code or state capital of states and much more
Installation
Install with npm:
$ npm install states-utils --save
Usage
var STATE_UTILS = ; STATE_UTILS;//=> true STATE_UTILS;//=> California STATE_UTILS;//=> NY
Clone the repo
$ git clone https://github.com/luthraG/states-utils.git
API
isUSAState(key, strict)
This API takes two parameters
- key : It could either be state name or USPS code. It returns boolean value indicating if provided value if valid US state
- strict : It is an optional parameter which when set to true then it does not check for associated states, inhabited territories and federal districts rather it only checks 50 US states
Example
var STATE_UTILS = ; // USPS code in upper caseSTATE_UTILS;//=> true // USPS code in lower caseSTATE_UTILS;//=> true // Invalid USPS codeSTATE_UTILS;//=> false // State name in title caseSTATE_UTILS;//=> true // State name in upper caseSTATE_UTILS;//=> true // Invalid state nameSTATE_UTILS;//=> false // No state name providedSTATE_UTILS;//=> false // Check for an associated stateSTATE_UTILS;//=> true // With strict parameter set to trueSTATE_UTILS;//=> false // Check for an inhabited territorySTATE_UTILS;//=> true // With strict parameter set to trueSTATE_UTILS;//=> false
getStates(strict)
This API returns all the state names of The united States of America.
- It takes an optional parameter - strict which when set to true returns only 50 state names of USA else it returns name of associated states, inhabited territories and federal districts as well besides 50 states of USA
Example
var STATE_UTILS = ; STATE_UTILS;//=> ['Alaska', 'Alabama', ......, 'Texas', ...] // When strict parameter is not presentSTATE_UTILSlength;//=> 59 // When strict parameter is set to falseSTATE_UTILSlength;//=> 59 // When strict parameter is set to trueSTATE_UTILSlength;//=> 50
getUSPSCodes(strict)
This API returns the USPS codes for all the states of The united States of America.
- It takes an optional parameter - strict which when set to true returns only 50 state USPS codes else it returns USPS codes of associated states, inhabited territories and federal districts as well besides 50 states of USA
Example
var STATE_UTILS = ; STATE_UTILS;//=> ['AK', 'AL', ......, 'TX', ...] // When strict parameter is not presentSTATE_UTILSlength;//=> 59 // When strict parameter is set to falseSTATE_UTILSlength;//=> 59 // When strict parameter is set to trueSTATE_UTILSlength;//=> 50
getStateName(key, strict)
Given the USPS code or state capital, this API returns the state name for a valid US state. It takes two parameters:
- key : It could either be state capital name or USPS code.
- strict : It is an optional parameter which when set to true then it does not check for associated states, inhabited territories and federal districts rather it only checks 50 US states
Example
var STATE_UTILS = ; // USPS code in upper caseSTATE_UTILS;//=> 'Texas' // USPS code in lower caseSTATE_UTILS;//=> 'California' // USPS code with strict set to false for an inhabited territorySTATE_UTILS;//=> 'Guam' // USPS code with strict set to true for an inhabited territorySTATE_UTILS;//=> undefined // Invalid USPS codeSTATE_UTILS;//=> undefined // A valid capital nameSTATE_UTILS;//=> 'Arkansas' // An invalid capital nameSTATE_UTILS;//=> undefined // Capital name with strict set to false for an inhabited territory stateSTATE_UTILS;//=> 'American Samoa' // Capital name with strict set to true for an inhabited territory stateSTATE_UTILS;//=> undefined
getUSPSCode(key, strict)
Given the state name, this API returns the USPS code for a valid US state.
- key : It could either be state capital name or state name.
- strict : It is an optional parameter which when set to true then it does not check for associated states, inhabited territories and federal districts rather it only checks 50 US states
Example
var STATE_UTILS = ; // A valid State nameSTATE_UTILS;//=> 'MS' // State name in lower caseSTATE_UTILS;//=> 'MA' // State name in upper caseSTATE_UTILS;//=> 'NY' // State capital nameSTATE_UTILS;//=> 'SD' // Invalid State nameSTATE_UTILS;//=> undefined
getStateCapital(key, strict)
Given the state name or USPS code, this API returns the state capital for a valid US state.
- key : It could either be state USPS code or state name.
- strict : It is an optional parameter which when set to true then it does not check for associated states, inhabited territories and federal districts rather it only checks 50 US states
Example
var STATE_UTILS = ; // A valid State nameSTATE_UTILS;//=> 'Jackson' // A valid USPS codeSTATE_UTILS;//=> 'Denver' // State name for an associated stateSTATE_UTILS;//=> 'Saipan' // State name for an associated state with strict set to trueSTATE_UTILS;//=> undefined // Invalid State nameSTATE_UTILS;//=> undefined
Author
Gaurav Luthra
License
MIT © Gaurav Luthra