namefully 2.0.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.
- package/dist/cjs/config.js +14 -5
- package/dist/cjs/constants.js +1 -1
- package/dist/cjs/error.js +1 -1
- package/dist/cjs/fullname.js +10 -11
- package/dist/cjs/name.js +16 -20
- package/dist/cjs/namefully.js +31 -39
- package/dist/cjs/parser.js +5 -5
- package/dist/cjs/utils.js +16 -21
- package/dist/cjs/validator.js +19 -34
- 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 +1 -1
- package/dist/esm/constants.js +1 -1
- package/dist/esm/error.d.ts +3 -7
- package/dist/esm/error.js +1 -1
- package/dist/esm/fullname.d.ts +10 -12
- package/dist/esm/fullname.js +10 -11
- package/dist/esm/name.d.ts +9 -17
- package/dist/esm/name.js +16 -20
- package/dist/esm/namefully.d.ts +61 -63
- package/dist/esm/namefully.js +31 -39
- package/dist/esm/parser.d.ts +1 -1
- package/dist/esm/parser.js +5 -5
- package/dist/esm/types.d.ts +16 -48
- package/dist/esm/utils.d.ts +3 -4
- package/dist/esm/utils.js +16 -21
- package/dist/esm/validator.js +19 -34
- package/dist/namefully.js +105 -129
- package/dist/namefully.min.js +1 -1
- package/package.json +7 -7
- package/readme.md +14 -13
package/dist/cjs/config.js
CHANGED
|
@@ -46,7 +46,7 @@ class Config {
|
|
|
46
46
|
}
|
|
47
47
|
static create(name = defaultName) {
|
|
48
48
|
if (!_a.cache.has(name))
|
|
49
|
-
_a.cache.set(name, new
|
|
49
|
+
_a.cache.set(name, new _a(name));
|
|
50
50
|
return _a.cache.get(name);
|
|
51
51
|
}
|
|
52
52
|
static merge(other) {
|
|
@@ -87,10 +87,19 @@ class Config {
|
|
|
87
87
|
this.#surname = types_js_1.Surname.FATHER;
|
|
88
88
|
_a.cache.set(this.name, this);
|
|
89
89
|
}
|
|
90
|
-
updateOrder(
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
90
|
+
updateOrder(orderedBy) {
|
|
91
|
+
this.update({ orderedBy });
|
|
92
|
+
}
|
|
93
|
+
update({ orderedBy, title, ending }) {
|
|
94
|
+
const config = _a.cache.get(this.name);
|
|
95
|
+
if (!config)
|
|
96
|
+
return;
|
|
97
|
+
if (orderedBy !== this.#orderedBy)
|
|
98
|
+
config.#orderedBy = orderedBy;
|
|
99
|
+
if (title !== this.#title)
|
|
100
|
+
config.#title = title;
|
|
101
|
+
if (ending !== this.#ending)
|
|
102
|
+
config.#ending = ending;
|
|
94
103
|
}
|
|
95
104
|
#genNewName(name) {
|
|
96
105
|
return name === this.name || _a.cache.has(name) ? this.#genNewName(name + copyAlias) : name;
|
package/dist/cjs/constants.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ALLOWED_TOKENS = exports.MAX_NUMBER_OF_NAME_PARTS = exports.MIN_NUMBER_OF_NAME_PARTS = exports.VERSION = void 0;
|
|
4
|
-
exports.VERSION = '2.0.
|
|
4
|
+
exports.VERSION = '2.0.1';
|
|
5
5
|
exports.MIN_NUMBER_OF_NAME_PARTS = 2;
|
|
6
6
|
exports.MAX_NUMBER_OF_NAME_PARTS = 5;
|
|
7
7
|
exports.ALLOWED_TOKENS = [
|
package/dist/cjs/error.js
CHANGED
package/dist/cjs/fullname.js
CHANGED
|
@@ -2,10 +2,10 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.FullName = void 0;
|
|
4
4
|
const config_js_1 = require("./config.js");
|
|
5
|
+
const validator_js_1 = require("./validator.js");
|
|
6
|
+
const types_js_1 = require("./types.js");
|
|
5
7
|
const error_js_1 = require("./error.js");
|
|
6
8
|
const name_js_1 = require("./name.js");
|
|
7
|
-
const types_js_1 = require("./types.js");
|
|
8
|
-
const validator_js_1 = require("./validator.js");
|
|
9
9
|
class FullName {
|
|
10
10
|
#prefix;
|
|
11
11
|
#firstName;
|
|
@@ -36,13 +36,12 @@ class FullName {
|
|
|
36
36
|
}
|
|
37
37
|
static parse(json, config) {
|
|
38
38
|
try {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
return fullName;
|
|
39
|
+
return new FullName(config)
|
|
40
|
+
.setPrefix(json.prefix)
|
|
41
|
+
.setFirstName(json.firstName)
|
|
42
|
+
.setMiddleName(json.middleName ?? [])
|
|
43
|
+
.setLastName(json.lastName)
|
|
44
|
+
.setSuffix(json.suffix);
|
|
46
45
|
}
|
|
47
46
|
catch (error) {
|
|
48
47
|
if (error instanceof error_js_1.NameError)
|
|
@@ -50,7 +49,7 @@ class FullName {
|
|
|
50
49
|
throw new error_js_1.UnknownError({
|
|
51
50
|
source: Object.values(json).join(' '),
|
|
52
51
|
message: 'could not parse JSON content',
|
|
53
|
-
|
|
52
|
+
origin: error instanceof Error ? error : new Error(String(error)),
|
|
54
53
|
});
|
|
55
54
|
}
|
|
56
55
|
}
|
|
@@ -80,7 +79,7 @@ class FullName {
|
|
|
80
79
|
return this;
|
|
81
80
|
if (!this.#config.bypass)
|
|
82
81
|
validator_js_1.Validators.middleName.validate(names);
|
|
83
|
-
this.#middleName = names.map((
|
|
82
|
+
this.#middleName = names.map((n) => (n instanceof name_js_1.Name ? n : name_js_1.Name.middle(n)));
|
|
84
83
|
return this;
|
|
85
84
|
}
|
|
86
85
|
setSuffix(name) {
|
package/dist/cjs/name.js
CHANGED
|
@@ -43,19 +43,19 @@ class Name {
|
|
|
43
43
|
return this.type === types_js_1.Namon.SUFFIX;
|
|
44
44
|
}
|
|
45
45
|
static prefix(value) {
|
|
46
|
-
return new
|
|
46
|
+
return new Name(value, types_js_1.Namon.PREFIX);
|
|
47
47
|
}
|
|
48
48
|
static first(value) {
|
|
49
|
-
return new
|
|
49
|
+
return new Name(value, types_js_1.Namon.FIRST_NAME);
|
|
50
50
|
}
|
|
51
51
|
static middle(value) {
|
|
52
|
-
return new
|
|
52
|
+
return new Name(value, types_js_1.Namon.MIDDLE_NAME);
|
|
53
53
|
}
|
|
54
54
|
static last(value) {
|
|
55
|
-
return new
|
|
55
|
+
return new Name(value, types_js_1.Namon.LAST_NAME);
|
|
56
56
|
}
|
|
57
57
|
static suffix(value) {
|
|
58
|
-
return new
|
|
58
|
+
return new Name(value, types_js_1.Namon.SUFFIX);
|
|
59
59
|
}
|
|
60
60
|
initials() {
|
|
61
61
|
return [this.initial];
|
|
@@ -85,7 +85,7 @@ class FirstName extends Name {
|
|
|
85
85
|
#more;
|
|
86
86
|
constructor(value, ...more) {
|
|
87
87
|
super(value, types_js_1.Namon.FIRST_NAME);
|
|
88
|
-
more.forEach(
|
|
88
|
+
more.forEach(this.validate);
|
|
89
89
|
this.#more = more;
|
|
90
90
|
}
|
|
91
91
|
get hasMore() {
|
|
@@ -96,9 +96,8 @@ class FirstName extends Name {
|
|
|
96
96
|
}
|
|
97
97
|
get asNames() {
|
|
98
98
|
const names = [Name.first(this.value)];
|
|
99
|
-
if (this.hasMore)
|
|
100
|
-
names.push(...this.#more.map(
|
|
101
|
-
}
|
|
99
|
+
if (this.hasMore)
|
|
100
|
+
names.push(...this.#more.map(Name.first));
|
|
102
101
|
return names;
|
|
103
102
|
}
|
|
104
103
|
get more() {
|
|
@@ -109,9 +108,8 @@ class FirstName extends Name {
|
|
|
109
108
|
}
|
|
110
109
|
initials(withMore = false) {
|
|
111
110
|
const inits = [this.initial];
|
|
112
|
-
if (withMore && this.hasMore)
|
|
111
|
+
if (withMore && this.hasMore)
|
|
113
112
|
inits.push(...this.#more.map((n) => n[0]));
|
|
114
|
-
}
|
|
115
113
|
return inits;
|
|
116
114
|
}
|
|
117
115
|
caps(range) {
|
|
@@ -174,34 +172,32 @@ class LastName extends Name {
|
|
|
174
172
|
}
|
|
175
173
|
}
|
|
176
174
|
initials(format) {
|
|
177
|
-
format = format || this.format;
|
|
178
175
|
const inits = [];
|
|
179
|
-
switch (format) {
|
|
180
|
-
case types_js_1.Surname.MOTHER:
|
|
181
|
-
if (this.#mother)
|
|
182
|
-
inits.push(this.#mother[0]);
|
|
183
|
-
break;
|
|
176
|
+
switch (format ?? this.format) {
|
|
184
177
|
case types_js_1.Surname.HYPHENATED:
|
|
185
178
|
case types_js_1.Surname.ALL:
|
|
186
179
|
inits.push(this.initial);
|
|
187
180
|
if (this.#mother)
|
|
188
181
|
inits.push(this.#mother[0]);
|
|
189
182
|
break;
|
|
190
|
-
case types_js_1.Surname.
|
|
183
|
+
case types_js_1.Surname.MOTHER:
|
|
184
|
+
if (this.#mother)
|
|
185
|
+
inits.push(this.#mother[0]);
|
|
186
|
+
break;
|
|
191
187
|
default:
|
|
192
188
|
inits.push(this.initial);
|
|
193
189
|
}
|
|
194
190
|
return inits;
|
|
195
191
|
}
|
|
196
192
|
caps(range) {
|
|
197
|
-
range
|
|
193
|
+
range ??= this.capsRange;
|
|
198
194
|
this.value = (0, utils_js_1.capitalize)(this.value, range);
|
|
199
195
|
if (this.hasMother)
|
|
200
196
|
this.#mother = (0, utils_js_1.capitalize)(this.#mother, range);
|
|
201
197
|
return this;
|
|
202
198
|
}
|
|
203
199
|
decaps(range) {
|
|
204
|
-
range
|
|
200
|
+
range ??= this.capsRange;
|
|
205
201
|
this.value = (0, utils_js_1.decapitalize)(this.value, range);
|
|
206
202
|
if (this.hasMother)
|
|
207
203
|
this.#mother = (0, utils_js_1.decapitalize)(this.#mother, range);
|
package/dist/cjs/namefully.js
CHANGED
|
@@ -4,9 +4,9 @@ exports.Namefully = void 0;
|
|
|
4
4
|
const constants_js_1 = require("./constants.js");
|
|
5
5
|
const error_js_1 = require("./error.js");
|
|
6
6
|
const name_js_1 = require("./name.js");
|
|
7
|
-
const parser_js_1 = require("./parser.js");
|
|
8
7
|
const types_js_1 = require("./types.js");
|
|
9
8
|
const utils_js_1 = require("./utils.js");
|
|
9
|
+
const parser_js_1 = require("./parser.js");
|
|
10
10
|
class Namefully {
|
|
11
11
|
#fullName;
|
|
12
12
|
constructor(names, options) {
|
|
@@ -93,16 +93,16 @@ class Namefully {
|
|
|
93
93
|
suffix: this.suffix,
|
|
94
94
|
};
|
|
95
95
|
}
|
|
96
|
+
json = this.toJson;
|
|
96
97
|
has(namon) {
|
|
97
98
|
return this.#fullName.has(namon);
|
|
98
99
|
}
|
|
99
100
|
fullName(orderedBy) {
|
|
100
101
|
const sep = this.config.ending ? ',' : '';
|
|
101
102
|
const names = [];
|
|
102
|
-
orderedBy = orderedBy || this.config.orderedBy;
|
|
103
103
|
if (this.prefix)
|
|
104
104
|
names.push(this.prefix);
|
|
105
|
-
if (orderedBy === types_js_1.NameOrder.FIRST_NAME) {
|
|
105
|
+
if ((orderedBy ?? this.config.orderedBy) === types_js_1.NameOrder.FIRST_NAME) {
|
|
106
106
|
names.push(this.first, ...this.middleName(), this.last + sep);
|
|
107
107
|
}
|
|
108
108
|
else {
|
|
@@ -113,7 +113,7 @@ class Namefully {
|
|
|
113
113
|
return names.join(' ').trim();
|
|
114
114
|
}
|
|
115
115
|
birthName(orderedBy) {
|
|
116
|
-
orderedBy
|
|
116
|
+
orderedBy ??= this.config.orderedBy;
|
|
117
117
|
return orderedBy === types_js_1.NameOrder.FIRST_NAME
|
|
118
118
|
? [this.first, ...this.middleName(), this.last].join(' ')
|
|
119
119
|
: [this.last, this.first, ...this.middleName()].join(' ');
|
|
@@ -128,50 +128,42 @@ class Namefully {
|
|
|
128
128
|
return this.#fullName.lastName.toString(format);
|
|
129
129
|
}
|
|
130
130
|
initials(options) {
|
|
131
|
+
const { orderedBy = this.config.orderedBy, only = types_js_1.NameType.BIRTH_NAME, asJson } = options ?? {};
|
|
131
132
|
const firstInits = this.#fullName.firstName.initials();
|
|
132
|
-
const midInits = this.#fullName.middleName.map((n) => n.
|
|
133
|
+
const midInits = this.#fullName.middleName.map((n) => n.value[0]);
|
|
133
134
|
const lastInits = this.#fullName.lastName.initials();
|
|
134
|
-
if (
|
|
135
|
+
if (asJson)
|
|
135
136
|
return { firstName: firstInits, middleName: midInits, lastName: lastInits };
|
|
136
|
-
const initials = [];
|
|
137
|
-
const { orderedBy = this.config.orderedBy, only = types_js_1.NameType.BIRTH_NAME } = options ?? {};
|
|
138
137
|
if (only !== types_js_1.NameType.BIRTH_NAME) {
|
|
139
|
-
|
|
140
|
-
initials.push(...firstInits);
|
|
141
|
-
}
|
|
142
|
-
else if (only === types_js_1.NameType.MIDDLE_NAME) {
|
|
143
|
-
initials.push(...midInits);
|
|
144
|
-
}
|
|
145
|
-
else {
|
|
146
|
-
initials.push(...lastInits);
|
|
147
|
-
}
|
|
138
|
+
return only === types_js_1.NameType.FIRST_NAME ? firstInits : only === types_js_1.NameType.MIDDLE_NAME ? midInits : lastInits;
|
|
148
139
|
}
|
|
149
140
|
else if (orderedBy === types_js_1.NameOrder.FIRST_NAME) {
|
|
150
|
-
|
|
141
|
+
return [...firstInits, ...midInits, ...lastInits];
|
|
151
142
|
}
|
|
152
143
|
else {
|
|
153
|
-
|
|
144
|
+
return [...lastInits, ...firstInits, ...midInits];
|
|
154
145
|
}
|
|
155
|
-
return initials;
|
|
156
146
|
}
|
|
157
147
|
shorten(orderedBy) {
|
|
158
|
-
orderedBy
|
|
148
|
+
orderedBy ??= this.config.orderedBy;
|
|
149
|
+
const { firstName, lastName } = this.#fullName;
|
|
159
150
|
return orderedBy === types_js_1.NameOrder.FIRST_NAME
|
|
160
|
-
? [
|
|
161
|
-
: [
|
|
151
|
+
? [firstName.value, lastName.toString()].join(' ')
|
|
152
|
+
: [lastName.toString(), firstName.value].join(' ');
|
|
162
153
|
}
|
|
163
154
|
flatten(options) {
|
|
164
155
|
const { by = types_js_1.Flat.MIDDLE_NAME, limit = 20, recursive = false, withMore = false, withPeriod = true, surname, } = options;
|
|
165
156
|
if (this.length <= limit)
|
|
166
157
|
return this.full;
|
|
158
|
+
const { firstName, lastName, middleName } = this.#fullName;
|
|
167
159
|
const sep = withPeriod ? '.' : '';
|
|
168
|
-
const fn = this.#fullName.firstName.toString();
|
|
169
|
-
const mn = this.middleName().join(' ');
|
|
170
|
-
const ln = this.#fullName.lastName.toString();
|
|
171
160
|
const hasMid = this.hasMiddle;
|
|
172
|
-
const
|
|
173
|
-
const
|
|
174
|
-
const
|
|
161
|
+
const fn = firstName.toString();
|
|
162
|
+
const mn = this.middleName().join(' ');
|
|
163
|
+
const ln = lastName.toString();
|
|
164
|
+
const f = firstName.initials(withMore).join(sep + ' ') + sep;
|
|
165
|
+
const l = lastName.initials(surname).join(sep + ' ') + sep;
|
|
166
|
+
const m = hasMid ? middleName.map((n) => n.value[0]).join(sep + ' ') + sep : '';
|
|
175
167
|
let name = [];
|
|
176
168
|
if (this.config.orderedBy === types_js_1.NameOrder.FIRST_NAME) {
|
|
177
169
|
switch (by) {
|
|
@@ -252,7 +244,7 @@ class Namefully {
|
|
|
252
244
|
pattern = 'o';
|
|
253
245
|
let group = '';
|
|
254
246
|
const formatted = [];
|
|
255
|
-
for (const char of pattern
|
|
247
|
+
for (const char of pattern) {
|
|
256
248
|
if (constants_js_1.ALLOWED_TOKENS.indexOf(char) === -1) {
|
|
257
249
|
throw new error_js_1.NotAllowedError({
|
|
258
250
|
source: this.full,
|
|
@@ -269,7 +261,8 @@ class Namefully {
|
|
|
269
261
|
return formatted.join('').trim();
|
|
270
262
|
}
|
|
271
263
|
flip() {
|
|
272
|
-
this.config.
|
|
264
|
+
const order = this.config.orderedBy === types_js_1.NameOrder.FIRST_NAME ? types_js_1.NameOrder.LAST_NAME : types_js_1.NameOrder.FIRST_NAME;
|
|
265
|
+
this.config.update({ orderedBy: order });
|
|
273
266
|
}
|
|
274
267
|
split(separator = /[' -]/g) {
|
|
275
268
|
return this.birth.replace(separator, ' ').split(' ');
|
|
@@ -320,7 +313,7 @@ class Namefully {
|
|
|
320
313
|
return new parser_js_1.ArrayNameParser(raw);
|
|
321
314
|
if (typeof raw === 'object')
|
|
322
315
|
return new parser_js_1.NamaParser(raw);
|
|
323
|
-
throw new error_js_1.InputError({ source: raw, message: 'Cannot parse raw data
|
|
316
|
+
throw new error_js_1.InputError({ source: raw, message: 'Cannot parse raw data; review expected data types.' });
|
|
324
317
|
}
|
|
325
318
|
#map(char) {
|
|
326
319
|
switch (char) {
|
|
@@ -348,16 +341,15 @@ class Namefully {
|
|
|
348
341
|
case 'o':
|
|
349
342
|
case 'O':
|
|
350
343
|
return ((character) => {
|
|
351
|
-
const sep = this.config.ending ? ',' : ''
|
|
344
|
+
const sep = this.config.ending ? ',' : '';
|
|
345
|
+
const names = [];
|
|
352
346
|
if (this.prefix)
|
|
353
347
|
names.push(this.prefix);
|
|
354
348
|
names.push(`${this.last},`.toUpperCase());
|
|
355
|
-
if (this.hasMiddle)
|
|
349
|
+
if (this.hasMiddle)
|
|
356
350
|
names.push(this.first, this.middleName().join(' ') + sep);
|
|
357
|
-
|
|
358
|
-
else {
|
|
351
|
+
else
|
|
359
352
|
names.push(this.first + sep);
|
|
360
|
-
}
|
|
361
353
|
if (this.suffix)
|
|
362
354
|
names.push(this.suffix);
|
|
363
355
|
const nama = names.join(' ').trim();
|
|
@@ -373,10 +365,10 @@ class Namefully {
|
|
|
373
365
|
return this.suffix?.toUpperCase();
|
|
374
366
|
case '$f':
|
|
375
367
|
case '$F':
|
|
376
|
-
return this.#fullName.firstName.
|
|
368
|
+
return this.#fullName.firstName.value[0];
|
|
377
369
|
case '$l':
|
|
378
370
|
case '$L':
|
|
379
|
-
return this.#fullName.lastName.
|
|
371
|
+
return this.#fullName.lastName.value[0];
|
|
380
372
|
case '$m':
|
|
381
373
|
case '$M':
|
|
382
374
|
return this.hasMiddle ? this.middle[0] : undefined;
|
package/dist/cjs/parser.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ArrayNameParser = exports.NamaParser = exports.ArrayStringParser = exports.StringParser = exports.Parser = void 0;
|
|
4
|
-
const fullname_js_1 = require("./fullname.js");
|
|
5
4
|
const config_js_1 = require("./config.js");
|
|
6
5
|
const utils_js_1 = require("./utils.js");
|
|
7
|
-
const validator_js_1 = require("./validator.js");
|
|
8
|
-
const name_js_1 = require("./name.js");
|
|
9
|
-
const types_js_1 = require("./types.js");
|
|
10
6
|
const error_js_1 = require("./error.js");
|
|
7
|
+
const fullname_js_1 = require("./fullname.js");
|
|
8
|
+
const types_js_1 = require("./types.js");
|
|
9
|
+
const name_js_1 = require("./name.js");
|
|
10
|
+
const validator_js_1 = require("./validator.js");
|
|
11
11
|
class Parser {
|
|
12
12
|
raw;
|
|
13
13
|
constructor(raw) {
|
|
@@ -17,7 +17,7 @@ class Parser {
|
|
|
17
17
|
const parts = text.trim().split(types_js_1.Separator.SPACE.token);
|
|
18
18
|
const length = parts.length;
|
|
19
19
|
if (index instanceof utils_js_1.NameIndex) {
|
|
20
|
-
const names = Object.entries(index.
|
|
20
|
+
const names = Object.entries(index.json())
|
|
21
21
|
.filter(([, position]) => position > -1 && position < length)
|
|
22
22
|
.map(([key, position]) => new name_js_1.Name(parts[position], types_js_1.Namon.all.get(key)));
|
|
23
23
|
return new ArrayNameParser(names);
|
package/dist/cjs/utils.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.isStringArray = exports.toggleCase = exports.decapitalize = exports.capitalize = exports.NameIndex = void 0;
|
|
4
|
-
const constants_js_1 = require("./constants.js");
|
|
5
4
|
const types_js_1 = require("./types.js");
|
|
5
|
+
const constants_js_1 = require("./constants.js");
|
|
6
6
|
class NameIndex {
|
|
7
7
|
prefix;
|
|
8
8
|
firstName;
|
|
@@ -23,19 +23,19 @@ class NameIndex {
|
|
|
23
23
|
this.suffix = suffix;
|
|
24
24
|
}
|
|
25
25
|
static base() {
|
|
26
|
-
return new
|
|
26
|
+
return new NameIndex(-1, 0, -1, 1, -1);
|
|
27
27
|
}
|
|
28
28
|
static when(order, count = 2) {
|
|
29
29
|
if (order === types_js_1.NameOrder.FIRST_NAME) {
|
|
30
30
|
switch (count) {
|
|
31
31
|
case 2:
|
|
32
|
-
return new
|
|
32
|
+
return new NameIndex(-1, 0, -1, 1, -1);
|
|
33
33
|
case 3:
|
|
34
|
-
return new
|
|
34
|
+
return new NameIndex(-1, 0, 1, 2, -1);
|
|
35
35
|
case 4:
|
|
36
|
-
return new
|
|
36
|
+
return new NameIndex(0, 1, 2, 3, -1);
|
|
37
37
|
case 5:
|
|
38
|
-
return new
|
|
38
|
+
return new NameIndex(0, 1, 2, 3, 4);
|
|
39
39
|
default:
|
|
40
40
|
return NameIndex.base();
|
|
41
41
|
}
|
|
@@ -43,20 +43,20 @@ class NameIndex {
|
|
|
43
43
|
else {
|
|
44
44
|
switch (count) {
|
|
45
45
|
case 2:
|
|
46
|
-
return new
|
|
46
|
+
return new NameIndex(-1, 1, -1, 0, -1);
|
|
47
47
|
case 3:
|
|
48
|
-
return new
|
|
48
|
+
return new NameIndex(-1, 1, 2, 0, -1);
|
|
49
49
|
case 4:
|
|
50
|
-
return new
|
|
50
|
+
return new NameIndex(0, 2, 3, 1, -1);
|
|
51
51
|
case 5:
|
|
52
|
-
return new
|
|
52
|
+
return new NameIndex(0, 2, 3, 1, 4);
|
|
53
53
|
default:
|
|
54
54
|
return NameIndex.base();
|
|
55
55
|
}
|
|
56
56
|
}
|
|
57
57
|
}
|
|
58
58
|
static only({ prefix = -1, firstName, middleName = -1, lastName, suffix = -1 }) {
|
|
59
|
-
return new
|
|
59
|
+
return new NameIndex(prefix, firstName, middleName, lastName, suffix);
|
|
60
60
|
}
|
|
61
61
|
toJson() {
|
|
62
62
|
return {
|
|
@@ -67,6 +67,7 @@ class NameIndex {
|
|
|
67
67
|
suffix: this.suffix,
|
|
68
68
|
};
|
|
69
69
|
}
|
|
70
|
+
json = this.toJson;
|
|
70
71
|
}
|
|
71
72
|
exports.NameIndex = NameIndex;
|
|
72
73
|
function capitalize(str, range = types_js_1.CapsRange.INITIAL) {
|
|
@@ -86,16 +87,10 @@ function decapitalize(str, range = types_js_1.CapsRange.INITIAL) {
|
|
|
86
87
|
}
|
|
87
88
|
exports.decapitalize = decapitalize;
|
|
88
89
|
function toggleCase(str) {
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
}
|
|
94
|
-
else {
|
|
95
|
-
chars.push(c.toUpperCase());
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
return chars.join('');
|
|
90
|
+
return str
|
|
91
|
+
.split('')
|
|
92
|
+
.map((c) => (c === c.toUpperCase() ? c.toLowerCase() : c.toUpperCase()))
|
|
93
|
+
.join('');
|
|
99
94
|
}
|
|
100
95
|
exports.toggleCase = toggleCase;
|
|
101
96
|
function isStringArray(value) {
|
package/dist/cjs/validator.js
CHANGED
|
@@ -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);
|
|
@@ -188,16 +179,10 @@ class NamaValidator {
|
|
|
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) {
|