namefully 1.3.0 → 2.0.1

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.
Files changed (51) hide show
  1. package/dist/{lib → cjs}/builder.js +15 -13
  2. package/dist/cjs/config.js +109 -0
  3. package/dist/{lib → cjs}/constants.js +1 -1
  4. package/dist/{lib → cjs}/error.js +9 -6
  5. package/dist/cjs/fullname.js +101 -0
  6. package/dist/{lib → cjs}/index.js +18 -15
  7. package/dist/cjs/name.js +214 -0
  8. package/dist/cjs/namefully.js +383 -0
  9. package/dist/cjs/package.json +1 -0
  10. package/dist/cjs/parser.js +135 -0
  11. package/dist/{lib → cjs}/types.js +40 -36
  12. package/dist/{lib → cjs}/utils.js +32 -37
  13. package/dist/cjs/validator.js +251 -0
  14. package/dist/{types → esm}/builder.d.ts +11 -9
  15. package/dist/esm/builder.js +78 -0
  16. package/dist/{types → esm}/config.d.ts +7 -2
  17. package/dist/esm/config.js +105 -0
  18. package/dist/{types → esm}/constants.d.ts +1 -1
  19. package/dist/esm/constants.js +27 -0
  20. package/dist/{types → esm}/error.d.ts +5 -10
  21. package/dist/esm/error.js +87 -0
  22. package/dist/{types/full-name.d.ts → esm/fullname.d.ts} +12 -14
  23. package/dist/esm/fullname.js +97 -0
  24. package/dist/esm/index.d.ts +25 -0
  25. package/dist/esm/index.js +12 -0
  26. package/dist/{types → esm}/name.d.ts +11 -18
  27. package/dist/esm/name.js +207 -0
  28. package/dist/{types → esm}/namefully.d.ts +68 -70
  29. package/dist/esm/namefully.js +379 -0
  30. package/dist/esm/package.json +1 -0
  31. package/dist/{types → esm}/parser.d.ts +4 -4
  32. package/dist/esm/parser.js +127 -0
  33. package/dist/{types → esm}/types.d.ts +16 -48
  34. package/dist/esm/types.js +106 -0
  35. package/dist/{types → esm}/utils.d.ts +4 -6
  36. package/dist/esm/utils.js +91 -0
  37. package/dist/{types → esm}/validator.d.ts +3 -3
  38. package/dist/esm/validator.js +244 -0
  39. package/dist/namefully.js +1556 -0
  40. package/dist/namefully.min.js +1 -0
  41. package/package.json +45 -28
  42. package/readme.md +15 -14
  43. package/dist/lib/config.js +0 -112
  44. package/dist/lib/full-name.js +0 -115
  45. package/dist/lib/name.js +0 -230
  46. package/dist/lib/namefully.js +0 -417
  47. package/dist/lib/parser.js +0 -144
  48. package/dist/lib/validator.js +0 -285
  49. package/dist/types/index.d.ts +0 -25
  50. package/dist/umd/namefully.js +0 -1931
  51. package/dist/umd/namefully.min.js +0 -1
