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/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,10 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
exports.VERSION = '2.0.
|
|
3
|
+
exports.ALLOWED_FORMAT_TOKENS = exports.MAX_NUMBER_OF_NAME_PARTS = exports.MIN_NUMBER_OF_NAME_PARTS = exports.VERSION = void 0;
|
|
4
|
+
exports.VERSION = '2.0.2';
|
|
5
5
|
exports.MIN_NUMBER_OF_NAME_PARTS = 2;
|
|
6
6
|
exports.MAX_NUMBER_OF_NAME_PARTS = 5;
|
|
7
|
-
exports.
|
|
7
|
+
exports.ALLOWED_FORMAT_TOKENS = [
|
|
8
8
|
'.',
|
|
9
9
|
',',
|
|
10
10
|
' ',
|
package/dist/cjs/error.js
CHANGED
|
@@ -19,14 +19,11 @@ class NameError extends Error {
|
|
|
19
19
|
this.name = 'NameError';
|
|
20
20
|
}
|
|
21
21
|
get sourceAsString() {
|
|
22
|
-
let input = '';
|
|
23
|
-
if (!this.source)
|
|
24
|
-
input = '<undefined>';
|
|
25
22
|
if (typeof this.source === 'string')
|
|
26
|
-
|
|
23
|
+
return this.source;
|
|
27
24
|
if ((0, utils_js_1.isStringArray)(this.source))
|
|
28
|
-
|
|
29
|
-
return
|
|
25
|
+
return this.source.join(' ');
|
|
26
|
+
return '<undefined>';
|
|
30
27
|
}
|
|
31
28
|
get hasMessage() {
|
|
32
29
|
return this.message.trim().length > 0;
|
|
@@ -82,7 +79,7 @@ class UnknownError extends NameError {
|
|
|
82
79
|
origin;
|
|
83
80
|
constructor(error) {
|
|
84
81
|
super(error.source, error.message, NameErrorType.UNKNOWN);
|
|
85
|
-
this.origin = error.
|
|
82
|
+
this.origin = error.origin;
|
|
86
83
|
this.name = 'UnknownError';
|
|
87
84
|
}
|
|
88
85
|
toString() {
|
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];
|
|
@@ -75,8 +75,8 @@ class Name {
|
|
|
75
75
|
return this;
|
|
76
76
|
}
|
|
77
77
|
validate(name) {
|
|
78
|
-
if (name && name
|
|
79
|
-
throw new error_js_1.InputError({ source: name, message: 'must be
|
|
78
|
+
if (typeof name === 'string' && name.trim().length < 1) {
|
|
79
|
+
throw new error_js_1.InputError({ source: name, message: 'must be 1+ characters' });
|
|
80
80
|
}
|
|
81
81
|
}
|
|
82
82
|
}
|
|
@@ -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,8 +244,8 @@ class Namefully {
|
|
|
252
244
|
pattern = 'o';
|
|
253
245
|
let group = '';
|
|
254
246
|
const formatted = [];
|
|
255
|
-
for (const char of pattern
|
|
256
|
-
if (constants_js_1.
|
|
247
|
+
for (const char of pattern) {
|
|
248
|
+
if (constants_js_1.ALLOWED_FORMAT_TOKENS.indexOf(char) === -1) {
|
|
257
249
|
throw new error_js_1.NotAllowedError({
|
|
258
250
|
source: this.full,
|
|
259
251
|
operation: 'format',
|
|
@@ -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);
|
|
@@ -84,16 +84,7 @@ exports.ArrayStringParser = ArrayStringParser;
|
|
|
84
84
|
class NamaParser extends Parser {
|
|
85
85
|
parse(options) {
|
|
86
86
|
const config = config_js_1.Config.merge(options);
|
|
87
|
-
|
|
88
|
-
validator_js_1.NamaValidator.create().validateKeys(this.#asNama());
|
|
89
|
-
}
|
|
90
|
-
else {
|
|
91
|
-
validator_js_1.NamaValidator.create().validate(this.#asNama());
|
|
92
|
-
}
|
|
93
|
-
return fullname_js_1.FullName.parse(this.raw, config);
|
|
94
|
-
}
|
|
95
|
-
#asNama() {
|
|
96
|
-
return new Map(Object.entries(this.raw).map(([key, value]) => {
|
|
87
|
+
const names = new Map(Object.entries(this.raw).map(([key, value]) => {
|
|
97
88
|
const namon = types_js_1.Namon.cast(key);
|
|
98
89
|
if (!namon) {
|
|
99
90
|
throw new error_js_1.InputError({
|
|
@@ -103,6 +94,13 @@ class NamaParser extends Parser {
|
|
|
103
94
|
}
|
|
104
95
|
return [namon, value];
|
|
105
96
|
}));
|
|
97
|
+
if (config.bypass) {
|
|
98
|
+
validator_js_1.NamaValidator.create().validateKeys(names);
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
validator_js_1.NamaValidator.create().validate(names);
|
|
102
|
+
}
|
|
103
|
+
return fullname_js_1.FullName.parse(this.raw, config);
|
|
106
104
|
}
|
|
107
105
|
}
|
|
108
106
|
exports.NamaParser = NamaParser;
|
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,35 +67,28 @@ 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) {
|
|
73
74
|
if (!str || range === types_js_1.CapsRange.NONE)
|
|
74
75
|
return str;
|
|
75
|
-
const initial = str[0].toUpperCase();
|
|
76
|
-
const rest = str.slice(1).toLowerCase();
|
|
76
|
+
const [initial, rest] = [str[0].toUpperCase(), str.slice(1).toLowerCase()];
|
|
77
77
|
return range === types_js_1.CapsRange.INITIAL ? initial.concat(rest) : str.toUpperCase();
|
|
78
78
|
}
|
|
79
79
|
exports.capitalize = capitalize;
|
|
80
80
|
function decapitalize(str, range = types_js_1.CapsRange.INITIAL) {
|
|
81
81
|
if (!str || range === types_js_1.CapsRange.NONE)
|
|
82
82
|
return str;
|
|
83
|
-
const initial = str[0].toLowerCase();
|
|
84
|
-
const rest = str.slice(1);
|
|
83
|
+
const [initial, rest] = [str[0].toLowerCase(), str.slice(1)];
|
|
85
84
|
return range === types_js_1.CapsRange.INITIAL ? initial.concat(rest) : str.toLowerCase();
|
|
86
85
|
}
|
|
87
86
|
exports.decapitalize = decapitalize;
|
|
88
87
|
function toggleCase(str) {
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
}
|
|
94
|
-
else {
|
|
95
|
-
chars.push(c.toUpperCase());
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
return chars.join('');
|
|
88
|
+
return str
|
|
89
|
+
.split('')
|
|
90
|
+
.map((c) => (c === c.toUpperCase() ? c.toLowerCase() : c.toUpperCase()))
|
|
91
|
+
.join('');
|
|
99
92
|
}
|
|
100
93
|
exports.toggleCase = toggleCase;
|
|
101
94
|
function isStringArray(value) {
|