namefully 2.0.0 → 2.0.2
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/dist/cjs/config.js +14 -5
- package/dist/cjs/constants.js +3 -3
- package/dist/cjs/error.js +4 -7
- package/dist/cjs/fullname.js +10 -11
- package/dist/cjs/name.js +18 -22
- package/dist/cjs/namefully.js +32 -40
- package/dist/cjs/parser.js +13 -15
- package/dist/cjs/utils.js +18 -25
- package/dist/cjs/validator.js +24 -39
- package/dist/esm/builder.d.ts +4 -2
- package/dist/esm/config.d.ts +6 -1
- package/dist/esm/config.js +14 -5
- package/dist/esm/constants.d.ts +2 -2
- package/dist/esm/constants.js +2 -2
- package/dist/esm/error.d.ts +10 -14
- package/dist/esm/error.js +4 -7
- package/dist/esm/fullname.d.ts +10 -12
- package/dist/esm/fullname.js +10 -11
- package/dist/esm/name.d.ts +12 -20
- package/dist/esm/name.js +18 -22
- package/dist/esm/namefully.d.ts +64 -65
- package/dist/esm/namefully.js +33 -41
- package/dist/esm/parser.d.ts +3 -6
- package/dist/esm/parser.js +13 -15
- package/dist/esm/types.d.ts +16 -48
- package/dist/esm/utils.d.ts +4 -5
- package/dist/esm/utils.js +18 -25
- package/dist/esm/validator.d.ts +1 -1
- package/dist/esm/validator.js +29 -44
- package/dist/namefully.js +124 -155
- package/dist/namefully.min.js +1 -1
- package/package.json +7 -7
- package/readme.md +16 -17
package/dist/cjs/validator.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Validators = exports.ArrayNameValidator = exports.ArrayStringValidator = exports.NamaValidator = void 0;
|
|
4
|
-
const constants_js_1 = require("./constants.js");
|
|
5
|
-
const error_js_1 = require("./error.js");
|
|
6
|
-
const name_js_1 = require("./name.js");
|
|
7
4
|
const types_js_1 = require("./types.js");
|
|
8
5
|
const utils_js_1 = require("./utils.js");
|
|
6
|
+
const name_js_1 = require("./name.js");
|
|
7
|
+
const error_js_1 = require("./error.js");
|
|
8
|
+
const constants_js_1 = require("./constants.js");
|
|
9
9
|
class ValidationRule {
|
|
10
10
|
static base = /[a-zA-Z\u00C0-\u00D6\u00D8-\u00f6\u00f8-\u00ff\u0400-\u04FFΆ-ωΑ-ώ]/;
|
|
11
11
|
static namon = new RegExp(`^${ValidationRule.base.source}+(([' -]${ValidationRule.base.source})?${ValidationRule.base.source}*)*$`);
|
|
@@ -21,7 +21,7 @@ class ArrayValidator {
|
|
|
21
21
|
if (values.length === 0 || values.length < constants_js_1.MIN_NUMBER_OF_NAME_PARTS || values.length > constants_js_1.MAX_NUMBER_OF_NAME_PARTS) {
|
|
22
22
|
throw new error_js_1.InputError({
|
|
23
23
|
source: values.map((n) => n.toString()),
|
|
24
|
-
message: `expecting a list of ${constants_js_1.MIN_NUMBER_OF_NAME_PARTS}-${constants_js_1.
|
|
24
|
+
message: `expecting a list of ${constants_js_1.MIN_NUMBER_OF_NAME_PARTS}-${constants_js_1.MAX_NUMBER_OF_NAME_PARTS} elements`,
|
|
25
25
|
});
|
|
26
26
|
}
|
|
27
27
|
}
|
|
@@ -29,7 +29,7 @@ class ArrayValidator {
|
|
|
29
29
|
class NamonValidator {
|
|
30
30
|
static #validator;
|
|
31
31
|
static create() {
|
|
32
|
-
return this.#validator || (this.#validator = new
|
|
32
|
+
return this.#validator || (this.#validator = new NamonValidator());
|
|
33
33
|
}
|
|
34
34
|
validate(value, type) {
|
|
35
35
|
if (value instanceof name_js_1.Name) {
|
|
@@ -40,22 +40,19 @@ class NamonValidator {
|
|
|
40
40
|
throw new error_js_1.ValidationError({
|
|
41
41
|
source: value,
|
|
42
42
|
nameType: 'namon',
|
|
43
|
-
message: 'invalid content',
|
|
43
|
+
message: 'invalid name content failing namon regex',
|
|
44
44
|
});
|
|
45
45
|
}
|
|
46
46
|
}
|
|
47
47
|
else {
|
|
48
|
-
throw new error_js_1.InputError({
|
|
49
|
-
source: typeof value,
|
|
50
|
-
message: 'expecting types of string | Name',
|
|
51
|
-
});
|
|
48
|
+
throw new error_js_1.InputError({ source: typeof value, message: 'expecting types of string or Name' });
|
|
52
49
|
}
|
|
53
50
|
}
|
|
54
51
|
}
|
|
55
52
|
class FirstNameValidator {
|
|
56
53
|
static #validator;
|
|
57
54
|
static create() {
|
|
58
|
-
return this.#validator || (this.#validator = new
|
|
55
|
+
return this.#validator || (this.#validator = new FirstNameValidator());
|
|
59
56
|
}
|
|
60
57
|
validate(value) {
|
|
61
58
|
if (value instanceof name_js_1.FirstName) {
|
|
@@ -66,22 +63,19 @@ class FirstNameValidator {
|
|
|
66
63
|
throw new error_js_1.ValidationError({
|
|
67
64
|
source: value,
|
|
68
65
|
nameType: 'firstName',
|
|
69
|
-
message: 'invalid content',
|
|
66
|
+
message: 'invalid name content failing firstName regex',
|
|
70
67
|
});
|
|
71
68
|
}
|
|
72
69
|
}
|
|
73
70
|
else {
|
|
74
|
-
throw new error_js_1.InputError({
|
|
75
|
-
source: typeof value,
|
|
76
|
-
message: 'expecting types string | FirstName',
|
|
77
|
-
});
|
|
71
|
+
throw new error_js_1.InputError({ source: typeof value, message: 'expecting types string or FirstName' });
|
|
78
72
|
}
|
|
79
73
|
}
|
|
80
74
|
}
|
|
81
75
|
class MiddleNameValidator {
|
|
82
76
|
static #validator;
|
|
83
77
|
static create() {
|
|
84
|
-
return this.#validator || (this.#validator = new
|
|
78
|
+
return this.#validator || (this.#validator = new MiddleNameValidator());
|
|
85
79
|
}
|
|
86
80
|
validate(value) {
|
|
87
81
|
if (typeof value === 'string') {
|
|
@@ -89,7 +83,7 @@ class MiddleNameValidator {
|
|
|
89
83
|
throw new error_js_1.ValidationError({
|
|
90
84
|
source: value,
|
|
91
85
|
nameType: 'middleName',
|
|
92
|
-
message: 'invalid content',
|
|
86
|
+
message: 'invalid name content failing middleName regex',
|
|
93
87
|
});
|
|
94
88
|
}
|
|
95
89
|
}
|
|
@@ -110,7 +104,7 @@ class MiddleNameValidator {
|
|
|
110
104
|
else {
|
|
111
105
|
throw new error_js_1.InputError({
|
|
112
106
|
source: typeof value,
|
|
113
|
-
message: 'expecting types of string
|
|
107
|
+
message: 'expecting types of string, string[] or Name[]',
|
|
114
108
|
});
|
|
115
109
|
}
|
|
116
110
|
}
|
|
@@ -118,7 +112,7 @@ class MiddleNameValidator {
|
|
|
118
112
|
class LastNameValidator {
|
|
119
113
|
static #validator;
|
|
120
114
|
static create() {
|
|
121
|
-
return this.#validator || (this.#validator = new
|
|
115
|
+
return this.#validator || (this.#validator = new LastNameValidator());
|
|
122
116
|
}
|
|
123
117
|
validate(value) {
|
|
124
118
|
if (value instanceof name_js_1.LastName) {
|
|
@@ -129,36 +123,33 @@ class LastNameValidator {
|
|
|
129
123
|
throw new error_js_1.ValidationError({
|
|
130
124
|
source: value,
|
|
131
125
|
nameType: 'lastName',
|
|
132
|
-
message: 'invalid content',
|
|
126
|
+
message: 'invalid name content failing lastName regex',
|
|
133
127
|
});
|
|
134
128
|
}
|
|
135
129
|
}
|
|
136
130
|
else {
|
|
137
|
-
throw new error_js_1.InputError({
|
|
138
|
-
source: typeof value,
|
|
139
|
-
message: 'expecting types string | LastName',
|
|
140
|
-
});
|
|
131
|
+
throw new error_js_1.InputError({ source: typeof value, message: 'expecting types string or LastName' });
|
|
141
132
|
}
|
|
142
133
|
}
|
|
143
134
|
}
|
|
144
135
|
class NameValidator {
|
|
145
136
|
static #validator;
|
|
146
137
|
static create() {
|
|
147
|
-
return this.#validator || (this.#validator = new
|
|
138
|
+
return this.#validator || (this.#validator = new NameValidator());
|
|
148
139
|
}
|
|
149
140
|
validate(name, type) {
|
|
150
141
|
if (type && name.type !== type) {
|
|
151
142
|
throw new error_js_1.ValidationError({
|
|
152
143
|
source: name.toString(),
|
|
153
144
|
nameType: name.type.toString(),
|
|
154
|
-
message: 'wrong type',
|
|
145
|
+
message: 'wrong name type; only Namon types are supported',
|
|
155
146
|
});
|
|
156
147
|
}
|
|
157
148
|
if (!ValidationRule.namon.test(name.value)) {
|
|
158
149
|
throw new error_js_1.ValidationError({
|
|
159
150
|
source: name.toString(),
|
|
160
151
|
nameType: name.type.toString(),
|
|
161
|
-
message: 'invalid content',
|
|
152
|
+
message: 'invalid name content failing namon regex',
|
|
162
153
|
});
|
|
163
154
|
}
|
|
164
155
|
}
|
|
@@ -166,7 +157,7 @@ class NameValidator {
|
|
|
166
157
|
class NamaValidator {
|
|
167
158
|
static #validator;
|
|
168
159
|
static create() {
|
|
169
|
-
return this.#validator || (this.#validator = new
|
|
160
|
+
return this.#validator || (this.#validator = new NamaValidator());
|
|
170
161
|
}
|
|
171
162
|
validate(value) {
|
|
172
163
|
this.validateKeys(value);
|
|
@@ -184,20 +175,14 @@ class NamaValidator {
|
|
|
184
175
|
else if (nama.size < constants_js_1.MIN_NUMBER_OF_NAME_PARTS || nama.size > constants_js_1.MAX_NUMBER_OF_NAME_PARTS) {
|
|
185
176
|
throw new error_js_1.InputError({
|
|
186
177
|
source: [...nama.values()],
|
|
187
|
-
message: `expecting ${constants_js_1.MIN_NUMBER_OF_NAME_PARTS}-${constants_js_1.
|
|
178
|
+
message: `expecting ${constants_js_1.MIN_NUMBER_OF_NAME_PARTS}-${constants_js_1.MAX_NUMBER_OF_NAME_PARTS} fields`,
|
|
188
179
|
});
|
|
189
180
|
}
|
|
190
181
|
if (!nama.has(types_js_1.Namon.FIRST_NAME)) {
|
|
191
|
-
throw new error_js_1.InputError({
|
|
192
|
-
source: [...nama.values()],
|
|
193
|
-
message: '"firstName" is a required key',
|
|
194
|
-
});
|
|
182
|
+
throw new error_js_1.InputError({ source: [...nama.values()], message: '"firstName" is a required key' });
|
|
195
183
|
}
|
|
196
184
|
if (!nama.has(types_js_1.Namon.LAST_NAME)) {
|
|
197
|
-
throw new error_js_1.InputError({
|
|
198
|
-
source: [...nama.values()],
|
|
199
|
-
message: '"lastName" is a required key',
|
|
200
|
-
});
|
|
185
|
+
throw new error_js_1.InputError({ source: [...nama.values()], message: '"lastName" is a required key' });
|
|
201
186
|
}
|
|
202
187
|
}
|
|
203
188
|
}
|
|
@@ -227,7 +212,7 @@ exports.ArrayStringValidator = ArrayStringValidator;
|
|
|
227
212
|
class ArrayNameValidator {
|
|
228
213
|
static #validator;
|
|
229
214
|
static create() {
|
|
230
|
-
return this.#validator || (this.#validator = new
|
|
215
|
+
return this.#validator || (this.#validator = new ArrayNameValidator());
|
|
231
216
|
}
|
|
232
217
|
validate(value) {
|
|
233
218
|
if (value.length < constants_js_1.MIN_NUMBER_OF_NAME_PARTS) {
|
package/dist/esm/builder.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Name } from './name.js';
|
|
2
|
-
import { Namefully } from './namefully.js';
|
|
3
2
|
import { Config } from './config.js';
|
|
3
|
+
import { Namefully } from './namefully.js';
|
|
4
4
|
type VoidCallback = () => void;
|
|
5
5
|
type Callback<Type, Return> = (value: Type) => Return;
|
|
6
6
|
/**
|
|
@@ -44,9 +44,11 @@ declare abstract class Builder<T, I> {
|
|
|
44
44
|
* to finally construct a complete Namefully instance.
|
|
45
45
|
*
|
|
46
46
|
* @example
|
|
47
|
+
* ```js
|
|
47
48
|
* const builder = NameBuilder.of([Name.first('Thomas'), Name.last('Edison')]);
|
|
48
49
|
* builder.add(Name.middle('Alva'));
|
|
49
50
|
* console.log(builder.build()); // 'Thomas Alva Edison'
|
|
51
|
+
* ```
|
|
50
52
|
*/
|
|
51
53
|
export declare class NameBuilder extends Builder<Name, Namefully> {
|
|
52
54
|
private constructor();
|
|
@@ -66,7 +68,7 @@ export declare class NameBuilder extends Builder<Name, Namefully> {
|
|
|
66
68
|
* Builds an instance of Namefully from the previously collected names.
|
|
67
69
|
*
|
|
68
70
|
* Regardless of how the names are added, both first and last names must exist
|
|
69
|
-
* to complete a fine build. Otherwise, it throws a
|
|
71
|
+
* to complete a fine build. Otherwise, it throws a NameError.
|
|
70
72
|
*/
|
|
71
73
|
build(config?: Partial<Config>): Namefully;
|
|
72
74
|
}
|
package/dist/esm/config.d.ts
CHANGED
|
@@ -92,6 +92,11 @@ export declare class Config {
|
|
|
92
92
|
/**
|
|
93
93
|
* Alters the name order between the first and last name, and rearrange the
|
|
94
94
|
* order of appearance of a name set.
|
|
95
|
+
* @deprecated use `update()` method instead.
|
|
95
96
|
*/
|
|
96
|
-
updateOrder(
|
|
97
|
+
updateOrder(orderedBy: NameOrder): void;
|
|
98
|
+
/**
|
|
99
|
+
* Allows the possibility to alter some options after creating a name set.
|
|
100
|
+
*/
|
|
101
|
+
update({ orderedBy, title, ending }: Partial<Pick<Config, 'orderedBy' | 'title' | 'ending'>>): void;
|
|
97
102
|
}
|
package/dist/esm/config.js
CHANGED
|
@@ -43,7 +43,7 @@ export class Config {
|
|
|
43
43
|
}
|
|
44
44
|
static create(name = defaultName) {
|
|
45
45
|
if (!_a.cache.has(name))
|
|
46
|
-
_a.cache.set(name, new
|
|
46
|
+
_a.cache.set(name, new _a(name));
|
|
47
47
|
return _a.cache.get(name);
|
|
48
48
|
}
|
|
49
49
|
static merge(other) {
|
|
@@ -84,10 +84,19 @@ export class Config {
|
|
|
84
84
|
this.#surname = Surname.FATHER;
|
|
85
85
|
_a.cache.set(this.name, this);
|
|
86
86
|
}
|
|
87
|
-
updateOrder(
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
87
|
+
updateOrder(orderedBy) {
|
|
88
|
+
this.update({ orderedBy });
|
|
89
|
+
}
|
|
90
|
+
update({ orderedBy, title, ending }) {
|
|
91
|
+
const config = _a.cache.get(this.name);
|
|
92
|
+
if (!config)
|
|
93
|
+
return;
|
|
94
|
+
if (orderedBy !== this.#orderedBy)
|
|
95
|
+
config.#orderedBy = orderedBy;
|
|
96
|
+
if (title !== this.#title)
|
|
97
|
+
config.#title = title;
|
|
98
|
+
if (ending !== this.#ending)
|
|
99
|
+
config.#ending = ending;
|
|
91
100
|
}
|
|
92
101
|
#genNewName(name) {
|
|
93
102
|
return name === this.name || _a.cache.has(name) ? this.#genNewName(name + copyAlias) : name;
|
package/dist/esm/constants.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export declare const VERSION = "2.0.
|
|
1
|
+
export declare const VERSION = "2.0.2";
|
|
2
2
|
export declare const MIN_NUMBER_OF_NAME_PARTS = 2;
|
|
3
3
|
export declare const MAX_NUMBER_OF_NAME_PARTS = 5;
|
|
4
|
-
export declare const
|
|
4
|
+
export declare const ALLOWED_FORMAT_TOKENS: string[];
|
package/dist/esm/constants.js
CHANGED
package/dist/esm/error.d.ts
CHANGED
|
@@ -4,9 +4,7 @@ interface ErrorMessage {
|
|
|
4
4
|
source: NameSource;
|
|
5
5
|
message?: string;
|
|
6
6
|
}
|
|
7
|
-
/**
|
|
8
|
-
* The error types supported by `Namefully`.
|
|
9
|
-
*/
|
|
7
|
+
/** The error types supported by `Namefully`. */
|
|
10
8
|
export declare enum NameErrorType {
|
|
11
9
|
/**
|
|
12
10
|
* Thrown when a name entry/argument is incorrect.
|
|
@@ -29,9 +27,7 @@ export declare enum NameErrorType {
|
|
|
29
27
|
* See also: `NotAllowedError`, `Namefully.format`.
|
|
30
28
|
*/
|
|
31
29
|
NOT_ALLOWED = 2,
|
|
32
|
-
/**
|
|
33
|
-
* Thrown by any other unknown sources or unexpected situation.
|
|
34
|
-
*/
|
|
30
|
+
/** Thrown by any other unknown sources or unexpected situation. */
|
|
35
31
|
UNKNOWN = 3
|
|
36
32
|
}
|
|
37
33
|
/**
|
|
@@ -44,8 +40,8 @@ export declare enum NameErrorType {
|
|
|
44
40
|
* program failure. Au contraire, it is expected that a programmer using this utility
|
|
45
41
|
* would consider validating a name using its own business rules. That is not
|
|
46
42
|
* this utility's job to guess those rules. So, the predefined `ValidationRules`
|
|
47
|
-
* obey some common validation techniques when it comes to sanitizing a
|
|
48
|
-
* name. For this reason, the
|
|
43
|
+
* obey some common validation techniques when it comes to sanitizing a personal
|
|
44
|
+
* name. For this reason, the `Config.bypass` is set to `true` by default,
|
|
49
45
|
* indicating that those predefined rules should be skipped for the sake of the
|
|
50
46
|
* program.
|
|
51
47
|
*
|
|
@@ -54,7 +50,7 @@ export declare enum NameErrorType {
|
|
|
54
50
|
*
|
|
55
51
|
* A name error intends to provide useful information about what causes the error
|
|
56
52
|
* and let the user take initiative on what happens next to the given name:
|
|
57
|
-
* reconstructing it or
|
|
53
|
+
* reconstructing it or discarding it.
|
|
58
54
|
*/
|
|
59
55
|
export declare class NameError extends Error {
|
|
60
56
|
readonly source: NameSource;
|
|
@@ -62,8 +58,8 @@ export declare class NameError extends Error {
|
|
|
62
58
|
/**
|
|
63
59
|
* Creates an error with a message describing the issue for a name source.
|
|
64
60
|
* @param source name input that caused the error
|
|
65
|
-
* @param message
|
|
66
|
-
* @param type of `NameErrorType`
|
|
61
|
+
* @param message describing the failure.
|
|
62
|
+
* @param type of error via `NameErrorType`
|
|
67
63
|
*/
|
|
68
64
|
constructor(source: NameSource, message?: string, type?: NameErrorType);
|
|
69
65
|
/** The actual source input which caused the error. */
|
|
@@ -76,9 +72,9 @@ export declare class NameError extends Error {
|
|
|
76
72
|
/**
|
|
77
73
|
* An error thrown when a name source input is incorrect.
|
|
78
74
|
*
|
|
79
|
-
* A `Name` is a name for this utility under certain criteria (i.e.,
|
|
75
|
+
* A `Name` is a name for this utility under certain criteria (i.e., 1+ chars),
|
|
80
76
|
* hence, a wrong input will cause this kind of error. Another common reason
|
|
81
|
-
* may be a wrong key in a
|
|
77
|
+
* may be a wrong key in a JSON name parsing mechanism.
|
|
82
78
|
*
|
|
83
79
|
* Keep in mind that this error is different from a `ValidationError`.
|
|
84
80
|
*/
|
|
@@ -150,7 +146,7 @@ export declare class UnknownError extends NameError {
|
|
|
150
146
|
* Optionally, the original error revealing the true nature of the failure.
|
|
151
147
|
*/
|
|
152
148
|
constructor(error: ErrorMessage & {
|
|
153
|
-
|
|
149
|
+
origin?: Error;
|
|
154
150
|
});
|
|
155
151
|
toString(): string;
|
|
156
152
|
}
|
package/dist/esm/error.js
CHANGED
|
@@ -16,14 +16,11 @@ export class NameError extends Error {
|
|
|
16
16
|
this.name = 'NameError';
|
|
17
17
|
}
|
|
18
18
|
get sourceAsString() {
|
|
19
|
-
let input = '';
|
|
20
|
-
if (!this.source)
|
|
21
|
-
input = '<undefined>';
|
|
22
19
|
if (typeof this.source === 'string')
|
|
23
|
-
|
|
20
|
+
return this.source;
|
|
24
21
|
if (isStringArray(this.source))
|
|
25
|
-
|
|
26
|
-
return
|
|
22
|
+
return this.source.join(' ');
|
|
23
|
+
return '<undefined>';
|
|
27
24
|
}
|
|
28
25
|
get hasMessage() {
|
|
29
26
|
return this.message.trim().length > 0;
|
|
@@ -75,7 +72,7 @@ export class UnknownError extends NameError {
|
|
|
75
72
|
origin;
|
|
76
73
|
constructor(error) {
|
|
77
74
|
super(error.source, error.message, NameErrorType.UNKNOWN);
|
|
78
|
-
this.origin = error.
|
|
75
|
+
this.origin = error.origin;
|
|
79
76
|
this.name = 'UnknownError';
|
|
80
77
|
}
|
|
81
78
|
toString() {
|
package/dist/esm/fullname.d.ts
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
import { Config } from './config.js';
|
|
2
|
-
import { FirstName, LastName, Name, JsonName } from './name.js';
|
|
3
2
|
import { Nullable, Namon } from './types.js';
|
|
3
|
+
import { FirstName, LastName, Name, JsonName } from './name.js';
|
|
4
4
|
/**
|
|
5
5
|
* The core component of this utility.
|
|
6
6
|
*
|
|
7
|
-
* This component is
|
|
7
|
+
* This component is composed of five entities that make it easy to handle a
|
|
8
8
|
* full name set: prefix, first name, middle name, last name, and suffix.
|
|
9
|
-
*
|
|
9
|
+
* It is indeed intended for internal processes. However, it is understandable
|
|
10
10
|
* that it might be needed at some point for additional purposes. For this reason,
|
|
11
11
|
* it's made available.
|
|
12
12
|
*
|
|
13
13
|
* It is recommended to avoid using this class unless it is highly necessary or
|
|
14
|
-
* a custom parser is used for uncommon use cases
|
|
15
|
-
* as many use cases as possible.
|
|
14
|
+
* a custom parser is used for uncommon use cases although this utility tries to
|
|
15
|
+
* cover as many use cases as possible.
|
|
16
16
|
*
|
|
17
17
|
* Additionally, an optional configuration can be used to indicate some specific
|
|
18
18
|
* behaviors related to that name handling.
|
|
@@ -21,7 +21,7 @@ export declare class FullName {
|
|
|
21
21
|
#private;
|
|
22
22
|
/**
|
|
23
23
|
* Creates a full name as it goes
|
|
24
|
-
* @param options
|
|
24
|
+
* @param options settings for additional features.
|
|
25
25
|
*/
|
|
26
26
|
constructor(options?: Partial<Config>);
|
|
27
27
|
/** A snapshot of the configuration used to set up this full name. */
|
|
@@ -37,9 +37,9 @@ export declare class FullName {
|
|
|
37
37
|
/** The suffix part of the full name. */
|
|
38
38
|
get suffix(): Nullable<Name>;
|
|
39
39
|
/**
|
|
40
|
-
* Parses a
|
|
41
|
-
* @param json parsable name element
|
|
42
|
-
* @param config
|
|
40
|
+
* Parses a JSON name into a full name.
|
|
41
|
+
* @param {JsonName} json parsable name element
|
|
42
|
+
* @param {Config} config for additional features.
|
|
43
43
|
*/
|
|
44
44
|
static parse(json: JsonName, config?: Config): FullName;
|
|
45
45
|
setPrefix(name: Nullable<string | Name>): FullName;
|
|
@@ -47,8 +47,6 @@ export declare class FullName {
|
|
|
47
47
|
setLastName(name: string | LastName): FullName;
|
|
48
48
|
setMiddleName(names: string[] | Name[]): FullName;
|
|
49
49
|
setSuffix(name: Nullable<string | Name>): FullName;
|
|
50
|
-
/**
|
|
51
|
-
* Returns true if a namon has been set.
|
|
52
|
-
*/
|
|
50
|
+
/** Returns true if a namon has been set. */
|
|
53
51
|
has(namon: Namon): boolean;
|
|
54
52
|
}
|
package/dist/esm/fullname.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { Config } from './config.js';
|
|
2
|
+
import { Validators } from './validator.js';
|
|
3
|
+
import { Namon, Title } from './types.js';
|
|
2
4
|
import { NameError, UnknownError } from './error.js';
|
|
3
5
|
import { FirstName, LastName, Name } from './name.js';
|
|
4
|
-
import { Namon, Title } from './types.js';
|
|
5
|
-
import { Validators } from './validator.js';
|
|
6
6
|
export class FullName {
|
|
7
7
|
#prefix;
|
|
8
8
|
#firstName;
|
|
@@ -33,13 +33,12 @@ export class FullName {
|
|
|
33
33
|
}
|
|
34
34
|
static parse(json, config) {
|
|
35
35
|
try {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
return fullName;
|
|
36
|
+
return new FullName(config)
|
|
37
|
+
.setPrefix(json.prefix)
|
|
38
|
+
.setFirstName(json.firstName)
|
|
39
|
+
.setMiddleName(json.middleName ?? [])
|
|
40
|
+
.setLastName(json.lastName)
|
|
41
|
+
.setSuffix(json.suffix);
|
|
43
42
|
}
|
|
44
43
|
catch (error) {
|
|
45
44
|
if (error instanceof NameError)
|
|
@@ -47,7 +46,7 @@ export class FullName {
|
|
|
47
46
|
throw new UnknownError({
|
|
48
47
|
source: Object.values(json).join(' '),
|
|
49
48
|
message: 'could not parse JSON content',
|
|
50
|
-
|
|
49
|
+
origin: error instanceof Error ? error : new Error(String(error)),
|
|
51
50
|
});
|
|
52
51
|
}
|
|
53
52
|
}
|
|
@@ -77,7 +76,7 @@ export class FullName {
|
|
|
77
76
|
return this;
|
|
78
77
|
if (!this.#config.bypass)
|
|
79
78
|
Validators.middleName.validate(names);
|
|
80
|
-
this.#middleName = names.map((
|
|
79
|
+
this.#middleName = names.map((n) => (n instanceof Name ? n : Name.middle(n)));
|
|
81
80
|
return this;
|
|
82
81
|
}
|
|
83
82
|
setSuffix(name) {
|
package/dist/esm/name.d.ts
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
import { CapsRange, Namon, Surname } from './types.js';
|
|
2
|
-
/**
|
|
3
|
-
* Representation of a string type name with some extra capabilities.
|
|
4
|
-
*/
|
|
2
|
+
/** Representation of a string type name with some extra capabilities. */
|
|
5
3
|
export declare class Name {
|
|
6
4
|
#private;
|
|
7
5
|
readonly type: Namon;
|
|
@@ -9,9 +7,9 @@ export declare class Name {
|
|
|
9
7
|
protected capsRange: CapsRange;
|
|
10
8
|
/**
|
|
11
9
|
* Creates augmented names by adding extra functionality to a string name.
|
|
12
|
-
* @param type must be indicated to categorize the name so it can be
|
|
10
|
+
* @param {Namon} type must be indicated to categorize the name so it can be
|
|
13
11
|
* treated accordingly.
|
|
14
|
-
* @param capsRange determines how the name should be capitalized initially.
|
|
12
|
+
* @param {CapsRange} capsRange determines how the name should be capitalized initially.
|
|
15
13
|
*/
|
|
16
14
|
constructor(value: string, type: Namon, capsRange?: CapsRange);
|
|
17
15
|
set value(newValue: string);
|
|
@@ -51,16 +49,14 @@ export declare class Name {
|
|
|
51
49
|
decaps(range?: CapsRange): Name;
|
|
52
50
|
protected validate(name?: string): void;
|
|
53
51
|
}
|
|
54
|
-
/**
|
|
55
|
-
* Representation of a first name with some extra functionality.
|
|
56
|
-
*/
|
|
52
|
+
/** Representation of a first name with some extra functionality. */
|
|
57
53
|
export declare class FirstName extends Name {
|
|
58
54
|
#private;
|
|
59
55
|
/**
|
|
60
56
|
* Creates an extended version of `Name` and flags it as a first name `type`.
|
|
61
57
|
*
|
|
62
58
|
* Some may consider `more` additional name parts of a given name as their
|
|
63
|
-
* first names, but not as their middle names. Though
|
|
59
|
+
* first names, but not as their middle names. Though it may mean the same,
|
|
64
60
|
* `more` provides the freedom to do it as it pleases.
|
|
65
61
|
*/
|
|
66
62
|
constructor(value: string, ...more: string[]);
|
|
@@ -81,22 +77,20 @@ export declare class FirstName extends Name {
|
|
|
81
77
|
more?: string[];
|
|
82
78
|
}): FirstName;
|
|
83
79
|
}
|
|
84
|
-
/**
|
|
85
|
-
* Representation of a last name with some extra functionality.
|
|
86
|
-
*/
|
|
80
|
+
/** Representation of a last name with some extra functionality. */
|
|
87
81
|
export declare class LastName extends Name {
|
|
88
82
|
#private;
|
|
89
83
|
readonly format: Surname;
|
|
90
84
|
/**
|
|
91
85
|
* Creates an extended version of `Name` and flags it as a last name `type`.
|
|
92
86
|
*
|
|
93
|
-
* Some people may keep their
|
|
94
|
-
* from their
|
|
87
|
+
* Some people may keep their @param mother's surname and want to keep a clear cut
|
|
88
|
+
* from their @param father's surname. However, there are no clear rules about it.
|
|
95
89
|
*/
|
|
96
90
|
constructor(father: string, mother?: string, format?: Surname);
|
|
97
|
-
/** The surname inherited from
|
|
91
|
+
/** The surname inherited from the father side. */
|
|
98
92
|
get father(): string;
|
|
99
|
-
/** The surname inherited from
|
|
93
|
+
/** The surname inherited from the mother side. */
|
|
100
94
|
get mother(): string | undefined;
|
|
101
95
|
/** Returns `true` if the mother's surname is defined. */
|
|
102
96
|
get hasMother(): boolean;
|
|
@@ -114,10 +108,8 @@ export declare class LastName extends Name {
|
|
|
114
108
|
format?: Surname;
|
|
115
109
|
}): LastName;
|
|
116
110
|
}
|
|
117
|
-
export declare function isNameArray(value?: unknown):
|
|
118
|
-
/**
|
|
119
|
-
* JSON signature for `FullName` data.
|
|
120
|
-
*/
|
|
111
|
+
export declare function isNameArray(value?: unknown): value is Name[];
|
|
112
|
+
/** JSON signature for `FullName` data. */
|
|
121
113
|
export interface JsonName {
|
|
122
114
|
prefix?: string;
|
|
123
115
|
firstName: string;
|