@@ -1,49 +1,50 @@
1
- import { Config } from './config';
2
- import { Name, JsonName } from './name';
3
- import { Parser } from './parser';
4
- import { Flat, NameOrder, NameType, Namon, Nullable, Surname } from './types';
5
- import { NameIndex } from './utils';
1
+ import { Config } from './config.js';
2
+ import { Name, JsonName } from './name.js';
3
+ import { Flat, NameOrder, NameType, Namon, Nullable, Surname } from './types.js';
4
+ import { NameIndex } from './utils.js';
5
+ import { Parser } from './parser.js';
6
6
  /**
7
- * A helper for organizing person names in a particular order, way, or shape.
7
+ * A utility for organizing human names in a particular order, way, or shape.
8
8
  *
9
- * Though `namefully` is easy to use, it does not magically guess which part of
10
- * the name is what (prefix, suffix, first, last, or middle names). It relies
11
- * actually on how the name parts are indicated (i.e., their roles) so that
12
- * it can perform internally certain operations and saves us some extra
13
- * calculations/processing. In addition, `Namefully` can be created using
14
- * distinct raw data shapes. This is intended to give some flexibility to the
15
- * developer so that he or she is not bound to a particular data format.
16
- * By following closely the API reference to know how to harness its usability,
17
- * this utility aims to save time in formatting names.
9
+ * Though `namefully` is designed to be easy to use, it does not magically guess
10
+ * which part of the name is what (prefix, suffix, first, last, or middle names).
11
+ * It relies actually on how the name parts are indicated (i.e., their roles)
12
+ * so that it can perform internally certain operations and saves us some extra
13
+ * calculations/processing. Additionally, `Namefully` objects may be created using
14
+ * distinct raw data shapes. This is intended to give some flexibility to you
15
+ * so that you are not bound to a particular data format (e.g., string).
16
+ * Follow the API reference to know how to harness its usability as this utility
17
+ * aims to save time in formatting names.
18
18
  *
19
- * `namefully` also works like a trapdoor. Once a raw data is provided and
20
- * validated, a developer can only *access* in a vast amount of, yet effective
21
- * ways the name info. *No editing* is possible. If the name is mistaken, a new
22
- * instance of `Namefully` must be created. In other words, it's immutable.
23
- * Remember, this utility's primary objective is to help manipulate a person name.
19
+ * `namefully` also works like a trapdoor. Once some name data is provided and
20
+ * validated, you may only *access* it but not update any part of it. This
21
+ * means that *no editing* is possible. If the name is mistaken, a new
22
+ * instance of `Namefully` must be created. In simple terms, it's immutable.
23
+ * Remember, this utility's primary objective is to help manipulate human names,
24
+ * not the opposite.
24
25
  *
25
- * Note that the name standards used for the current version of this library
26
- * are as follows:
26
+ * Note that the name standards used for the current version of this utility are
27
+ * as follows:
27
28
  * `[prefix] firstName [middleName] lastName [suffix]`
28
- * The opening `[` and closing `]` symbols mean that these parts are optional.
29
+ * where the opening `[` and closing `]` brackets mean that these parts are optional.
29
30
  * In other words, the most basic and typical case is a name that looks like
30
31
  * this: `John Smith`, where `John` is the first name piece and `Smith`, the last
31
32
  * name piece.
32
33
  *
33
- * @see https://www.fbiic.gov/public/2008/nov/Naming_practice_guide_UK_2006.pdf
34
+ * @see {link https://www.fbiic.gov/public/2008/nov/Naming_practice_guide_UK_2006.pdf}
34
35
  * for more info on name standards.
35
36
  *
36
37
  * **IMPORTANT**: Keep in mind that the order of appearance (or name order) matters
37
- * and may be altered through configured parameters, which will be seen later.
38
+ * and may be altered through configurable parameters, which will be seen later.
38
39
  * By default, the order of appearance is as shown above and will be used as a
39
40
  * basis for future examples and use cases.
40
41
  *
41
- * Once imported, all that is required to do is to create an instance of
42
- * `Namefully` and the rest will follow.
42
+ * Once imported, all that is required to do is to create instances of `Namefully`
43
+ * as you see fit and the rest will follow.
43
44
  *
44
45
  * Some terminologies used across the library are:
45
- * - namon: 1 piece of name (e.g., first name)
46
- * - nama: 2+ pieces of name (e.g., first name + last name)
46
+ * - namon: 1 piece of name (e.g., prefix)
47
+ * - nama: a combination of 2+ pieces of name (e.g., first name + last name)
47
48
  *
48
49
  * Happy name handling 😊!
49
50
  */
