namefully 1.2.1 → 1.3.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.
- package/dist/lib/builder.js +80 -0
- package/dist/lib/config.js +10 -4
- package/dist/lib/constants.js +3 -26
- package/dist/lib/error.js +5 -10
- package/dist/lib/full-name.js +39 -14
- package/dist/lib/index.js +3 -0
- package/dist/lib/name.js +28 -29
- package/dist/lib/namefully.js +99 -78
- package/dist/lib/parser.js +33 -43
- package/dist/lib/types.js +19 -1
- package/dist/lib/utils.js +30 -30
- package/dist/lib/validator.js +42 -72
- package/dist/types/builder.d.ts +75 -0
- package/dist/types/config.d.ts +2 -5
- package/dist/types/constants.d.ts +2 -2
- package/dist/types/error.d.ts +11 -16
- package/dist/types/full-name.d.ts +16 -13
- package/dist/types/index.d.ts +5 -1
- package/dist/types/name.d.ts +25 -26
- package/dist/types/namefully.d.ts +126 -92
- package/dist/types/parser.d.ts +11 -13
- package/dist/types/types.d.ts +19 -48
- package/dist/types/utils.d.ts +7 -7
- package/dist/types/validator.d.ts +1 -1
- package/dist/umd/namefully.js +1149 -1058
- package/dist/umd/namefully.min.js +1 -1
- package/package.json +20 -17
- package/readme.md +63 -35
package/dist/lib/validator.js
CHANGED
|
@@ -13,11 +13,11 @@ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (
|
|
|
13
13
|
var _a, _NamonValidator_validator, _b, _FirstNameValidator_validator, _c, _MiddleNameValidator_validator, _d, _LastNameValidator_validator, _e, _NameValidator_validator, _f, _NamaValidator_validator, _ArrayNameValidator_instances, _g, _ArrayNameValidator_validator, _ArrayNameValidator_hasBasicNames;
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
15
|
exports.Validators = exports.ArrayNameValidator = exports.ArrayStringValidator = exports.NamaValidator = void 0;
|
|
16
|
-
const constants_1 = require("./constants");
|
|
17
|
-
const error_1 = require("./error");
|
|
18
|
-
const name_1 = require("./name");
|
|
19
16
|
const types_1 = require("./types");
|
|
20
17
|
const utils_1 = require("./utils");
|
|
18
|
+
const name_1 = require("./name");
|
|
19
|
+
const error_1 = require("./error");
|
|
20
|
+
const constants_1 = require("./constants");
|
|
21
21
|
class ValidationRule {
|
|
22
22
|
}
|
|
23
23
|
ValidationRule.base = /[a-zA-Z\u00C0-\u00D6\u00D8-\u00f6\u00f8-\u00ff\u0400-\u04FFΆ-ωΑ-ώ]/;
|
|
@@ -25,19 +25,22 @@ ValidationRule.namon = new RegExp(`^${ValidationRule.base.source}+(([' -]${Valid
|
|
|
25
25
|
ValidationRule.firstName = ValidationRule.namon;
|
|
26
26
|
ValidationRule.middleName = new RegExp(`^${ValidationRule.base.source}+(([' -]${ValidationRule.base.source})?${ValidationRule.base.source}*)*$`);
|
|
27
27
|
ValidationRule.lastName = ValidationRule.namon;
|
|
28
|
+
const toNameSource = (values) => {
|
|
29
|
+
return (0, name_1.isNameArray)(values) ? values.map((n) => n.toString()).join(' ') : '';
|
|
30
|
+
};
|
|
28
31
|
class ArrayValidator {
|
|
29
32
|
validate(values) {
|
|
30
33
|
if (values.length === 0 || values.length < constants_1.MIN_NUMBER_OF_NAME_PARTS || values.length > constants_1.MAX_NUMBER_OF_NAME_PARTS) {
|
|
31
34
|
throw new error_1.InputError({
|
|
32
35
|
source: values.map((n) => n.toString()),
|
|
33
|
-
message: `expecting a list of ${constants_1.MIN_NUMBER_OF_NAME_PARTS}-${constants_1.
|
|
36
|
+
message: `expecting a list of ${constants_1.MIN_NUMBER_OF_NAME_PARTS}-${constants_1.MAX_NUMBER_OF_NAME_PARTS} elements`,
|
|
34
37
|
});
|
|
35
38
|
}
|
|
36
39
|
}
|
|
37
40
|
}
|
|
38
41
|
class NamonValidator {
|
|
39
42
|
static create() {
|
|
40
|
-
return __classPrivateFieldGet(this, _a, "f", _NamonValidator_validator) || (__classPrivateFieldSet(this, _a, new
|
|
43
|
+
return __classPrivateFieldGet(this, _a, "f", _NamonValidator_validator) || (__classPrivateFieldSet(this, _a, new _a(), "f", _NamonValidator_validator));
|
|
41
44
|
}
|
|
42
45
|
validate(value, type) {
|
|
43
46
|
if (value instanceof name_1.Name) {
|
|
@@ -48,15 +51,12 @@ class NamonValidator {
|
|
|
48
51
|
throw new error_1.ValidationError({
|
|
49
52
|
source: value,
|
|
50
53
|
nameType: 'namon',
|
|
51
|
-
message: 'invalid content',
|
|
54
|
+
message: 'invalid name content failing namon regex',
|
|
52
55
|
});
|
|
53
56
|
}
|
|
54
57
|
}
|
|
55
58
|
else {
|
|
56
|
-
throw new error_1.InputError({
|
|
57
|
-
source: typeof value,
|
|
58
|
-
message: 'expecting types of string | Name',
|
|
59
|
-
});
|
|
59
|
+
throw new error_1.InputError({ source: typeof value, message: 'expecting types of string or Name' });
|
|
60
60
|
}
|
|
61
61
|
}
|
|
62
62
|
}
|
|
@@ -64,7 +64,7 @@ _a = NamonValidator;
|
|
|
64
64
|
_NamonValidator_validator = { value: void 0 };
|
|
65
65
|
class FirstNameValidator {
|
|
66
66
|
static create() {
|
|
67
|
-
return __classPrivateFieldGet(this, _b, "f", _FirstNameValidator_validator) || (__classPrivateFieldSet(this, _b, new
|
|
67
|
+
return __classPrivateFieldGet(this, _b, "f", _FirstNameValidator_validator) || (__classPrivateFieldSet(this, _b, new _b(), "f", _FirstNameValidator_validator));
|
|
68
68
|
}
|
|
69
69
|
validate(value) {
|
|
70
70
|
if (value instanceof name_1.FirstName) {
|
|
@@ -75,15 +75,12 @@ class FirstNameValidator {
|
|
|
75
75
|
throw new error_1.ValidationError({
|
|
76
76
|
source: value,
|
|
77
77
|
nameType: 'firstName',
|
|
78
|
-
message: 'invalid content',
|
|
78
|
+
message: 'invalid name content failing firstName regex',
|
|
79
79
|
});
|
|
80
80
|
}
|
|
81
81
|
}
|
|
82
82
|
else {
|
|
83
|
-
throw new error_1.InputError({
|
|
84
|
-
source: typeof value,
|
|
85
|
-
message: 'expecting types string | FirstName',
|
|
86
|
-
});
|
|
83
|
+
throw new error_1.InputError({ source: typeof value, message: 'expecting types string or FirstName' });
|
|
87
84
|
}
|
|
88
85
|
}
|
|
89
86
|
}
|
|
@@ -91,7 +88,7 @@ _b = FirstNameValidator;
|
|
|
91
88
|
_FirstNameValidator_validator = { value: void 0 };
|
|
92
89
|
class MiddleNameValidator {
|
|
93
90
|
static create() {
|
|
94
|
-
return __classPrivateFieldGet(this, _c, "f", _MiddleNameValidator_validator) || (__classPrivateFieldSet(this, _c, new
|
|
91
|
+
return __classPrivateFieldGet(this, _c, "f", _MiddleNameValidator_validator) || (__classPrivateFieldSet(this, _c, new _c(), "f", _MiddleNameValidator_validator));
|
|
95
92
|
}
|
|
96
93
|
validate(value) {
|
|
97
94
|
if (typeof value === 'string') {
|
|
@@ -99,7 +96,7 @@ class MiddleNameValidator {
|
|
|
99
96
|
throw new error_1.ValidationError({
|
|
100
97
|
source: value,
|
|
101
98
|
nameType: 'middleName',
|
|
102
|
-
message: 'invalid content',
|
|
99
|
+
message: 'invalid name content failing middleName regex',
|
|
103
100
|
});
|
|
104
101
|
}
|
|
105
102
|
}
|
|
@@ -111,7 +108,7 @@ class MiddleNameValidator {
|
|
|
111
108
|
}
|
|
112
109
|
catch (error) {
|
|
113
110
|
throw new error_1.ValidationError({
|
|
114
|
-
source: value,
|
|
111
|
+
source: toNameSource(value),
|
|
115
112
|
nameType: 'middleName',
|
|
116
113
|
message: error === null || error === void 0 ? void 0 : error.message,
|
|
117
114
|
});
|
|
@@ -120,7 +117,7 @@ class MiddleNameValidator {
|
|
|
120
117
|
else {
|
|
121
118
|
throw new error_1.InputError({
|
|
122
119
|
source: typeof value,
|
|
123
|
-
message: 'expecting types of string
|
|
120
|
+
message: 'expecting types of string, string[] or Name[]',
|
|
124
121
|
});
|
|
125
122
|
}
|
|
126
123
|
}
|
|
@@ -129,7 +126,7 @@ _c = MiddleNameValidator;
|
|
|
129
126
|
_MiddleNameValidator_validator = { value: void 0 };
|
|
130
127
|
class LastNameValidator {
|
|
131
128
|
static create() {
|
|
132
|
-
return __classPrivateFieldGet(this, _d, "f", _LastNameValidator_validator) || (__classPrivateFieldSet(this, _d, new
|
|
129
|
+
return __classPrivateFieldGet(this, _d, "f", _LastNameValidator_validator) || (__classPrivateFieldSet(this, _d, new _d(), "f", _LastNameValidator_validator));
|
|
133
130
|
}
|
|
134
131
|
validate(value) {
|
|
135
132
|
if (value instanceof name_1.LastName) {
|
|
@@ -140,15 +137,12 @@ class LastNameValidator {
|
|
|
140
137
|
throw new error_1.ValidationError({
|
|
141
138
|
source: value,
|
|
142
139
|
nameType: 'lastName',
|
|
143
|
-
message: 'invalid content',
|
|
140
|
+
message: 'invalid name content failing lastName regex',
|
|
144
141
|
});
|
|
145
142
|
}
|
|
146
143
|
}
|
|
147
144
|
else {
|
|
148
|
-
throw new error_1.InputError({
|
|
149
|
-
source: typeof value,
|
|
150
|
-
message: 'expecting types string | LastName',
|
|
151
|
-
});
|
|
145
|
+
throw new error_1.InputError({ source: typeof value, message: 'expecting types string or LastName' });
|
|
152
146
|
}
|
|
153
147
|
}
|
|
154
148
|
}
|
|
@@ -156,21 +150,21 @@ _d = LastNameValidator;
|
|
|
156
150
|
_LastNameValidator_validator = { value: void 0 };
|
|
157
151
|
class NameValidator {
|
|
158
152
|
static create() {
|
|
159
|
-
return __classPrivateFieldGet(this, _e, "f", _NameValidator_validator) || (__classPrivateFieldSet(this, _e, new
|
|
153
|
+
return __classPrivateFieldGet(this, _e, "f", _NameValidator_validator) || (__classPrivateFieldSet(this, _e, new _e(), "f", _NameValidator_validator));
|
|
160
154
|
}
|
|
161
155
|
validate(name, type) {
|
|
162
156
|
if (type && name.type !== type) {
|
|
163
157
|
throw new error_1.ValidationError({
|
|
164
|
-
source:
|
|
158
|
+
source: name.toString(),
|
|
165
159
|
nameType: name.type.toString(),
|
|
166
|
-
message: 'wrong type',
|
|
160
|
+
message: 'wrong name type; only Namon types are supported',
|
|
167
161
|
});
|
|
168
162
|
}
|
|
169
163
|
if (!ValidationRule.namon.test(name.value)) {
|
|
170
164
|
throw new error_1.ValidationError({
|
|
171
|
-
source:
|
|
165
|
+
source: name.toString(),
|
|
172
166
|
nameType: name.type.toString(),
|
|
173
|
-
message: 'invalid content',
|
|
167
|
+
message: 'invalid name content failing namon regex',
|
|
174
168
|
});
|
|
175
169
|
}
|
|
176
170
|
}
|
|
@@ -179,18 +173,16 @@ _e = NameValidator;
|
|
|
179
173
|
_NameValidator_validator = { value: void 0 };
|
|
180
174
|
class NamaValidator {
|
|
181
175
|
static create() {
|
|
182
|
-
return __classPrivateFieldGet(this, _f, "f", _NamaValidator_validator) || (__classPrivateFieldSet(this, _f, new
|
|
176
|
+
return __classPrivateFieldGet(this, _f, "f", _NamaValidator_validator) || (__classPrivateFieldSet(this, _f, new _f(), "f", _NamaValidator_validator));
|
|
183
177
|
}
|
|
184
178
|
validate(value) {
|
|
185
179
|
this.validateKeys(value);
|
|
186
180
|
Validators.firstName.validate(value.get(types_1.Namon.FIRST_NAME));
|
|
187
181
|
Validators.lastName.validate(value.get(types_1.Namon.LAST_NAME));
|
|
188
|
-
if (value.has(types_1.Namon.PREFIX))
|
|
182
|
+
if (value.has(types_1.Namon.PREFIX))
|
|
189
183
|
Validators.namon.validate(value.get(types_1.Namon.PREFIX));
|
|
190
|
-
|
|
191
|
-
if (value.has(types_1.Namon.SUFFIX)) {
|
|
184
|
+
if (value.has(types_1.Namon.SUFFIX))
|
|
192
185
|
Validators.namon.validate(value.get(types_1.Namon.SUFFIX));
|
|
193
|
-
}
|
|
194
186
|
}
|
|
195
187
|
validateKeys(nama) {
|
|
196
188
|
if (!nama.size) {
|
|
@@ -199,20 +191,14 @@ class NamaValidator {
|
|
|
199
191
|
else if (nama.size < constants_1.MIN_NUMBER_OF_NAME_PARTS || nama.size > constants_1.MAX_NUMBER_OF_NAME_PARTS) {
|
|
200
192
|
throw new error_1.InputError({
|
|
201
193
|
source: [...nama.values()],
|
|
202
|
-
message: `expecting ${constants_1.MIN_NUMBER_OF_NAME_PARTS}-${constants_1.
|
|
194
|
+
message: `expecting ${constants_1.MIN_NUMBER_OF_NAME_PARTS}-${constants_1.MAX_NUMBER_OF_NAME_PARTS} fields`,
|
|
203
195
|
});
|
|
204
196
|
}
|
|
205
197
|
if (!nama.has(types_1.Namon.FIRST_NAME)) {
|
|
206
|
-
throw new error_1.InputError({
|
|
207
|
-
source: [...nama.values()],
|
|
208
|
-
message: '"firstName" is a required key',
|
|
209
|
-
});
|
|
198
|
+
throw new error_1.InputError({ source: [...nama.values()], message: '"firstName" is a required key' });
|
|
210
199
|
}
|
|
211
200
|
if (!nama.has(types_1.Namon.LAST_NAME)) {
|
|
212
|
-
throw new error_1.InputError({
|
|
213
|
-
source: [...nama.values()],
|
|
214
|
-
message: '"lastName" is a required key',
|
|
215
|
-
});
|
|
201
|
+
throw new error_1.InputError({ source: [...nama.values()], message: '"lastName" is a required key' });
|
|
216
202
|
}
|
|
217
203
|
}
|
|
218
204
|
}
|
|
@@ -226,30 +212,14 @@ class ArrayStringValidator extends ArrayValidator {
|
|
|
226
212
|
}
|
|
227
213
|
validate(values) {
|
|
228
214
|
this.validateIndex(values);
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
Validators.lastName.validate(values[this.index.lastName]);
|
|
238
|
-
break;
|
|
239
|
-
case 4:
|
|
240
|
-
Validators.namon.validate(values[this.index.prefix]);
|
|
241
|
-
Validators.firstName.validate(values[this.index.firstName]);
|
|
242
|
-
Validators.middleName.validate(values[this.index.middleName]);
|
|
243
|
-
Validators.lastName.validate(values[this.index.lastName]);
|
|
244
|
-
break;
|
|
245
|
-
case 5:
|
|
246
|
-
Validators.namon.validate(values[this.index.prefix]);
|
|
247
|
-
Validators.firstName.validate(values[this.index.firstName]);
|
|
248
|
-
Validators.middleName.validate(values[this.index.middleName]);
|
|
249
|
-
Validators.lastName.validate(values[this.index.lastName]);
|
|
250
|
-
Validators.namon.validate(values[this.index.suffix]);
|
|
251
|
-
break;
|
|
252
|
-
}
|
|
215
|
+
Validators.firstName.validate(values[this.index.firstName]);
|
|
216
|
+
Validators.lastName.validate(values[this.index.lastName]);
|
|
217
|
+
if (values.length >= 3)
|
|
218
|
+
Validators.middleName.validate(values[this.index.middleName]);
|
|
219
|
+
if (values.length >= 4)
|
|
220
|
+
Validators.namon.validate(values[this.index.prefix]);
|
|
221
|
+
if (values.length === 5)
|
|
222
|
+
Validators.namon.validate(values[this.index.suffix]);
|
|
253
223
|
}
|
|
254
224
|
validateIndex(values) {
|
|
255
225
|
super.validate(values);
|
|
@@ -261,18 +231,18 @@ class ArrayNameValidator {
|
|
|
261
231
|
_ArrayNameValidator_instances.add(this);
|
|
262
232
|
}
|
|
263
233
|
static create() {
|
|
264
|
-
return __classPrivateFieldGet(this, _g, "f", _ArrayNameValidator_validator) || (__classPrivateFieldSet(this, _g, new
|
|
234
|
+
return __classPrivateFieldGet(this, _g, "f", _ArrayNameValidator_validator) || (__classPrivateFieldSet(this, _g, new _g(), "f", _ArrayNameValidator_validator));
|
|
265
235
|
}
|
|
266
236
|
validate(value) {
|
|
267
237
|
if (value.length < constants_1.MIN_NUMBER_OF_NAME_PARTS) {
|
|
268
238
|
throw new error_1.InputError({
|
|
269
|
-
source: value,
|
|
239
|
+
source: toNameSource(value),
|
|
270
240
|
message: `expecting at least ${constants_1.MIN_NUMBER_OF_NAME_PARTS} elements`,
|
|
271
241
|
});
|
|
272
242
|
}
|
|
273
243
|
if (!__classPrivateFieldGet(this, _ArrayNameValidator_instances, "m", _ArrayNameValidator_hasBasicNames).call(this, value)) {
|
|
274
244
|
throw new error_1.InputError({
|
|
275
|
-
source: value,
|
|
245
|
+
source: toNameSource(value),
|
|
276
246
|
message: 'both first and last names are required',
|
|
277
247
|
});
|
|
278
248
|
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { Name } from './name';
|
|
2
|
+
import { Config } from './config';
|
|
3
|
+
import { Namefully } from './namefully';
|
|
4
|
+
type VoidCallback = () => void;
|
|
5
|
+
type Callback<Type, Return> = (value: Type) => Return;
|
|
6
|
+
/**
|
|
7
|
+
* A generic builder class that provides common functionality for building instances.
|
|
8
|
+
*/
|
|
9
|
+
declare abstract class Builder<T, I> {
|
|
10
|
+
protected readonly prebuild?: VoidCallback;
|
|
11
|
+
protected readonly postbuild?: Callback<I, void>;
|
|
12
|
+
protected readonly preclear?: Callback<I, void>;
|
|
13
|
+
protected readonly postclear?: VoidCallback;
|
|
14
|
+
protected queue: T[];
|
|
15
|
+
protected instance: I | null;
|
|
16
|
+
constructor(prebuild?: VoidCallback, postbuild?: Callback<I, void>, preclear?: Callback<I, void>, postclear?: VoidCallback);
|
|
17
|
+
/** Gets the current size of the builder. */
|
|
18
|
+
get size(): number;
|
|
19
|
+
/** Removes and returns the first element of the queue. */
|
|
20
|
+
removeFirst(): T | undefined;
|
|
21
|
+
/** Removes and returns the last element of the queue. */
|
|
22
|
+
removeLast(): T | undefined;
|
|
23
|
+
/** Adds a value at the beginning of the queue. */
|
|
24
|
+
addFirst(value: T): void;
|
|
25
|
+
/** Adds a value at the end of the queue. */
|
|
26
|
+
addLast(value: T): void;
|
|
27
|
+
/** Adds a value at the end of the queue. */
|
|
28
|
+
add(...values: T[]): void;
|
|
29
|
+
/** Removes a single instance of a value from the queue. */
|
|
30
|
+
remove(value: T): boolean;
|
|
31
|
+
/** Removes all elements matched by the test function from the queue. */
|
|
32
|
+
removeWhere(callback: Callback<T, boolean>): void;
|
|
33
|
+
/** Removes all elements not matched by the test function from the queue. */
|
|
34
|
+
retainWhere(callback: Callback<T, boolean>): void;
|
|
35
|
+
/** Removes all elements in the queue. */
|
|
36
|
+
clear(): void;
|
|
37
|
+
/** Builds the desired instance with optional parameters. */
|
|
38
|
+
abstract build(options?: Partial<Config>): I;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* An on-the-fly name builder.
|
|
42
|
+
*
|
|
43
|
+
* The builder uses a lazy-building method while capturing all necessary Names
|
|
44
|
+
* to finally construct a complete Namefully instance.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```js
|
|
48
|
+
* const builder = NameBuilder.of([Name.first('Thomas'), Name.last('Edison')]);
|
|
49
|
+
* builder.add(Name.middle('Alva'));
|
|
50
|
+
* console.log(builder.build()); // 'Thomas Alva Edison'
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
export declare class NameBuilder extends Builder<Name, Namefully> {
|
|
54
|
+
private constructor();
|
|
55
|
+
/** Creates a base builder from one Name to construct Namefully later. */
|
|
56
|
+
static create(name?: Name): NameBuilder;
|
|
57
|
+
/** Creates a base builder from many Names to construct Namefully later. */
|
|
58
|
+
static of(...initialNames: Name[]): NameBuilder;
|
|
59
|
+
/** Creates a base builder from many Names with lifecycle hooks. */
|
|
60
|
+
static use({ names, prebuild, postbuild, preclear, postclear, }: {
|
|
61
|
+
names?: Name[];
|
|
62
|
+
prebuild?: VoidCallback;
|
|
63
|
+
postbuild?: Callback<Namefully, void>;
|
|
64
|
+
preclear?: Callback<Namefully, void>;
|
|
65
|
+
postclear?: VoidCallback;
|
|
66
|
+
}): NameBuilder;
|
|
67
|
+
/**
|
|
68
|
+
* Builds an instance of Namefully from the previously collected names.
|
|
69
|
+
*
|
|
70
|
+
* Regardless of how the names are added, both first and last names must exist
|
|
71
|
+
* to complete a fine build. Otherwise, it throws a NameError.
|
|
72
|
+
*/
|
|
73
|
+
build(config?: Partial<Config>): Namefully;
|
|
74
|
+
}
|
|
75
|
+
export {};
|
package/dist/types/config.d.ts
CHANGED
|
@@ -89,9 +89,6 @@ export declare class Config {
|
|
|
89
89
|
clone(): Config;
|
|
90
90
|
/** Resets the configuration by setting it back to its default values. */
|
|
91
91
|
reset(): void;
|
|
92
|
-
/**
|
|
93
|
-
|
|
94
|
-
* order of appearance of a name set.
|
|
95
|
-
*/
|
|
96
|
-
updateOrder(order: NameOrder): void;
|
|
92
|
+
/** Allows the possibility to alter behavior-related options after creating a name set. */
|
|
93
|
+
update({ orderedBy, title, ending }: Partial<Pick<Config, 'orderedBy' | 'title' | 'ending'>>): void;
|
|
97
94
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export declare const VERSION = "1.
|
|
1
|
+
export declare const VERSION = "1.3.1";
|
|
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 = " .,_-()[]<>'\"bBfFlLmMnNoOpPsS$";
|
package/dist/types/error.d.ts
CHANGED
|
@@ -1,13 +1,10 @@
|
|
|
1
|
-
import { Name } from './name';
|
|
2
1
|
import { Nullable } from './types';
|
|
3
|
-
type NameSource = Nullable<string | string[]
|
|
2
|
+
type NameSource = Nullable<string | string[]>;
|
|
4
3
|
interface ErrorMessage {
|
|
5
4
|
source: NameSource;
|
|
6
5
|
message?: string;
|
|
7
6
|
}
|
|
8
|
-
/**
|
|
9
|
-
* The error types supported by `Namefully`.
|
|
10
|
-
*/
|
|
7
|
+
/** The error types supported by `Namefully`. */
|
|
11
8
|
export declare enum NameErrorType {
|
|
12
9
|
/**
|
|
13
10
|
* Thrown when a name entry/argument is incorrect.
|
|
@@ -30,9 +27,7 @@ export declare enum NameErrorType {
|
|
|
30
27
|
* See also: `NotAllowedError`, `Namefully.format`.
|
|
31
28
|
*/
|
|
32
29
|
NOT_ALLOWED = 2,
|
|
33
|
-
/**
|
|
34
|
-
* Thrown by any other unknown sources or unexpected situation.
|
|
35
|
-
*/
|
|
30
|
+
/** Thrown by any other unknown sources or unexpected situation. */
|
|
36
31
|
UNKNOWN = 3
|
|
37
32
|
}
|
|
38
33
|
/**
|
|
@@ -45,8 +40,8 @@ export declare enum NameErrorType {
|
|
|
45
40
|
* program failure. Au contraire, it is expected that a programmer using this utility
|
|
46
41
|
* would consider validating a name using its own business rules. That is not
|
|
47
42
|
* this utility's job to guess those rules. So, the predefined `ValidationRules`
|
|
48
|
-
* obey some common validation techniques when it comes to sanitizing a
|
|
49
|
-
* 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,
|
|
50
45
|
* indicating that those predefined rules should be skipped for the sake of the
|
|
51
46
|
* program.
|
|
52
47
|
*
|
|
@@ -55,7 +50,7 @@ export declare enum NameErrorType {
|
|
|
55
50
|
*
|
|
56
51
|
* A name error intends to provide useful information about what causes the error
|
|
57
52
|
* and let the user take initiative on what happens next to the given name:
|
|
58
|
-
* reconstructing it or
|
|
53
|
+
* reconstructing it or discarding it.
|
|
59
54
|
*/
|
|
60
55
|
export declare class NameError extends Error {
|
|
61
56
|
readonly source: NameSource;
|
|
@@ -63,8 +58,8 @@ export declare class NameError extends Error {
|
|
|
63
58
|
/**
|
|
64
59
|
* Creates an error with a message describing the issue for a name source.
|
|
65
60
|
* @param source name input that caused the error
|
|
66
|
-
* @param message
|
|
67
|
-
* @param type of `NameErrorType`
|
|
61
|
+
* @param message describing the failure.
|
|
62
|
+
* @param type of error via `NameErrorType`
|
|
68
63
|
*/
|
|
69
64
|
constructor(source: NameSource, message?: string, type?: NameErrorType);
|
|
70
65
|
/** The actual source input which caused the error. */
|
|
@@ -77,9 +72,9 @@ export declare class NameError extends Error {
|
|
|
77
72
|
/**
|
|
78
73
|
* An error thrown when a name source input is incorrect.
|
|
79
74
|
*
|
|
80
|
-
* 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),
|
|
81
76
|
* hence, a wrong input will cause this kind of error. Another common reason
|
|
82
|
-
* may be a wrong key in a
|
|
77
|
+
* may be a wrong key in a JSON name parsing mechanism.
|
|
83
78
|
*
|
|
84
79
|
* Keep in mind that this error is different from a `ValidationError`.
|
|
85
80
|
*/
|
|
@@ -151,7 +146,7 @@ export declare class UnknownError extends NameError {
|
|
|
151
146
|
* Optionally, the original error revealing the true nature of the failure.
|
|
152
147
|
*/
|
|
153
148
|
constructor(error: ErrorMessage & {
|
|
154
|
-
|
|
149
|
+
origin?: Error;
|
|
155
150
|
});
|
|
156
151
|
toString(): string;
|
|
157
152
|
}
|
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
import { Config } from './config';
|
|
2
|
-
import { FirstName, LastName, Name, JsonName } from './name';
|
|
3
2
|
import { Nullable, Namon } from './types';
|
|
3
|
+
import { FirstName, LastName, Name, JsonName } from './name';
|
|
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,11 @@ 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
|
-
|
|
52
|
-
|
|
53
|
-
|
|
50
|
+
/** Returns true if a namon has been set. */
|
|
51
|
+
has(key: Namon | string): boolean;
|
|
52
|
+
toString(): string;
|
|
53
|
+
/** Returns an `Iterable` of existing `Name`s. */
|
|
54
|
+
toIterable(flat?: boolean): Iterable<Name>;
|
|
55
|
+
/** Returns the default iterator for this name set (enabling for-of statements). */
|
|
56
|
+
[Symbol.iterator](): Iterator<Name>;
|
|
54
57
|
}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,15 +1,18 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Welcome to namefully!
|
|
3
3
|
*
|
|
4
|
-
* `namefully` is a JavaScript utility for
|
|
4
|
+
* `namefully` is a JavaScript utility for handling personal names.
|
|
5
5
|
*
|
|
6
6
|
* Sources
|
|
7
7
|
* - repo: https://github.com/ralflorent/namefully
|
|
8
8
|
* - docs: https://namefully.netlify.app
|
|
9
9
|
* - npm: https://npmjs.com/package/namefully
|
|
10
|
+
* - jsr: https://jsr.io/@ralflorent/namefully
|
|
10
11
|
*
|
|
11
12
|
* @license MIT
|
|
12
13
|
*/
|
|
14
|
+
import namefully from './namefully';
|
|
15
|
+
export * from './builder';
|
|
13
16
|
export * from './config';
|
|
14
17
|
export { VERSION as version } from './constants';
|
|
15
18
|
export * from './error';
|
|
@@ -19,3 +22,4 @@ export * from './namefully';
|
|
|
19
22
|
export { Parser } from './parser';
|
|
20
23
|
export * from './types';
|
|
21
24
|
export { NameIndex } from './utils';
|
|
25
|
+
export default namefully;
|
package/dist/types/name.d.ts
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
import { CapsRange, Namon, Surname } from './types';
|
|
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,30 +77,28 @@ 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
|
-
readonly format: Surname;
|
|
83
|
+
readonly format: Surname | 'father' | 'mother' | 'hyphenated' | 'all';
|
|
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
|
-
constructor(father: string, mother?: string, format?: Surname);
|
|
97
|
-
/** The surname inherited from
|
|
90
|
+
constructor(father: string, mother?: string, format?: Surname | 'father' | 'mother' | 'hyphenated' | 'all');
|
|
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;
|
|
103
97
|
get length(): number;
|
|
104
98
|
/** Returns a combined version of the `father` and `mother` if any. */
|
|
105
99
|
get asNames(): Name[];
|
|
106
|
-
toString(format?: Surname): string;
|
|
107
|
-
initials(format?: Surname): string[];
|
|
100
|
+
toString(format?: Surname | 'father' | 'mother' | 'hyphenated' | 'all'): string;
|
|
101
|
+
initials(format?: Surname | 'father' | 'mother' | 'hyphenated' | 'all'): string[];
|
|
108
102
|
caps(range?: CapsRange): LastName;
|
|
109
103
|
decaps(range?: CapsRange): LastName;
|
|
110
104
|
/** Makes a copy of the current name. */
|
|
@@ -114,13 +108,18 @@ export declare class LastName extends Name {
|
|
|
114
108
|
format?: Surname;
|
|
115
109
|
}): LastName;
|
|
116
110
|
}
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
*/
|
|
111
|
+
export declare function isNameArray(value?: unknown): value is Name[];
|
|
112
|
+
/** JSON signature for `FullName` data. */
|
|
120
113
|
export interface JsonName {
|
|
121
114
|
prefix?: string;
|
|
122
|
-
firstName: string
|
|
123
|
-
|
|
124
|
-
|
|
115
|
+
firstName: string | {
|
|
116
|
+
value: string;
|
|
117
|
+
more?: string[];
|
|
118
|
+
};
|
|
119
|
+
middleName?: string | string[];
|
|
120
|
+
lastName: string | {
|
|
121
|
+
father: string;
|
|
122
|
+
mother?: string;
|
|
123
|
+
};
|
|
125
124
|
suffix?: string;
|
|
126
125
|
}
|