Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 50 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,27 +67,30 @@ import * as journalize from 'journalize';
- [aptime](#aptime)
- [Parameters](#parameters-3)
- [Examples](#examples-3)
- [capfirst](#capfirst)
- [apstate](#apstate)
- [Parameters](#parameters-4)
- [Examples](#examples-4)
- [intcomma](#intcomma)
- [capfirst](#capfirst)
- [Parameters](#parameters-5)
- [Examples](#examples-5)
- [intword](#intword)
- [intcomma](#intcomma)
- [Parameters](#parameters-6)
- [Examples](#examples-6)
- [ordinal](#ordinal)
- [intword](#intword)
- [Parameters](#parameters-7)
- [Examples](#examples-7)
- [pluralize](#pluralize)
- [ordinal](#ordinal)
- [Parameters](#parameters-8)
- [Examples](#examples-8)
- [widont](#widont)
- [pluralize](#pluralize)
- [Parameters](#parameters-9)
- [Examples](#examples-9)
- [yesno](#yesno)
- [widont](#widont)
- [Parameters](#parameters-10)
- [Examples](#examples-10)
- [yesno](#yesno)
- [Parameters](#parameters-11)
- [Examples](#examples-11)

### apdate

Expand Down Expand Up @@ -196,6 +199,46 @@ journalize.aptime();
// returns '6:45 p.m.' (pretend it is actually 6:45 p.m. right now)
```

### apstate

Returns a state name, state abbreviation, or AP-formatted state string that corresponds with the supplied input and input/output types.

#### Parameters

- `val` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**
- `from` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**
- `to` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**

#### Examples

```javascript
var journalize = require('journalize');

// State name to AP style
journalize.apstate('New York', 'state', 'ap');
// returns 'N.Y.'

//State name to state abbreviation
journalize.apstate('Wisconsin', 'state', 'abbrev');
// returns 'WI'

//State abbreviation to AP style
journalize.apstate('CA', 'abbrev', 'ap');
// returns 'Calif.'

//State abbreviation to state name
journalize.apstate('SD', 'abbrev', 'state');
// returns 'South Dakota'

//AP style to state abbreviation
journalize.apstate('Ariz.', 'ap', 'abbrev');
// returns 'AZ'

//AP style to state name
journalize.apstate('N.H.', 'ap', 'state');
// returns 'New Hampshire'
```

Returns **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**

### capfirst
Expand Down
48 changes: 48 additions & 0 deletions __tests__/apstate.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import apstate from '../src/apstate';

describe('apstate', () => {
it('should convert state to ap style', () => {
expect(apstate('New York', 'state', 'ap')).toBe('N.Y.');
});

it('should convert state to abbrev', () => {
expect(apstate('Wisconsin', 'state', 'abbrev')).toBe('WI');
});

it('should convert abbrev to ap style`', () => {
expect(apstate('CA', 'abbrev', 'ap')).toBe('Calif.');
});

it('should convert abbrev to state`', () => {
expect(apstate('SD', 'abbrev', 'state')).toBe('South Dakota');
});

it('should convert ap style to abbrev', () => {
expect(apstate('Ariz.', 'ap', 'abbrev')).toBe('AZ');
});

it('should convert ap style to state', () => {
expect(apstate('N.H.', 'ap', 'state')).toBe('New Hampshire');
});

it('should throw error for invalid argument', () => {
expect(() => {
apstate('N.H.', 'blah', 'state');
}).toThrow(Error);
expect(() => {
apstate('N.H.', 'state', 'blah');
}).toThrow(Error);
});

it('should throw error for invalid state or mismatched state', () => {
expect(() => {
apstate('i am not a state', 'blah', 'state');
}).toThrow(Error);
expect(() => {
apstate('N.H.', 'state', 'state');
}).toThrow(Error);
expect(() => {
apstate('New Hampshire', 'abbrev', 'state');
}).toThrow(Error);
});
});
Loading