@@ -53,16 +54,16 @@ export declare class Namefully {
53
54
  * @param names element to parse.
54
55
  * @param options additional settings.
55
56
  *
56
- * An optional configuration may be provided with specifics on how to treat
57
- * a full name during its course. By default, all name parts are validated
58
- * against some basic validation rules to avoid common runtime exceptions.
57
+ * Optional parameters may be provided with specifics on how to treat a full
58
+ * name during its existence. All name parts must have at least two (2) characters
59
+ * or more to be considered a potential. That is the only requirement of namefully.
59
60
  */
60
61
  constructor(names: string | string[] | Name[] | JsonName | Parser, options?: Partial<Config>);
61
62
  /**
62
63
  * Constructs a `Namefully` instance from a text.
63
64
  *
64
- * It works like `parse` except that this function returns `null` where `parse`
65
- * would throw a `NameError`.
65
+ * It works like `parse` except that this function returns `undefined` when
66
+ * `parse` would throw a `NameError`.
66
67
  */
67
68
  static tryParse(text: string, index?: NameIndex): Namefully | undefined;
68
69
  /**
@@ -81,27 +82,27 @@ export declare class Namefully {
81
82
  * process.
82
83
  */
83
84
  static parse(text: string, index?: NameIndex): Promise<Namefully>;
84
- /** The current configuration. */
85
+ /** The configuration dictating this name's behavior. */
85
86
  get config(): Config;
86
87
  /** The number of characters of the `birthName`, including spaces. */
87
88
  get length(): number;
88
- /** The prefix part. */
89
- get prefix(): Nullable<string>;
90
- /** The firt name part. */
89
+ /** The prefix part of the name set. */
90
+ get prefix(): string | undefined;
91
+ /** The firt name part of the name set. */
91
92
  get first(): string;
92
- /** The first middle name part if any. */
93
- get middle(): Nullable<string>;
93
+ /** The first middle name part of the name set if any. */
94
+ get middle(): string | undefined;
94
95
  /** Returns true if any middle name has been set. */
95
96
  get hasMiddle(): boolean;
96
- /** The last name part. */
97
+ /** The last name part of the name set. */
97
98
  get last(): string;
98
- /** The suffix part. */
99
- get suffix(): Nullable<string>;
100
- /** The birth name part. */
99
+ /** The suffix part of the name set. */
100
+ get suffix(): string | undefined;
101
+ /** The birth name part of the name set. */
101
102
  get birth(): string;
102
- /** The shortest version of a person name. */
103
+ /** The shortest version of a human name (first + last name). */
103
104
  get short(): string;
104
- /** The longest version of a person name. */
105
+ /** The longest version of a human name (a.k.a birth name). */
105
106
  get long(): string;
106
107
  /** The entire name set. */
107
108
  get full(): string;
@@ -117,18 +118,17 @@ export declare class Namefully {
117
118
  equal(other: Namefully): boolean;
118
119
  /** Gets a JSON representation of the full name. */
119
120
  toJson(): JsonName;
121
+ json: () => JsonName;
120
122
  /** Confirms that a name part has been set. */
121
123
  has(namon: Namon): boolean;
122
124
  /**
123
125
  * Gets the full name ordered as configured.
124
126
  *
125
- * The name order `orderedBy` forces to order by first or last name by
126
- * overriding the preset configuration.
127
- *
128
- * `Namefully.format` may also be used to alter manually the order of appearance
129
- * of full name.
127
+ * @param {NameOrder} orderedBy forces to arrange a name set by first or last
128
+ * name, overriding the preset configuration.
130
129
  *
131
- * For example:
130
+ * `Namefully.format()` may also be used to alter manually the order of appearance
131
+ * of full name. For example:
132
132
  * ```ts
133
133
  * const name = new Namefully('Jon Stark Snow');
134
134
  * console.log(name.fullName(NameOrder.LAST_NAME)); // "Snow Jon Stark"
@@ -145,26 +145,26 @@ export declare class Namefully {
145
145
  birthName(orderedBy?: NameOrder): string;
146
146
  /**
147
147
  * Gets the first name part of the `FullName`.
148
- *
149
- * @param withMore determines whether to include other pieces of the first
150
- * name.
148
+ * @param {boolean} withMore determines whether to include other pieces of the
149
+ * first name.
151
150
  */
152
151
  firstName(withMore?: boolean): string;
153
152
  /** Gets the middle name part of the `FullName`. */
154
153
  middleName(): string[];
155
154
  /**
156
155
  * Gets the last name part of the `FullName`.
157
- *
158
- * @param format overrides the how-to formatting of a surname output,
156
+ * @param {Surname} format overrides the how-to formatting of a surname output,
159
157
  * considering its sub-parts.
160
158
  */
161
159
  lastName(format?: Surname): string;
162
160
  /**
163
161
  * Gets the initials of the `FullName`.
164
162
  *
165
- * @param {options.orderedBy} forces to order by first or last name by
163
+ * @param {object} options when getting the initials.
164
+ * @param {NameOrder} options.orderedBy forces to order by first or last name by
166
165
  * overriding the preset configuration.
167
- * @param
166
+ * @param {NameType} options.only selects initials of only certain name parts.
167
+ * @param {boolean} options.asJson whether to return initials as an array or JSON.
168
168
  *
169
169
  * For example, given the names:
170
170
  * - `John Smith` => `['J', 'S']`
@@ -179,23 +179,22 @@ export declare class Namefully {
179
179
  * Shortens a complex full name to a simple typical name, a combination of
180
180
  * first and last name.
181
181
  *
182
- * @param orderedBy forces to order by first or last name by overriding the
183
- * preset configuration.
182
+ * @param {NameOrder} orderedBy forces to order by first or last name, overriding
183
+ * the preset configuration.
184
184
  *
185
185
  * For a given name such as `Mr Keanu Charles Reeves`, shortening this name
186
186
  * is equivalent to making it `Keanu Reeves`.
187
187
  *
188
188
  * As a shortened name, the namon of the first name is favored over the other
189
- * names forming part of the entire first names, if any. Meanwhile, for
190
- * the last name, the configured `surname` is prioritized.
189
+ * names forming part of the entire first names, if any. Meanwhile, for the
190
+ * last name, the configured `surname` is prioritized.
191
191
  *
192
192
  * For a given `FirstName FatherName MotherName`, shortening this name when
193
- * the surname is set as `mother` is equivalent to making it:
194
- * `FirstName MotherName`.
193
+ * the surname is set as `mother` is equivalent to making it: `FirstName MotherName`.
195
194
  */
196
195
  shorten(orderedBy?: NameOrder): string;
197
196
  /**
198
- * Flattens a long name using the name types as variants.
197
+ * Flattens long names using the name types as variants.
199
198
  *
200
199
  * While @param limit sets a threshold as a limited number of characters
201
200
  * supported to flatten a `FullName`, @param by indicates which variant
@@ -233,13 +232,12 @@ export declare class Namefully {
233
232
  }>): string;
234
233
  /**
235
234
  * Zips or compacts a name using different forms of variants.
236
- *
237
235
  * @see `flatten()` for more details.
238
236
  */
239
237
  zip(by?: Flat, withPeriod?: boolean): string;
240
238
  /**
241
239
  * Formats the full name as desired.
242
- * @param pattern character used to format it.
240
+ * @param {string} pattern character used to format it.
243
241
  *
244
242
  * string format
245
243
  * -------------
@@ -285,7 +283,7 @@ export declare class Namefully {
285
283
  * first, middle, and last names.
286
284
  */
287
285
  format(pattern: string): string;
288
- /** Flips definitely the name order from the preset/current config. */
286
+ /** Flips or swaps the name order from the preset/current config. */
289
287
  flip(): void;
290
288
  /**
291
289
  * Splits the name parts of a birth name.
@@ -294,7 +292,7 @@ export declare class Namefully {
294
292
  split(separator?: string | RegExp): string[];
295
293
  /**
296
294
  * Joins the name parts of a birth name.
297
- * @param separator token for the junction.
295
+ * @param {string} separator token for the junction.
298
296
  */
299
297
  join(separator?: string): string;
300
298
  /** Transforms a birth name into UPPERCASE. */
@@ -0,0 +1,379 @@
1
+ import { ALLOWED_TOKENS } from './constants.js';
2
+ import { InputError, NotAllowedError } from './error.js';
3
+ import { isNameArray } from './name.js';
4
+ import { Flat, NameOrder, NameType, Namon } from './types.js';
5
+ import { capitalize, decapitalize, isStringArray, toggleCase } from './utils.js';
6
+ import { ArrayNameParser, ArrayStringParser, NamaParser, Parser, StringParser } from './parser.js';
7
+ export class Namefully {
8
+ #fullName;
9
+ constructor(names, options) {
10
+ this.#fullName = this.#toParser(names).parse(options);
11
+ }
12
+ static tryParse(text, index) {
13
+ try {
14
+ return new Namefully(Parser.build(text, index));
15
+ }
16
+ catch {
17
+ return undefined;
18
+ }
19
+ }
20
+ static async parse(text, index) {
21
+ return Parser.buildAsync(text, index).then((parser) => new Namefully(parser));
22
+ }
23
+ get config() {
24
+ return this.#fullName.config;
25
+ }
26
+ get length() {
27
+ return this.birth.length;
28
+ }
29
+ get prefix() {
30
+ return this.#fullName.prefix?.toString();
31
+ }
32
+ get first() {
33
+ return this.firstName();
34
+ }
35
+ get middle() {
36
+ return this.hasMiddle ? this.middleName()[0] : undefined;
37
+ }
38
+ get hasMiddle() {
39
+ return this.#fullName.has(Namon.MIDDLE_NAME);
40
+ }
41
+ get last() {
42
+ return this.lastName();
43
+ }
44
+ get suffix() {
45
+ return this.#fullName.suffix?.toString();
46
+ }
47
+ get birth() {
48
+ return this.birthName();
49
+ }
50
+ get short() {
51
+ return this.shorten();
52
+ }
53
+ get long() {
54
+ return this.birth;
55
+ }
56
+ get full() {
57
+ return this.fullName();
58
+ }
59
+ get public() {
60
+ return this.format('f $l');
61
+ }
62
+ get salutation() {
63
+ return this.format('p l');
64
+ }
65
+ toString() {
66
+ return this.full;
67
+ }
68
+ get(namon) {
69
+ if (namon.equal(Namon.PREFIX))
70
+ return this.#fullName.prefix;
71
+ if (namon.equal(Namon.FIRST_NAME))
72
+ return this.#fullName.firstName;
73
+ if (namon.equal(Namon.MIDDLE_NAME))
74
+ return this.#fullName.middleName;
75
+ if (namon.equal(Namon.LAST_NAME))
76
+ return this.#fullName.lastName;
77
+ if (namon.equal(Namon.SUFFIX))
78
+ return this.#fullName.suffix;
79
+ return undefined;
80
+ }
81
+ equal(other) {
82
+ return this.toString() === other.toString();
83
+ }
84
+ toJson() {
85
+ return {
86
+ prefix: this.prefix,
87
+ firstName: this.first,
88
+ middleName: this.middleName(),
89
+ lastName: this.last,
90
+ suffix: this.suffix,
91
+ };
92
+ }
93
+ json = this.toJson;
94
+ has(namon) {
95
+ return this.#fullName.has(namon);
96
+ }
97
+ fullName(orderedBy) {
98
+ const sep = this.config.ending ? ',' : '';
99
+ const names = [];
100
+ if (this.prefix)
101
+ names.push(this.prefix);
102
+ if ((orderedBy ?? this.config.orderedBy) === NameOrder.FIRST_NAME) {
103
+ names.push(this.first, ...this.middleName(), this.last + sep);
104
+ }
105
+ else {
106
+ names.push(this.last, this.first, this.middleName().join(' ') + sep);
107
+ }
108
+ if (this.suffix)
109
+ names.push(this.suffix);
110
+ return names.join(' ').trim();
111
+ }
112
+ birthName(orderedBy) {
113
+ orderedBy ??= this.config.orderedBy;
114
+ return orderedBy === NameOrder.FIRST_NAME
115
+ ? [this.first, ...this.middleName(), this.last].join(' ')
116
+ : [this.last, this.first, ...this.middleName()].join(' ');
117
+ }
118
+ firstName(withMore = true) {
119
+ return this.#fullName.firstName.toString(withMore);
120
+ }
121
+ middleName() {
122
+ return this.#fullName.middleName.map((n) => n.value);
123
+ }
124
+ lastName(format) {
125
+ return this.#fullName.lastName.toString(format);
126
+ }
127
+ initials(options) {
128
+ const { orderedBy = this.config.orderedBy, only = NameType.BIRTH_NAME, asJson } = options ?? {};
129
+ const firstInits = this.#fullName.firstName.initials();
130
+ const midInits = this.#fullName.middleName.map((n) => n.value[0]);
131
+ const lastInits = this.#fullName.lastName.initials();
132
+ if (asJson)
133
+ return { firstName: firstInits, middleName: midInits, lastName: lastInits };
134
+ if (only !== NameType.BIRTH_NAME) {
135
+ return only === NameType.FIRST_NAME ? firstInits : only === NameType.MIDDLE_NAME ? midInits : lastInits;
136
+ }
137
+ else if (orderedBy === NameOrder.FIRST_NAME) {
138
+ return [...firstInits, ...midInits, ...lastInits];
139
+ }
140
+ else {
141
+ return [...lastInits, ...firstInits, ...midInits];
142
+ }
143
+ }
144
+ shorten(orderedBy) {
145
+ orderedBy ??= this.config.orderedBy;
146
+ const { firstName, lastName } = this.#fullName;
147
+ return orderedBy === NameOrder.FIRST_NAME
148
+ ? [firstName.value, lastName.toString()].join(' ')
149
+ : [lastName.toString(), firstName.value].join(' ');
150
+ }
151
+ flatten(options) {
152
+ const { by = Flat.MIDDLE_NAME, limit = 20, recursive = false, withMore = false, withPeriod = true, surname, } = options;
153
+ if (this.length <= limit)
154
+ return this.full;
155
+ const { firstName, lastName, middleName } = this.#fullName;
156
+ const sep = withPeriod ? '.' : '';
157
+ const hasMid = this.hasMiddle;
158
+ const fn = firstName.toString();
159
+ const mn = this.middleName().join(' ');
160
+ const ln = lastName.toString();
161
+ const f = firstName.initials(withMore).join(sep + ' ') + sep;
162
+ const l = lastName.initials(surname).join(sep + ' ') + sep;
163
+ const m = hasMid ? middleName.map((n) => n.value[0]).join(sep + ' ') + sep : '';
164
+ let name = [];
165
+ if (this.config.orderedBy === NameOrder.FIRST_NAME) {
166
+ switch (by) {
167
+ case Flat.FIRST_NAME:
168
+ name = hasMid ? [f, mn, ln] : [f, ln];
169
+ break;
170
+ case Flat.LAST_NAME:
171
+ name = hasMid ? [fn, mn, l] : [fn, l];
172
+ break;
173
+ case Flat.MIDDLE_NAME:
174
+ name = hasMid ? [fn, m, ln] : [fn, ln];
175
+ break;
176
+ case Flat.FIRST_MID:
177
+ name = hasMid ? [f, m, ln] : [f, ln];
178
+ break;
179
+ case Flat.MID_LAST:
180
+ name = hasMid ? [fn, m, l] : [fn, l];
181
+ break;
182
+ case Flat.ALL:
183
+ name = hasMid ? [f, m, l] : [f, l];
184
+ break;
185
+ }
186
+ }
187
+ else {
188
+ switch (by) {
189
+ case Flat.FIRST_NAME:
190
+ name = hasMid ? [ln, f, mn] : [ln, f];
191
+ break;
192
+ case Flat.LAST_NAME:
193
+ name = hasMid ? [l, fn, mn] : [l, fn];
194
+ break;
195
+ case Flat.MIDDLE_NAME:
196
+ name = hasMid ? [ln, fn, m] : [ln, fn];
197
+ break;
198
+ case Flat.FIRST_MID:
199
+ name = hasMid ? [ln, f, m] : [ln, f];
200
+ break;
201
+ case Flat.MID_LAST:
202
+ name = hasMid ? [l, fn, m] : [l, fn];
203
+ break;
204
+ case Flat.ALL:
205
+ name = hasMid ? [l, f, m] : [l, f];
206
+ break;
207
+ }
208
+ }
209
+ const flat = name.join(' ');
210
+ if (recursive && flat.length > limit) {
211
+ const next = by === Flat.FIRST_NAME
212
+ ? Flat.MIDDLE_NAME
213
+ : by === Flat.MIDDLE_NAME
214
+ ? Flat.LAST_NAME
215
+ : by === Flat.LAST_NAME
216
+ ? Flat.FIRST_MID
217
+ : by === Flat.FIRST_MID
218
+ ? Flat.MID_LAST
219
+ : by === Flat.MID_LAST
220
+ ? Flat.ALL
221
+ : by === Flat.ALL
222
+ ? Flat.ALL
223
+ : by;
224
+ if (next === by)
225
+ return flat;
226
+ return this.flatten({ ...options, by: next });
227
+ }
228
+ return flat;
229
+ }
230
+ zip(by = Flat.MID_LAST, withPeriod = true) {
231
+ return this.flatten({ limit: 0, by, withPeriod });
232
+ }
233
+ format(pattern) {
234
+ if (pattern === 'short')
235
+ return this.short;
236
+ if (pattern === 'long')
237
+ return this.long;
238
+ if (pattern === 'public')
239
+ return this.public;
240
+ if (pattern === 'official')
241
+ pattern = 'o';
242
+ let group = '';
243
+ const formatted = [];
244
+ for (const char of pattern) {
245
+ if (ALLOWED_TOKENS.indexOf(char) === -1) {
246
+ throw new NotAllowedError({
247
+ source: this.full,
248
+ operation: 'format',
249
+ message: `unsupported character <${char}> from ${pattern}.`,
250
+ });
251
+ }
252
+ group += char;
253
+ if (char === '$')
254
+ continue;
255
+ formatted.push(this.#map(group) ?? '');
256
+ group = '';
257
+ }
258
+ return formatted.join('').trim();
259
+ }
260
+ flip() {
261
+ const order = this.config.orderedBy === NameOrder.FIRST_NAME ? NameOrder.LAST_NAME : NameOrder.FIRST_NAME;
262
+ this.config.update({ orderedBy: order });
263
+ }
264
+ split(separator = /[' -]/g) {
265
+ return this.birth.replace(separator, ' ').split(' ');
266
+ }
267
+ join(separator = '') {
268
+ return this.split().join(separator);
269
+ }
270
+ toUpperCase() {
271
+ return this.birth.toUpperCase();
272
+ }
273
+ toLowerCase() {
274
+ return this.birth.toLowerCase();
275
+ }
276
+ toCamelCase() {
277
+ return decapitalize(this.toPascalCase());
278
+ }
279
+ toPascalCase() {
280
+ return this.split()
281
+ .map((n) => capitalize(n))
282
+ .join('');
283
+ }
284
+ toSnakeCase() {
285
+ return this.split()
286
+ .map((n) => n.toLowerCase())
287
+ .join('_');
288
+ }
289
+ toHyphenCase() {
290
+ return this.split()
291
+ .map((n) => n.toLowerCase())
292
+ .join('-');
293
+ }
294
+ toDotCase() {
295
+ return this.split()
296
+ .map((n) => n.toLowerCase())
297
+ .join('.');
298
+ }
299
+ toToggleCase() {
300
+ return toggleCase(this.birth);
301
+ }
302
+ #toParser(raw) {
303
+ if (raw instanceof Parser)
304
+ return raw;
305
+ if (typeof raw === 'string')
306
+ return new StringParser(raw);
307
+ if (isStringArray(raw))
308
+ return new ArrayStringParser(raw);
309
+ if (isNameArray(raw))
310
+ return new ArrayNameParser(raw);
311
+ if (typeof raw === 'object')
312
+ return new NamaParser(raw);
313
+ throw new InputError({ source: raw, message: 'Cannot parse raw data; review expected data types.' });
314
+ }
315
+ #map(char) {
316
+ switch (char) {
317
+ case '.':
318
+ case ',':
319
+ case ' ':
320
+ case '-':
321
+ case '_':
322
+ return char;
323
+ case 'b':
324
+ return this.birth;
325
+ case 'B':
326
+ return this.birth.toUpperCase();
327
+ case 'f':
328
+ return this.first;
329
+ case 'F':
330
+ return this.first.toUpperCase();
331
+ case 'l':
332
+ return this.last;
333
+ case 'L':
334
+ return this.last.toUpperCase();
335
+ case 'm':
336
+ case 'M':
337
+ return char === 'm' ? this.middleName().join(' ') : this.middleName().join(' ').toUpperCase();
338
+ case 'o':
339
+ case 'O':
340
+ return ((character) => {
341
+ const sep = this.config.ending ? ',' : '';
342
+ const names = [];
343
+ if (this.prefix)
344
+ names.push(this.prefix);
345
+ names.push(`${this.last},`.toUpperCase());
346
+ if (this.hasMiddle)
347
+ names.push(this.first, this.middleName().join(' ') + sep);
348
+ else
349
+ names.push(this.first + sep);
350
+ if (this.suffix)
351
+ names.push(this.suffix);
352
+ const nama = names.join(' ').trim();
353
+ return character === 'o' ? nama : nama.toUpperCase();
354
+ })(char);
355
+ case 'p':
356
+ return this.prefix;
357
+ case 'P':
358
+ return this.prefix?.toUpperCase();
359
+ case 's':
360
+ return this.suffix;
361
+ case 'S':
362
+ return this.suffix?.toUpperCase();
363
+ case '$f':
364
+ case '$F':
365
+ return this.#fullName.firstName.value[0];
366
+ case '$l':
367
+ case '$L':
368
+ return this.#fullName.lastName.value[0];
369
+ case '$m':
370
+ case '$M':
371
+ return this.hasMiddle ? this.middle[0] : undefined;
372
+ default:
373
+ return undefined;
374
+ }
375
+ }
376
+ }
377
+ export default (names, options) => {
378
+ return new Namefully(names, options);
379
+ };
@@ -0,0 +1 @@
1
+ {"type": "module"}
@@ -1,7 +1,7 @@
1
- import { FullName } from './full-name';
2
- import { Config } from './config';
3
- import { NameIndex } from './utils';
4
- import { Name, JsonName } from './name';
1
+ import { Config } from './config.js';
2
+ import { NameIndex } from './utils.js';
3
+ import { FullName } from './fullname.js';
4
+ import { Name, JsonName } from './name.js';
5
5
  /**
6
6
  * A parser signature that helps to organize the names accordingly.
7
7
  */