pronouny 0.3.1 → 0.5.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/README.md +26 -3
- package/lib/cjs/index.d.ts +67 -14
- package/lib/cjs/index.js +178 -77
- package/lib/esm/index.d.ts +67 -14
- package/lib/esm/index.js +178 -77
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Pronouny
|
|
2
2
|
|
|
3
|
-
**Pronouny** is a typed library intended to make programmatically resolving English pronouns easier.
|
|
3
|
+
**Pronouny** is a typed library intended to make programmatically resolving English pronouns easier.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
@@ -26,6 +26,9 @@ const p = new Pronouny({
|
|
|
26
26
|
// Will default to using random pronouns in arrays.
|
|
27
27
|
// Set to false to force using 0 index.
|
|
28
28
|
useRandom: true,
|
|
29
|
+
|
|
30
|
+
// Will default to using "they" as a fallback pronoun.
|
|
31
|
+
fallbackPronoun: "they",
|
|
29
32
|
});
|
|
30
33
|
|
|
31
34
|
// Create a new Pronoun object using Pronouny.new()
|
|
@@ -49,7 +52,7 @@ p.add(pronounZe);
|
|
|
49
52
|
p.remove(p.resolve("I"));
|
|
50
53
|
// this deletes the "I" pronoun from the map.
|
|
51
54
|
|
|
52
|
-
// Methods are chainable
|
|
55
|
+
// Methods are chainable so you can reach the Pronoun class.
|
|
53
56
|
p.add(pronounZe).set("he/they").use().subject();
|
|
54
57
|
// ^Pronouny ^PronounSet ^Pronoun ^string
|
|
55
58
|
|
|
@@ -58,7 +61,7 @@ const vayne = {
|
|
|
58
61
|
username: "vaynegarden",
|
|
59
62
|
pronouns: p.set("she/ze/they"),
|
|
60
63
|
};
|
|
61
|
-
// from there, you can use `[PronounSet].use()` to get a random
|
|
64
|
+
// from there, you can use `[PronounSet].use()` to get a random `Pronoun`.
|
|
62
65
|
|
|
63
66
|
// Resolve the pronouns in your app by referencing the form
|
|
64
67
|
// that you need. There's `subject`, `object`, `possessive`,
|
|
@@ -69,10 +72,30 @@ console.log(
|
|
|
69
72
|
// This returns "vaynegarden updated her status", "vaynegarden
|
|
70
73
|
// updated hir status", or "vaynegarden updated their status",
|
|
71
74
|
// selected randomly among the three.
|
|
75
|
+
|
|
76
|
+
// Alternatively, you can use the `parse()` template string method
|
|
77
|
+
// to parse a string with pronouns and write in reasonably natural syntax.
|
|
78
|
+
// This functionality will be updated more in the future.
|
|
79
|
+
console.log(
|
|
80
|
+
vayne.pronouns.parse`${vayne.username} has updated ${"her"} status.`
|
|
81
|
+
);
|
|
82
|
+
// Returns the same as above.
|
|
72
83
|
```
|
|
73
84
|
|
|
74
85
|
## Changelog
|
|
75
86
|
|
|
87
|
+
### v0.5.0
|
|
88
|
+
|
|
89
|
+
- Added `parse()` method to `Pronoun` and `PronounSet` to allow for parsing of strings into their correct pronoun forms.
|
|
90
|
+
- Added optional `resolver` property to `Pronoun`s to allow retracing to related Pronouny instance.
|
|
91
|
+
- Added `identify()` method to `Pronouny` to allow for identification for type of pronouns.
|
|
92
|
+
|
|
93
|
+
### v0.4.0
|
|
94
|
+
|
|
95
|
+
- Optimized `forEach()` calls to use `for` loops instead.
|
|
96
|
+
- Added `fallbackPronoun` option to allow for configuration of pronoun to use in case of quiet failure states.
|
|
97
|
+
- Removed redundant `set` parameter from `[PronounSet].remove()` calls.
|
|
98
|
+
|
|
76
99
|
### v0.3.1
|
|
77
100
|
|
|
78
101
|
- Added documentation.
|
package/lib/cjs/index.d.ts
CHANGED
|
@@ -6,11 +6,13 @@
|
|
|
6
6
|
* and `rfx`.
|
|
7
7
|
*/
|
|
8
8
|
declare class Pronoun {
|
|
9
|
+
[key: string]: any;
|
|
9
10
|
sbj: Array<string>;
|
|
10
11
|
obj: Array<string>;
|
|
11
12
|
psv: Array<string>;
|
|
12
13
|
psj: Array<string>;
|
|
13
14
|
rfx: Array<string>;
|
|
15
|
+
resolver?: Pronouny;
|
|
14
16
|
/** @constructor */
|
|
15
17
|
constructor(pronouns: {
|
|
16
18
|
subject: Array<string> | string;
|
|
@@ -18,7 +20,7 @@ declare class Pronoun {
|
|
|
18
20
|
possessive: Array<string> | string;
|
|
19
21
|
psAdjective: Array<string> | string;
|
|
20
22
|
reflexive: Array<string> | string;
|
|
21
|
-
});
|
|
23
|
+
}, resolver?: Pronouny);
|
|
22
24
|
/**# Subject Getter
|
|
23
25
|
* Get a subject pronoun. By default, uses one at
|
|
24
26
|
* random and fails silently by using the last
|
|
@@ -115,6 +117,11 @@ declare class Pronoun {
|
|
|
115
117
|
* Default is `true`.
|
|
116
118
|
*/
|
|
117
119
|
reflexive(index?: number, useRandom?: boolean, failQuietly?: boolean): string;
|
|
120
|
+
/**# `.parse()` a Template String
|
|
121
|
+
* A method to parse tagged template strings and provide
|
|
122
|
+
* the correct pronouns, as per a particular PronounSet.
|
|
123
|
+
*/
|
|
124
|
+
parse(strings: TemplateStringsArray, ...args: Array<string>): string;
|
|
118
125
|
}
|
|
119
126
|
/**# `PronounSet` class
|
|
120
127
|
* A private class to construct the methods on
|
|
@@ -140,7 +147,7 @@ declare class PronounSet {
|
|
|
140
147
|
* @param failQuietly
|
|
141
148
|
* Override default behavior to fail into 'they'.
|
|
142
149
|
*/
|
|
143
|
-
add(pronoun: string | Pronoun | Array<string> | Array<Pronoun>, failQuietly?: boolean
|
|
150
|
+
add(pronoun: string | Pronoun | Array<string> | Array<Pronoun>, failQuietly?: boolean): this;
|
|
144
151
|
/**# `.remove()` from PronounSet
|
|
145
152
|
* Remove pronoun/s from a given Pronoun or strings
|
|
146
153
|
* (resolved using the {@linkcode Pronouny} instance
|
|
@@ -153,7 +160,7 @@ declare class PronounSet {
|
|
|
153
160
|
* @param failQuietly
|
|
154
161
|
* Override default behavior to fail into no removal.
|
|
155
162
|
*/
|
|
156
|
-
remove(
|
|
163
|
+
remove(pronoun: string | Pronoun | Array<string> | Array<Pronoun>, failQuietly?: boolean): this;
|
|
157
164
|
/**# `.use()` a PronounSet
|
|
158
165
|
* A method to retrieve one Pronoun at random. Use with
|
|
159
166
|
* {@link Pronoun|`Pronoun` methods} to get a specific
|
|
@@ -162,7 +169,7 @@ declare class PronounSet {
|
|
|
162
169
|
* {@link PronounSet.possessive|`.possessive()`}, or
|
|
163
170
|
* {@link PronounSet.reflexive|`.reflexive()`}. This is mostly
|
|
164
171
|
* here if you'd like to explicitly get a `Pronoun` instance
|
|
165
|
-
* from a `PronounSet`.
|
|
172
|
+
* from a `PronounSet`. Also useful for narrowing {@link PronounSet.parse|`.parse()`}.
|
|
166
173
|
*
|
|
167
174
|
* @param index
|
|
168
175
|
* Retrieve `index`th pronoun from insertion.
|
|
@@ -174,17 +181,24 @@ declare class PronounSet {
|
|
|
174
181
|
* Override default behavior to use random as per
|
|
175
182
|
* Pronouny config.
|
|
176
183
|
*/
|
|
177
|
-
use(index?: number, failQuietly?: boolean
|
|
184
|
+
use(index?: number, failQuietly?: boolean, useRandom?: boolean): Pronoun;
|
|
178
185
|
/**Retrieves a random subject pronoun. */
|
|
179
|
-
subject(index?: number, failQuietly?: boolean
|
|
186
|
+
subject(index?: number, failQuietly?: boolean, useRandom?: boolean): string;
|
|
180
187
|
/**Retrieves a random object pronoun. */
|
|
181
|
-
object(index?: number, failQuietly?: boolean
|
|
188
|
+
object(index?: number, failQuietly?: boolean, useRandom?: boolean): string;
|
|
182
189
|
/**Retrieves a random possessive pronoun. */
|
|
183
|
-
possessive(index?: number, failQuietly?: boolean
|
|
190
|
+
possessive(index?: number, failQuietly?: boolean, useRandom?: boolean): string;
|
|
184
191
|
/**Retrieves a random possessive adjective pronoun. */
|
|
185
|
-
psAdjective(index?: number, failQuietly?: boolean
|
|
192
|
+
psAdjective(index?: number, failQuietly?: boolean, useRandom?: boolean): string;
|
|
186
193
|
/**Retrieves a random reflexive pronoun. */
|
|
187
|
-
reflexive(index?: number, failQuietly?: boolean
|
|
194
|
+
reflexive(index?: number, failQuietly?: boolean, useRandom?: boolean): string;
|
|
195
|
+
/**# Template Parsing
|
|
196
|
+
* A method to select only one pronoun from the set
|
|
197
|
+
* and reuse it for the entire string. This is useful
|
|
198
|
+
* for when you want to use the same pronoun for a
|
|
199
|
+
* sentence.
|
|
200
|
+
*/
|
|
201
|
+
parse(strings: TemplateStringsArray, ...args: Array<string>): string;
|
|
188
202
|
}
|
|
189
203
|
/**# Config
|
|
190
204
|
* `failQuietly`: Controls various quiet failure states.
|
|
@@ -197,19 +211,33 @@ declare class PronounSet {
|
|
|
197
211
|
* used. If false, it will default to 0. If failing
|
|
198
212
|
* quietly and indexing out of bounds, it will use the
|
|
199
213
|
* last item.
|
|
214
|
+
*
|
|
215
|
+
* `fallbackPronoun`: Controls what is considered the
|
|
216
|
+
* default pronoun for when one is not assigned or for
|
|
217
|
+
* quiet failure states. By default, this is `they`. **It
|
|
218
|
+
* is not recommended to change this.**
|
|
200
219
|
*/
|
|
201
220
|
type PronounyConfig = {
|
|
202
221
|
/**Controls various quiet failure states. */
|
|
203
|
-
failQuietly
|
|
222
|
+
failQuietly: boolean;
|
|
204
223
|
/**Controls how deeply Pronouny will search for
|
|
205
224
|
* queries. By default, `string` parameters will
|
|
206
225
|
* only resolve using the first subject pronoun. */
|
|
207
|
-
deepSearch
|
|
226
|
+
deepSearch: boolean;
|
|
208
227
|
/**Controls whether random selection will be used.
|
|
209
228
|
* If false, it will default to 0. If failing
|
|
210
229
|
* quietly and indexing out of bounds, it will
|
|
211
230
|
* use the last item. */
|
|
212
|
-
useRandom
|
|
231
|
+
useRandom: boolean;
|
|
232
|
+
/**Controls what is considered the default pronoun
|
|
233
|
+
* for when one is not assigned or for quiet failure
|
|
234
|
+
* states. By default, this is `they`. **It is not
|
|
235
|
+
* recommended to change this.**
|
|
236
|
+
*
|
|
237
|
+
* If you remove `they` from the Pronouny instance,
|
|
238
|
+
* you are responsible for setting this to a valid
|
|
239
|
+
* pronoun string and ensuring it will resolve. */
|
|
240
|
+
fallbackPronoun: string;
|
|
213
241
|
};
|
|
214
242
|
/**# Pronouny
|
|
215
243
|
* A typed library intended to make English pronoun
|
|
@@ -236,15 +264,40 @@ type PronounyConfig = {
|
|
|
236
264
|
* `Pronoun` object.
|
|
237
265
|
*/
|
|
238
266
|
export default class Pronouny {
|
|
267
|
+
[keyof: string]: any;
|
|
239
268
|
config: PronounyConfig;
|
|
240
269
|
resolveMap: Map<string, Pronoun>;
|
|
270
|
+
default: {
|
|
271
|
+
he: Pronoun;
|
|
272
|
+
she: Pronoun;
|
|
273
|
+
they: Pronoun;
|
|
274
|
+
you: Pronoun;
|
|
275
|
+
i: Pronoun;
|
|
276
|
+
we: Pronoun;
|
|
277
|
+
config: {
|
|
278
|
+
failQuietly: boolean;
|
|
279
|
+
deepSearch: boolean;
|
|
280
|
+
useRandom: boolean;
|
|
281
|
+
fallbackPronoun: string;
|
|
282
|
+
};
|
|
283
|
+
};
|
|
241
284
|
constructor(config?: PronounyConfig);
|
|
242
285
|
/**# `.resolve()` a Pronoun
|
|
243
286
|
* Returns a pronoun from a given string. Uses
|
|
244
287
|
* Pronouny's config to determine the depth of search.
|
|
245
288
|
* By default, only checks the first subject pronoun.
|
|
246
289
|
*/
|
|
247
|
-
resolve(pronoun: string, deepSearch?: boolean
|
|
290
|
+
resolve(pronoun: string, deepSearch?: boolean): Pronoun;
|
|
291
|
+
/**# `.identify()` a Pronoun
|
|
292
|
+
* Returns a string describing the type of pronoun
|
|
293
|
+
* passed in. Returns `subject`, `object`,
|
|
294
|
+
* `possessive`, `possessive adjective`, or
|
|
295
|
+
* `reflexive`. You can use this to access other
|
|
296
|
+
* pronouns by retrieving via string indexing.
|
|
297
|
+
* **This method is case-sensitive and does not
|
|
298
|
+
* have a quiet failure state.**
|
|
299
|
+
*/
|
|
300
|
+
identify(pronoun: string): string;
|
|
248
301
|
/**# `.add()` an Unlisted Pronoun
|
|
249
302
|
* If you made a `.new()` Pronoun but disabled
|
|
250
303
|
* `autoAppend`, you can later append it back by
|
package/lib/cjs/index.js
CHANGED
|
@@ -3,6 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
function randomToLimit(limit) {
|
|
4
4
|
return Math.floor(Math.random() * limit);
|
|
5
5
|
}
|
|
6
|
+
function isUpperCase(str) {
|
|
7
|
+
return str === str.toUpperCase() && str !== str.toLowerCase();
|
|
8
|
+
}
|
|
6
9
|
/**# `Pronoun` class
|
|
7
10
|
* A private class to construct the methods for pronoun
|
|
8
11
|
* access. Use {@link Pronouny} to create and
|
|
@@ -12,7 +15,7 @@ function randomToLimit(limit) {
|
|
|
12
15
|
*/
|
|
13
16
|
class Pronoun {
|
|
14
17
|
/** @constructor */
|
|
15
|
-
constructor(pronouns) {
|
|
18
|
+
constructor(pronouns, resolver) {
|
|
16
19
|
this.sbj =
|
|
17
20
|
typeof pronouns.subject === "string"
|
|
18
21
|
? [pronouns.subject]
|
|
@@ -33,6 +36,9 @@ class Pronoun {
|
|
|
33
36
|
typeof pronouns.reflexive === "string"
|
|
34
37
|
? [pronouns.reflexive]
|
|
35
38
|
: pronouns.reflexive;
|
|
39
|
+
if (resolver) {
|
|
40
|
+
this.resolver = resolver;
|
|
41
|
+
}
|
|
36
42
|
return this;
|
|
37
43
|
}
|
|
38
44
|
/**# Subject Getter
|
|
@@ -201,6 +207,25 @@ class Pronoun {
|
|
|
201
207
|
throw new Error(`could not retrieve index ${index} from "${this.sbj[0]}" reflexive pronoun`);
|
|
202
208
|
}
|
|
203
209
|
}
|
|
210
|
+
/**# `.parse()` a Template String
|
|
211
|
+
* A method to parse tagged template strings and provide
|
|
212
|
+
* the correct pronouns, as per a particular PronounSet.
|
|
213
|
+
*/
|
|
214
|
+
parse(strings, ...args) {
|
|
215
|
+
let result = strings[0];
|
|
216
|
+
for (let i = 0; i < args.length; i++) {
|
|
217
|
+
const upperCaseFlag = isUpperCase(args[i][0]);
|
|
218
|
+
let argSub = this.resolver?.identify(args[i].toLowerCase()) !== ""
|
|
219
|
+
? this.use()[this.resolver?.identify(args[i].toLowerCase()) ||
|
|
220
|
+
new Pronouny().identify(args[i].toLowerCase())]()
|
|
221
|
+
: args[i];
|
|
222
|
+
if (upperCaseFlag) {
|
|
223
|
+
argSub = argSub[0].toUpperCase() + argSub.slice(1);
|
|
224
|
+
}
|
|
225
|
+
result += argSub + strings[i + 1];
|
|
226
|
+
}
|
|
227
|
+
return result;
|
|
228
|
+
}
|
|
204
229
|
}
|
|
205
230
|
/**# `PronounSet` class
|
|
206
231
|
* A private class to construct the methods on
|
|
@@ -216,27 +241,27 @@ class PronounSet {
|
|
|
216
241
|
this.resolver = resolver;
|
|
217
242
|
switch (true) {
|
|
218
243
|
case Array.isArray(pronouns):
|
|
219
|
-
|
|
244
|
+
for (const x of pronouns) {
|
|
220
245
|
if (x instanceof Pronoun) {
|
|
221
246
|
this.pronouns.add(x);
|
|
222
247
|
}
|
|
223
248
|
else {
|
|
224
249
|
this.pronouns.add(resolver.resolve(x));
|
|
225
250
|
}
|
|
226
|
-
}
|
|
251
|
+
}
|
|
227
252
|
break;
|
|
228
253
|
case typeof pronouns === "string":
|
|
229
254
|
this.pronouns.add(resolver.resolve(pronouns));
|
|
230
255
|
break;
|
|
231
256
|
default:
|
|
232
|
-
|
|
257
|
+
for (const x of pronouns) {
|
|
233
258
|
if (x instanceof Pronoun) {
|
|
234
259
|
this.pronouns.add(x);
|
|
235
260
|
}
|
|
236
261
|
else {
|
|
237
262
|
this.pronouns.add(resolver.resolve(x));
|
|
238
263
|
}
|
|
239
|
-
}
|
|
264
|
+
}
|
|
240
265
|
break;
|
|
241
266
|
}
|
|
242
267
|
return this;
|
|
@@ -262,12 +287,12 @@ class PronounSet {
|
|
|
262
287
|
this.pronouns.add(this.resolver.resolve(pronoun));
|
|
263
288
|
break;
|
|
264
289
|
case pronoun instanceof Array:
|
|
265
|
-
|
|
290
|
+
for (const p of pronoun) {
|
|
266
291
|
this.add(p, failQuietly);
|
|
267
|
-
}
|
|
292
|
+
}
|
|
268
293
|
break;
|
|
269
294
|
case failQuietly:
|
|
270
|
-
this.pronouns.add(this.resolver.resolve(
|
|
295
|
+
this.pronouns.add(this.resolver.resolve(this.resolver.config.fallbackPronoun));
|
|
271
296
|
break;
|
|
272
297
|
case !failQuietly:
|
|
273
298
|
default:
|
|
@@ -287,18 +312,18 @@ class PronounSet {
|
|
|
287
312
|
* @param failQuietly
|
|
288
313
|
* Override default behavior to fail into no removal.
|
|
289
314
|
*/
|
|
290
|
-
remove(
|
|
315
|
+
remove(pronoun, failQuietly = this.resolver.config.failQuietly) {
|
|
291
316
|
switch (true) {
|
|
292
317
|
case pronoun instanceof Pronoun:
|
|
293
|
-
|
|
318
|
+
this.pronouns.delete(pronoun);
|
|
294
319
|
break;
|
|
295
320
|
case typeof pronoun === "string":
|
|
296
|
-
|
|
321
|
+
this.pronouns.delete(this.resolver.resolve(pronoun));
|
|
297
322
|
break;
|
|
298
323
|
case pronoun instanceof Array:
|
|
299
|
-
|
|
300
|
-
this.remove(
|
|
301
|
-
}
|
|
324
|
+
for (const p of pronoun) {
|
|
325
|
+
this.remove(p);
|
|
326
|
+
}
|
|
302
327
|
break;
|
|
303
328
|
case failQuietly:
|
|
304
329
|
break;
|
|
@@ -316,7 +341,7 @@ class PronounSet {
|
|
|
316
341
|
* {@link PronounSet.possessive|`.possessive()`}, or
|
|
317
342
|
* {@link PronounSet.reflexive|`.reflexive()`}. This is mostly
|
|
318
343
|
* here if you'd like to explicitly get a `Pronoun` instance
|
|
319
|
-
* from a `PronounSet`.
|
|
344
|
+
* from a `PronounSet`. Also useful for narrowing {@link PronounSet.parse|`.parse()`}.
|
|
320
345
|
*
|
|
321
346
|
* @param index
|
|
322
347
|
* Retrieve `index`th pronoun from insertion.
|
|
@@ -346,7 +371,7 @@ class PronounSet {
|
|
|
346
371
|
case indexable[index] !== undefined:
|
|
347
372
|
return indexable[index];
|
|
348
373
|
case failQuietly:
|
|
349
|
-
return this.resolver.resolve(
|
|
374
|
+
return this.resolver.resolve(this.resolver.config.fallbackPronoun);
|
|
350
375
|
default:
|
|
351
376
|
throw new Error(`Index ${index} out of range`);
|
|
352
377
|
}
|
|
@@ -371,54 +396,27 @@ class PronounSet {
|
|
|
371
396
|
reflexive(index = -1, failQuietly = this.resolver.config.failQuietly, useRandom = this.resolver.config.useRandom) {
|
|
372
397
|
return this.use(index, failQuietly, useRandom).reflexive(-1, failQuietly, useRandom);
|
|
373
398
|
}
|
|
399
|
+
/**# Template Parsing
|
|
400
|
+
* A method to select only one pronoun from the set
|
|
401
|
+
* and reuse it for the entire string. This is useful
|
|
402
|
+
* for when you want to use the same pronoun for a
|
|
403
|
+
* sentence.
|
|
404
|
+
*/
|
|
405
|
+
parse(strings, ...args) {
|
|
406
|
+
let result = strings[0];
|
|
407
|
+
for (let i = 0; i < args.length; i++) {
|
|
408
|
+
const upperCaseFlag = isUpperCase(args[i][0]);
|
|
409
|
+
let argSub = this.resolver.identify(args[i].toLowerCase()) !== ""
|
|
410
|
+
? this.use()[this.resolver.identify(args[i].toLowerCase())]()
|
|
411
|
+
: args[i];
|
|
412
|
+
if (upperCaseFlag) {
|
|
413
|
+
argSub = argSub[0].toUpperCase() + argSub.slice(1);
|
|
414
|
+
}
|
|
415
|
+
result += argSub + strings[i + 1];
|
|
416
|
+
}
|
|
417
|
+
return result;
|
|
418
|
+
}
|
|
374
419
|
}
|
|
375
|
-
const pronounHe = new Pronoun({
|
|
376
|
-
subject: ["he"],
|
|
377
|
-
object: ["him"],
|
|
378
|
-
possessive: ["his"],
|
|
379
|
-
psAdjective: ["his"],
|
|
380
|
-
reflexive: ["himself"],
|
|
381
|
-
});
|
|
382
|
-
const pronounShe = new Pronoun({
|
|
383
|
-
subject: ["she"],
|
|
384
|
-
object: ["her"],
|
|
385
|
-
possessive: ["hers"],
|
|
386
|
-
psAdjective: ["her"],
|
|
387
|
-
reflexive: ["herself"],
|
|
388
|
-
});
|
|
389
|
-
const pronounThey = new Pronoun({
|
|
390
|
-
subject: ["they"],
|
|
391
|
-
object: ["them"],
|
|
392
|
-
possessive: ["theirs"],
|
|
393
|
-
psAdjective: ["their"],
|
|
394
|
-
reflexive: ["theirself", "theirselves", "themselves", "themself"],
|
|
395
|
-
});
|
|
396
|
-
const pronounYou = new Pronoun({
|
|
397
|
-
subject: ["you"],
|
|
398
|
-
object: ["you"],
|
|
399
|
-
possessive: ["yours"],
|
|
400
|
-
psAdjective: ["your"],
|
|
401
|
-
reflexive: ["yourself"],
|
|
402
|
-
});
|
|
403
|
-
const pronounI = new Pronoun({
|
|
404
|
-
subject: ["I"],
|
|
405
|
-
object: ["me"],
|
|
406
|
-
possessive: ["mine"],
|
|
407
|
-
psAdjective: ["my"],
|
|
408
|
-
reflexive: ["myself"],
|
|
409
|
-
});
|
|
410
|
-
const pronounWe = new Pronoun({
|
|
411
|
-
subject: ["we"],
|
|
412
|
-
object: ["us"],
|
|
413
|
-
possessive: ["our"],
|
|
414
|
-
psAdjective: ["ours"],
|
|
415
|
-
reflexive: ["ourselves"],
|
|
416
|
-
});
|
|
417
|
-
const PronounyDefaultConfig = {
|
|
418
|
-
failQuietly: true,
|
|
419
|
-
deepSearch: false,
|
|
420
|
-
useRandom: true,
|
|
421
|
-
};
|
|
422
420
|
/**# Pronouny
|
|
423
421
|
* A typed library intended to make English pronoun
|
|
424
422
|
* resolution easy. Instantiate in your project using
|
|
@@ -444,15 +442,76 @@ const PronounyDefaultConfig = {
|
|
|
444
442
|
* `Pronoun` object.
|
|
445
443
|
*/
|
|
446
444
|
class Pronouny {
|
|
447
|
-
constructor(config =
|
|
448
|
-
|
|
445
|
+
constructor(config = {
|
|
446
|
+
failQuietly: true,
|
|
447
|
+
deepSearch: false,
|
|
448
|
+
useRandom: true,
|
|
449
|
+
fallbackPronoun: "they",
|
|
450
|
+
}) {
|
|
451
|
+
this.default = {
|
|
452
|
+
he: new Pronoun({
|
|
453
|
+
subject: ["he"],
|
|
454
|
+
object: ["him"],
|
|
455
|
+
possessive: ["his"],
|
|
456
|
+
psAdjective: ["his"],
|
|
457
|
+
reflexive: ["himself"],
|
|
458
|
+
}, this),
|
|
459
|
+
she: new Pronoun({
|
|
460
|
+
subject: ["she"],
|
|
461
|
+
object: ["her"],
|
|
462
|
+
possessive: ["hers"],
|
|
463
|
+
psAdjective: ["her"],
|
|
464
|
+
reflexive: ["herself"],
|
|
465
|
+
}, this),
|
|
466
|
+
they: new Pronoun({
|
|
467
|
+
subject: ["they"],
|
|
468
|
+
object: ["them"],
|
|
469
|
+
possessive: ["theirs"],
|
|
470
|
+
psAdjective: ["their"],
|
|
471
|
+
reflexive: [
|
|
472
|
+
"theirself",
|
|
473
|
+
"theirselves",
|
|
474
|
+
"themselves",
|
|
475
|
+
"themself",
|
|
476
|
+
],
|
|
477
|
+
}, this),
|
|
478
|
+
you: new Pronoun({
|
|
479
|
+
subject: ["you"],
|
|
480
|
+
object: ["you"],
|
|
481
|
+
possessive: ["yours"],
|
|
482
|
+
psAdjective: ["your"],
|
|
483
|
+
reflexive: ["yourself"],
|
|
484
|
+
}, this),
|
|
485
|
+
i: new Pronoun({
|
|
486
|
+
subject: ["I"],
|
|
487
|
+
object: ["me"],
|
|
488
|
+
possessive: ["mine"],
|
|
489
|
+
psAdjective: ["my"],
|
|
490
|
+
reflexive: ["myself"],
|
|
491
|
+
}, this),
|
|
492
|
+
we: new Pronoun({
|
|
493
|
+
subject: ["we"],
|
|
494
|
+
object: ["us"],
|
|
495
|
+
possessive: ["ours"],
|
|
496
|
+
psAdjective: ["our"],
|
|
497
|
+
reflexive: ["ourselves"],
|
|
498
|
+
}, this),
|
|
499
|
+
config: {
|
|
500
|
+
failQuietly: true,
|
|
501
|
+
deepSearch: false,
|
|
502
|
+
useRandom: true,
|
|
503
|
+
fallbackPronoun: "they",
|
|
504
|
+
},
|
|
505
|
+
};
|
|
506
|
+
const defaults = this.default;
|
|
507
|
+
this.config = Object.assign(defaults.config, config);
|
|
449
508
|
this.resolveMap = new Map([
|
|
450
|
-
[
|
|
451
|
-
[
|
|
452
|
-
[
|
|
453
|
-
[
|
|
454
|
-
[
|
|
455
|
-
[
|
|
509
|
+
[defaults.they.sbj[0], defaults.they],
|
|
510
|
+
[defaults.you.sbj[0], defaults.you],
|
|
511
|
+
[defaults.we.sbj[0], defaults.we],
|
|
512
|
+
[defaults.i.sbj[0], defaults.i],
|
|
513
|
+
[defaults.he.sbj[0], defaults.he],
|
|
514
|
+
[defaults.she.sbj[0], defaults.she],
|
|
456
515
|
]);
|
|
457
516
|
return this;
|
|
458
517
|
}
|
|
@@ -468,28 +527,66 @@ class Pronouny {
|
|
|
468
527
|
return resolvedPronoun;
|
|
469
528
|
case deepSearch:
|
|
470
529
|
let searchResult;
|
|
471
|
-
this.resolveMap
|
|
472
|
-
Object.values(
|
|
530
|
+
for (const [_, value] of this.resolveMap) {
|
|
531
|
+
for (const type of Object.values(value)) {
|
|
473
532
|
if (type.includes(pronoun)) {
|
|
474
|
-
searchResult =
|
|
533
|
+
searchResult = value;
|
|
475
534
|
}
|
|
476
|
-
}
|
|
477
|
-
}
|
|
535
|
+
}
|
|
536
|
+
}
|
|
478
537
|
if (searchResult) {
|
|
479
538
|
return searchResult;
|
|
480
539
|
}
|
|
481
540
|
case this.config.failQuietly:
|
|
482
|
-
return this.resolveMap.get(
|
|
541
|
+
return this.resolveMap.get(this.config.fallbackPronoun);
|
|
483
542
|
default:
|
|
484
543
|
throw new Error(`Pronoun "${pronoun}" not found`);
|
|
485
544
|
}
|
|
486
545
|
}
|
|
546
|
+
/**# `.identify()` a Pronoun
|
|
547
|
+
* Returns a string describing the type of pronoun
|
|
548
|
+
* passed in. Returns `subject`, `object`,
|
|
549
|
+
* `possessive`, `possessive adjective`, or
|
|
550
|
+
* `reflexive`. You can use this to access other
|
|
551
|
+
* pronouns by retrieving via string indexing.
|
|
552
|
+
* **This method is case-sensitive and does not
|
|
553
|
+
* have a quiet failure state.**
|
|
554
|
+
*/
|
|
555
|
+
identify(pronoun) {
|
|
556
|
+
let pronounType = "";
|
|
557
|
+
for (const [_, value] of this.resolveMap) {
|
|
558
|
+
for (let i = 0, types = Object.values(value); i < types.length; i++) {
|
|
559
|
+
let type = types[i];
|
|
560
|
+
if (Object.keys(value)[i] === "resolver") {
|
|
561
|
+
continue;
|
|
562
|
+
}
|
|
563
|
+
if (type.includes(pronoun)) {
|
|
564
|
+
pronounType = Object.keys(value)[i];
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
switch (pronounType) {
|
|
569
|
+
case "sbj":
|
|
570
|
+
return "subject";
|
|
571
|
+
case "obj":
|
|
572
|
+
return "object";
|
|
573
|
+
case "psv":
|
|
574
|
+
return "possessive";
|
|
575
|
+
case "psj":
|
|
576
|
+
return "psAdjective";
|
|
577
|
+
case "rfx":
|
|
578
|
+
return "reflexive";
|
|
579
|
+
default:
|
|
580
|
+
return "";
|
|
581
|
+
}
|
|
582
|
+
}
|
|
487
583
|
/**# `.add()` an Unlisted Pronoun
|
|
488
584
|
* If you made a `.new()` Pronoun but disabled
|
|
489
585
|
* `autoAppend`, you can later append it back by
|
|
490
586
|
* using this method. Returns `Pronouny`.
|
|
491
587
|
*/
|
|
492
588
|
add(pronoun) {
|
|
589
|
+
pronoun.resolver = this;
|
|
493
590
|
this.resolveMap.set(pronoun.sbj[0], pronoun);
|
|
494
591
|
return this;
|
|
495
592
|
}
|
|
@@ -498,6 +595,10 @@ class Pronouny {
|
|
|
498
595
|
* Pronouny instance. Returns `Pronouny`.
|
|
499
596
|
*/
|
|
500
597
|
remove(pronoun) {
|
|
598
|
+
if (pronoun === this.resolveMap.get(this.config.fallbackPronoun)) {
|
|
599
|
+
throw new Error(`Cannot remove fallback pronoun. Assign a new one before removal.`);
|
|
600
|
+
}
|
|
601
|
+
pronoun.resolver = undefined;
|
|
501
602
|
this.resolveMap.delete(pronoun.sbj[0]);
|
|
502
603
|
return this;
|
|
503
604
|
}
|
package/lib/esm/index.d.ts
CHANGED
|
@@ -6,11 +6,13 @@
|
|
|
6
6
|
* and `rfx`.
|
|
7
7
|
*/
|
|
8
8
|
declare class Pronoun {
|
|
9
|
+
[key: string]: any;
|
|
9
10
|
sbj: Array<string>;
|
|
10
11
|
obj: Array<string>;
|
|
11
12
|
psv: Array<string>;
|
|
12
13
|
psj: Array<string>;
|
|
13
14
|
rfx: Array<string>;
|
|
15
|
+
resolver?: Pronouny;
|
|
14
16
|
/** @constructor */
|
|
15
17
|
constructor(pronouns: {
|
|
16
18
|
subject: Array<string> | string;
|
|
@@ -18,7 +20,7 @@ declare class Pronoun {
|
|
|
18
20
|
possessive: Array<string> | string;
|
|
19
21
|
psAdjective: Array<string> | string;
|
|
20
22
|
reflexive: Array<string> | string;
|
|
21
|
-
});
|
|
23
|
+
}, resolver?: Pronouny);
|
|
22
24
|
/**# Subject Getter
|
|
23
25
|
* Get a subject pronoun. By default, uses one at
|
|
24
26
|
* random and fails silently by using the last
|
|
@@ -115,6 +117,11 @@ declare class Pronoun {
|
|
|
115
117
|
* Default is `true`.
|
|
116
118
|
*/
|
|
117
119
|
reflexive(index?: number, useRandom?: boolean, failQuietly?: boolean): string;
|
|
120
|
+
/**# `.parse()` a Template String
|
|
121
|
+
* A method to parse tagged template strings and provide
|
|
122
|
+
* the correct pronouns, as per a particular PronounSet.
|
|
123
|
+
*/
|
|
124
|
+
parse(strings: TemplateStringsArray, ...args: Array<string>): string;
|
|
118
125
|
}
|
|
119
126
|
/**# `PronounSet` class
|
|
120
127
|
* A private class to construct the methods on
|
|
@@ -140,7 +147,7 @@ declare class PronounSet {
|
|
|
140
147
|
* @param failQuietly
|
|
141
148
|
* Override default behavior to fail into 'they'.
|
|
142
149
|
*/
|
|
143
|
-
add(pronoun: string | Pronoun | Array<string> | Array<Pronoun>, failQuietly?: boolean
|
|
150
|
+
add(pronoun: string | Pronoun | Array<string> | Array<Pronoun>, failQuietly?: boolean): this;
|
|
144
151
|
/**# `.remove()` from PronounSet
|
|
145
152
|
* Remove pronoun/s from a given Pronoun or strings
|
|
146
153
|
* (resolved using the {@linkcode Pronouny} instance
|
|
@@ -153,7 +160,7 @@ declare class PronounSet {
|
|
|
153
160
|
* @param failQuietly
|
|
154
161
|
* Override default behavior to fail into no removal.
|
|
155
162
|
*/
|
|
156
|
-
remove(
|
|
163
|
+
remove(pronoun: string | Pronoun | Array<string> | Array<Pronoun>, failQuietly?: boolean): this;
|
|
157
164
|
/**# `.use()` a PronounSet
|
|
158
165
|
* A method to retrieve one Pronoun at random. Use with
|
|
159
166
|
* {@link Pronoun|`Pronoun` methods} to get a specific
|
|
@@ -162,7 +169,7 @@ declare class PronounSet {
|
|
|
162
169
|
* {@link PronounSet.possessive|`.possessive()`}, or
|
|
163
170
|
* {@link PronounSet.reflexive|`.reflexive()`}. This is mostly
|
|
164
171
|
* here if you'd like to explicitly get a `Pronoun` instance
|
|
165
|
-
* from a `PronounSet`.
|
|
172
|
+
* from a `PronounSet`. Also useful for narrowing {@link PronounSet.parse|`.parse()`}.
|
|
166
173
|
*
|
|
167
174
|
* @param index
|
|
168
175
|
* Retrieve `index`th pronoun from insertion.
|
|
@@ -174,17 +181,24 @@ declare class PronounSet {
|
|
|
174
181
|
* Override default behavior to use random as per
|
|
175
182
|
* Pronouny config.
|
|
176
183
|
*/
|
|
177
|
-
use(index?: number, failQuietly?: boolean
|
|
184
|
+
use(index?: number, failQuietly?: boolean, useRandom?: boolean): Pronoun;
|
|
178
185
|
/**Retrieves a random subject pronoun. */
|
|
179
|
-
subject(index?: number, failQuietly?: boolean
|
|
186
|
+
subject(index?: number, failQuietly?: boolean, useRandom?: boolean): string;
|
|
180
187
|
/**Retrieves a random object pronoun. */
|
|
181
|
-
object(index?: number, failQuietly?: boolean
|
|
188
|
+
object(index?: number, failQuietly?: boolean, useRandom?: boolean): string;
|
|
182
189
|
/**Retrieves a random possessive pronoun. */
|
|
183
|
-
possessive(index?: number, failQuietly?: boolean
|
|
190
|
+
possessive(index?: number, failQuietly?: boolean, useRandom?: boolean): string;
|
|
184
191
|
/**Retrieves a random possessive adjective pronoun. */
|
|
185
|
-
psAdjective(index?: number, failQuietly?: boolean
|
|
192
|
+
psAdjective(index?: number, failQuietly?: boolean, useRandom?: boolean): string;
|
|
186
193
|
/**Retrieves a random reflexive pronoun. */
|
|
187
|
-
reflexive(index?: number, failQuietly?: boolean
|
|
194
|
+
reflexive(index?: number, failQuietly?: boolean, useRandom?: boolean): string;
|
|
195
|
+
/**# Template Parsing
|
|
196
|
+
* A method to select only one pronoun from the set
|
|
197
|
+
* and reuse it for the entire string. This is useful
|
|
198
|
+
* for when you want to use the same pronoun for a
|
|
199
|
+
* sentence.
|
|
200
|
+
*/
|
|
201
|
+
parse(strings: TemplateStringsArray, ...args: Array<string>): string;
|
|
188
202
|
}
|
|
189
203
|
/**# Config
|
|
190
204
|
* `failQuietly`: Controls various quiet failure states.
|
|
@@ -197,19 +211,33 @@ declare class PronounSet {
|
|
|
197
211
|
* used. If false, it will default to 0. If failing
|
|
198
212
|
* quietly and indexing out of bounds, it will use the
|
|
199
213
|
* last item.
|
|
214
|
+
*
|
|
215
|
+
* `fallbackPronoun`: Controls what is considered the
|
|
216
|
+
* default pronoun for when one is not assigned or for
|
|
217
|
+
* quiet failure states. By default, this is `they`. **It
|
|
218
|
+
* is not recommended to change this.**
|
|
200
219
|
*/
|
|
201
220
|
type PronounyConfig = {
|
|
202
221
|
/**Controls various quiet failure states. */
|
|
203
|
-
failQuietly
|
|
222
|
+
failQuietly: boolean;
|
|
204
223
|
/**Controls how deeply Pronouny will search for
|
|
205
224
|
* queries. By default, `string` parameters will
|
|
206
225
|
* only resolve using the first subject pronoun. */
|
|
207
|
-
deepSearch
|
|
226
|
+
deepSearch: boolean;
|
|
208
227
|
/**Controls whether random selection will be used.
|
|
209
228
|
* If false, it will default to 0. If failing
|
|
210
229
|
* quietly and indexing out of bounds, it will
|
|
211
230
|
* use the last item. */
|
|
212
|
-
useRandom
|
|
231
|
+
useRandom: boolean;
|
|
232
|
+
/**Controls what is considered the default pronoun
|
|
233
|
+
* for when one is not assigned or for quiet failure
|
|
234
|
+
* states. By default, this is `they`. **It is not
|
|
235
|
+
* recommended to change this.**
|
|
236
|
+
*
|
|
237
|
+
* If you remove `they` from the Pronouny instance,
|
|
238
|
+
* you are responsible for setting this to a valid
|
|
239
|
+
* pronoun string and ensuring it will resolve. */
|
|
240
|
+
fallbackPronoun: string;
|
|
213
241
|
};
|
|
214
242
|
/**# Pronouny
|
|
215
243
|
* A typed library intended to make English pronoun
|
|
@@ -236,15 +264,40 @@ type PronounyConfig = {
|
|
|
236
264
|
* `Pronoun` object.
|
|
237
265
|
*/
|
|
238
266
|
export default class Pronouny {
|
|
267
|
+
[keyof: string]: any;
|
|
239
268
|
config: PronounyConfig;
|
|
240
269
|
resolveMap: Map<string, Pronoun>;
|
|
270
|
+
default: {
|
|
271
|
+
he: Pronoun;
|
|
272
|
+
she: Pronoun;
|
|
273
|
+
they: Pronoun;
|
|
274
|
+
you: Pronoun;
|
|
275
|
+
i: Pronoun;
|
|
276
|
+
we: Pronoun;
|
|
277
|
+
config: {
|
|
278
|
+
failQuietly: boolean;
|
|
279
|
+
deepSearch: boolean;
|
|
280
|
+
useRandom: boolean;
|
|
281
|
+
fallbackPronoun: string;
|
|
282
|
+
};
|
|
283
|
+
};
|
|
241
284
|
constructor(config?: PronounyConfig);
|
|
242
285
|
/**# `.resolve()` a Pronoun
|
|
243
286
|
* Returns a pronoun from a given string. Uses
|
|
244
287
|
* Pronouny's config to determine the depth of search.
|
|
245
288
|
* By default, only checks the first subject pronoun.
|
|
246
289
|
*/
|
|
247
|
-
resolve(pronoun: string, deepSearch?: boolean
|
|
290
|
+
resolve(pronoun: string, deepSearch?: boolean): Pronoun;
|
|
291
|
+
/**# `.identify()` a Pronoun
|
|
292
|
+
* Returns a string describing the type of pronoun
|
|
293
|
+
* passed in. Returns `subject`, `object`,
|
|
294
|
+
* `possessive`, `possessive adjective`, or
|
|
295
|
+
* `reflexive`. You can use this to access other
|
|
296
|
+
* pronouns by retrieving via string indexing.
|
|
297
|
+
* **This method is case-sensitive and does not
|
|
298
|
+
* have a quiet failure state.**
|
|
299
|
+
*/
|
|
300
|
+
identify(pronoun: string): string;
|
|
248
301
|
/**# `.add()` an Unlisted Pronoun
|
|
249
302
|
* If you made a `.new()` Pronoun but disabled
|
|
250
303
|
* `autoAppend`, you can later append it back by
|
package/lib/esm/index.js
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
function randomToLimit(limit) {
|
|
2
2
|
return Math.floor(Math.random() * limit);
|
|
3
3
|
}
|
|
4
|
+
function isUpperCase(str) {
|
|
5
|
+
return str === str.toUpperCase() && str !== str.toLowerCase();
|
|
6
|
+
}
|
|
4
7
|
/**# `Pronoun` class
|
|
5
8
|
* A private class to construct the methods for pronoun
|
|
6
9
|
* access. Use {@link Pronouny} to create and
|
|
@@ -10,7 +13,7 @@ function randomToLimit(limit) {
|
|
|
10
13
|
*/
|
|
11
14
|
class Pronoun {
|
|
12
15
|
/** @constructor */
|
|
13
|
-
constructor(pronouns) {
|
|
16
|
+
constructor(pronouns, resolver) {
|
|
14
17
|
this.sbj =
|
|
15
18
|
typeof pronouns.subject === "string"
|
|
16
19
|
? [pronouns.subject]
|
|
@@ -31,6 +34,9 @@ class Pronoun {
|
|
|
31
34
|
typeof pronouns.reflexive === "string"
|
|
32
35
|
? [pronouns.reflexive]
|
|
33
36
|
: pronouns.reflexive;
|
|
37
|
+
if (resolver) {
|
|
38
|
+
this.resolver = resolver;
|
|
39
|
+
}
|
|
34
40
|
return this;
|
|
35
41
|
}
|
|
36
42
|
/**# Subject Getter
|
|
@@ -199,6 +205,25 @@ class Pronoun {
|
|
|
199
205
|
throw new Error(`could not retrieve index ${index} from "${this.sbj[0]}" reflexive pronoun`);
|
|
200
206
|
}
|
|
201
207
|
}
|
|
208
|
+
/**# `.parse()` a Template String
|
|
209
|
+
* A method to parse tagged template strings and provide
|
|
210
|
+
* the correct pronouns, as per a particular PronounSet.
|
|
211
|
+
*/
|
|
212
|
+
parse(strings, ...args) {
|
|
213
|
+
let result = strings[0];
|
|
214
|
+
for (let i = 0; i < args.length; i++) {
|
|
215
|
+
const upperCaseFlag = isUpperCase(args[i][0]);
|
|
216
|
+
let argSub = this.resolver?.identify(args[i].toLowerCase()) !== ""
|
|
217
|
+
? this.use()[this.resolver?.identify(args[i].toLowerCase()) ||
|
|
218
|
+
new Pronouny().identify(args[i].toLowerCase())]()
|
|
219
|
+
: args[i];
|
|
220
|
+
if (upperCaseFlag) {
|
|
221
|
+
argSub = argSub[0].toUpperCase() + argSub.slice(1);
|
|
222
|
+
}
|
|
223
|
+
result += argSub + strings[i + 1];
|
|
224
|
+
}
|
|
225
|
+
return result;
|
|
226
|
+
}
|
|
202
227
|
}
|
|
203
228
|
/**# `PronounSet` class
|
|
204
229
|
* A private class to construct the methods on
|
|
@@ -214,27 +239,27 @@ class PronounSet {
|
|
|
214
239
|
this.resolver = resolver;
|
|
215
240
|
switch (true) {
|
|
216
241
|
case Array.isArray(pronouns):
|
|
217
|
-
|
|
242
|
+
for (const x of pronouns) {
|
|
218
243
|
if (x instanceof Pronoun) {
|
|
219
244
|
this.pronouns.add(x);
|
|
220
245
|
}
|
|
221
246
|
else {
|
|
222
247
|
this.pronouns.add(resolver.resolve(x));
|
|
223
248
|
}
|
|
224
|
-
}
|
|
249
|
+
}
|
|
225
250
|
break;
|
|
226
251
|
case typeof pronouns === "string":
|
|
227
252
|
this.pronouns.add(resolver.resolve(pronouns));
|
|
228
253
|
break;
|
|
229
254
|
default:
|
|
230
|
-
|
|
255
|
+
for (const x of pronouns) {
|
|
231
256
|
if (x instanceof Pronoun) {
|
|
232
257
|
this.pronouns.add(x);
|
|
233
258
|
}
|
|
234
259
|
else {
|
|
235
260
|
this.pronouns.add(resolver.resolve(x));
|
|
236
261
|
}
|
|
237
|
-
}
|
|
262
|
+
}
|
|
238
263
|
break;
|
|
239
264
|
}
|
|
240
265
|
return this;
|
|
@@ -260,12 +285,12 @@ class PronounSet {
|
|
|
260
285
|
this.pronouns.add(this.resolver.resolve(pronoun));
|
|
261
286
|
break;
|
|
262
287
|
case pronoun instanceof Array:
|
|
263
|
-
|
|
288
|
+
for (const p of pronoun) {
|
|
264
289
|
this.add(p, failQuietly);
|
|
265
|
-
}
|
|
290
|
+
}
|
|
266
291
|
break;
|
|
267
292
|
case failQuietly:
|
|
268
|
-
this.pronouns.add(this.resolver.resolve(
|
|
293
|
+
this.pronouns.add(this.resolver.resolve(this.resolver.config.fallbackPronoun));
|
|
269
294
|
break;
|
|
270
295
|
case !failQuietly:
|
|
271
296
|
default:
|
|
@@ -285,18 +310,18 @@ class PronounSet {
|
|
|
285
310
|
* @param failQuietly
|
|
286
311
|
* Override default behavior to fail into no removal.
|
|
287
312
|
*/
|
|
288
|
-
remove(
|
|
313
|
+
remove(pronoun, failQuietly = this.resolver.config.failQuietly) {
|
|
289
314
|
switch (true) {
|
|
290
315
|
case pronoun instanceof Pronoun:
|
|
291
|
-
|
|
316
|
+
this.pronouns.delete(pronoun);
|
|
292
317
|
break;
|
|
293
318
|
case typeof pronoun === "string":
|
|
294
|
-
|
|
319
|
+
this.pronouns.delete(this.resolver.resolve(pronoun));
|
|
295
320
|
break;
|
|
296
321
|
case pronoun instanceof Array:
|
|
297
|
-
|
|
298
|
-
this.remove(
|
|
299
|
-
}
|
|
322
|
+
for (const p of pronoun) {
|
|
323
|
+
this.remove(p);
|
|
324
|
+
}
|
|
300
325
|
break;
|
|
301
326
|
case failQuietly:
|
|
302
327
|
break;
|
|
@@ -314,7 +339,7 @@ class PronounSet {
|
|
|
314
339
|
* {@link PronounSet.possessive|`.possessive()`}, or
|
|
315
340
|
* {@link PronounSet.reflexive|`.reflexive()`}. This is mostly
|
|
316
341
|
* here if you'd like to explicitly get a `Pronoun` instance
|
|
317
|
-
* from a `PronounSet`.
|
|
342
|
+
* from a `PronounSet`. Also useful for narrowing {@link PronounSet.parse|`.parse()`}.
|
|
318
343
|
*
|
|
319
344
|
* @param index
|
|
320
345
|
* Retrieve `index`th pronoun from insertion.
|
|
@@ -344,7 +369,7 @@ class PronounSet {
|
|
|
344
369
|
case indexable[index] !== undefined:
|
|
345
370
|
return indexable[index];
|
|
346
371
|
case failQuietly:
|
|
347
|
-
return this.resolver.resolve(
|
|
372
|
+
return this.resolver.resolve(this.resolver.config.fallbackPronoun);
|
|
348
373
|
default:
|
|
349
374
|
throw new Error(`Index ${index} out of range`);
|
|
350
375
|
}
|
|
@@ -369,54 +394,27 @@ class PronounSet {
|
|
|
369
394
|
reflexive(index = -1, failQuietly = this.resolver.config.failQuietly, useRandom = this.resolver.config.useRandom) {
|
|
370
395
|
return this.use(index, failQuietly, useRandom).reflexive(-1, failQuietly, useRandom);
|
|
371
396
|
}
|
|
397
|
+
/**# Template Parsing
|
|
398
|
+
* A method to select only one pronoun from the set
|
|
399
|
+
* and reuse it for the entire string. This is useful
|
|
400
|
+
* for when you want to use the same pronoun for a
|
|
401
|
+
* sentence.
|
|
402
|
+
*/
|
|
403
|
+
parse(strings, ...args) {
|
|
404
|
+
let result = strings[0];
|
|
405
|
+
for (let i = 0; i < args.length; i++) {
|
|
406
|
+
const upperCaseFlag = isUpperCase(args[i][0]);
|
|
407
|
+
let argSub = this.resolver.identify(args[i].toLowerCase()) !== ""
|
|
408
|
+
? this.use()[this.resolver.identify(args[i].toLowerCase())]()
|
|
409
|
+
: args[i];
|
|
410
|
+
if (upperCaseFlag) {
|
|
411
|
+
argSub = argSub[0].toUpperCase() + argSub.slice(1);
|
|
412
|
+
}
|
|
413
|
+
result += argSub + strings[i + 1];
|
|
414
|
+
}
|
|
415
|
+
return result;
|
|
416
|
+
}
|
|
372
417
|
}
|
|
373
|
-
const pronounHe = new Pronoun({
|
|
374
|
-
subject: ["he"],
|
|
375
|
-
object: ["him"],
|
|
376
|
-
possessive: ["his"],
|
|
377
|
-
psAdjective: ["his"],
|
|
378
|
-
reflexive: ["himself"],
|
|
379
|
-
});
|
|
380
|
-
const pronounShe = new Pronoun({
|
|
381
|
-
subject: ["she"],
|
|
382
|
-
object: ["her"],
|
|
383
|
-
possessive: ["hers"],
|
|
384
|
-
psAdjective: ["her"],
|
|
385
|
-
reflexive: ["herself"],
|
|
386
|
-
});
|
|
387
|
-
const pronounThey = new Pronoun({
|
|
388
|
-
subject: ["they"],
|
|
389
|
-
object: ["them"],
|
|
390
|
-
possessive: ["theirs"],
|
|
391
|
-
psAdjective: ["their"],
|
|
392
|
-
reflexive: ["theirself", "theirselves", "themselves", "themself"],
|
|
393
|
-
});
|
|
394
|
-
const pronounYou = new Pronoun({
|
|
395
|
-
subject: ["you"],
|
|
396
|
-
object: ["you"],
|
|
397
|
-
possessive: ["yours"],
|
|
398
|
-
psAdjective: ["your"],
|
|
399
|
-
reflexive: ["yourself"],
|
|
400
|
-
});
|
|
401
|
-
const pronounI = new Pronoun({
|
|
402
|
-
subject: ["I"],
|
|
403
|
-
object: ["me"],
|
|
404
|
-
possessive: ["mine"],
|
|
405
|
-
psAdjective: ["my"],
|
|
406
|
-
reflexive: ["myself"],
|
|
407
|
-
});
|
|
408
|
-
const pronounWe = new Pronoun({
|
|
409
|
-
subject: ["we"],
|
|
410
|
-
object: ["us"],
|
|
411
|
-
possessive: ["our"],
|
|
412
|
-
psAdjective: ["ours"],
|
|
413
|
-
reflexive: ["ourselves"],
|
|
414
|
-
});
|
|
415
|
-
const PronounyDefaultConfig = {
|
|
416
|
-
failQuietly: true,
|
|
417
|
-
deepSearch: false,
|
|
418
|
-
useRandom: true,
|
|
419
|
-
};
|
|
420
418
|
/**# Pronouny
|
|
421
419
|
* A typed library intended to make English pronoun
|
|
422
420
|
* resolution easy. Instantiate in your project using
|
|
@@ -442,15 +440,76 @@ const PronounyDefaultConfig = {
|
|
|
442
440
|
* `Pronoun` object.
|
|
443
441
|
*/
|
|
444
442
|
export default class Pronouny {
|
|
445
|
-
constructor(config =
|
|
446
|
-
|
|
443
|
+
constructor(config = {
|
|
444
|
+
failQuietly: true,
|
|
445
|
+
deepSearch: false,
|
|
446
|
+
useRandom: true,
|
|
447
|
+
fallbackPronoun: "they",
|
|
448
|
+
}) {
|
|
449
|
+
this.default = {
|
|
450
|
+
he: new Pronoun({
|
|
451
|
+
subject: ["he"],
|
|
452
|
+
object: ["him"],
|
|
453
|
+
possessive: ["his"],
|
|
454
|
+
psAdjective: ["his"],
|
|
455
|
+
reflexive: ["himself"],
|
|
456
|
+
}, this),
|
|
457
|
+
she: new Pronoun({
|
|
458
|
+
subject: ["she"],
|
|
459
|
+
object: ["her"],
|
|
460
|
+
possessive: ["hers"],
|
|
461
|
+
psAdjective: ["her"],
|
|
462
|
+
reflexive: ["herself"],
|
|
463
|
+
}, this),
|
|
464
|
+
they: new Pronoun({
|
|
465
|
+
subject: ["they"],
|
|
466
|
+
object: ["them"],
|
|
467
|
+
possessive: ["theirs"],
|
|
468
|
+
psAdjective: ["their"],
|
|
469
|
+
reflexive: [
|
|
470
|
+
"theirself",
|
|
471
|
+
"theirselves",
|
|
472
|
+
"themselves",
|
|
473
|
+
"themself",
|
|
474
|
+
],
|
|
475
|
+
}, this),
|
|
476
|
+
you: new Pronoun({
|
|
477
|
+
subject: ["you"],
|
|
478
|
+
object: ["you"],
|
|
479
|
+
possessive: ["yours"],
|
|
480
|
+
psAdjective: ["your"],
|
|
481
|
+
reflexive: ["yourself"],
|
|
482
|
+
}, this),
|
|
483
|
+
i: new Pronoun({
|
|
484
|
+
subject: ["I"],
|
|
485
|
+
object: ["me"],
|
|
486
|
+
possessive: ["mine"],
|
|
487
|
+
psAdjective: ["my"],
|
|
488
|
+
reflexive: ["myself"],
|
|
489
|
+
}, this),
|
|
490
|
+
we: new Pronoun({
|
|
491
|
+
subject: ["we"],
|
|
492
|
+
object: ["us"],
|
|
493
|
+
possessive: ["ours"],
|
|
494
|
+
psAdjective: ["our"],
|
|
495
|
+
reflexive: ["ourselves"],
|
|
496
|
+
}, this),
|
|
497
|
+
config: {
|
|
498
|
+
failQuietly: true,
|
|
499
|
+
deepSearch: false,
|
|
500
|
+
useRandom: true,
|
|
501
|
+
fallbackPronoun: "they",
|
|
502
|
+
},
|
|
503
|
+
};
|
|
504
|
+
const defaults = this.default;
|
|
505
|
+
this.config = Object.assign(defaults.config, config);
|
|
447
506
|
this.resolveMap = new Map([
|
|
448
|
-
[
|
|
449
|
-
[
|
|
450
|
-
[
|
|
451
|
-
[
|
|
452
|
-
[
|
|
453
|
-
[
|
|
507
|
+
[defaults.they.sbj[0], defaults.they],
|
|
508
|
+
[defaults.you.sbj[0], defaults.you],
|
|
509
|
+
[defaults.we.sbj[0], defaults.we],
|
|
510
|
+
[defaults.i.sbj[0], defaults.i],
|
|
511
|
+
[defaults.he.sbj[0], defaults.he],
|
|
512
|
+
[defaults.she.sbj[0], defaults.she],
|
|
454
513
|
]);
|
|
455
514
|
return this;
|
|
456
515
|
}
|
|
@@ -466,28 +525,66 @@ export default class Pronouny {
|
|
|
466
525
|
return resolvedPronoun;
|
|
467
526
|
case deepSearch:
|
|
468
527
|
let searchResult;
|
|
469
|
-
this.resolveMap
|
|
470
|
-
Object.values(
|
|
528
|
+
for (const [_, value] of this.resolveMap) {
|
|
529
|
+
for (const type of Object.values(value)) {
|
|
471
530
|
if (type.includes(pronoun)) {
|
|
472
|
-
searchResult =
|
|
531
|
+
searchResult = value;
|
|
473
532
|
}
|
|
474
|
-
}
|
|
475
|
-
}
|
|
533
|
+
}
|
|
534
|
+
}
|
|
476
535
|
if (searchResult) {
|
|
477
536
|
return searchResult;
|
|
478
537
|
}
|
|
479
538
|
case this.config.failQuietly:
|
|
480
|
-
return this.resolveMap.get(
|
|
539
|
+
return this.resolveMap.get(this.config.fallbackPronoun);
|
|
481
540
|
default:
|
|
482
541
|
throw new Error(`Pronoun "${pronoun}" not found`);
|
|
483
542
|
}
|
|
484
543
|
}
|
|
544
|
+
/**# `.identify()` a Pronoun
|
|
545
|
+
* Returns a string describing the type of pronoun
|
|
546
|
+
* passed in. Returns `subject`, `object`,
|
|
547
|
+
* `possessive`, `possessive adjective`, or
|
|
548
|
+
* `reflexive`. You can use this to access other
|
|
549
|
+
* pronouns by retrieving via string indexing.
|
|
550
|
+
* **This method is case-sensitive and does not
|
|
551
|
+
* have a quiet failure state.**
|
|
552
|
+
*/
|
|
553
|
+
identify(pronoun) {
|
|
554
|
+
let pronounType = "";
|
|
555
|
+
for (const [_, value] of this.resolveMap) {
|
|
556
|
+
for (let i = 0, types = Object.values(value); i < types.length; i++) {
|
|
557
|
+
let type = types[i];
|
|
558
|
+
if (Object.keys(value)[i] === "resolver") {
|
|
559
|
+
continue;
|
|
560
|
+
}
|
|
561
|
+
if (type.includes(pronoun)) {
|
|
562
|
+
pronounType = Object.keys(value)[i];
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
switch (pronounType) {
|
|
567
|
+
case "sbj":
|
|
568
|
+
return "subject";
|
|
569
|
+
case "obj":
|
|
570
|
+
return "object";
|
|
571
|
+
case "psv":
|
|
572
|
+
return "possessive";
|
|
573
|
+
case "psj":
|
|
574
|
+
return "psAdjective";
|
|
575
|
+
case "rfx":
|
|
576
|
+
return "reflexive";
|
|
577
|
+
default:
|
|
578
|
+
return "";
|
|
579
|
+
}
|
|
580
|
+
}
|
|
485
581
|
/**# `.add()` an Unlisted Pronoun
|
|
486
582
|
* If you made a `.new()` Pronoun but disabled
|
|
487
583
|
* `autoAppend`, you can later append it back by
|
|
488
584
|
* using this method. Returns `Pronouny`.
|
|
489
585
|
*/
|
|
490
586
|
add(pronoun) {
|
|
587
|
+
pronoun.resolver = this;
|
|
491
588
|
this.resolveMap.set(pronoun.sbj[0], pronoun);
|
|
492
589
|
return this;
|
|
493
590
|
}
|
|
@@ -496,6 +593,10 @@ export default class Pronouny {
|
|
|
496
593
|
* Pronouny instance. Returns `Pronouny`.
|
|
497
594
|
*/
|
|
498
595
|
remove(pronoun) {
|
|
596
|
+
if (pronoun === this.resolveMap.get(this.config.fallbackPronoun)) {
|
|
597
|
+
throw new Error(`Cannot remove fallback pronoun. Assign a new one before removal.`);
|
|
598
|
+
}
|
|
599
|
+
pronoun.resolver = undefined;
|
|
499
600
|
this.resolveMap.delete(pronoun.sbj[0]);
|
|
500
601
|
return this;
|
|
501
602
|
}
|
package/package.json
CHANGED