plurly 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +18 -0
- package/LICENSE +21 -0
- package/README.md +177 -2
- package/dist/index.cjs +337 -0
- package/dist/index.d.cts +44 -0
- package/dist/index.d.ts +44 -0
- package/dist/index.js +314 -0
- package/package.json +65 -4
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 1.0.0
|
|
4
|
+
|
|
5
|
+
First full release: pluralize and singularize English words.
|
|
6
|
+
|
|
7
|
+
- `plural`, `singular`, `isPlural` and `isSingular`, with irregular, Latin and Greek forms and uncountable words (including software words like data and metadata).
|
|
8
|
+
- Words already in the requested form stay the same: `plural("cats")` is `"cats"`.
|
|
9
|
+
- Keeps casing, and changes only the last word of names like `userProfile`, `user_category` and `blog post`.
|
|
10
|
+
- Acronyms: `API` → `APIs`, `URLs` → `URL`.
|
|
11
|
+
- `count(n, word, options)` for "3 files", with `zero` and locale number formatting.
|
|
12
|
+
- A default export with the familiar `(word, count, inclusive)` signature, plus `addIrregularRule`, `addUncountableRule`, `addPluralRule` and `addSingularRule`.
|
|
13
|
+
- `createPlurly()` for separate instances with their own rules.
|
|
14
|
+
- Zero dependencies, TypeScript types, ESM and CommonJS. Never throws; linear time.
|
|
15
|
+
|
|
16
|
+
## 0.0.1
|
|
17
|
+
|
|
18
|
+
Name reservation.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 CK Giri
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,178 @@
|
|
|
1
|
-
|
|
1
|
+
<div align="center">
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
<h1>plurly</h1>
|
|
4
|
+
|
|
5
|
+
<h3>Pluralize and singularize English words, the right way.</h3>
|
|
6
|
+
|
|
7
|
+
<p>
|
|
8
|
+
<a href="https://www.npmjs.com/package/plurly"><img src="https://img.shields.io/npm/v/plurly?color=blue" alt="npm version"></a>
|
|
9
|
+
<a href="https://github.com/ckgiri/plurly/actions/workflows/ci.yml"><img src="https://github.com/ckgiri/plurly/actions/workflows/ci.yml/badge.svg" alt="CI"></a>
|
|
10
|
+
<a href="https://www.npmjs.com/package/plurly?activeTab=dependencies"><img src="https://img.shields.io/badge/dependencies-0-brightgreen" alt="zero dependencies"></a>
|
|
11
|
+
<a href="https://www.npmjs.com/package/plurly"><img src="https://img.shields.io/npm/types/plurly" alt="TypeScript types included"></a>
|
|
12
|
+
<a href="LICENSE"><img src="https://img.shields.io/npm/l/plurly" alt="MIT license"></a>
|
|
13
|
+
<a href="https://github.com/ckgiri/plurly/issues"><img src="https://img.shields.io/badge/contributions-welcome-brightgreen" alt="contributions welcome"></a>
|
|
14
|
+
</p>
|
|
15
|
+
|
|
16
|
+
<p>
|
|
17
|
+
<a href="#installation">Installation</a> ·
|
|
18
|
+
<a href="#quick-start">Quick start</a> ·
|
|
19
|
+
<a href="#api">API</a> ·
|
|
20
|
+
<a href="#custom-rules">Custom rules</a> ·
|
|
21
|
+
<a href="#contributing">Contributing</a>
|
|
22
|
+
</p>
|
|
23
|
+
|
|
24
|
+
</div>
|
|
25
|
+
|
|
26
|
+
**plurly** is a lightweight, zero-dependency library that turns English words into their plural or singular form. It handles irregular words, keeps your casing, understands acronyms like `API`, and writes counts like "3 files" for you.
|
|
27
|
+
|
|
28
|
+
```js
|
|
29
|
+
import { plural, singular, count } from "plurly";
|
|
30
|
+
|
|
31
|
+
plural("person"); // "people"
|
|
32
|
+
plural("API"); // "APIs"
|
|
33
|
+
singular("Categories"); // "Category"
|
|
34
|
+
count(3, "file"); // "3 files"
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## ✨ Features
|
|
38
|
+
|
|
39
|
+
- 🔤 **Plural and singular**: regular words, irregular words (child → children) and Latin and Greek forms (criterion → criteria)
|
|
40
|
+
- 🧠 **Already-plural safe**: `plural("cats")` stays `"cats"`, and `singular("bus")` stays `"bus"`
|
|
41
|
+
- 🔠 **Keeps your casing**: `Person` → `People`, `PERSON` → `PEOPLE`, `salesPerson` → `salesPeople`
|
|
42
|
+
- 🧩 **Works on names in code**: `userProfile`, `user_category` and `blog post` change only the last word
|
|
43
|
+
- 🆔 **Acronyms**: `API` → `APIs`, `URL` → `URLs`, `user ID` → `user IDs`
|
|
44
|
+
- 🔢 **Counts**: `count(1200, "person", { locale: "en-US" })` → `"1,200 people"`, with Indian formatting via `en-IN`
|
|
45
|
+
- 💻 **Software words**: data, metadata, software, feedback and similar words stay the same
|
|
46
|
+
- 🛠️ **Custom rules**: add your own irregular and uncountable words, in a separate instance if you like
|
|
47
|
+
- 📦 **Zero dependencies**: TypeScript types, ESM and CommonJS, Node.js, browsers and edge runtimes
|
|
48
|
+
|
|
49
|
+
## Installation
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
npm install plurly
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Quick start
|
|
56
|
+
|
|
57
|
+
```js
|
|
58
|
+
import { plural, singular, isPlural, count } from "plurly";
|
|
59
|
+
|
|
60
|
+
plural("child"); // "children"
|
|
61
|
+
plural("analysis"); // "analyses"
|
|
62
|
+
plural("hero"); // "heroes"
|
|
63
|
+
plural("photo"); // "photos"
|
|
64
|
+
plural("data"); // "data"
|
|
65
|
+
|
|
66
|
+
singular("matrices"); // "matrix"
|
|
67
|
+
singular("wolves"); // "wolf"
|
|
68
|
+
|
|
69
|
+
isPlural("mice"); // true
|
|
70
|
+
|
|
71
|
+
count(0, "item"); // "0 items"
|
|
72
|
+
count(0, "item", { zero: "no" }); // "no items"
|
|
73
|
+
count(1, "item"); // "1 item"
|
|
74
|
+
count(120000, "rupee", { locale: "en-IN" }); // "1,20,000 rupees"
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
The default export takes a word, an optional count, and an optional flag to include the count:
|
|
78
|
+
|
|
79
|
+
```js
|
|
80
|
+
import plurly from "plurly";
|
|
81
|
+
|
|
82
|
+
plurly("test"); // "tests"
|
|
83
|
+
plurly("test", 1); // "test"
|
|
84
|
+
plurly("test", 5, true); // "5 tests"
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
If your code already uses `pluralize(word, count, inclusive)` and its `plural`, `singular` and `add...Rule` methods, you can switch the import to plurly without other changes.
|
|
88
|
+
|
|
89
|
+
## Examples
|
|
90
|
+
|
|
91
|
+
| Input | `plural()` | `singular()` of the plural |
|
|
92
|
+
|---|---|---|
|
|
93
|
+
| `person` | `people` | `person` |
|
|
94
|
+
| `category` | `categories` | `category` |
|
|
95
|
+
| `address` | `addresses` | `address` |
|
|
96
|
+
| `status` | `statuses` | `status` |
|
|
97
|
+
| `knife` | `knives` | `knife` |
|
|
98
|
+
| `index` | `indices` | `index` |
|
|
99
|
+
| `salesperson` | `salespeople` | `salesperson` |
|
|
100
|
+
| `userProfile` | `userProfiles` | `userProfile` |
|
|
101
|
+
| `API` | `APIs` | `API` |
|
|
102
|
+
| `sheep` | `sheep` | `sheep` |
|
|
103
|
+
|
|
104
|
+
## API
|
|
105
|
+
|
|
106
|
+
| Function | Returns | What it does |
|
|
107
|
+
|---|---|---|
|
|
108
|
+
| `plural(word)` | `string` | The plural form |
|
|
109
|
+
| `singular(word)` | `string` | The singular form |
|
|
110
|
+
| `isPlural(word)` | `boolean` | True for plural words, and for words with no separate plural |
|
|
111
|
+
| `isSingular(word)` | `boolean` | True for singular words, and for words with no separate plural |
|
|
112
|
+
| `count(n, word, options?)` | `string` | The number with the right form: `"1 file"`, `"3 files"` |
|
|
113
|
+
| `plurly(word, count?, inclusive?)` | `string` | Default export: singular when `count` is 1, plural otherwise |
|
|
114
|
+
| `createPlurly()` | `Plurly` | A separate instance with its own custom rules |
|
|
115
|
+
| `addIrregularRule(singular, plural)` | `void` | Adds an irregular pair |
|
|
116
|
+
| `addUncountableRule(word \| RegExp)` | `void` | Marks a word as having no plural |
|
|
117
|
+
| `addPluralRule(rule, replacement)` | `void` | Adds a plural rule |
|
|
118
|
+
| `addSingularRule(rule, replacement)` | `void` | Adds a singular rule |
|
|
119
|
+
|
|
120
|
+
None of them throw. Input that doesn't end in a word, such as `""` or `"123"`, is returned unchanged.
|
|
121
|
+
|
|
122
|
+
### `count` options
|
|
123
|
+
|
|
124
|
+
| Option | Default | What it does |
|
|
125
|
+
|---|---|---|
|
|
126
|
+
| `zero` | none | Word for zero: `{ zero: "no" }` → `"no files"` |
|
|
127
|
+
| `locale` | none | Formats the number: `"en-US"` → `1,200`, `"en-IN"` → `1,20,000` |
|
|
128
|
+
| `number` | `true` | `false` returns just the word in the right form |
|
|
129
|
+
|
|
130
|
+
## Custom rules
|
|
131
|
+
|
|
132
|
+
```js
|
|
133
|
+
import { addIrregularRule, addUncountableRule, addPluralRule, plural } from "plurly";
|
|
134
|
+
|
|
135
|
+
addIrregularRule("index", "indexes"); // prefer "indexes" over "indices"
|
|
136
|
+
addUncountableRule("kubernetes");
|
|
137
|
+
addPluralRule(/(octop)us$/i, "$1i");
|
|
138
|
+
|
|
139
|
+
plural("index"); // "indexes"
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
A string rule matches the whole word. A regular expression can match part of it, with `$1`-style replacements.
|
|
143
|
+
|
|
144
|
+
The functions above change the shared default instance. In a library, use your own instance so you don't change results for the rest of the app:
|
|
145
|
+
|
|
146
|
+
```js
|
|
147
|
+
import { createPlurly } from "plurly";
|
|
148
|
+
|
|
149
|
+
const words = createPlurly();
|
|
150
|
+
words.addIrregularRule("index", "indexes");
|
|
151
|
+
|
|
152
|
+
words.plural("index"); // "indexes"
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
## Notes
|
|
156
|
+
|
|
157
|
+
- plurly covers English nouns. Some words have two accepted plurals (`indices` and `indexes`, `cacti` and `cactuses`). plurly picks one, and you can change it with `addIrregularRule`.
|
|
158
|
+
- All-caps words of 2 to 5 letters are treated as acronyms (`API` → `APIs`). Longer all-caps words follow the normal rules (`PERSON` → `PEOPLE`).
|
|
159
|
+
- Only the last word changes, so phrases like `mother-in-law` need a custom rule.
|
|
160
|
+
|
|
161
|
+
## Contributing
|
|
162
|
+
|
|
163
|
+
**plurly is open source and contributions are welcome.** Found a word with the wrong plural? [Open an issue](https://github.com/ckgiri/plurly/issues) with the word, what plurly returned, and what you expected.
|
|
164
|
+
|
|
165
|
+
```bash
|
|
166
|
+
git clone https://github.com/ckgiri/plurly.git
|
|
167
|
+
cd plurly
|
|
168
|
+
npm install
|
|
169
|
+
npm test
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
## Changelog
|
|
173
|
+
|
|
174
|
+
See [CHANGELOG.md](CHANGELOG.md).
|
|
175
|
+
|
|
176
|
+
## License
|
|
177
|
+
|
|
178
|
+
[MIT](LICENSE) © CK Giri
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,337 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
addIrregularRule: () => addIrregularRule,
|
|
24
|
+
addPluralRule: () => addPluralRule,
|
|
25
|
+
addSingularRule: () => addSingularRule,
|
|
26
|
+
addUncountableRule: () => addUncountableRule,
|
|
27
|
+
count: () => count,
|
|
28
|
+
createPlurly: () => createPlurly,
|
|
29
|
+
default: () => src_default,
|
|
30
|
+
isPlural: () => isPlural,
|
|
31
|
+
isSingular: () => isSingular,
|
|
32
|
+
plural: () => plural,
|
|
33
|
+
singular: () => singular
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(src_exports);
|
|
36
|
+
|
|
37
|
+
// src/words.ts
|
|
38
|
+
var IRREGULAR = [
|
|
39
|
+
["person", "people"],
|
|
40
|
+
["man", "men"],
|
|
41
|
+
["woman", "women"],
|
|
42
|
+
["child", "children"],
|
|
43
|
+
["tooth", "teeth"],
|
|
44
|
+
["foot", "feet"],
|
|
45
|
+
["goose", "geese"],
|
|
46
|
+
["mongoose", "mongooses"],
|
|
47
|
+
["mouse", "mice"],
|
|
48
|
+
["louse", "lice"],
|
|
49
|
+
["blouse", "blouses"],
|
|
50
|
+
["ox", "oxen"],
|
|
51
|
+
["die", "dice"],
|
|
52
|
+
["criterion", "criteria"],
|
|
53
|
+
["phenomenon", "phenomena"],
|
|
54
|
+
["medium", "media"],
|
|
55
|
+
["bacterium", "bacteria"],
|
|
56
|
+
["curriculum", "curricula"],
|
|
57
|
+
["memorandum", "memoranda"],
|
|
58
|
+
["datum", "data"],
|
|
59
|
+
["genus", "genera"],
|
|
60
|
+
["corpus", "corpora"],
|
|
61
|
+
["octopus", "octopuses"],
|
|
62
|
+
["leaf", "leaves"],
|
|
63
|
+
["loaf", "loaves"],
|
|
64
|
+
["thief", "thieves"],
|
|
65
|
+
["knife", "knives"],
|
|
66
|
+
["wife", "wives"],
|
|
67
|
+
["life", "lives"],
|
|
68
|
+
["wolf", "wolves"],
|
|
69
|
+
["half", "halves"],
|
|
70
|
+
["self", "selves"],
|
|
71
|
+
["shelf", "shelves"],
|
|
72
|
+
["elf", "elves"],
|
|
73
|
+
["calf", "calves"],
|
|
74
|
+
["scarf", "scarves"],
|
|
75
|
+
["movie", "movies"],
|
|
76
|
+
["cookie", "cookies"],
|
|
77
|
+
["rookie", "rookies"],
|
|
78
|
+
["zombie", "zombies"],
|
|
79
|
+
["selfie", "selfies"],
|
|
80
|
+
["smoothie", "smoothies"],
|
|
81
|
+
["calorie", "calories"],
|
|
82
|
+
["brownie", "brownies"],
|
|
83
|
+
["goalie", "goalies"],
|
|
84
|
+
["freebie", "freebies"],
|
|
85
|
+
["hippie", "hippies"],
|
|
86
|
+
["prairie", "prairies"],
|
|
87
|
+
["pie", "pies"],
|
|
88
|
+
["tie", "ties"],
|
|
89
|
+
["lie", "lies"],
|
|
90
|
+
["quiz", "quizzes"],
|
|
91
|
+
["axis", "axes"],
|
|
92
|
+
["passerby", "passersby"],
|
|
93
|
+
["canvas", "canvases"],
|
|
94
|
+
["this", "these"],
|
|
95
|
+
["that", "those"],
|
|
96
|
+
["is", "are"],
|
|
97
|
+
["was", "were"],
|
|
98
|
+
["has", "have"]
|
|
99
|
+
];
|
|
100
|
+
var COMPOUND = /* @__PURE__ */ new Set(["person", "woman", "child", "tooth", "foot", "goose", "mouse", "movie", "cookie"]);
|
|
101
|
+
var UNCOUNTABLE = [
|
|
102
|
+
"advice",
|
|
103
|
+
"aircraft",
|
|
104
|
+
"baggage",
|
|
105
|
+
"bison",
|
|
106
|
+
"cattle",
|
|
107
|
+
"chassis",
|
|
108
|
+
"clothing",
|
|
109
|
+
"corps",
|
|
110
|
+
"deer",
|
|
111
|
+
"equipment",
|
|
112
|
+
"fish",
|
|
113
|
+
"furniture",
|
|
114
|
+
"homework",
|
|
115
|
+
"information",
|
|
116
|
+
"jewelry",
|
|
117
|
+
"knowledge",
|
|
118
|
+
"luggage",
|
|
119
|
+
"means",
|
|
120
|
+
"moose",
|
|
121
|
+
"money",
|
|
122
|
+
"music",
|
|
123
|
+
"news",
|
|
124
|
+
"offspring",
|
|
125
|
+
"police",
|
|
126
|
+
"rice",
|
|
127
|
+
"salmon",
|
|
128
|
+
"series",
|
|
129
|
+
"sheep",
|
|
130
|
+
"shrimp",
|
|
131
|
+
"species",
|
|
132
|
+
"staff",
|
|
133
|
+
"swine",
|
|
134
|
+
"traffic",
|
|
135
|
+
"trout",
|
|
136
|
+
"weather",
|
|
137
|
+
"wildlife",
|
|
138
|
+
"headquarters",
|
|
139
|
+
"mathematics",
|
|
140
|
+
"physics",
|
|
141
|
+
"economics",
|
|
142
|
+
"athletics",
|
|
143
|
+
"gymnastics",
|
|
144
|
+
"pants",
|
|
145
|
+
"scissors",
|
|
146
|
+
"jeans",
|
|
147
|
+
"research",
|
|
148
|
+
"evidence",
|
|
149
|
+
"progress",
|
|
150
|
+
"health",
|
|
151
|
+
"pollution",
|
|
152
|
+
"garbage",
|
|
153
|
+
"water",
|
|
154
|
+
"chess",
|
|
155
|
+
"aid",
|
|
156
|
+
"bream",
|
|
157
|
+
"carp",
|
|
158
|
+
"cod",
|
|
159
|
+
"elk",
|
|
160
|
+
"pike",
|
|
161
|
+
"tuna",
|
|
162
|
+
// software words
|
|
163
|
+
"data",
|
|
164
|
+
"metadata",
|
|
165
|
+
"software",
|
|
166
|
+
"hardware",
|
|
167
|
+
"firmware",
|
|
168
|
+
"middleware",
|
|
169
|
+
"malware",
|
|
170
|
+
"spyware",
|
|
171
|
+
"ransomware",
|
|
172
|
+
"feedback",
|
|
173
|
+
"analytics",
|
|
174
|
+
"telemetry",
|
|
175
|
+
"documentation",
|
|
176
|
+
"bandwidth",
|
|
177
|
+
"storage",
|
|
178
|
+
"infrastructure",
|
|
179
|
+
"tooling",
|
|
180
|
+
"auth",
|
|
181
|
+
"debris",
|
|
182
|
+
"sms",
|
|
183
|
+
"media",
|
|
184
|
+
"pokemon",
|
|
185
|
+
"emoji",
|
|
186
|
+
"kudos"
|
|
187
|
+
];
|
|
188
|
+
var PLURAL_RULES = [
|
|
189
|
+
[/(matr|append)ix$/i, "$1ices"],
|
|
190
|
+
[/(vert|ind)ex$/i, "$1ices"],
|
|
191
|
+
[/(alumn|cact|foc|fung|nucle|radi|stimul|syllab|bacill|hippopotam)us$/i, "$1i"],
|
|
192
|
+
[/(her|potat|tomat|ech|vet|torped|embarg|volcan|mosquit|domin)o$/i, "$1oes"],
|
|
193
|
+
[/(analy|ba|diagno|parenthe|progno|synop|the|cri|hypothe|ellip|oa|empha|paraly)sis$/i, "$1ses"],
|
|
194
|
+
[/(m|l)ouse$/i, "$1ice"],
|
|
195
|
+
[/(quiz)$/i, "$1zes"],
|
|
196
|
+
[/([^aeiouy]|qu)y$/i, "$1ies"],
|
|
197
|
+
[/(x|ch|ss|sh|zz|s)$/i, "$1es"],
|
|
198
|
+
[/$/, "s"]
|
|
199
|
+
];
|
|
200
|
+
var S_ENDINGS = "alias|status|campus|bus|virus|bonus|census|lens|gas|bias|atlas|plus|prospectus|apparatus|nexus|surplus|minus|chorus|circus|genius|walrus|iris|tennis|cosmos|ethos|thermos|asbestos";
|
|
201
|
+
var SINGULAR_RULES = [
|
|
202
|
+
[/(matr|append)ices$/i, "$1ix"],
|
|
203
|
+
[/(vert|ind)ices$/i, "$1ex"],
|
|
204
|
+
[/(alumn|cact|foc|fung|nucle|radi|stimul|syllab|bacill|hippopotam)i$/i, "$1us"],
|
|
205
|
+
[/(her|potat|tomat|ech|vet|torped|embarg|volcan|mosquit|domin)oes$/i, "$1o"],
|
|
206
|
+
[/(analy|ba|diagno|parenthe|progno|synop|the|cri|hypothe|ellip|oa|empha|paraly)ses$/i, "$1sis"],
|
|
207
|
+
[/(m|l)ice$/i, "$1ouse"],
|
|
208
|
+
[/(quiz)zes$/i, "$1"],
|
|
209
|
+
[/([^aeiouy]|qu)ies$/i, "$1y"],
|
|
210
|
+
[new RegExp(`(${S_ENDINGS})(es)?$`, "i"), "$1"],
|
|
211
|
+
[/(x|ch|ss|sh|zz)es$/i, "$1"],
|
|
212
|
+
[/(ss|us|is)$/i, "$1"],
|
|
213
|
+
[/s$/i, ""]
|
|
214
|
+
];
|
|
215
|
+
|
|
216
|
+
// src/index.ts
|
|
217
|
+
var isLower = (c) => c !== void 0 && c >= "a" && c <= "z";
|
|
218
|
+
var isUpper = (c) => c !== void 0 && c >= "A" && c <= "Z";
|
|
219
|
+
function lastWordStart(text) {
|
|
220
|
+
let k = text.length;
|
|
221
|
+
if (isLower(text[k - 1])) {
|
|
222
|
+
while (isLower(text[k - 1])) k--;
|
|
223
|
+
if (isUpper(text[k - 1])) k--;
|
|
224
|
+
return k;
|
|
225
|
+
}
|
|
226
|
+
if (isUpper(text[k - 1])) {
|
|
227
|
+
while (isUpper(text[k - 1])) k--;
|
|
228
|
+
return k;
|
|
229
|
+
}
|
|
230
|
+
return -1;
|
|
231
|
+
}
|
|
232
|
+
var ACRONYM = /(^|[^A-Z])[A-Z]{2,5}$/;
|
|
233
|
+
var ACRONYM_PLURAL = /(^|[^A-Z])[A-Z]{2,5}s$/;
|
|
234
|
+
function restoreCase(original, word) {
|
|
235
|
+
if (original === original.toLowerCase()) return word.toLowerCase();
|
|
236
|
+
if (original === original.toUpperCase()) return word.toUpperCase();
|
|
237
|
+
if (original.slice(1) === original.slice(1).toLowerCase()) {
|
|
238
|
+
return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
|
|
239
|
+
}
|
|
240
|
+
let k = 0;
|
|
241
|
+
while (k < original.length && k < word.length && original[k].toLowerCase() === word[k].toLowerCase()) k++;
|
|
242
|
+
return original.slice(0, k) + word.slice(k).toLowerCase();
|
|
243
|
+
}
|
|
244
|
+
function applyRules(word, rules) {
|
|
245
|
+
for (const [re, replacement] of rules) {
|
|
246
|
+
if (re.test(word)) return word.replace(re, replacement);
|
|
247
|
+
}
|
|
248
|
+
return word;
|
|
249
|
+
}
|
|
250
|
+
function toRegExp(rule) {
|
|
251
|
+
if (rule instanceof RegExp) return rule;
|
|
252
|
+
return new RegExp(`^${String(rule).replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}$`, "i");
|
|
253
|
+
}
|
|
254
|
+
function createPlurly() {
|
|
255
|
+
const toPlural = new Map(IRREGULAR);
|
|
256
|
+
const toSingular = new Map(IRREGULAR.map(([s, p]) => [p, s]));
|
|
257
|
+
const uncountable = new Set(UNCOUNTABLE);
|
|
258
|
+
const uncountablePatterns = [];
|
|
259
|
+
const customPlural = [];
|
|
260
|
+
const customSingular = [];
|
|
261
|
+
const isUncountable = (lower) => uncountable.has(lower) || uncountablePatterns.some((re) => re.test(lower));
|
|
262
|
+
function convert(input, toPluralForm) {
|
|
263
|
+
const text = input == null ? "" : String(input);
|
|
264
|
+
if (toPluralForm && ACRONYM.test(text)) return text + "s";
|
|
265
|
+
if (!toPluralForm && ACRONYM_PLURAL.test(text)) return text.slice(0, -1);
|
|
266
|
+
const start = lastWordStart(text);
|
|
267
|
+
if (start < 0) return text;
|
|
268
|
+
const prefix = text.slice(0, start);
|
|
269
|
+
const word = text.slice(start);
|
|
270
|
+
const lower = word.toLowerCase();
|
|
271
|
+
if (isUncountable(lower)) return text;
|
|
272
|
+
const map = toPluralForm ? toPlural : toSingular;
|
|
273
|
+
const other = toPluralForm ? toSingular : toPlural;
|
|
274
|
+
for (const [re, replacement] of toPluralForm ? customPlural : customSingular) {
|
|
275
|
+
if (re.test(lower)) return prefix + restoreCase(word, lower.replace(re, replacement));
|
|
276
|
+
}
|
|
277
|
+
const irregular = map.get(lower);
|
|
278
|
+
if (irregular !== void 0) return prefix + restoreCase(word, irregular);
|
|
279
|
+
if (other.has(lower)) return text;
|
|
280
|
+
for (const [from, to] of map) {
|
|
281
|
+
const base = toPluralForm ? from : to;
|
|
282
|
+
if (!COMPOUND.has(base) || lower.length <= Math.min(from.length, to.length)) continue;
|
|
283
|
+
if (lower.endsWith(from)) return prefix + restoreCase(word, lower.slice(0, -from.length) + to);
|
|
284
|
+
if (lower.endsWith(to)) return text;
|
|
285
|
+
}
|
|
286
|
+
if (toPluralForm) {
|
|
287
|
+
const single = applyRules(lower, SINGULAR_RULES);
|
|
288
|
+
if (single !== lower && applyRules(single, PLURAL_RULES) === lower) return text;
|
|
289
|
+
return prefix + restoreCase(word, applyRules(lower, PLURAL_RULES));
|
|
290
|
+
}
|
|
291
|
+
return prefix + restoreCase(word, applyRules(lower, SINGULAR_RULES));
|
|
292
|
+
}
|
|
293
|
+
const plural2 = (word) => convert(word, true);
|
|
294
|
+
const singular2 = (word) => convert(word, false);
|
|
295
|
+
const api = ((word, n, inclusive) => {
|
|
296
|
+
const form = n === 1 || n === -1 ? singular2(word) : plural2(word);
|
|
297
|
+
return inclusive ? `${n} ${form}` : form;
|
|
298
|
+
});
|
|
299
|
+
api.plural = plural2;
|
|
300
|
+
api.singular = singular2;
|
|
301
|
+
api.isPlural = (word) => plural2(word) === (word == null ? "" : String(word));
|
|
302
|
+
api.isSingular = (word) => singular2(word) === (word == null ? "" : String(word));
|
|
303
|
+
api.count = (n, word, options = {}) => {
|
|
304
|
+
const num = Number(n);
|
|
305
|
+
const form = Math.abs(num) === 1 ? singular2(word) : plural2(word);
|
|
306
|
+
if (options.number === false) return form;
|
|
307
|
+
if (num === 0 && options.zero !== void 0) return `${options.zero} ${form}`;
|
|
308
|
+
const shown = options.locale ? num.toLocaleString(options.locale) : String(num);
|
|
309
|
+
return `${shown} ${form}`;
|
|
310
|
+
};
|
|
311
|
+
api.addPluralRule = (rule, replacement) => void customPlural.unshift([toRegExp(rule), replacement]);
|
|
312
|
+
api.addSingularRule = (rule, replacement) => void customSingular.unshift([toRegExp(rule), replacement]);
|
|
313
|
+
api.addIrregularRule = (s, p) => {
|
|
314
|
+
const single = s.toLowerCase();
|
|
315
|
+
const many = p.toLowerCase();
|
|
316
|
+
toPlural.set(single, many);
|
|
317
|
+
toSingular.set(many, single);
|
|
318
|
+
uncountable.delete(single);
|
|
319
|
+
uncountable.delete(many);
|
|
320
|
+
};
|
|
321
|
+
api.addUncountableRule = (word) => {
|
|
322
|
+
if (word instanceof RegExp) uncountablePatterns.push(word);
|
|
323
|
+
else uncountable.add(String(word).toLowerCase());
|
|
324
|
+
};
|
|
325
|
+
return api;
|
|
326
|
+
}
|
|
327
|
+
var plurly = createPlurly();
|
|
328
|
+
var plural = plurly.plural;
|
|
329
|
+
var singular = plurly.singular;
|
|
330
|
+
var isPlural = plurly.isPlural;
|
|
331
|
+
var isSingular = plurly.isSingular;
|
|
332
|
+
var count = plurly.count;
|
|
333
|
+
var addPluralRule = plurly.addPluralRule;
|
|
334
|
+
var addSingularRule = plurly.addSingularRule;
|
|
335
|
+
var addIrregularRule = plurly.addIrregularRule;
|
|
336
|
+
var addUncountableRule = plurly.addUncountableRule;
|
|
337
|
+
var src_default = plurly;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
interface CountOptions {
|
|
2
|
+
/** Word to use for zero: count(0, "file", { zero: "no" }) → "no files". */
|
|
3
|
+
zero?: string;
|
|
4
|
+
/** Format the number for a locale: "en-US" → "1,200", "en-IN" → "1,20,000". */
|
|
5
|
+
locale?: string;
|
|
6
|
+
/** Leave the number out: count(3, "file", { number: false }) → "files". */
|
|
7
|
+
number?: boolean;
|
|
8
|
+
}
|
|
9
|
+
interface Plurly {
|
|
10
|
+
/** pluralize-style call: plurly("test", 5, true) → "5 tests". */
|
|
11
|
+
(word: string, count?: number, inclusive?: boolean): string;
|
|
12
|
+
/** The plural form: plural("person") → "people", plural("API") → "APIs". */
|
|
13
|
+
plural(word: string): string;
|
|
14
|
+
/** The singular form: singular("people") → "person". */
|
|
15
|
+
singular(word: string): string;
|
|
16
|
+
/** True when the word is plural, or has no separate plural ("sheep"). */
|
|
17
|
+
isPlural(word: string): boolean;
|
|
18
|
+
/** True when the word is singular, or has no separate plural ("sheep"). */
|
|
19
|
+
isSingular(word: string): boolean;
|
|
20
|
+
/** count(3, "file") → "3 files", count(1, "person") → "1 person". */
|
|
21
|
+
count(n: number, word: string, options?: CountOptions): string;
|
|
22
|
+
/** Adds a plural rule: addPluralRule(/(octop)us$/i, "$1i"). Strings match the whole word. */
|
|
23
|
+
addPluralRule(rule: string | RegExp, replacement: string): void;
|
|
24
|
+
/** Adds a singular rule. */
|
|
25
|
+
addSingularRule(rule: string | RegExp, replacement: string): void;
|
|
26
|
+
/** Adds an irregular pair: addIrregularRule("index", "indexes"). */
|
|
27
|
+
addIrregularRule(singular: string, plural: string): void;
|
|
28
|
+
/** Marks a word or pattern as having no plural: addUncountableRule("kubernetes"). */
|
|
29
|
+
addUncountableRule(word: string | RegExp): void;
|
|
30
|
+
}
|
|
31
|
+
/** A separate instance with its own custom rules, so libraries don't change each other's results. */
|
|
32
|
+
declare function createPlurly(): Plurly;
|
|
33
|
+
declare const plurly: Plurly;
|
|
34
|
+
declare const plural: (word: string) => string;
|
|
35
|
+
declare const singular: (word: string) => string;
|
|
36
|
+
declare const isPlural: (word: string) => boolean;
|
|
37
|
+
declare const isSingular: (word: string) => boolean;
|
|
38
|
+
declare const count: (n: number, word: string, options?: CountOptions) => string;
|
|
39
|
+
declare const addPluralRule: (rule: string | RegExp, replacement: string) => void;
|
|
40
|
+
declare const addSingularRule: (rule: string | RegExp, replacement: string) => void;
|
|
41
|
+
declare const addIrregularRule: (singular: string, plural: string) => void;
|
|
42
|
+
declare const addUncountableRule: (word: string | RegExp) => void;
|
|
43
|
+
|
|
44
|
+
export { type CountOptions, type Plurly, addIrregularRule, addPluralRule, addSingularRule, addUncountableRule, count, createPlurly, plurly as default, isPlural, isSingular, plural, singular };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
interface CountOptions {
|
|
2
|
+
/** Word to use for zero: count(0, "file", { zero: "no" }) → "no files". */
|
|
3
|
+
zero?: string;
|
|
4
|
+
/** Format the number for a locale: "en-US" → "1,200", "en-IN" → "1,20,000". */
|
|
5
|
+
locale?: string;
|
|
6
|
+
/** Leave the number out: count(3, "file", { number: false }) → "files". */
|
|
7
|
+
number?: boolean;
|
|
8
|
+
}
|
|
9
|
+
interface Plurly {
|
|
10
|
+
/** pluralize-style call: plurly("test", 5, true) → "5 tests". */
|
|
11
|
+
(word: string, count?: number, inclusive?: boolean): string;
|
|
12
|
+
/** The plural form: plural("person") → "people", plural("API") → "APIs". */
|
|
13
|
+
plural(word: string): string;
|
|
14
|
+
/** The singular form: singular("people") → "person". */
|
|
15
|
+
singular(word: string): string;
|
|
16
|
+
/** True when the word is plural, or has no separate plural ("sheep"). */
|
|
17
|
+
isPlural(word: string): boolean;
|
|
18
|
+
/** True when the word is singular, or has no separate plural ("sheep"). */
|
|
19
|
+
isSingular(word: string): boolean;
|
|
20
|
+
/** count(3, "file") → "3 files", count(1, "person") → "1 person". */
|
|
21
|
+
count(n: number, word: string, options?: CountOptions): string;
|
|
22
|
+
/** Adds a plural rule: addPluralRule(/(octop)us$/i, "$1i"). Strings match the whole word. */
|
|
23
|
+
addPluralRule(rule: string | RegExp, replacement: string): void;
|
|
24
|
+
/** Adds a singular rule. */
|
|
25
|
+
addSingularRule(rule: string | RegExp, replacement: string): void;
|
|
26
|
+
/** Adds an irregular pair: addIrregularRule("index", "indexes"). */
|
|
27
|
+
addIrregularRule(singular: string, plural: string): void;
|
|
28
|
+
/** Marks a word or pattern as having no plural: addUncountableRule("kubernetes"). */
|
|
29
|
+
addUncountableRule(word: string | RegExp): void;
|
|
30
|
+
}
|
|
31
|
+
/** A separate instance with its own custom rules, so libraries don't change each other's results. */
|
|
32
|
+
declare function createPlurly(): Plurly;
|
|
33
|
+
declare const plurly: Plurly;
|
|
34
|
+
declare const plural: (word: string) => string;
|
|
35
|
+
declare const singular: (word: string) => string;
|
|
36
|
+
declare const isPlural: (word: string) => boolean;
|
|
37
|
+
declare const isSingular: (word: string) => boolean;
|
|
38
|
+
declare const count: (n: number, word: string, options?: CountOptions) => string;
|
|
39
|
+
declare const addPluralRule: (rule: string | RegExp, replacement: string) => void;
|
|
40
|
+
declare const addSingularRule: (rule: string | RegExp, replacement: string) => void;
|
|
41
|
+
declare const addIrregularRule: (singular: string, plural: string) => void;
|
|
42
|
+
declare const addUncountableRule: (word: string | RegExp) => void;
|
|
43
|
+
|
|
44
|
+
export { type CountOptions, type Plurly, addIrregularRule, addPluralRule, addSingularRule, addUncountableRule, count, createPlurly, plurly as default, isPlural, isSingular, plural, singular };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
// src/words.ts
|
|
2
|
+
var IRREGULAR = [
|
|
3
|
+
["person", "people"],
|
|
4
|
+
["man", "men"],
|
|
5
|
+
["woman", "women"],
|
|
6
|
+
["child", "children"],
|
|
7
|
+
["tooth", "teeth"],
|
|
8
|
+
["foot", "feet"],
|
|
9
|
+
["goose", "geese"],
|
|
10
|
+
["mongoose", "mongooses"],
|
|
11
|
+
["mouse", "mice"],
|
|
12
|
+
["louse", "lice"],
|
|
13
|
+
["blouse", "blouses"],
|
|
14
|
+
["ox", "oxen"],
|
|
15
|
+
["die", "dice"],
|
|
16
|
+
["criterion", "criteria"],
|
|
17
|
+
["phenomenon", "phenomena"],
|
|
18
|
+
["medium", "media"],
|
|
19
|
+
["bacterium", "bacteria"],
|
|
20
|
+
["curriculum", "curricula"],
|
|
21
|
+
["memorandum", "memoranda"],
|
|
22
|
+
["datum", "data"],
|
|
23
|
+
["genus", "genera"],
|
|
24
|
+
["corpus", "corpora"],
|
|
25
|
+
["octopus", "octopuses"],
|
|
26
|
+
["leaf", "leaves"],
|
|
27
|
+
["loaf", "loaves"],
|
|
28
|
+
["thief", "thieves"],
|
|
29
|
+
["knife", "knives"],
|
|
30
|
+
["wife", "wives"],
|
|
31
|
+
["life", "lives"],
|
|
32
|
+
["wolf", "wolves"],
|
|
33
|
+
["half", "halves"],
|
|
34
|
+
["self", "selves"],
|
|
35
|
+
["shelf", "shelves"],
|
|
36
|
+
["elf", "elves"],
|
|
37
|
+
["calf", "calves"],
|
|
38
|
+
["scarf", "scarves"],
|
|
39
|
+
["movie", "movies"],
|
|
40
|
+
["cookie", "cookies"],
|
|
41
|
+
["rookie", "rookies"],
|
|
42
|
+
["zombie", "zombies"],
|
|
43
|
+
["selfie", "selfies"],
|
|
44
|
+
["smoothie", "smoothies"],
|
|
45
|
+
["calorie", "calories"],
|
|
46
|
+
["brownie", "brownies"],
|
|
47
|
+
["goalie", "goalies"],
|
|
48
|
+
["freebie", "freebies"],
|
|
49
|
+
["hippie", "hippies"],
|
|
50
|
+
["prairie", "prairies"],
|
|
51
|
+
["pie", "pies"],
|
|
52
|
+
["tie", "ties"],
|
|
53
|
+
["lie", "lies"],
|
|
54
|
+
["quiz", "quizzes"],
|
|
55
|
+
["axis", "axes"],
|
|
56
|
+
["passerby", "passersby"],
|
|
57
|
+
["canvas", "canvases"],
|
|
58
|
+
["this", "these"],
|
|
59
|
+
["that", "those"],
|
|
60
|
+
["is", "are"],
|
|
61
|
+
["was", "were"],
|
|
62
|
+
["has", "have"]
|
|
63
|
+
];
|
|
64
|
+
var COMPOUND = /* @__PURE__ */ new Set(["person", "woman", "child", "tooth", "foot", "goose", "mouse", "movie", "cookie"]);
|
|
65
|
+
var UNCOUNTABLE = [
|
|
66
|
+
"advice",
|
|
67
|
+
"aircraft",
|
|
68
|
+
"baggage",
|
|
69
|
+
"bison",
|
|
70
|
+
"cattle",
|
|
71
|
+
"chassis",
|
|
72
|
+
"clothing",
|
|
73
|
+
"corps",
|
|
74
|
+
"deer",
|
|
75
|
+
"equipment",
|
|
76
|
+
"fish",
|
|
77
|
+
"furniture",
|
|
78
|
+
"homework",
|
|
79
|
+
"information",
|
|
80
|
+
"jewelry",
|
|
81
|
+
"knowledge",
|
|
82
|
+
"luggage",
|
|
83
|
+
"means",
|
|
84
|
+
"moose",
|
|
85
|
+
"money",
|
|
86
|
+
"music",
|
|
87
|
+
"news",
|
|
88
|
+
"offspring",
|
|
89
|
+
"police",
|
|
90
|
+
"rice",
|
|
91
|
+
"salmon",
|
|
92
|
+
"series",
|
|
93
|
+
"sheep",
|
|
94
|
+
"shrimp",
|
|
95
|
+
"species",
|
|
96
|
+
"staff",
|
|
97
|
+
"swine",
|
|
98
|
+
"traffic",
|
|
99
|
+
"trout",
|
|
100
|
+
"weather",
|
|
101
|
+
"wildlife",
|
|
102
|
+
"headquarters",
|
|
103
|
+
"mathematics",
|
|
104
|
+
"physics",
|
|
105
|
+
"economics",
|
|
106
|
+
"athletics",
|
|
107
|
+
"gymnastics",
|
|
108
|
+
"pants",
|
|
109
|
+
"scissors",
|
|
110
|
+
"jeans",
|
|
111
|
+
"research",
|
|
112
|
+
"evidence",
|
|
113
|
+
"progress",
|
|
114
|
+
"health",
|
|
115
|
+
"pollution",
|
|
116
|
+
"garbage",
|
|
117
|
+
"water",
|
|
118
|
+
"chess",
|
|
119
|
+
"aid",
|
|
120
|
+
"bream",
|
|
121
|
+
"carp",
|
|
122
|
+
"cod",
|
|
123
|
+
"elk",
|
|
124
|
+
"pike",
|
|
125
|
+
"tuna",
|
|
126
|
+
// software words
|
|
127
|
+
"data",
|
|
128
|
+
"metadata",
|
|
129
|
+
"software",
|
|
130
|
+
"hardware",
|
|
131
|
+
"firmware",
|
|
132
|
+
"middleware",
|
|
133
|
+
"malware",
|
|
134
|
+
"spyware",
|
|
135
|
+
"ransomware",
|
|
136
|
+
"feedback",
|
|
137
|
+
"analytics",
|
|
138
|
+
"telemetry",
|
|
139
|
+
"documentation",
|
|
140
|
+
"bandwidth",
|
|
141
|
+
"storage",
|
|
142
|
+
"infrastructure",
|
|
143
|
+
"tooling",
|
|
144
|
+
"auth",
|
|
145
|
+
"debris",
|
|
146
|
+
"sms",
|
|
147
|
+
"media",
|
|
148
|
+
"pokemon",
|
|
149
|
+
"emoji",
|
|
150
|
+
"kudos"
|
|
151
|
+
];
|
|
152
|
+
var PLURAL_RULES = [
|
|
153
|
+
[/(matr|append)ix$/i, "$1ices"],
|
|
154
|
+
[/(vert|ind)ex$/i, "$1ices"],
|
|
155
|
+
[/(alumn|cact|foc|fung|nucle|radi|stimul|syllab|bacill|hippopotam)us$/i, "$1i"],
|
|
156
|
+
[/(her|potat|tomat|ech|vet|torped|embarg|volcan|mosquit|domin)o$/i, "$1oes"],
|
|
157
|
+
[/(analy|ba|diagno|parenthe|progno|synop|the|cri|hypothe|ellip|oa|empha|paraly)sis$/i, "$1ses"],
|
|
158
|
+
[/(m|l)ouse$/i, "$1ice"],
|
|
159
|
+
[/(quiz)$/i, "$1zes"],
|
|
160
|
+
[/([^aeiouy]|qu)y$/i, "$1ies"],
|
|
161
|
+
[/(x|ch|ss|sh|zz|s)$/i, "$1es"],
|
|
162
|
+
[/$/, "s"]
|
|
163
|
+
];
|
|
164
|
+
var S_ENDINGS = "alias|status|campus|bus|virus|bonus|census|lens|gas|bias|atlas|plus|prospectus|apparatus|nexus|surplus|minus|chorus|circus|genius|walrus|iris|tennis|cosmos|ethos|thermos|asbestos";
|
|
165
|
+
var SINGULAR_RULES = [
|
|
166
|
+
[/(matr|append)ices$/i, "$1ix"],
|
|
167
|
+
[/(vert|ind)ices$/i, "$1ex"],
|
|
168
|
+
[/(alumn|cact|foc|fung|nucle|radi|stimul|syllab|bacill|hippopotam)i$/i, "$1us"],
|
|
169
|
+
[/(her|potat|tomat|ech|vet|torped|embarg|volcan|mosquit|domin)oes$/i, "$1o"],
|
|
170
|
+
[/(analy|ba|diagno|parenthe|progno|synop|the|cri|hypothe|ellip|oa|empha|paraly)ses$/i, "$1sis"],
|
|
171
|
+
[/(m|l)ice$/i, "$1ouse"],
|
|
172
|
+
[/(quiz)zes$/i, "$1"],
|
|
173
|
+
[/([^aeiouy]|qu)ies$/i, "$1y"],
|
|
174
|
+
[new RegExp(`(${S_ENDINGS})(es)?$`, "i"), "$1"],
|
|
175
|
+
[/(x|ch|ss|sh|zz)es$/i, "$1"],
|
|
176
|
+
[/(ss|us|is)$/i, "$1"],
|
|
177
|
+
[/s$/i, ""]
|
|
178
|
+
];
|
|
179
|
+
|
|
180
|
+
// src/index.ts
|
|
181
|
+
var isLower = (c) => c !== void 0 && c >= "a" && c <= "z";
|
|
182
|
+
var isUpper = (c) => c !== void 0 && c >= "A" && c <= "Z";
|
|
183
|
+
function lastWordStart(text) {
|
|
184
|
+
let k = text.length;
|
|
185
|
+
if (isLower(text[k - 1])) {
|
|
186
|
+
while (isLower(text[k - 1])) k--;
|
|
187
|
+
if (isUpper(text[k - 1])) k--;
|
|
188
|
+
return k;
|
|
189
|
+
}
|
|
190
|
+
if (isUpper(text[k - 1])) {
|
|
191
|
+
while (isUpper(text[k - 1])) k--;
|
|
192
|
+
return k;
|
|
193
|
+
}
|
|
194
|
+
return -1;
|
|
195
|
+
}
|
|
196
|
+
var ACRONYM = /(^|[^A-Z])[A-Z]{2,5}$/;
|
|
197
|
+
var ACRONYM_PLURAL = /(^|[^A-Z])[A-Z]{2,5}s$/;
|
|
198
|
+
function restoreCase(original, word) {
|
|
199
|
+
if (original === original.toLowerCase()) return word.toLowerCase();
|
|
200
|
+
if (original === original.toUpperCase()) return word.toUpperCase();
|
|
201
|
+
if (original.slice(1) === original.slice(1).toLowerCase()) {
|
|
202
|
+
return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
|
|
203
|
+
}
|
|
204
|
+
let k = 0;
|
|
205
|
+
while (k < original.length && k < word.length && original[k].toLowerCase() === word[k].toLowerCase()) k++;
|
|
206
|
+
return original.slice(0, k) + word.slice(k).toLowerCase();
|
|
207
|
+
}
|
|
208
|
+
function applyRules(word, rules) {
|
|
209
|
+
for (const [re, replacement] of rules) {
|
|
210
|
+
if (re.test(word)) return word.replace(re, replacement);
|
|
211
|
+
}
|
|
212
|
+
return word;
|
|
213
|
+
}
|
|
214
|
+
function toRegExp(rule) {
|
|
215
|
+
if (rule instanceof RegExp) return rule;
|
|
216
|
+
return new RegExp(`^${String(rule).replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}$`, "i");
|
|
217
|
+
}
|
|
218
|
+
function createPlurly() {
|
|
219
|
+
const toPlural = new Map(IRREGULAR);
|
|
220
|
+
const toSingular = new Map(IRREGULAR.map(([s, p]) => [p, s]));
|
|
221
|
+
const uncountable = new Set(UNCOUNTABLE);
|
|
222
|
+
const uncountablePatterns = [];
|
|
223
|
+
const customPlural = [];
|
|
224
|
+
const customSingular = [];
|
|
225
|
+
const isUncountable = (lower) => uncountable.has(lower) || uncountablePatterns.some((re) => re.test(lower));
|
|
226
|
+
function convert(input, toPluralForm) {
|
|
227
|
+
const text = input == null ? "" : String(input);
|
|
228
|
+
if (toPluralForm && ACRONYM.test(text)) return text + "s";
|
|
229
|
+
if (!toPluralForm && ACRONYM_PLURAL.test(text)) return text.slice(0, -1);
|
|
230
|
+
const start = lastWordStart(text);
|
|
231
|
+
if (start < 0) return text;
|
|
232
|
+
const prefix = text.slice(0, start);
|
|
233
|
+
const word = text.slice(start);
|
|
234
|
+
const lower = word.toLowerCase();
|
|
235
|
+
if (isUncountable(lower)) return text;
|
|
236
|
+
const map = toPluralForm ? toPlural : toSingular;
|
|
237
|
+
const other = toPluralForm ? toSingular : toPlural;
|
|
238
|
+
for (const [re, replacement] of toPluralForm ? customPlural : customSingular) {
|
|
239
|
+
if (re.test(lower)) return prefix + restoreCase(word, lower.replace(re, replacement));
|
|
240
|
+
}
|
|
241
|
+
const irregular = map.get(lower);
|
|
242
|
+
if (irregular !== void 0) return prefix + restoreCase(word, irregular);
|
|
243
|
+
if (other.has(lower)) return text;
|
|
244
|
+
for (const [from, to] of map) {
|
|
245
|
+
const base = toPluralForm ? from : to;
|
|
246
|
+
if (!COMPOUND.has(base) || lower.length <= Math.min(from.length, to.length)) continue;
|
|
247
|
+
if (lower.endsWith(from)) return prefix + restoreCase(word, lower.slice(0, -from.length) + to);
|
|
248
|
+
if (lower.endsWith(to)) return text;
|
|
249
|
+
}
|
|
250
|
+
if (toPluralForm) {
|
|
251
|
+
const single = applyRules(lower, SINGULAR_RULES);
|
|
252
|
+
if (single !== lower && applyRules(single, PLURAL_RULES) === lower) return text;
|
|
253
|
+
return prefix + restoreCase(word, applyRules(lower, PLURAL_RULES));
|
|
254
|
+
}
|
|
255
|
+
return prefix + restoreCase(word, applyRules(lower, SINGULAR_RULES));
|
|
256
|
+
}
|
|
257
|
+
const plural2 = (word) => convert(word, true);
|
|
258
|
+
const singular2 = (word) => convert(word, false);
|
|
259
|
+
const api = ((word, n, inclusive) => {
|
|
260
|
+
const form = n === 1 || n === -1 ? singular2(word) : plural2(word);
|
|
261
|
+
return inclusive ? `${n} ${form}` : form;
|
|
262
|
+
});
|
|
263
|
+
api.plural = plural2;
|
|
264
|
+
api.singular = singular2;
|
|
265
|
+
api.isPlural = (word) => plural2(word) === (word == null ? "" : String(word));
|
|
266
|
+
api.isSingular = (word) => singular2(word) === (word == null ? "" : String(word));
|
|
267
|
+
api.count = (n, word, options = {}) => {
|
|
268
|
+
const num = Number(n);
|
|
269
|
+
const form = Math.abs(num) === 1 ? singular2(word) : plural2(word);
|
|
270
|
+
if (options.number === false) return form;
|
|
271
|
+
if (num === 0 && options.zero !== void 0) return `${options.zero} ${form}`;
|
|
272
|
+
const shown = options.locale ? num.toLocaleString(options.locale) : String(num);
|
|
273
|
+
return `${shown} ${form}`;
|
|
274
|
+
};
|
|
275
|
+
api.addPluralRule = (rule, replacement) => void customPlural.unshift([toRegExp(rule), replacement]);
|
|
276
|
+
api.addSingularRule = (rule, replacement) => void customSingular.unshift([toRegExp(rule), replacement]);
|
|
277
|
+
api.addIrregularRule = (s, p) => {
|
|
278
|
+
const single = s.toLowerCase();
|
|
279
|
+
const many = p.toLowerCase();
|
|
280
|
+
toPlural.set(single, many);
|
|
281
|
+
toSingular.set(many, single);
|
|
282
|
+
uncountable.delete(single);
|
|
283
|
+
uncountable.delete(many);
|
|
284
|
+
};
|
|
285
|
+
api.addUncountableRule = (word) => {
|
|
286
|
+
if (word instanceof RegExp) uncountablePatterns.push(word);
|
|
287
|
+
else uncountable.add(String(word).toLowerCase());
|
|
288
|
+
};
|
|
289
|
+
return api;
|
|
290
|
+
}
|
|
291
|
+
var plurly = createPlurly();
|
|
292
|
+
var plural = plurly.plural;
|
|
293
|
+
var singular = plurly.singular;
|
|
294
|
+
var isPlural = plurly.isPlural;
|
|
295
|
+
var isSingular = plurly.isSingular;
|
|
296
|
+
var count = plurly.count;
|
|
297
|
+
var addPluralRule = plurly.addPluralRule;
|
|
298
|
+
var addSingularRule = plurly.addSingularRule;
|
|
299
|
+
var addIrregularRule = plurly.addIrregularRule;
|
|
300
|
+
var addUncountableRule = plurly.addUncountableRule;
|
|
301
|
+
var src_default = plurly;
|
|
302
|
+
export {
|
|
303
|
+
addIrregularRule,
|
|
304
|
+
addPluralRule,
|
|
305
|
+
addSingularRule,
|
|
306
|
+
addUncountableRule,
|
|
307
|
+
count,
|
|
308
|
+
createPlurly,
|
|
309
|
+
src_default as default,
|
|
310
|
+
isPlural,
|
|
311
|
+
isSingular,
|
|
312
|
+
plural,
|
|
313
|
+
singular
|
|
314
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,67 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "plurly",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Pluralize and singularize English words, with TypeScript types, acronyms (API → APIs), case keeping and counts (\"3 files\"). A drop-in for the pluralize API.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"pluralize",
|
|
7
|
+
"plural",
|
|
8
|
+
"singular",
|
|
9
|
+
"inflection",
|
|
10
|
+
"inflector",
|
|
11
|
+
"english",
|
|
12
|
+
"words",
|
|
13
|
+
"count",
|
|
14
|
+
"i18n",
|
|
15
|
+
"text",
|
|
16
|
+
"typescript",
|
|
17
|
+
"grammar",
|
|
18
|
+
"nouns"
|
|
19
|
+
],
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"author": "CK Giri <ckgdev@gmail.com>",
|
|
22
|
+
"homepage": "https://github.com/ckgiri/plurly",
|
|
23
|
+
"repository": {
|
|
24
|
+
"type": "git",
|
|
25
|
+
"url": "git+https://github.com/ckgiri/plurly.git"
|
|
26
|
+
},
|
|
27
|
+
"bugs": {
|
|
28
|
+
"url": "https://github.com/ckgiri/plurly/issues"
|
|
29
|
+
},
|
|
30
|
+
"type": "module",
|
|
31
|
+
"sideEffects": false,
|
|
32
|
+
"main": "./dist/index.cjs",
|
|
33
|
+
"module": "./dist/index.js",
|
|
34
|
+
"types": "./dist/index.d.ts",
|
|
35
|
+
"exports": {
|
|
36
|
+
".": {
|
|
37
|
+
"import": {
|
|
38
|
+
"types": "./dist/index.d.ts",
|
|
39
|
+
"default": "./dist/index.js"
|
|
40
|
+
},
|
|
41
|
+
"require": {
|
|
42
|
+
"types": "./dist/index.d.cts",
|
|
43
|
+
"default": "./dist/index.cjs"
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
"./package.json": "./package.json"
|
|
47
|
+
},
|
|
48
|
+
"files": [
|
|
49
|
+
"dist",
|
|
50
|
+
"CHANGELOG.md"
|
|
51
|
+
],
|
|
52
|
+
"engines": {
|
|
53
|
+
"node": ">=18"
|
|
54
|
+
},
|
|
55
|
+
"scripts": {
|
|
56
|
+
"build": "tsup",
|
|
57
|
+
"test": "vitest run",
|
|
58
|
+
"typecheck": "tsc -p .",
|
|
59
|
+
"prepublishOnly": "npm run typecheck && npm test && npm run build"
|
|
60
|
+
},
|
|
61
|
+
"devDependencies": {
|
|
62
|
+
"@types/node": "^20.14.0",
|
|
63
|
+
"tsup": "^8.3.0",
|
|
64
|
+
"typescript": "^5.6.0",
|
|
65
|
+
"vitest": "^2.1.0"
|
|
66
|
+
}
|
|
67
|
+
}
|