Skip to content

Commit 02731ca

Browse files
committed
Add improved docs
1 parent 383784c commit 02731ca

File tree

2 files changed

+118
-30
lines changed

2 files changed

+118
-30
lines changed

index.js

+16-7
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,28 @@
11
/**
22
* @typedef Options
3+
* Configuration (optional).
4+
* @property {boolean} [keepSpaces=false]
5+
* Keep superfluous whitespace.
6+
* Whitespace is turned into a space by default.
37
* @property {Array<string>} [stopwords]
4-
* @property {boolean} [keepSpaces]
8+
* List of stopwords.
9+
* When a lowercased word is included in this list, it will be used as
10+
* lowercase.
11+
* Otherwise words are capitalized.
512
*/
613

714
const stopwords = 'a an and at but by for in nor of on or so the to up yet'
815
const defaults = stopwords.split(' ')
916

1017
/**
18+
* Convert a value to title case using AP/APA style.
19+
*
1120
* @param {string} [value]
21+
* Short text of unknown casing.
1222
* @param {Options} [options]
23+
* Configuration (optional).
1324
* @returns {string}
25+
* Title-cased version of `value`.
1426
*/
1527
export function apStyleTitleCase(value, options) {
1628
const configuration = options || {}
@@ -26,13 +38,10 @@ export function apStyleTitleCase(value, options) {
2638
.map((word, index, all) => {
2739
if (/\s+/.test(word)) return keep ? word : ' '
2840
if (splitter.test(word)) return word
41+
const lower = word.toLowerCase()
2942

30-
if (
31-
index !== 0 &&
32-
index !== all.length - 1 &&
33-
stop.includes(word.toLowerCase())
34-
) {
35-
return word.toLowerCase()
43+
if (index !== 0 && index !== all.length - 1 && stop.includes(lower)) {
44+
return lower
3645
}
3746

3847
return capitalize(word)

readme.md

+102-23
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,61 @@
55
[![Downloads][downloads-badge]][downloads]
66
[![Size][size-badge]][size]
77

8-
Convert a value to title case using [AP][]/[APA][] style.
8+
Convert a value to [AP][]/[APA][] title case.
9+
10+
## Contents
11+
12+
* [What is this?](#what-is-this)
13+
* [When should I use this?](#when-should-i-use-this)
14+
* [Install](#install)
15+
* [Use](#use)
16+
* [API](#api)
17+
* [`apStyleTitleCase(value[, options])`](#apstyletitlecasevalue-options)
18+
* [Algorithm](#algorithm)
19+
* [Types](#types)
20+
* [Compatibility](#compatibility)
21+
* [Contribute](#contribute)
22+
* [Security](#security)
23+
* [License](#license)
24+
25+
## What is this?
26+
27+
This small package turns a sentence into title case.
28+
29+
## When should I use this?
30+
31+
You can use this when you have short text of unknown casing and want to display
32+
it in a heading or so.
933

1034
## Install
1135

12-
[npm][]:
36+
This package is [ESM only][esm].
37+
In Node.js (version 14.14+, 16.0+), install with [npm][]:
1338

14-
```bash
39+
```sh
1540
npm install ap-style-title-case
1641
```
1742

43+
In Deno with [`esm.sh`][esmsh]:
44+
45+
```js
46+
import {apStyleTitleCase} from 'https://esm.sh/ap-style-title-case@1'
47+
```
48+
49+
In browsers with [`esm.sh`][esmsh]:
50+
51+
```html
52+
<script type="module">
53+
import {apStyleTitleCase} from 'https://esm.sh/ap-style-title-case@1?bundle'
54+
</script>
55+
```
56+
1857
## Use
1958

2059
```js
21-
var titleCase = require('ap-style-title-case')
60+
import {apStyleTitleCase} from 'ap-style-title-case'
2261

23-
console.log(titleCase('why sunless tanning is A hot trend'))
62+
console.log(apStyleTitleCase('why sunless tanning is A hot trend'))
2463
// 'Why Sunless Tanning Is a Hot Trend'
2564
```
2665

@@ -30,25 +69,35 @@ console.log(titleCase('why sunless tanning is A hot trend'))
3069

3170
Convert a value (`string`) to title case (`string`) using [AP][]/[APA][] style.
3271

33-
* `options.keepSpaces` (`boolean`, default: `false`)
34-
— Superfluous whitespace is ignored by default, turn this on to allow it
35-
* `options.stopwords` (`Array.<string>`, default: see list below)
36-
— Lowercase the given stop words instead of the defaults
72+
##### `options`
73+
74+
Configuration (optional).
75+
76+
###### `options.keepSpaces`
3777

38-
## The Rules
78+
Keep superfluous whitespace (`boolean`, default: `false`).
79+
Whitespace is turned into a space by default.
3980

40-
* Always capitalize the first word, even if it’s a stop word
41-
* Always capitalize the last word, even if it’s a stop word
42-
* Lowercase these words: `a an and at but by for in nor of on or so the to up
43-
yet`
81+
###### `options.stopwords`
4482

45-
> Many writers make the error of leaving “to be” verbs in lower case.
46-
> Even though “is,” “are,” “was,” and “be,” are all short words, they should
83+
List of stopwords (`Array<string>`, default: see below).
84+
When a lowercased word is included in this list, it will be used as lowercase.
85+
Otherwise words are capitalized.
86+
87+
## Algorithm
88+
89+
* always capitalize the first word, even if it’s a stop word
90+
* always capitalize the last word, even if it’s a stop word
91+
* lowercase these words: `a`, `an`, `and`, `at`, `but`, `by`, `for`, `in`,
92+
`nor`, `of`, `on`, `or`, `so`, `the`, `to`, `up`, `yet`
93+
94+
> Many writers make the error of leaving `to be` verbs in lower case.
95+
> Even though `is`, `are`, `was`, and `be`, are all short words, they should
4796
> still be capitalized in a title because they are verbs.
4897
>
4998
> When you write titles that contain prepositions, your word processor will
50-
> likely tell you that you should leave words like with,” “about,” and “around”
51-
> lowercase.
99+
> likely tell you that you should leave words like `with`, `about`, and
100+
> `around` lowercase.
52101
> Defiantly look past the squiggly line indicating a potential error, and
53102
> remember that in AP title case, prepositions with four or more letters should
54103
> be capitalized.
@@ -57,12 +106,34 @@ Convert a value (`string`) to title case (`string`) using [AP][]/[APA][] style.
57106
> AP style does not recommend the use of title case for newspaper headlines, but
58107
> rather sentence case.
59108
60-
## References
109+
###### References
110+
111+
* [*How to correctly use AP (and APA) style title case* on
112+
`bkacontent.com`](https://www.bkacontent.com/how-to-correctly-use-apa-style-title-case/)
113+
* [*AP Stylebook* on `wikipedia.com`](https://en.wikipedia.org/wiki/AP_Stylebook)
114+
* [*APA style* on `wikipedia.com`](https://en.wikipedia.org/wiki/APA_style)
115+
* [*Title case and sentence case capitalization in APA style* on
116+
`apastyle.org`](http://blog.apastyle.org/apastyle/2012/03/title-case-and-sentence-case-capitalization-in-apa-style.html)
117+
118+
## Types
119+
120+
This package is fully typed with [TypeScript][].
121+
It exports the additional type `Options`.
122+
123+
## Compatibility
124+
125+
This package is at least compatible with all maintained versions of Node.js.
126+
As of now, that is Node.js 14.14+ and 16.0+.
127+
It also works in Deno and modern browsers.
61128

62-
* <https://www.bkacontent.com/how-to-correctly-use-apa-style-title-case/>
63-
* <https://en.wikipedia.org/wiki/AP_Stylebook>
64-
* <https://en.wikipedia.org/wiki/APA_style>
65-
* <http://blog.apastyle.org/apastyle/2012/03/title-case-and-sentence-case-capitalization-in-apa-style.html>
129+
## Contribute
130+
131+
Yes please!
132+
See [How to Contribute to Open Source][contribute].
133+
134+
## Security
135+
136+
This package is safe.
66137

67138
## License
68139

@@ -88,6 +159,14 @@ Convert a value (`string`) to title case (`string`) using [AP][]/[APA][] style.
88159

89160
[npm]: https://docs.npmjs.com/cli/install
90161

162+
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
163+
164+
[esmsh]: https://esm.sh
165+
166+
[typescript]: https://www.typescriptlang.org
167+
168+
[contribute]: https://opensource.guide/how-to-contribute/
169+
91170
[license]: license
92171

93172
[author]: http://zeke.sikelianos.com

0 commit comments

Comments
 (0)