namefully 2.1.0 → 2.2.0
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/builder.js +5 -2
- package/dist/cjs/config.js +10 -5
- package/dist/cjs/constants.js +3 -2
- package/dist/cjs/data.js +5 -2
- package/dist/cjs/fullname.js +52 -7
- package/dist/cjs/namefully.js +44 -22
- package/dist/cjs/parser.js +28 -12
- package/dist/esm/builder.js +5 -2
- package/dist/esm/config.d.ts +4 -10
- package/dist/esm/config.js +10 -5
- package/dist/esm/constants.d.ts +2 -1
- package/dist/esm/constants.js +2 -1
- package/dist/esm/data.d.ts +3 -13
- package/dist/esm/data.js +5 -2
- package/dist/esm/fullname.d.ts +33 -0
- package/dist/esm/fullname.js +50 -6
- package/dist/esm/name.d.ts +9 -3
- package/dist/esm/namefully.d.ts +5 -2
- package/dist/esm/namefully.js +44 -22
- package/dist/esm/parser.d.ts +9 -6
- package/dist/esm/parser.js +28 -13
- package/dist/namefully.js +140 -49
- package/dist/namefully.min.js +1 -1
- package/package.json +2 -2
- package/readme.md +28 -4
package/dist/cjs/builder.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.NameBuilder = void 0;
|
|
4
|
+
const config_js_1 = require("./config.js");
|
|
4
5
|
const validator_js_1 = require("./validator.js");
|
|
5
6
|
const namefully_js_1 = require("./namefully.js");
|
|
6
7
|
class Builder {
|
|
@@ -73,8 +74,10 @@ class NameBuilder extends Builder {
|
|
|
73
74
|
build(options) {
|
|
74
75
|
this.prebuild?.();
|
|
75
76
|
const names = [...this.queue];
|
|
76
|
-
|
|
77
|
-
|
|
77
|
+
const config = config_js_1.Config.merge(options);
|
|
78
|
+
if (!config.mono)
|
|
79
|
+
validator_js_1.ArrayNameValidator.create().validate(names);
|
|
80
|
+
this.instance = new namefully_js_1.Namefully(names, config);
|
|
78
81
|
this.postbuild?.(this.instance);
|
|
79
82
|
return this.instance;
|
|
80
83
|
}
|
package/dist/cjs/config.js
CHANGED
|
@@ -13,6 +13,7 @@ class Config {
|
|
|
13
13
|
#ending;
|
|
14
14
|
#bypass;
|
|
15
15
|
#surname;
|
|
16
|
+
#mono;
|
|
16
17
|
static cache = new Map();
|
|
17
18
|
get orderedBy() {
|
|
18
19
|
return this.#orderedBy;
|
|
@@ -32,10 +33,13 @@ class Config {
|
|
|
32
33
|
get surname() {
|
|
33
34
|
return this.#surname;
|
|
34
35
|
}
|
|
36
|
+
get mono() {
|
|
37
|
+
return this.#mono;
|
|
38
|
+
}
|
|
35
39
|
get name() {
|
|
36
40
|
return this.#name;
|
|
37
41
|
}
|
|
38
|
-
constructor(name, orderedBy = types_js_1.NameOrder.FIRST_NAME, separator = types_js_1.Separator.SPACE, title = types_js_1.Title.UK, ending = false, bypass = true, surname = types_js_1.Surname.FATHER) {
|
|
42
|
+
constructor(name, orderedBy = types_js_1.NameOrder.FIRST_NAME, separator = types_js_1.Separator.SPACE, title = types_js_1.Title.UK, ending = false, bypass = true, surname = types_js_1.Surname.FATHER, mono = false) {
|
|
39
43
|
this.#name = name;
|
|
40
44
|
this.#orderedBy = orderedBy;
|
|
41
45
|
this.#separator = separator;
|
|
@@ -43,6 +47,7 @@ class Config {
|
|
|
43
47
|
this.#ending = ending;
|
|
44
48
|
this.#bypass = bypass;
|
|
45
49
|
this.#surname = surname;
|
|
50
|
+
this.#mono = mono;
|
|
46
51
|
}
|
|
47
52
|
static create(name = defaultName) {
|
|
48
53
|
if (!_a.cache.has(name))
|
|
@@ -61,11 +66,12 @@ class Config {
|
|
|
61
66
|
config.#ending = other.ending ?? config.ending;
|
|
62
67
|
config.#bypass = other.bypass ?? config.bypass;
|
|
63
68
|
config.#surname = other.surname ?? config.surname;
|
|
69
|
+
config.#mono = other.mono ?? config.mono;
|
|
64
70
|
return config;
|
|
65
71
|
}
|
|
66
72
|
}
|
|
67
73
|
copyWith(options = {}) {
|
|
68
|
-
const { name, orderedBy, separator, title, ending, bypass, surname } = options;
|
|
74
|
+
const { name, orderedBy, separator, title, ending, bypass, surname, mono } = options;
|
|
69
75
|
const config = _a.create(this.#genNewName(name ?? this.name + copyAlias));
|
|
70
76
|
config.#orderedBy = orderedBy ?? this.orderedBy;
|
|
71
77
|
config.#separator = separator ?? this.separator;
|
|
@@ -73,6 +79,7 @@ class Config {
|
|
|
73
79
|
config.#ending = ending ?? this.ending;
|
|
74
80
|
config.#bypass = bypass ?? this.bypass;
|
|
75
81
|
config.#surname = surname ?? this.surname;
|
|
82
|
+
config.#mono = mono ?? this.mono;
|
|
76
83
|
return config;
|
|
77
84
|
}
|
|
78
85
|
clone() {
|
|
@@ -85,11 +92,9 @@ class Config {
|
|
|
85
92
|
this.#ending = false;
|
|
86
93
|
this.#bypass = true;
|
|
87
94
|
this.#surname = types_js_1.Surname.FATHER;
|
|
95
|
+
this.#mono = false;
|
|
88
96
|
_a.cache.set(this.name, this);
|
|
89
97
|
}
|
|
90
|
-
updateOrder(orderedBy) {
|
|
91
|
-
this.update({ orderedBy });
|
|
92
|
-
}
|
|
93
98
|
update({ orderedBy, title, ending }) {
|
|
94
99
|
const config = _a.cache.get(this.name);
|
|
95
100
|
if (!config)
|
package/dist/cjs/constants.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
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.
|
|
3
|
+
exports.ZERO_WIDTH_SPACE = exports.ALLOWED_FORMAT_TOKENS = exports.MAX_NUMBER_OF_NAME_PARTS = exports.MIN_NUMBER_OF_NAME_PARTS = exports.VERSION = void 0;
|
|
4
|
+
exports.VERSION = '2.2.0';
|
|
5
5
|
exports.MIN_NUMBER_OF_NAME_PARTS = 2;
|
|
6
6
|
exports.MAX_NUMBER_OF_NAME_PARTS = 5;
|
|
7
7
|
exports.ALLOWED_FORMAT_TOKENS = ` .,_-()[]<>'"bBfFlLmMnNoOpPsS$`;
|
|
8
|
+
exports.ZERO_WIDTH_SPACE = String.fromCharCode(8203);
|
package/dist/cjs/data.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.deserialize = void 0;
|
|
4
4
|
const builder_js_1 = require("./builder.js");
|
|
5
|
+
const types_js_1 = require("./types.js");
|
|
5
6
|
const name_js_1 = require("./name.js");
|
|
6
7
|
const error_js_1 = require("./error.js");
|
|
7
8
|
function deserialize(data) {
|
|
@@ -21,10 +22,12 @@ function deserialize(data) {
|
|
|
21
22
|
if (sx)
|
|
22
23
|
builder.add(name_js_1.Name.suffix(sx));
|
|
23
24
|
if (mn)
|
|
24
|
-
builder.add(...mn.map((n) => name_js_1.Name.middle(n)));
|
|
25
|
+
builder.add(...(typeof mn === 'string' ? [name_js_1.Name.middle(mn)] : mn.map((n) => name_js_1.Name.middle(n))));
|
|
25
26
|
builder.add(typeof fn === 'string' ? name_js_1.Name.first(fn) : new name_js_1.FirstName(fn.value, ...(fn.more ?? [])));
|
|
26
27
|
builder.add(typeof ln === 'string' ? name_js_1.Name.last(ln) : new name_js_1.LastName(ln.father, ln.mother));
|
|
27
|
-
|
|
28
|
+
const separator = types_js_1.Separator.cast(config.separator);
|
|
29
|
+
const mono = typeof config.mono === 'string' ? (types_js_1.Namon.cast(config.mono) ?? false) : config.mono;
|
|
30
|
+
return builder.build({ ...config, separator, mono });
|
|
28
31
|
}
|
|
29
32
|
catch (error) {
|
|
30
33
|
if (error instanceof error_js_1.NameError)
|
package/dist/cjs/fullname.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.FullName = void 0;
|
|
3
|
+
exports.Mononym = exports.FullName = void 0;
|
|
4
4
|
const config_js_1 = require("./config.js");
|
|
5
5
|
const validator_js_1 = require("./validator.js");
|
|
6
|
+
const constants_js_1 = require("./constants.js");
|
|
6
7
|
const types_js_1 = require("./types.js");
|
|
7
8
|
const error_js_1 = require("./error.js");
|
|
8
9
|
const name_js_1 = require("./name.js");
|
|
@@ -34,14 +35,18 @@ class FullName {
|
|
|
34
35
|
get suffix() {
|
|
35
36
|
return this.#suffix;
|
|
36
37
|
}
|
|
38
|
+
get isMono() {
|
|
39
|
+
return this instanceof Mononym;
|
|
40
|
+
}
|
|
37
41
|
static parse(json, config) {
|
|
38
42
|
try {
|
|
43
|
+
const { prefix, firstName: fn, middleName: mn, lastName: ln, suffix } = json;
|
|
39
44
|
return new FullName(config)
|
|
40
|
-
.setPrefix(
|
|
41
|
-
.setFirstName(
|
|
42
|
-
.setMiddleName(
|
|
43
|
-
.setLastName(
|
|
44
|
-
.setSuffix(
|
|
45
|
+
.setPrefix(prefix)
|
|
46
|
+
.setFirstName(typeof fn === 'string' ? fn : new name_js_1.FirstName(fn.value, ...(fn.more ?? [])))
|
|
47
|
+
.setMiddleName(typeof mn === 'string' ? [mn] : (mn ?? []))
|
|
48
|
+
.setLastName(typeof ln === 'string' ? ln : new name_js_1.LastName(ln.father, ln.mother))
|
|
49
|
+
.setSuffix(suffix);
|
|
45
50
|
}
|
|
46
51
|
catch (error) {
|
|
47
52
|
if (error instanceof error_js_1.NameError)
|
|
@@ -59,7 +64,7 @@ class FullName {
|
|
|
59
64
|
if (!this.#config.bypass)
|
|
60
65
|
validator_js_1.Validators.prefix.validate(name);
|
|
61
66
|
const prefix = name instanceof name_js_1.Name ? name.value : name;
|
|
62
|
-
this.#prefix = name_js_1.Name.prefix(this.#config.title === types_js_1.Title.US ? `${prefix}.` : prefix);
|
|
67
|
+
this.#prefix = name_js_1.Name.prefix(this.#config.title === types_js_1.Title.US && !prefix.endsWith('.') ? `${prefix}.` : prefix);
|
|
63
68
|
return this;
|
|
64
69
|
}
|
|
65
70
|
setFirstName(name) {
|
|
@@ -100,6 +105,11 @@ class FullName {
|
|
|
100
105
|
return !!this.#suffix;
|
|
101
106
|
return namon.equal(types_js_1.Namon.MIDDLE_NAME) ? this.#middleName.length > 0 : true;
|
|
102
107
|
}
|
|
108
|
+
toString() {
|
|
109
|
+
if (this.isMono)
|
|
110
|
+
return this.value;
|
|
111
|
+
return Array.from(this.toIterable(true)).join(' ');
|
|
112
|
+
}
|
|
103
113
|
*toIterable(flat = false) {
|
|
104
114
|
if (this.#prefix)
|
|
105
115
|
yield this.#prefix;
|
|
@@ -121,3 +131,38 @@ class FullName {
|
|
|
121
131
|
}
|
|
122
132
|
}
|
|
123
133
|
exports.FullName = FullName;
|
|
134
|
+
class Mononym extends FullName {
|
|
135
|
+
#namon;
|
|
136
|
+
#type;
|
|
137
|
+
constructor(name, options) {
|
|
138
|
+
super(options ?? { name: 'mononym', mono: true });
|
|
139
|
+
this.#namon = name.toString();
|
|
140
|
+
this.type = name instanceof name_js_1.Name ? name.type : types_js_1.Namon.FIRST_NAME;
|
|
141
|
+
}
|
|
142
|
+
set type(type) {
|
|
143
|
+
this.#type = typeof type === 'string' ? (types_js_1.Namon.cast(type) ?? types_js_1.Namon.FIRST_NAME) : type;
|
|
144
|
+
this.#build(this.#namon);
|
|
145
|
+
}
|
|
146
|
+
get type() {
|
|
147
|
+
return this.#type;
|
|
148
|
+
}
|
|
149
|
+
get value() {
|
|
150
|
+
return this.#namon;
|
|
151
|
+
}
|
|
152
|
+
#build(name) {
|
|
153
|
+
this.setFirstName(constants_js_1.ZERO_WIDTH_SPACE).setLastName(constants_js_1.ZERO_WIDTH_SPACE).setMiddleName([]).setPrefix(null).setSuffix(null);
|
|
154
|
+
if (this.#type.equal(types_js_1.Namon.FIRST_NAME))
|
|
155
|
+
this.setFirstName(name);
|
|
156
|
+
else if (this.#type.equal(types_js_1.Namon.LAST_NAME))
|
|
157
|
+
this.setLastName(name);
|
|
158
|
+
else if (this.#type.equal(types_js_1.Namon.MIDDLE_NAME))
|
|
159
|
+
this.setMiddleName([name]);
|
|
160
|
+
else if (this.#type.equal(types_js_1.Namon.PREFIX))
|
|
161
|
+
this.setPrefix(name);
|
|
162
|
+
else if (this.#type.equal(types_js_1.Namon.SUFFIX))
|
|
163
|
+
this.setSuffix(name);
|
|
164
|
+
else
|
|
165
|
+
throw new error_js_1.NameError(name, 'invalid mononym type');
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
exports.Mononym = Mononym;
|
package/dist/cjs/namefully.js
CHANGED
|
@@ -26,7 +26,12 @@ class Namefully {
|
|
|
26
26
|
get config() {
|
|
27
27
|
return this.#fullName.config;
|
|
28
28
|
}
|
|
29
|
+
get isMono() {
|
|
30
|
+
return this.#fullName.isMono;
|
|
31
|
+
}
|
|
29
32
|
get length() {
|
|
33
|
+
if (this.isMono)
|
|
34
|
+
return this.#fullName.toString().length;
|
|
30
35
|
return this.birth.length;
|
|
31
36
|
}
|
|
32
37
|
get prefix() {
|
|
@@ -116,6 +121,8 @@ class Namefully {
|
|
|
116
121
|
return this.#fullName.has(namon);
|
|
117
122
|
}
|
|
118
123
|
fullName(orderedBy) {
|
|
124
|
+
if (this.isMono)
|
|
125
|
+
return this.#fullName.toString();
|
|
119
126
|
const sep = this.config.ending ? ',' : '';
|
|
120
127
|
const names = [];
|
|
121
128
|
if (this.prefix)
|
|
@@ -124,13 +131,21 @@ class Namefully {
|
|
|
124
131
|
names.push(this.first, ...this.middleName(), this.last + sep);
|
|
125
132
|
}
|
|
126
133
|
else {
|
|
127
|
-
names.push(this.last
|
|
134
|
+
names.push(this.last);
|
|
135
|
+
if (this.hasMiddle) {
|
|
136
|
+
names.push(this.first, this.middleName().join(' ') + sep);
|
|
137
|
+
}
|
|
138
|
+
else {
|
|
139
|
+
names.push(this.first + sep);
|
|
140
|
+
}
|
|
128
141
|
}
|
|
129
142
|
if (this.suffix)
|
|
130
143
|
names.push(this.suffix);
|
|
131
144
|
return names.join(' ').trim();
|
|
132
145
|
}
|
|
133
146
|
birthName(orderedBy) {
|
|
147
|
+
if (this.isMono)
|
|
148
|
+
return this.#fullName.toString();
|
|
134
149
|
orderedBy ??= this.config.orderedBy;
|
|
135
150
|
return orderedBy === types_js_1.NameOrder.FIRST_NAME
|
|
136
151
|
? [this.first, ...this.middleName(), this.last].join(' ')
|
|
@@ -146,6 +161,8 @@ class Namefully {
|
|
|
146
161
|
return this.#fullName.lastName.toString(format);
|
|
147
162
|
}
|
|
148
163
|
initials(options) {
|
|
164
|
+
if (this.isMono)
|
|
165
|
+
return [this.#fullName.toString()[0]];
|
|
149
166
|
const { orderedBy = this.config.orderedBy, only = types_js_1.NameType.BIRTH_NAME, asJson } = options ?? {};
|
|
150
167
|
const firstInits = this.#fullName.firstName.initials();
|
|
151
168
|
const midInits = this.#fullName.middleName.map((n) => n.value[0]);
|
|
@@ -163,6 +180,8 @@ class Namefully {
|
|
|
163
180
|
}
|
|
164
181
|
}
|
|
165
182
|
shorten(orderedBy) {
|
|
183
|
+
if (this.isMono)
|
|
184
|
+
return this.#fullName.toString();
|
|
166
185
|
orderedBy ??= this.config.orderedBy;
|
|
167
186
|
const { firstName, lastName } = this.#fullName;
|
|
168
187
|
return orderedBy === types_js_1.NameOrder.FIRST_NAME
|
|
@@ -173,6 +192,8 @@ class Namefully {
|
|
|
173
192
|
const { by = types_js_1.Flat.MIDDLE_NAME, limit = 20, recursive = false, withMore = false, withPeriod = true, surname, } = options;
|
|
174
193
|
if (this.length <= limit)
|
|
175
194
|
return this.full;
|
|
195
|
+
if (this.isMono)
|
|
196
|
+
return `${this.initials()}${withPeriod ? '.' : ''}`;
|
|
176
197
|
const { firstName, lastName, middleName } = this.#fullName;
|
|
177
198
|
const sep = withPeriod ? '.' : '';
|
|
178
199
|
const hasMid = this.hasMiddle;
|
|
@@ -320,6 +341,28 @@ class Namefully {
|
|
|
320
341
|
toToggleCase() {
|
|
321
342
|
return (0, utils_js_1.toggleCase)(this.birth);
|
|
322
343
|
}
|
|
344
|
+
serialize() {
|
|
345
|
+
const { config, firstName: fn, lastName: ln } = this.#fullName;
|
|
346
|
+
return {
|
|
347
|
+
names: {
|
|
348
|
+
prefix: this.prefix,
|
|
349
|
+
firstName: fn.hasMore ? { value: fn.value, more: fn.more } : fn.value,
|
|
350
|
+
middleName: this.hasMiddle ? this.middleName() : undefined,
|
|
351
|
+
lastName: ln.hasMother ? { father: ln.father, mother: ln.mother } : ln.value,
|
|
352
|
+
suffix: this.suffix,
|
|
353
|
+
},
|
|
354
|
+
config: {
|
|
355
|
+
name: config.name,
|
|
356
|
+
orderedBy: config.orderedBy,
|
|
357
|
+
separator: config.separator.token,
|
|
358
|
+
title: config.title,
|
|
359
|
+
ending: config.ending,
|
|
360
|
+
bypass: config.bypass,
|
|
361
|
+
surname: config.surname,
|
|
362
|
+
mono: config.mono instanceof types_js_1.Namon ? config.mono.key : config.mono,
|
|
363
|
+
},
|
|
364
|
+
};
|
|
365
|
+
}
|
|
323
366
|
#toParser(raw) {
|
|
324
367
|
if (raw instanceof parser_js_1.Parser)
|
|
325
368
|
return raw;
|
|
@@ -391,27 +434,6 @@ class Namefully {
|
|
|
391
434
|
return constants_js_1.ALLOWED_FORMAT_TOKENS.includes(char) ? char : undefined;
|
|
392
435
|
}
|
|
393
436
|
}
|
|
394
|
-
serialize() {
|
|
395
|
-
const { config, firstName: fn, lastName: ln } = this.#fullName;
|
|
396
|
-
return {
|
|
397
|
-
names: {
|
|
398
|
-
prefix: this.prefix,
|
|
399
|
-
firstName: fn.hasMore ? { value: fn.value, more: fn.more } : fn.value,
|
|
400
|
-
middleName: this.hasMiddle ? this.middleName() : undefined,
|
|
401
|
-
lastName: ln.hasMother ? { father: ln.father, mother: ln.mother } : ln.value,
|
|
402
|
-
suffix: this.suffix,
|
|
403
|
-
},
|
|
404
|
-
config: {
|
|
405
|
-
name: config.name,
|
|
406
|
-
orderedBy: config.orderedBy,
|
|
407
|
-
separator: config.separator.token,
|
|
408
|
-
title: config.title,
|
|
409
|
-
ending: config.ending,
|
|
410
|
-
bypass: config.bypass,
|
|
411
|
-
surname: config.surname,
|
|
412
|
-
},
|
|
413
|
-
};
|
|
414
|
-
}
|
|
415
437
|
}
|
|
416
438
|
exports.Namefully = Namefully;
|
|
417
439
|
exports.default = (names, options) => {
|
package/dist/cjs/parser.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ArrayNameParser = exports.NamaParser = exports.ArrayStringParser = exports.StringParser = exports.Parser = void 0;
|
|
3
|
+
exports.MonoParser = exports.ArrayNameParser = exports.NamaParser = exports.ArrayStringParser = exports.StringParser = exports.Parser = void 0;
|
|
4
4
|
const config_js_1 = require("./config.js");
|
|
5
5
|
const utils_js_1 = require("./utils.js");
|
|
6
6
|
const error_js_1 = require("./error.js");
|
|
@@ -23,10 +23,7 @@ class Parser {
|
|
|
23
23
|
return new ArrayNameParser(names);
|
|
24
24
|
}
|
|
25
25
|
if (length < 2) {
|
|
26
|
-
throw new error_js_1.InputError({
|
|
27
|
-
source: text,
|
|
28
|
-
message: 'cannot build from invalid input',
|
|
29
|
-
});
|
|
26
|
+
throw new error_js_1.InputError({ source: text, message: 'expecting at least 2 name parts' });
|
|
30
27
|
}
|
|
31
28
|
else if (length === 2 || length === 3) {
|
|
32
29
|
return new StringParser(text);
|
|
@@ -51,14 +48,16 @@ class StringParser extends Parser {
|
|
|
51
48
|
parse(options) {
|
|
52
49
|
const config = config_js_1.Config.merge(options);
|
|
53
50
|
const names = this.raw.split(config.separator.token);
|
|
54
|
-
return new ArrayStringParser(names).parse(
|
|
51
|
+
return new ArrayStringParser(names).parse(config);
|
|
55
52
|
}
|
|
56
53
|
}
|
|
57
54
|
exports.StringParser = StringParser;
|
|
58
55
|
class ArrayStringParser extends Parser {
|
|
59
56
|
parse(options) {
|
|
60
57
|
const config = config_js_1.Config.merge(options);
|
|
61
|
-
|
|
58
|
+
if (this.raw.length === 1 && config.mono) {
|
|
59
|
+
return new MonoParser(this.raw[0]).parse(config);
|
|
60
|
+
}
|
|
62
61
|
const raw = this.raw.map((n) => n.trim());
|
|
63
62
|
const index = utils_js_1.NameIndex.when(config.orderedBy, raw.length);
|
|
64
63
|
const validator = new validator_js_1.ArrayStringValidator(index);
|
|
@@ -69,8 +68,9 @@ class ArrayStringParser extends Parser {
|
|
|
69
68
|
validator.validate(raw);
|
|
70
69
|
}
|
|
71
70
|
const { firstName, lastName, middleName, prefix, suffix } = index;
|
|
72
|
-
fullName
|
|
73
|
-
|
|
71
|
+
const fullName = new fullname_js_1.FullName(config)
|
|
72
|
+
.setFirstName(new name_js_1.FirstName(raw[firstName]))
|
|
73
|
+
.setLastName(new name_js_1.LastName(raw[lastName]));
|
|
74
74
|
if (raw.length >= 3)
|
|
75
75
|
fullName.setMiddleName(raw[middleName].split(config.separator.token));
|
|
76
76
|
if (raw.length >= 4)
|
|
@@ -95,10 +95,10 @@ class NamaParser extends Parser {
|
|
|
95
95
|
return [namon, value];
|
|
96
96
|
}));
|
|
97
97
|
if (config.bypass) {
|
|
98
|
-
validator_js_1.
|
|
98
|
+
validator_js_1.Validators.nama.validateKeys(names);
|
|
99
99
|
}
|
|
100
100
|
else {
|
|
101
|
-
validator_js_1.
|
|
101
|
+
validator_js_1.Validators.nama.validate(names);
|
|
102
102
|
}
|
|
103
103
|
return fullname_js_1.FullName.parse(this.raw, config);
|
|
104
104
|
}
|
|
@@ -107,8 +107,13 @@ exports.NamaParser = NamaParser;
|
|
|
107
107
|
class ArrayNameParser extends Parser {
|
|
108
108
|
parse(options) {
|
|
109
109
|
const config = config_js_1.Config.merge(options);
|
|
110
|
+
if (this.raw.length === 1 && config.mono) {
|
|
111
|
+
return new MonoParser(this.raw[0]).parse(options);
|
|
112
|
+
}
|
|
113
|
+
else {
|
|
114
|
+
validator_js_1.ArrayNameValidator.create().validate(this.raw);
|
|
115
|
+
}
|
|
110
116
|
const fullName = new fullname_js_1.FullName(config);
|
|
111
|
-
validator_js_1.ArrayNameValidator.create().validate(this.raw);
|
|
112
117
|
for (const name of this.raw) {
|
|
113
118
|
if (name.isPrefix) {
|
|
114
119
|
fullName.setPrefix(name);
|
|
@@ -131,3 +136,14 @@ class ArrayNameParser extends Parser {
|
|
|
131
136
|
}
|
|
132
137
|
}
|
|
133
138
|
exports.ArrayNameParser = ArrayNameParser;
|
|
139
|
+
class MonoParser extends Parser {
|
|
140
|
+
parse(options) {
|
|
141
|
+
const config = config_js_1.Config.merge(options);
|
|
142
|
+
if (config.bypass)
|
|
143
|
+
validator_js_1.Validators.namon.validate(this.raw);
|
|
144
|
+
const type = config.mono instanceof types_js_1.Namon ? config.mono : types_js_1.Namon.FIRST_NAME;
|
|
145
|
+
const name = this.raw instanceof name_js_1.Name ? this.raw : new name_js_1.Name(this.raw.trim(), type);
|
|
146
|
+
return new fullname_js_1.Mononym(name, config);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
exports.MonoParser = MonoParser;
|
package/dist/esm/builder.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Config } from './config.js';
|
|
1
2
|
import { ArrayNameValidator } from './validator.js';
|
|
2
3
|
import { Namefully } from './namefully.js';
|
|
3
4
|
class Builder {
|
|
@@ -70,8 +71,10 @@ export class NameBuilder extends Builder {
|
|
|
70
71
|
build(options) {
|
|
71
72
|
this.prebuild?.();
|
|
72
73
|
const names = [...this.queue];
|
|
73
|
-
|
|
74
|
-
|
|
74
|
+
const config = Config.merge(options);
|
|
75
|
+
if (!config.mono)
|
|
76
|
+
ArrayNameValidator.create().validate(names);
|
|
77
|
+
this.instance = new Namefully(names, config);
|
|
75
78
|
this.postbuild?.(this.instance);
|
|
76
79
|
return this.instance;
|
|
77
80
|
}
|
package/dist/esm/config.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { NameOrder, Separator, Title, Surname } from './types.js';
|
|
1
|
+
import { NameOrder, Namon, Separator, Title, Surname } from './types.js';
|
|
2
2
|
/**
|
|
3
3
|
* The Configuration to use across the other components.
|
|
4
4
|
*
|
|
@@ -61,6 +61,8 @@ export declare class Config {
|
|
|
61
61
|
* prioritized and viewed as the source of truth for future considerations.
|
|
62
62
|
*/
|
|
63
63
|
get surname(): Surname;
|
|
64
|
+
/** Whether to parse a single word name as a mononym. */
|
|
65
|
+
get mono(): boolean | Namon;
|
|
64
66
|
/** The name of the cached configuration. */
|
|
65
67
|
get name(): string;
|
|
66
68
|
private constructor();
|
|
@@ -89,14 +91,6 @@ export declare class Config {
|
|
|
89
91
|
clone(): Config;
|
|
90
92
|
/** Resets the configuration by setting it back to its default values. */
|
|
91
93
|
reset(): void;
|
|
92
|
-
/**
|
|
93
|
-
* Alters the name order between the first and last name, and rearrange the
|
|
94
|
-
* order of appearance of a name set.
|
|
95
|
-
* @deprecated use `update()` method instead.
|
|
96
|
-
*/
|
|
97
|
-
updateOrder(orderedBy: NameOrder): void;
|
|
98
|
-
/**
|
|
99
|
-
* Allows the possibility to alter some options after creating a name set.
|
|
100
|
-
*/
|
|
94
|
+
/** Allows the possibility to alter behavior-related options after creating a name set. */
|
|
101
95
|
update({ orderedBy, title, ending }: Partial<Pick<Config, 'orderedBy' | 'title' | 'ending'>>): void;
|
|
102
96
|
}
|
package/dist/esm/config.js
CHANGED
|
@@ -10,6 +10,7 @@ export class Config {
|
|
|
10
10
|
#ending;
|
|
11
11
|
#bypass;
|
|
12
12
|
#surname;
|
|
13
|
+
#mono;
|
|
13
14
|
static cache = new Map();
|
|
14
15
|
get orderedBy() {
|
|
15
16
|
return this.#orderedBy;
|
|
@@ -29,10 +30,13 @@ export class Config {
|
|
|
29
30
|
get surname() {
|
|
30
31
|
return this.#surname;
|
|
31
32
|
}
|
|
33
|
+
get mono() {
|
|
34
|
+
return this.#mono;
|
|
35
|
+
}
|
|
32
36
|
get name() {
|
|
33
37
|
return this.#name;
|
|
34
38
|
}
|
|
35
|
-
constructor(name, orderedBy = NameOrder.FIRST_NAME, separator = Separator.SPACE, title = Title.UK, ending = false, bypass = true, surname = Surname.FATHER) {
|
|
39
|
+
constructor(name, orderedBy = NameOrder.FIRST_NAME, separator = Separator.SPACE, title = Title.UK, ending = false, bypass = true, surname = Surname.FATHER, mono = false) {
|
|
36
40
|
this.#name = name;
|
|
37
41
|
this.#orderedBy = orderedBy;
|
|
38
42
|
this.#separator = separator;
|
|
@@ -40,6 +44,7 @@ export class Config {
|
|
|
40
44
|
this.#ending = ending;
|
|
41
45
|
this.#bypass = bypass;
|
|
42
46
|
this.#surname = surname;
|
|
47
|
+
this.#mono = mono;
|
|
43
48
|
}
|
|
44
49
|
static create(name = defaultName) {
|
|
45
50
|
if (!_a.cache.has(name))
|
|
@@ -58,11 +63,12 @@ export class Config {
|
|
|
58
63
|
config.#ending = other.ending ?? config.ending;
|
|
59
64
|
config.#bypass = other.bypass ?? config.bypass;
|
|
60
65
|
config.#surname = other.surname ?? config.surname;
|
|
66
|
+
config.#mono = other.mono ?? config.mono;
|
|
61
67
|
return config;
|
|
62
68
|
}
|
|
63
69
|
}
|
|
64
70
|
copyWith(options = {}) {
|
|
65
|
-
const { name, orderedBy, separator, title, ending, bypass, surname } = options;
|
|
71
|
+
const { name, orderedBy, separator, title, ending, bypass, surname, mono } = options;
|
|
66
72
|
const config = _a.create(this.#genNewName(name ?? this.name + copyAlias));
|
|
67
73
|
config.#orderedBy = orderedBy ?? this.orderedBy;
|
|
68
74
|
config.#separator = separator ?? this.separator;
|
|
@@ -70,6 +76,7 @@ export class Config {
|
|
|
70
76
|
config.#ending = ending ?? this.ending;
|
|
71
77
|
config.#bypass = bypass ?? this.bypass;
|
|
72
78
|
config.#surname = surname ?? this.surname;
|
|
79
|
+
config.#mono = mono ?? this.mono;
|
|
73
80
|
return config;
|
|
74
81
|
}
|
|
75
82
|
clone() {
|
|
@@ -82,11 +89,9 @@ export class Config {
|
|
|
82
89
|
this.#ending = false;
|
|
83
90
|
this.#bypass = true;
|
|
84
91
|
this.#surname = Surname.FATHER;
|
|
92
|
+
this.#mono = false;
|
|
85
93
|
_a.cache.set(this.name, this);
|
|
86
94
|
}
|
|
87
|
-
updateOrder(orderedBy) {
|
|
88
|
-
this.update({ orderedBy });
|
|
89
|
-
}
|
|
90
95
|
update({ orderedBy, title, ending }) {
|
|
91
96
|
const config = _a.cache.get(this.name);
|
|
92
97
|
if (!config)
|
package/dist/esm/constants.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
export declare const VERSION = "2.
|
|
1
|
+
export declare const VERSION = "2.2.0";
|
|
2
2
|
export declare const MIN_NUMBER_OF_NAME_PARTS = 2;
|
|
3
3
|
export declare const MAX_NUMBER_OF_NAME_PARTS = 5;
|
|
4
4
|
export declare const ALLOWED_FORMAT_TOKENS = " .,_-()[]<>'\"bBfFlLmMnNoOpPsS$";
|
|
5
|
+
export declare const ZERO_WIDTH_SPACE: string;
|
package/dist/esm/constants.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
export const VERSION = '2.
|
|
1
|
+
export const VERSION = '2.2.0';
|
|
2
2
|
export const MIN_NUMBER_OF_NAME_PARTS = 2;
|
|
3
3
|
export const MAX_NUMBER_OF_NAME_PARTS = 5;
|
|
4
4
|
export const ALLOWED_FORMAT_TOKENS = ` .,_-()[]<>'"bBfFlLmMnNoOpPsS$`;
|
|
5
|
+
export const ZERO_WIDTH_SPACE = String.fromCharCode(8203);
|
package/dist/esm/data.d.ts
CHANGED
|
@@ -1,20 +1,9 @@
|
|
|
1
|
+
import { JsonName } from './name.js';
|
|
1
2
|
import { type Namefully } from './namefully.js';
|
|
2
3
|
/** Serialized representation of a Namefully instance. */
|
|
3
4
|
export interface SerializedName {
|
|
4
5
|
/** The name data (with its hierarchy intact). */
|
|
5
|
-
names:
|
|
6
|
-
prefix?: string;
|
|
7
|
-
firstName: string | {
|
|
8
|
-
value: string;
|
|
9
|
-
more?: string[];
|
|
10
|
-
};
|
|
11
|
-
middleName?: string[];
|
|
12
|
-
lastName: string | {
|
|
13
|
-
father: string;
|
|
14
|
-
mother?: string;
|
|
15
|
-
};
|
|
16
|
-
suffix?: string;
|
|
17
|
-
};
|
|
6
|
+
names: JsonName;
|
|
18
7
|
/** The configuration data. */
|
|
19
8
|
config: {
|
|
20
9
|
name: string;
|
|
@@ -24,6 +13,7 @@ export interface SerializedName {
|
|
|
24
13
|
ending: boolean;
|
|
25
14
|
bypass: boolean;
|
|
26
15
|
surname: string;
|
|
16
|
+
mono: boolean | string;
|
|
27
17
|
};
|
|
28
18
|
}
|
|
29
19
|
/**
|
package/dist/esm/data.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { NameBuilder } from './builder.js';
|
|
2
|
+
import { Namon, Separator } from './types.js';
|
|
2
3
|
import { Name, FirstName, LastName } from './name.js';
|
|
3
4
|
import { InputError, NameError, UnknownError } from './error.js';
|
|
4
5
|
export function deserialize(data) {
|
|
@@ -18,10 +19,12 @@ export function deserialize(data) {
|
|
|
18
19
|
if (sx)
|
|
19
20
|
builder.add(Name.suffix(sx));
|
|
20
21
|
if (mn)
|
|
21
|
-
builder.add(...mn.map((n) => Name.middle(n)));
|
|
22
|
+
builder.add(...(typeof mn === 'string' ? [Name.middle(mn)] : mn.map((n) => Name.middle(n))));
|
|
22
23
|
builder.add(typeof fn === 'string' ? Name.first(fn) : new FirstName(fn.value, ...(fn.more ?? [])));
|
|
23
24
|
builder.add(typeof ln === 'string' ? Name.last(ln) : new LastName(ln.father, ln.mother));
|
|
24
|
-
|
|
25
|
+
const separator = Separator.cast(config.separator);
|
|
26
|
+
const mono = typeof config.mono === 'string' ? (Namon.cast(config.mono) ?? false) : config.mono;
|
|
27
|
+
return builder.build({ ...config, separator, mono });
|
|
25
28
|
}
|
|
26
29
|
catch (error) {
|
|
27
30
|
if (error instanceof NameError)
|
package/dist/esm/fullname.d.ts
CHANGED
|
@@ -36,6 +36,8 @@ export declare class FullName {
|
|
|
36
36
|
get middleName(): Name[];
|
|
37
37
|
/** The suffix part of the full name. */
|
|
38
38
|
get suffix(): Nullable<Name>;
|
|
39
|
+
/** Whether the full name is a single word name. */
|
|
40
|
+
get isMono(): boolean;
|
|
39
41
|
/**
|
|
40
42
|
* Parses a JSON name into a full name.
|
|
41
43
|
* @param {JsonName} json parsable name element
|
|
@@ -49,8 +51,39 @@ export declare class FullName {
|
|
|
49
51
|
setSuffix(name: Nullable<string | Name>): FullName;
|
|
50
52
|
/** Returns true if a namon has been set. */
|
|
51
53
|
has(key: Namon | string): boolean;
|
|
54
|
+
toString(): string;
|
|
52
55
|
/** Returns an `Iterable` of existing `Name`s. */
|
|
53
56
|
toIterable(flat?: boolean): Iterable<Name>;
|
|
54
57
|
/** Returns the default iterator for this name set (enabling for-of statements). */
|
|
55
58
|
[Symbol.iterator](): Iterator<Name>;
|
|
56
59
|
}
|
|
60
|
+
/**
|
|
61
|
+
* A single word name or mononym.
|
|
62
|
+
*
|
|
63
|
+
* This is a special case of `FullName` that is used to represent mononyms. This contradicts
|
|
64
|
+
* the original purpose of this library such as shaping and organizing name pieces accordingly.
|
|
65
|
+
*
|
|
66
|
+
* When enabled via `Config.mono`, this becomes the full name of a human. And as a single name,
|
|
67
|
+
* most of the `Namefully` methods become irrelevant.
|
|
68
|
+
*/
|
|
69
|
+
export declare class Mononym extends FullName {
|
|
70
|
+
#private;
|
|
71
|
+
/**
|
|
72
|
+
* Constructs a mononym from a piece of string.
|
|
73
|
+
* @param {string | Name} name to be used to construct the mononym.
|
|
74
|
+
*/
|
|
75
|
+
constructor(name: string | Name, options?: Partial<Config>);
|
|
76
|
+
/**
|
|
77
|
+
* Re-assigns which name type is being used to represent the mononym.
|
|
78
|
+
*
|
|
79
|
+
* Ideally, this doesn't really matter as the mononym is always a single piece of name.
|
|
80
|
+
* When used as `string`, it must be a valid `Namon` type or else it will default to
|
|
81
|
+
* `Namon.FIRST_NAME`.
|
|
82
|
+
* @param {string | Namon} type of name to use.
|
|
83
|
+
*/
|
|
84
|
+
set type(type: string | Namon);
|
|
85
|
+
/** The type of name being used to represent the mononym. */
|
|
86
|
+
get type(): Namon;
|
|
87
|
+
/** The piece of string treated as a name. */
|
|
88
|
+
get value(): string;
|
|
89
|
+
}
|