pronouny 0.4.0 → 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 +14 -0
- package/lib/cjs/index.d.ts +41 -2
- package/lib/cjs/index.js +155 -58
- package/lib/esm/index.d.ts +41 -2
- package/lib/esm/index.js +155 -58
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -72,10 +72,24 @@ console.log(
|
|
|
72
72
|
// This returns "vaynegarden updated her status", "vaynegarden
|
|
73
73
|
// updated hir status", or "vaynegarden updated their status",
|
|
74
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.
|
|
75
83
|
```
|
|
76
84
|
|
|
77
85
|
## Changelog
|
|
78
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
|
+
|
|
79
93
|
### v0.4.0
|
|
80
94
|
|
|
81
95
|
- Optimized `forEach()` calls to use `for` loops instead.
|
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
|
|
@@ -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.
|
|
@@ -185,6 +192,13 @@ declare class PronounSet {
|
|
|
185
192
|
psAdjective(index?: number, failQuietly?: boolean, useRandom?: boolean): string;
|
|
186
193
|
/**Retrieves a random reflexive pronoun. */
|
|
187
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.
|
|
@@ -250,8 +264,23 @@ type PronounyConfig = {
|
|
|
250
264
|
* `Pronoun` object.
|
|
251
265
|
*/
|
|
252
266
|
export default class Pronouny {
|
|
267
|
+
[keyof: string]: any;
|
|
253
268
|
config: PronounyConfig;
|
|
254
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
|
+
};
|
|
255
284
|
constructor(config?: PronounyConfig);
|
|
256
285
|
/**# `.resolve()` a Pronoun
|
|
257
286
|
* Returns a pronoun from a given string. Uses
|
|
@@ -259,6 +288,16 @@ export default class Pronouny {
|
|
|
259
288
|
* By default, only checks the first subject pronoun.
|
|
260
289
|
*/
|
|
261
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;
|
|
262
301
|
/**# `.add()` an Unlisted Pronoun
|
|
263
302
|
* If you made a `.new()` Pronoun but disabled
|
|
264
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
|
|
@@ -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.
|
|
@@ -371,55 +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
|
-
fallbackPronoun: "they",
|
|
422
|
-
};
|
|
423
420
|
/**# Pronouny
|
|
424
421
|
* A typed library intended to make English pronoun
|
|
425
422
|
* resolution easy. Instantiate in your project using
|
|
@@ -445,15 +442,76 @@ const PronounyDefaultConfig = {
|
|
|
445
442
|
* `Pronoun` object.
|
|
446
443
|
*/
|
|
447
444
|
class Pronouny {
|
|
448
|
-
constructor(config =
|
|
449
|
-
|
|
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);
|
|
450
508
|
this.resolveMap = new Map([
|
|
451
|
-
[
|
|
452
|
-
[
|
|
453
|
-
[
|
|
454
|
-
[
|
|
455
|
-
[
|
|
456
|
-
[
|
|
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],
|
|
457
515
|
]);
|
|
458
516
|
return this;
|
|
459
517
|
}
|
|
@@ -485,12 +543,50 @@ class Pronouny {
|
|
|
485
543
|
throw new Error(`Pronoun "${pronoun}" not found`);
|
|
486
544
|
}
|
|
487
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
|
+
}
|
|
488
583
|
/**# `.add()` an Unlisted Pronoun
|
|
489
584
|
* If you made a `.new()` Pronoun but disabled
|
|
490
585
|
* `autoAppend`, you can later append it back by
|
|
491
586
|
* using this method. Returns `Pronouny`.
|
|
492
587
|
*/
|
|
493
588
|
add(pronoun) {
|
|
589
|
+
pronoun.resolver = this;
|
|
494
590
|
this.resolveMap.set(pronoun.sbj[0], pronoun);
|
|
495
591
|
return this;
|
|
496
592
|
}
|
|
@@ -502,6 +598,7 @@ class Pronouny {
|
|
|
502
598
|
if (pronoun === this.resolveMap.get(this.config.fallbackPronoun)) {
|
|
503
599
|
throw new Error(`Cannot remove fallback pronoun. Assign a new one before removal.`);
|
|
504
600
|
}
|
|
601
|
+
pronoun.resolver = undefined;
|
|
505
602
|
this.resolveMap.delete(pronoun.sbj[0]);
|
|
506
603
|
return this;
|
|
507
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
|
|
@@ -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.
|
|
@@ -185,6 +192,13 @@ declare class PronounSet {
|
|
|
185
192
|
psAdjective(index?: number, failQuietly?: boolean, useRandom?: boolean): string;
|
|
186
193
|
/**Retrieves a random reflexive pronoun. */
|
|
187
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.
|
|
@@ -250,8 +264,23 @@ type PronounyConfig = {
|
|
|
250
264
|
* `Pronoun` object.
|
|
251
265
|
*/
|
|
252
266
|
export default class Pronouny {
|
|
267
|
+
[keyof: string]: any;
|
|
253
268
|
config: PronounyConfig;
|
|
254
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
|
+
};
|
|
255
284
|
constructor(config?: PronounyConfig);
|
|
256
285
|
/**# `.resolve()` a Pronoun
|
|
257
286
|
* Returns a pronoun from a given string. Uses
|
|
@@ -259,6 +288,16 @@ export default class Pronouny {
|
|
|
259
288
|
* By default, only checks the first subject pronoun.
|
|
260
289
|
*/
|
|
261
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;
|
|
262
301
|
/**# `.add()` an Unlisted Pronoun
|
|
263
302
|
* If you made a `.new()` Pronoun but disabled
|
|
264
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
|
|
@@ -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.
|
|
@@ -369,55 +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
|
-
fallbackPronoun: "they",
|
|
420
|
-
};
|
|
421
418
|
/**# Pronouny
|
|
422
419
|
* A typed library intended to make English pronoun
|
|
423
420
|
* resolution easy. Instantiate in your project using
|
|
@@ -443,15 +440,76 @@ const PronounyDefaultConfig = {
|
|
|
443
440
|
* `Pronoun` object.
|
|
444
441
|
*/
|
|
445
442
|
export default class Pronouny {
|
|
446
|
-
constructor(config =
|
|
447
|
-
|
|
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);
|
|
448
506
|
this.resolveMap = new Map([
|
|
449
|
-
[
|
|
450
|
-
[
|
|
451
|
-
[
|
|
452
|
-
[
|
|
453
|
-
[
|
|
454
|
-
[
|
|
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],
|
|
455
513
|
]);
|
|
456
514
|
return this;
|
|
457
515
|
}
|
|
@@ -483,12 +541,50 @@ export default class Pronouny {
|
|
|
483
541
|
throw new Error(`Pronoun "${pronoun}" not found`);
|
|
484
542
|
}
|
|
485
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
|
+
}
|
|
486
581
|
/**# `.add()` an Unlisted Pronoun
|
|
487
582
|
* If you made a `.new()` Pronoun but disabled
|
|
488
583
|
* `autoAppend`, you can later append it back by
|
|
489
584
|
* using this method. Returns `Pronouny`.
|
|
490
585
|
*/
|
|
491
586
|
add(pronoun) {
|
|
587
|
+
pronoun.resolver = this;
|
|
492
588
|
this.resolveMap.set(pronoun.sbj[0], pronoun);
|
|
493
589
|
return this;
|
|
494
590
|
}
|
|
@@ -500,6 +596,7 @@ export default class Pronouny {
|
|
|
500
596
|
if (pronoun === this.resolveMap.get(this.config.fallbackPronoun)) {
|
|
501
597
|
throw new Error(`Cannot remove fallback pronoun. Assign a new one before removal.`);
|
|
502
598
|
}
|
|
599
|
+
pronoun.resolver = undefined;
|
|
503
600
|
this.resolveMap.delete(pronoun.sbj[0]);
|
|
504
601
|
return this;
|
|
505
602
|
}
|
package/package.json
CHANGED