pronouny 0.3.0 → 0.4.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  # Pronouny
2
2
 
3
- **Pronouny** is a typed library intended to make programmatically resolving English pronouns easier. It does so by providing three classes: `Pronoun`, `PronounSet` and `Validate`, each with their respective uses.
3
+ **Pronouny** is a typed library intended to make programmatically resolving English pronouns easier.
4
4
 
5
5
  ## Installation
6
6
 
@@ -10,12 +10,12 @@ To install, simply run `npm install pronouny`.
10
10
 
11
11
  ```ts
12
12
  // Single import
13
- import Pronouny from 'pronouny';
13
+ import Pronouny from "pronouny";
14
14
 
15
- // Create a new instance of the Validate class
16
- // All options are optional
15
+ // Create a new instance of Pronouny. All config is optional.
16
+ // Below are defaults.
17
17
  const p = new Pronouny({
18
- // Will default to failing quietly into "they"
18
+ // Will default to failing quietly into "they".
19
19
  failQuietly: true,
20
20
 
21
21
  // Will default to only querying the first subject
@@ -26,16 +26,22 @@ 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()
32
- const pronounZe: Pronoun = p.new({
33
- subject: "ze",
34
- object: "hir",
35
- possessive: "hirs",
36
- psAdjective: "hir",
37
- reflexive: "hirself",
38
- }, false);
35
+ const pronounZe: Pronoun = p.new(
36
+ {
37
+ subject: "ze",
38
+ object: "hir",
39
+ possessive: "hirs",
40
+ psAdjective: "hir",
41
+ reflexive: "hirself",
42
+ },
43
+ false
44
+ );
39
45
 
40
46
  // .new() will automatically add new Pronouns to its
41
47
  // validation map. however, if you want to do so manually,
@@ -46,8 +52,8 @@ p.add(pronounZe);
46
52
  p.remove(p.resolve("I"));
47
53
  // this deletes the "I" pronoun from the map.
48
54
 
49
- // Methods are chainable and you can reach the Pronoun class.
50
- p.add(pronounZe).set("he/they").use().subject().
55
+ // Methods are chainable so you can reach the Pronoun class.
56
+ p.add(pronounZe).set("he/they").use().subject();
51
57
  // ^Pronouny ^PronounSet ^Pronoun ^string
52
58
 
53
59
  // Instantiate a set of pronouns using Pronouny.set().
@@ -55,7 +61,7 @@ const vayne = {
55
61
  username: "vaynegarden",
56
62
  pronouns: p.set("she/ze/they"),
57
63
  };
58
- // from there, you can use `[PronounSet].use()` to get a random pronoun.
64
+ // from there, you can use `[PronounSet].use()` to get a random `Pronoun`.
59
65
 
60
66
  // Resolve the pronouns in your app by referencing the form
61
67
  // that you need. There's `subject`, `object`, `possessive`,
@@ -70,6 +76,18 @@ console.log(
70
76
 
71
77
  ## Changelog
72
78
 
79
+ ### v0.4.0
80
+
81
+ - Optimized `forEach()` calls to use `for` loops instead.
82
+ - Added `fallbackPronoun` option to allow for configuration of pronoun to use in case of quiet failure states.
83
+ - Removed redundant `set` parameter from `[PronounSet].remove()` calls.
84
+
85
+ ### v0.3.1
86
+
87
+ - Added documentation.
88
+ - Reordered `failQuietly` and `useRandom` on `Pronoun`.
89
+ - Tested and fixed examples.
90
+
73
91
  ### v0.3.0
74
92
 
75
93
  - Rewritten to consolidate functionality into a single default export `Pronouny`.
@@ -90,8 +108,6 @@ console.log(
90
108
 
91
109
  ## TODO
92
110
 
93
- [ ] Full rewrite
94
-
95
111
  - [x] Consolidate implementation into one general-use class.
96
112
  - [x] Add global configuration for pronoun use.
97
113
  - [ ] Use more performant data structures.
@@ -1,9 +1,17 @@
1
+ /**# `Pronoun` class
2
+ * A private class to construct the methods for pronoun
3
+ * access. Use {@link Pronouny} to create and
4
+ * append pronouns to the main validator. You can directly
5
+ * fetch pronoun arrays with `sbj`, `obj`, `psv`, `psj`,
6
+ * and `rfx`.
7
+ */
1
8
  declare class Pronoun {
2
9
  sbj: Array<string>;
3
10
  obj: Array<string>;
4
11
  psv: Array<string>;
5
12
  psj: Array<string>;
6
13
  rfx: Array<string>;
14
+ /** @constructor */
7
15
  constructor(pronouns: {
8
16
  subject: Array<string> | string;
9
17
  object: Array<string> | string;
@@ -11,38 +19,282 @@ declare class Pronoun {
11
19
  psAdjective: Array<string> | string;
12
20
  reflexive: Array<string> | string;
13
21
  });
14
- subject(index?: number, failQuietly?: boolean, useRandom?: boolean): string;
15
- object(index?: number, failQuietly?: boolean, useRandom?: boolean): string;
16
- possessive(index?: number, failQuietly?: boolean, useRandom?: boolean): string;
17
- psAdjective(index?: number, failQuietly?: boolean, useRandom?: boolean): string;
18
- reflexive(index?: number, failQuietly?: boolean, useRandom?: boolean): string;
22
+ /**# Subject Getter
23
+ * Get a subject pronoun. By default, uses one at
24
+ * random and fails silently by using the last
25
+ * pronoun if you index out of bounds.
26
+ *
27
+ * @param index
28
+ * Get `index`th pronoun, if specified. Fails to
29
+ * last if out of bounds and failing quietly.
30
+ * Default is `-1`.
31
+ *
32
+ * @param useRandom
33
+ * Overrides default random use behavior.
34
+ * Default is `true`.
35
+ *
36
+ * @param failQuietly
37
+ * Overrides default quiet failure behavior.
38
+ * Default is `true`.
39
+ */
40
+ subject(index?: number, useRandom?: boolean, failQuietly?: boolean): string;
41
+ /**# Object Getter
42
+ * Get an object pronoun. By default, uses one at
43
+ * random and fails silently by using the last
44
+ * pronoun if you index out of bounds.
45
+ *
46
+ * @param index
47
+ * Get `index`th pronoun, if specified. Fails to
48
+ * last if out of bounds and failing quietly.
49
+ * Default is `-1`.
50
+ *
51
+ * @param useRandom
52
+ * Overrides default random use behavior.
53
+ * Default is `true`.
54
+ *
55
+ * @param failQuietly
56
+ * Overrides default quiet failure behavior.
57
+ * Default is `true`.
58
+ */
59
+ object(index?: number, useRandom?: boolean, failQuietly?: boolean): string;
60
+ /**# Possessive Getter
61
+ * Get a possessive pronoun. By default, uses one
62
+ * at random and fails silently by using the last
63
+ * pronoun if you index out of bounds.
64
+ *
65
+ * @param index
66
+ * Get `index`th pronoun, if specified. Fails to
67
+ * last if out of bounds and failing quietly.
68
+ * Default is `-1`.
69
+ *
70
+ * @param useRandom
71
+ * Overrides default random use behavior.
72
+ * Default is `true`.
73
+ *
74
+ * @param failQuietly
75
+ * Overrides default quiet failure behavior.
76
+ * Default is `true`.
77
+ */
78
+ possessive(index?: number, useRandom?: boolean, failQuietly?: boolean): string;
79
+ /**# Possessive Adjective Getter
80
+ * Get a possessive adjective pronoun. By
81
+ * default, uses one at random and fails
82
+ * silently by using the last pronoun if you
83
+ * index out of bounds.
84
+ *
85
+ * @param index
86
+ * Get `index`th pronoun, if specified. Fails to
87
+ * last if out of bounds and failing quietly.
88
+ * Default is `-1`.
89
+ *
90
+ * @param useRandom
91
+ * Overrides default random use behavior.
92
+ * Default is `true`.
93
+ *
94
+ * @param failQuietly
95
+ * Overrides default quiet failure behavior.
96
+ * Default is `true`.
97
+ */
98
+ psAdjective(index?: number, useRandom?: boolean, failQuietly?: boolean): string;
99
+ /**# Reflexive Getter
100
+ * Get a reflexive pronoun. By default, uses one
101
+ * at random and fails silently by using the last
102
+ * pronoun if you index out of bounds.
103
+ *
104
+ * @param index
105
+ * Get `index`th pronoun, if specified. Fails to
106
+ * last if out of bounds and failing quietly.
107
+ * Default is `-1`.
108
+ *
109
+ * @param useRandom
110
+ * Overrides default random use behavior.
111
+ * Default is `true`.
112
+ *
113
+ * @param failQuietly
114
+ * Overrides default quiet failure behavior.
115
+ * Default is `true`.
116
+ */
117
+ reflexive(index?: number, useRandom?: boolean, failQuietly?: boolean): string;
19
118
  }
119
+ /**# `PronounSet` class
120
+ * A private class to construct the methods on
121
+ * sets of pronouns. Use {@linkcode Pronouny.set()}
122
+ * to produce a PronounSet for an entity. You can
123
+ * add pronouns to a set using {@linkcode PronounSet.add()},
124
+ * remove them using {@linkcode PronounSet.remove()}
125
+ */
20
126
  declare class PronounSet {
21
127
  pronouns: Set<Pronoun>;
22
128
  resolver: Pronouny;
129
+ /** @constructor */
23
130
  constructor(resolver: Pronouny, pronouns?: Set<Pronoun> | Array<Pronoun> | Set<string> | Array<string> | string);
24
- add(pronoun: string | Pronoun | Array<string> | Array<Pronoun>, failQuietly?: boolean | undefined): this;
25
- remove(set: PronounSet, pronoun: string | Pronoun | Array<string> | Array<Pronoun>, failQuietly?: boolean | undefined): this;
26
- use(index?: number, failQuietly?: boolean | undefined, useRandom?: boolean | undefined): Pronoun;
27
- subject(index?: number, failQuietly?: boolean | undefined, useRandom?: boolean | undefined): string;
28
- object(index?: number, failQuietly?: boolean | undefined, useRandom?: boolean | undefined): string;
29
- possessive(index?: number, failQuietly?: boolean | undefined, useRandom?: boolean | undefined): string;
30
- psAdjective(index?: number, failQuietly?: boolean | undefined, useRandom?: boolean | undefined): string;
31
- reflexive(index?: number, failQuietly?: boolean | undefined, useRandom?: boolean | undefined): string;
131
+ /**# `.add()` to PronounSet
132
+ * Adds pronoun/s from a given Pronoun or strings
133
+ * (resolved using the {@linkcode Pronouny} instance
134
+ * used to create this PronounSet.)
135
+ *
136
+ * @param pronoun
137
+ * A string, Pronoun, or array of either, to be
138
+ * appended to the PronounSet.
139
+ *
140
+ * @param failQuietly
141
+ * Override default behavior to fail into 'they'.
142
+ */
143
+ add(pronoun: string | Pronoun | Array<string> | Array<Pronoun>, failQuietly?: boolean): this;
144
+ /**# `.remove()` from PronounSet
145
+ * Remove pronoun/s from a given Pronoun or strings
146
+ * (resolved using the {@linkcode Pronouny} instance
147
+ * used to create this PronounSet.)
148
+ *
149
+ * @param pronoun
150
+ * A string, Pronoun, or array of either, to be
151
+ * appended to the PronounSet.
152
+ *
153
+ * @param failQuietly
154
+ * Override default behavior to fail into no removal.
155
+ */
156
+ remove(pronoun: string | Pronoun | Array<string> | Array<Pronoun>, failQuietly?: boolean): this;
157
+ /**# `.use()` a PronounSet
158
+ * A method to retrieve one Pronoun at random. Use with
159
+ * {@link Pronoun|`Pronoun` methods} to get a specific
160
+ * form. You can also directly use {@link PronounSet.subject|`.subject()`},
161
+ * {@link PronounSet.object|`.object()`}, {@link PronounSet.possessive|`.possessive()`},
162
+ * {@link PronounSet.possessive|`.possessive()`}, or
163
+ * {@link PronounSet.reflexive|`.reflexive()`}. This is mostly
164
+ * here if you'd like to explicitly get a `Pronoun` instance
165
+ * from a `PronounSet`.
166
+ *
167
+ * @param index
168
+ * Retrieve `index`th pronoun from insertion.
169
+ *
170
+ * @param failQuietly
171
+ * Override default behavior to fail as per Pronouny config.
172
+ *
173
+ * @param useRandom
174
+ * Override default behavior to use random as per
175
+ * Pronouny config.
176
+ */
177
+ use(index?: number, failQuietly?: boolean, useRandom?: boolean): Pronoun;
178
+ /**Retrieves a random subject pronoun. */
179
+ subject(index?: number, failQuietly?: boolean, useRandom?: boolean): string;
180
+ /**Retrieves a random object pronoun. */
181
+ object(index?: number, failQuietly?: boolean, useRandom?: boolean): string;
182
+ /**Retrieves a random possessive pronoun. */
183
+ possessive(index?: number, failQuietly?: boolean, useRandom?: boolean): string;
184
+ /**Retrieves a random possessive adjective pronoun. */
185
+ psAdjective(index?: number, failQuietly?: boolean, useRandom?: boolean): string;
186
+ /**Retrieves a random reflexive pronoun. */
187
+ reflexive(index?: number, failQuietly?: boolean, useRandom?: boolean): string;
32
188
  }
189
+ /**# Config
190
+ * `failQuietly`: Controls various quiet failure states.
191
+ *
192
+ * `deepSearch`: Controls how deeply Pronouny will search
193
+ * for queries. By default, `string` parameters will
194
+ * only resolve using the first subject pronoun.
195
+ *
196
+ * `useRandom`: Controls whether random selection will be
197
+ * used. If false, it will default to 0. If failing
198
+ * quietly and indexing out of bounds, it will use the
199
+ * last item.
200
+ *
201
+ * `fallbackPronoun`: Controls what is considered the
202
+ * default pronoun for when one is not assigned or for
203
+ * quiet failure states. By default, this is `they`. **It
204
+ * is not recommended to change this.**
205
+ */
33
206
  type PronounyConfig = {
34
- failQuietly?: boolean;
35
- deepSearch?: boolean;
36
- useRandom?: boolean;
207
+ /**Controls various quiet failure states. */
208
+ failQuietly: boolean;
209
+ /**Controls how deeply Pronouny will search for
210
+ * queries. By default, `string` parameters will
211
+ * only resolve using the first subject pronoun. */
212
+ deepSearch: boolean;
213
+ /**Controls whether random selection will be used.
214
+ * If false, it will default to 0. If failing
215
+ * quietly and indexing out of bounds, it will
216
+ * use the last item. */
217
+ useRandom: boolean;
218
+ /**Controls what is considered the default pronoun
219
+ * for when one is not assigned or for quiet failure
220
+ * states. By default, this is `they`. **It is not
221
+ * recommended to change this.**
222
+ *
223
+ * If you remove `they` from the Pronouny instance,
224
+ * you are responsible for setting this to a valid
225
+ * pronoun string and ensuring it will resolve. */
226
+ fallbackPronoun: string;
37
227
  };
228
+ /**# Pronouny
229
+ * A typed library intended to make English pronoun
230
+ * resolution easy. Instantiate in your project using
231
+ * `new Pronouny()`, optionally passing in a
232
+ * {@link PronounyConfig|`PronounyConfig` object}.
233
+ *
234
+ * Once you have configured Pronouny, you may add new
235
+ * pronouns by using `new()` and passing in a set of
236
+ * pronouns. They are registered automatically. If you
237
+ * would like to register them manually, you can use
238
+ * `add()`. You can also `remove()` them if you would
239
+ * like to make them no longer valid.
240
+ *
241
+ * You can produce a {@linkcode PronounSet} by using
242
+ * `set()`. By passing in a string, it will
243
+ * automatically resolve each, using a delimiter if
244
+ * specified. From there, you can chain methods like
245
+ * `subject()` to retrieve a subject pronoun for use.
246
+ *
247
+ * If you would like to get a {@link Pronoun|`Pronoun` object}
248
+ * from a given text, you can use `resolve()`. Pass
249
+ * in a string and it will return the entire matched
250
+ * `Pronoun` object.
251
+ */
38
252
  export default class Pronouny {
39
253
  config: PronounyConfig;
40
254
  resolveMap: Map<string, Pronoun>;
41
255
  constructor(config?: PronounyConfig);
42
- resolve(pronoun: string, deepSearch?: boolean | undefined): Pronoun;
256
+ /**# `.resolve()` a Pronoun
257
+ * Returns a pronoun from a given string. Uses
258
+ * Pronouny's config to determine the depth of search.
259
+ * By default, only checks the first subject pronoun.
260
+ */
261
+ resolve(pronoun: string, deepSearch?: boolean): Pronoun;
262
+ /**# `.add()` an Unlisted Pronoun
263
+ * If you made a `.new()` Pronoun but disabled
264
+ * `autoAppend`, you can later append it back by
265
+ * using this method. Returns `Pronouny`.
266
+ */
43
267
  add(pronoun: Pronoun): this;
268
+ /**# `.remove()` a Pronoun
269
+ * Renders a given Pronoun invalid within this
270
+ * Pronouny instance. Returns `Pronouny`.
271
+ */
44
272
  remove(pronoun: Pronoun): this;
273
+ /**# Make a `.set()` of Pronouns
274
+ * Returns a {@linkcode PronounSet} from a given
275
+ * string or array of strings. You can specify the
276
+ * delimiter to be used. By default, it is `/`.
277
+ */
45
278
  set(pronouns: string | Array<string>, delimiter?: string): PronounSet;
279
+ /**# `.new()`
280
+ * Returns a new pronoun. Takes in an object with
281
+ * a subject, object, possessive, possessive adjective,
282
+ * and reflexive string/s, and returns the Pronoun.
283
+ * By default, appends the new `Pronoun` to the Pronouny
284
+ * `resolveMap`.
285
+ *
286
+ * @param pronouns
287
+ * Takes an object with the structure of a `Pronoun`,
288
+ * with `Array<string>` or a `string` for `subject`,
289
+ * `object`, `possessive`, `psAdjective`, and
290
+ * `reflexive`.
291
+ *
292
+ * @param autoAppend
293
+ * Will automatically add the new Pronoun to the
294
+ * Pronouny instance. If false, will just return a
295
+ * {@link Pronoun|`Pronoun` object} without registering.
296
+ * You may register it later with {@link Pronouny.add}.
297
+ */
46
298
  new(pronouns: {
47
299
  subject: Array<string> | string;
48
300
  object: Array<string> | string;