namefully 2.0.2 → 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 +6 -3
- package/dist/cjs/config.js +10 -5
- package/dist/cjs/constants.js +4 -26
- package/dist/cjs/data.js +42 -0
- package/dist/cjs/fullname.js +75 -8
- package/dist/cjs/index.js +1 -0
- package/dist/cjs/name.js +1 -1
- package/dist/cjs/namefully.js +80 -22
- package/dist/cjs/parser.js +30 -14
- package/dist/cjs/types.js +18 -1
- package/dist/esm/builder.d.ts +2 -2
- package/dist/esm/builder.js +6 -3
- package/dist/esm/config.d.ts +4 -10
- package/dist/esm/config.js +10 -5
- package/dist/esm/constants.d.ts +3 -2
- package/dist/esm/constants.js +3 -25
- package/dist/esm/data.d.ts +31 -0
- package/dist/esm/data.js +38 -0
- package/dist/esm/fullname.d.ts +38 -1
- package/dist/esm/fullname.js +73 -7
- package/dist/esm/index.d.ts +1 -0
- package/dist/esm/index.js +1 -0
- package/dist/esm/name.d.ts +13 -7
- package/dist/esm/name.js +1 -1
- package/dist/esm/namefully.d.ts +64 -26
- package/dist/esm/namefully.js +80 -22
- package/dist/esm/parser.d.ts +9 -6
- package/dist/esm/parser.js +30 -15
- package/dist/esm/types.d.ts +3 -0
- package/dist/esm/types.js +18 -1
- package/dist/namefully.js +251 -74
- package/dist/namefully.min.js +1 -1
- package/package.json +2 -2
- package/readme.md +28 -4
package/dist/cjs/builder.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.NameBuilder = void 0;
|
|
4
|
-
const
|
|
4
|
+
const config_js_1 = require("./config.js");
|
|
5
5
|
const validator_js_1 = require("./validator.js");
|
|
6
|
+
const namefully_js_1 = require("./namefully.js");
|
|
6
7
|
class Builder {
|
|
7
8
|
prebuild;
|
|
8
9
|
postbuild;
|
|
@@ -70,10 +71,12 @@ class NameBuilder extends Builder {
|
|
|
70
71
|
static use({ names, prebuild, postbuild, preclear, postclear, }) {
|
|
71
72
|
return new NameBuilder(names ?? [], prebuild, postbuild, preclear, postclear);
|
|
72
73
|
}
|
|
73
|
-
build(
|
|
74
|
+
build(options) {
|
|
74
75
|
this.prebuild?.();
|
|
75
76
|
const names = [...this.queue];
|
|
76
|
-
|
|
77
|
+
const config = config_js_1.Config.merge(options);
|
|
78
|
+
if (!config.mono)
|
|
79
|
+
validator_js_1.ArrayNameValidator.create().validate(names);
|
|
77
80
|
this.instance = new namefully_js_1.Namefully(names, config);
|
|
78
81
|
this.postbuild?.(this.instance);
|
|
79
82
|
return this.instance;
|
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,30 +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.0
|
|
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
|
-
exports.ALLOWED_FORMAT_TOKENS = [
|
|
8
|
-
|
|
9
|
-
',',
|
|
10
|
-
' ',
|
|
11
|
-
'-',
|
|
12
|
-
'_',
|
|
13
|
-
'b',
|
|
14
|
-
'B',
|
|
15
|
-
'f',
|
|
16
|
-
'F',
|
|
17
|
-
'l',
|
|
18
|
-
'L',
|
|
19
|
-
'm',
|
|
20
|
-
'M',
|
|
21
|
-
'n',
|
|
22
|
-
'N',
|
|
23
|
-
'o',
|
|
24
|
-
'O',
|
|
25
|
-
'p',
|
|
26
|
-
'P',
|
|
27
|
-
's',
|
|
28
|
-
'S',
|
|
29
|
-
'$',
|
|
30
|
-
];
|
|
7
|
+
exports.ALLOWED_FORMAT_TOKENS = ` .,_-()[]<>'"bBfFlLmMnNoOpPsS$`;
|
|
8
|
+
exports.ZERO_WIDTH_SPACE = String.fromCharCode(8203);
|
package/dist/cjs/data.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.deserialize = void 0;
|
|
4
|
+
const builder_js_1 = require("./builder.js");
|
|
5
|
+
const types_js_1 = require("./types.js");
|
|
6
|
+
const name_js_1 = require("./name.js");
|
|
7
|
+
const error_js_1 = require("./error.js");
|
|
8
|
+
function deserialize(data) {
|
|
9
|
+
try {
|
|
10
|
+
const parsed = typeof data === 'string' ? JSON.parse(data) : data;
|
|
11
|
+
if (!parsed || typeof parsed !== 'object') {
|
|
12
|
+
throw new error_js_1.InputError({
|
|
13
|
+
source: String(data),
|
|
14
|
+
message: 'invalid serialized data; must be an object or a string',
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
const { names, config } = parsed;
|
|
18
|
+
const { firstName: fn, lastName: ln, middleName: mn, prefix: px, suffix: sx } = names;
|
|
19
|
+
const builder = builder_js_1.NameBuilder.of();
|
|
20
|
+
if (px)
|
|
21
|
+
builder.add(name_js_1.Name.prefix(px));
|
|
22
|
+
if (sx)
|
|
23
|
+
builder.add(name_js_1.Name.suffix(sx));
|
|
24
|
+
if (mn)
|
|
25
|
+
builder.add(...(typeof mn === 'string' ? [name_js_1.Name.middle(mn)] : mn.map((n) => name_js_1.Name.middle(n))));
|
|
26
|
+
builder.add(typeof fn === 'string' ? name_js_1.Name.first(fn) : new name_js_1.FirstName(fn.value, ...(fn.more ?? [])));
|
|
27
|
+
builder.add(typeof ln === 'string' ? name_js_1.Name.last(ln) : new name_js_1.LastName(ln.father, ln.mother));
|
|
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 });
|
|
31
|
+
}
|
|
32
|
+
catch (error) {
|
|
33
|
+
if (error instanceof error_js_1.NameError)
|
|
34
|
+
throw error;
|
|
35
|
+
throw new error_js_1.UnknownError({
|
|
36
|
+
source: String(data),
|
|
37
|
+
message: 'could not deserialize data',
|
|
38
|
+
origin: error instanceof Error ? error : new Error(String(error)),
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
exports.deserialize = deserialize;
|
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) {
|
|
@@ -90,12 +95,74 @@ class FullName {
|
|
|
90
95
|
this.#suffix = name_js_1.Name.suffix(name instanceof name_js_1.Name ? name.value : name);
|
|
91
96
|
return this;
|
|
92
97
|
}
|
|
93
|
-
has(
|
|
98
|
+
has(key) {
|
|
99
|
+
const namon = typeof key === 'string' ? types_js_1.Namon.cast(key) : key;
|
|
100
|
+
if (!namon)
|
|
101
|
+
return false;
|
|
94
102
|
if (namon.equal(types_js_1.Namon.PREFIX))
|
|
95
103
|
return !!this.#prefix;
|
|
96
104
|
if (namon.equal(types_js_1.Namon.SUFFIX))
|
|
97
105
|
return !!this.#suffix;
|
|
98
106
|
return namon.equal(types_js_1.Namon.MIDDLE_NAME) ? this.#middleName.length > 0 : true;
|
|
99
107
|
}
|
|
108
|
+
toString() {
|
|
109
|
+
if (this.isMono)
|
|
110
|
+
return this.value;
|
|
111
|
+
return Array.from(this.toIterable(true)).join(' ');
|
|
112
|
+
}
|
|
113
|
+
*toIterable(flat = false) {
|
|
114
|
+
if (this.#prefix)
|
|
115
|
+
yield this.#prefix;
|
|
116
|
+
if (flat) {
|
|
117
|
+
yield* this.#firstName.asNames;
|
|
118
|
+
yield* this.#middleName;
|
|
119
|
+
yield* this.#lastName.asNames;
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
yield this.#firstName;
|
|
123
|
+
yield* this.#middleName;
|
|
124
|
+
yield this.#lastName;
|
|
125
|
+
}
|
|
126
|
+
if (this.#suffix)
|
|
127
|
+
yield this.#suffix;
|
|
128
|
+
}
|
|
129
|
+
*[Symbol.iterator]() {
|
|
130
|
+
yield* this.toIterable(true);
|
|
131
|
+
}
|
|
100
132
|
}
|
|
101
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/index.js
CHANGED
|
@@ -23,6 +23,7 @@ __exportStar(require("./builder.js"), exports);
|
|
|
23
23
|
__exportStar(require("./config.js"), exports);
|
|
24
24
|
var constants_js_1 = require("./constants.js");
|
|
25
25
|
Object.defineProperty(exports, "version", { enumerable: true, get: function () { return constants_js_1.VERSION; } });
|
|
26
|
+
__exportStar(require("./data.js"), exports);
|
|
26
27
|
__exportStar(require("./error.js"), exports);
|
|
27
28
|
__exportStar(require("./fullname.js"), exports);
|
|
28
29
|
__exportStar(require("./name.js"), exports);
|
package/dist/cjs/name.js
CHANGED
|
@@ -167,7 +167,7 @@ class LastName extends Name {
|
|
|
167
167
|
return this.mother ?? '';
|
|
168
168
|
case types_js_1.Surname.HYPHENATED:
|
|
169
169
|
return this.hasMother ? `${this.value}-${this.#mother}` : this.value;
|
|
170
|
-
|
|
170
|
+
default:
|
|
171
171
|
return this.hasMother ? `${this.value} ${this.#mother}` : this.value;
|
|
172
172
|
}
|
|
173
173
|
}
|
package/dist/cjs/namefully.js
CHANGED
|
@@ -2,10 +2,10 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Namefully = void 0;
|
|
4
4
|
const constants_js_1 = require("./constants.js");
|
|
5
|
-
const error_js_1 = require("./error.js");
|
|
6
5
|
const name_js_1 = require("./name.js");
|
|
7
|
-
const
|
|
6
|
+
const error_js_1 = require("./error.js");
|
|
8
7
|
const utils_js_1 = require("./utils.js");
|
|
8
|
+
const types_js_1 = require("./types.js");
|
|
9
9
|
const parser_js_1 = require("./parser.js");
|
|
10
10
|
class Namefully {
|
|
11
11
|
#fullName;
|
|
@@ -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() {
|
|
@@ -65,25 +70,43 @@ class Namefully {
|
|
|
65
70
|
get salutation() {
|
|
66
71
|
return this.format('p l');
|
|
67
72
|
}
|
|
73
|
+
get parts() {
|
|
74
|
+
return this.#fullName.toIterable();
|
|
75
|
+
}
|
|
76
|
+
get size() {
|
|
77
|
+
return Array.from(this.parts).length;
|
|
78
|
+
}
|
|
79
|
+
*[Symbol.iterator]() {
|
|
80
|
+
yield* this.#fullName.toIterable(true);
|
|
81
|
+
}
|
|
68
82
|
toString() {
|
|
69
83
|
return this.full;
|
|
70
84
|
}
|
|
71
|
-
get(
|
|
72
|
-
|
|
85
|
+
get(key) {
|
|
86
|
+
const namon = typeof key === 'string' ? types_js_1.Namon.cast(key) : key;
|
|
87
|
+
if (namon?.equal(types_js_1.Namon.PREFIX))
|
|
73
88
|
return this.#fullName.prefix;
|
|
74
|
-
if (namon
|
|
89
|
+
if (namon?.equal(types_js_1.Namon.FIRST_NAME))
|
|
75
90
|
return this.#fullName.firstName;
|
|
76
|
-
if (namon
|
|
91
|
+
if (namon?.equal(types_js_1.Namon.MIDDLE_NAME))
|
|
77
92
|
return this.#fullName.middleName;
|
|
78
|
-
if (namon
|
|
93
|
+
if (namon?.equal(types_js_1.Namon.LAST_NAME))
|
|
79
94
|
return this.#fullName.lastName;
|
|
80
|
-
if (namon
|
|
95
|
+
if (namon?.equal(types_js_1.Namon.SUFFIX))
|
|
81
96
|
return this.#fullName.suffix;
|
|
82
97
|
return undefined;
|
|
83
98
|
}
|
|
84
99
|
equal(other) {
|
|
85
100
|
return this.toString() === other.toString();
|
|
86
101
|
}
|
|
102
|
+
deepEqual(other) {
|
|
103
|
+
const others = Array.from(other.parts);
|
|
104
|
+
for (const part of this.parts) {
|
|
105
|
+
if (!others.some((name) => name.equal(part)))
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
return true;
|
|
109
|
+
}
|
|
87
110
|
toJson() {
|
|
88
111
|
return {
|
|
89
112
|
prefix: this.prefix,
|
|
@@ -98,6 +121,8 @@ class Namefully {
|
|
|
98
121
|
return this.#fullName.has(namon);
|
|
99
122
|
}
|
|
100
123
|
fullName(orderedBy) {
|
|
124
|
+
if (this.isMono)
|
|
125
|
+
return this.#fullName.toString();
|
|
101
126
|
const sep = this.config.ending ? ',' : '';
|
|
102
127
|
const names = [];
|
|
103
128
|
if (this.prefix)
|
|
@@ -106,13 +131,21 @@ class Namefully {
|
|
|
106
131
|
names.push(this.first, ...this.middleName(), this.last + sep);
|
|
107
132
|
}
|
|
108
133
|
else {
|
|
109
|
-
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
|
+
}
|
|
110
141
|
}
|
|
111
142
|
if (this.suffix)
|
|
112
143
|
names.push(this.suffix);
|
|
113
144
|
return names.join(' ').trim();
|
|
114
145
|
}
|
|
115
146
|
birthName(orderedBy) {
|
|
147
|
+
if (this.isMono)
|
|
148
|
+
return this.#fullName.toString();
|
|
116
149
|
orderedBy ??= this.config.orderedBy;
|
|
117
150
|
return orderedBy === types_js_1.NameOrder.FIRST_NAME
|
|
118
151
|
? [this.first, ...this.middleName(), this.last].join(' ')
|
|
@@ -128,6 +161,8 @@ class Namefully {
|
|
|
128
161
|
return this.#fullName.lastName.toString(format);
|
|
129
162
|
}
|
|
130
163
|
initials(options) {
|
|
164
|
+
if (this.isMono)
|
|
165
|
+
return [this.#fullName.toString()[0]];
|
|
131
166
|
const { orderedBy = this.config.orderedBy, only = types_js_1.NameType.BIRTH_NAME, asJson } = options ?? {};
|
|
132
167
|
const firstInits = this.#fullName.firstName.initials();
|
|
133
168
|
const midInits = this.#fullName.middleName.map((n) => n.value[0]);
|
|
@@ -145,6 +180,8 @@ class Namefully {
|
|
|
145
180
|
}
|
|
146
181
|
}
|
|
147
182
|
shorten(orderedBy) {
|
|
183
|
+
if (this.isMono)
|
|
184
|
+
return this.#fullName.toString();
|
|
148
185
|
orderedBy ??= this.config.orderedBy;
|
|
149
186
|
const { firstName, lastName } = this.#fullName;
|
|
150
187
|
return orderedBy === types_js_1.NameOrder.FIRST_NAME
|
|
@@ -155,6 +192,8 @@ class Namefully {
|
|
|
155
192
|
const { by = types_js_1.Flat.MIDDLE_NAME, limit = 20, recursive = false, withMore = false, withPeriod = true, surname, } = options;
|
|
156
193
|
if (this.length <= limit)
|
|
157
194
|
return this.full;
|
|
195
|
+
if (this.isMono)
|
|
196
|
+
return `${this.initials()}${withPeriod ? '.' : ''}`;
|
|
158
197
|
const { firstName, lastName, middleName } = this.#fullName;
|
|
159
198
|
const sep = withPeriod ? '.' : '';
|
|
160
199
|
const hasMid = this.hasMiddle;
|
|
@@ -182,7 +221,7 @@ class Namefully {
|
|
|
182
221
|
case types_js_1.Flat.MID_LAST:
|
|
183
222
|
name = hasMid ? [fn, m, l] : [fn, l];
|
|
184
223
|
break;
|
|
185
|
-
|
|
224
|
+
default:
|
|
186
225
|
name = hasMid ? [f, m, l] : [f, l];
|
|
187
226
|
break;
|
|
188
227
|
}
|
|
@@ -204,7 +243,7 @@ class Namefully {
|
|
|
204
243
|
case types_js_1.Flat.MID_LAST:
|
|
205
244
|
name = hasMid ? [l, fn, m] : [l, fn];
|
|
206
245
|
break;
|
|
207
|
-
|
|
246
|
+
default:
|
|
208
247
|
name = hasMid ? [l, f, m] : [l, f];
|
|
209
248
|
break;
|
|
210
249
|
}
|
|
@@ -245,7 +284,7 @@ class Namefully {
|
|
|
245
284
|
let group = '';
|
|
246
285
|
const formatted = [];
|
|
247
286
|
for (const char of pattern) {
|
|
248
|
-
if (constants_js_1.ALLOWED_FORMAT_TOKENS.
|
|
287
|
+
if (!constants_js_1.ALLOWED_FORMAT_TOKENS.includes(char)) {
|
|
249
288
|
throw new error_js_1.NotAllowedError({
|
|
250
289
|
source: this.full,
|
|
251
290
|
operation: 'format',
|
|
@@ -302,6 +341,28 @@ class Namefully {
|
|
|
302
341
|
toToggleCase() {
|
|
303
342
|
return (0, utils_js_1.toggleCase)(this.birth);
|
|
304
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
|
+
}
|
|
305
366
|
#toParser(raw) {
|
|
306
367
|
if (raw instanceof parser_js_1.Parser)
|
|
307
368
|
return raw;
|
|
@@ -317,12 +378,6 @@ class Namefully {
|
|
|
317
378
|
}
|
|
318
379
|
#map(char) {
|
|
319
380
|
switch (char) {
|
|
320
|
-
case '.':
|
|
321
|
-
case ',':
|
|
322
|
-
case ' ':
|
|
323
|
-
case '-':
|
|
324
|
-
case '_':
|
|
325
|
-
return char;
|
|
326
381
|
case 'b':
|
|
327
382
|
return this.birth;
|
|
328
383
|
case 'B':
|
|
@@ -364,16 +419,19 @@ class Namefully {
|
|
|
364
419
|
case 'S':
|
|
365
420
|
return this.suffix?.toUpperCase();
|
|
366
421
|
case '$f':
|
|
367
|
-
case '$F':
|
|
368
422
|
return this.#fullName.firstName.value[0];
|
|
423
|
+
case '$F':
|
|
424
|
+
return this.#fullName.firstName.initials(true).join('');
|
|
369
425
|
case '$l':
|
|
370
|
-
case '$L':
|
|
371
426
|
return this.#fullName.lastName.value[0];
|
|
427
|
+
case '$L':
|
|
428
|
+
return this.#fullName.lastName.initials().join('');
|
|
372
429
|
case '$m':
|
|
373
|
-
case '$M':
|
|
374
430
|
return this.hasMiddle ? this.middle[0] : undefined;
|
|
431
|
+
case '$M':
|
|
432
|
+
return this.hasMiddle ? this.#fullName.middleName.map((n) => n.value[0]).join('') : undefined;
|
|
375
433
|
default:
|
|
376
|
-
return undefined;
|
|
434
|
+
return constants_js_1.ALLOWED_FORMAT_TOKENS.includes(char) ? char : undefined;
|
|
377
435
|
}
|
|
378
436
|
}
|
|
379
437
|
}
|
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);
|
|
@@ -123,11 +128,22 @@ class ArrayNameParser extends Parser {
|
|
|
123
128
|
fullName.middleName.push(name);
|
|
124
129
|
}
|
|
125
130
|
else if (name.isLastName) {
|
|
126
|
-
const
|
|
127
|
-
fullName.setLastName(
|
|
131
|
+
const mother = name instanceof name_js_1.LastName ? name.mother : undefined;
|
|
132
|
+
fullName.setLastName(new name_js_1.LastName(name.value, mother, config.surname));
|
|
128
133
|
}
|
|
129
134
|
}
|
|
130
135
|
return fullName;
|
|
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/cjs/types.js
CHANGED
|
@@ -56,6 +56,13 @@ class Namon {
|
|
|
56
56
|
[Namon.LAST_NAME.key, Namon.LAST_NAME],
|
|
57
57
|
[Namon.SUFFIX.key, Namon.SUFFIX],
|
|
58
58
|
]);
|
|
59
|
+
static aliases = {
|
|
60
|
+
[Namon.PREFIX.key]: ['prefix', 'px', 'p'],
|
|
61
|
+
[Namon.FIRST_NAME.key]: ['firstname', 'first', 'fn', 'f'],
|
|
62
|
+
[Namon.MIDDLE_NAME.key]: ['middlename', 'middle', 'mid', 'mn', 'm'],
|
|
63
|
+
[Namon.LAST_NAME.key]: ['lastname', 'last', 'ln', 'l'],
|
|
64
|
+
[Namon.SUFFIX.key]: ['suffix', 'sx', 's'],
|
|
65
|
+
};
|
|
59
66
|
constructor(index, key) {
|
|
60
67
|
this.index = index;
|
|
61
68
|
this.key = key;
|
|
@@ -64,7 +71,9 @@ class Namon {
|
|
|
64
71
|
return Namon.all.has(key);
|
|
65
72
|
}
|
|
66
73
|
static cast(key) {
|
|
67
|
-
|
|
74
|
+
const searchValue = String(key).toLowerCase();
|
|
75
|
+
const namon = Object.entries(Namon.aliases).find(([, list]) => list.includes(searchValue))?.[0];
|
|
76
|
+
return Namon.has(namon ?? '') ? Namon.all.get(key) : undefined;
|
|
68
77
|
}
|
|
69
78
|
toString() {
|
|
70
79
|
return `Namon.${this.key}`;
|
|
@@ -104,6 +113,14 @@ class Separator {
|
|
|
104
113
|
this.name = name;
|
|
105
114
|
this.token = token;
|
|
106
115
|
}
|
|
116
|
+
static cast(key) {
|
|
117
|
+
for (const [name, separator] of Separator.all) {
|
|
118
|
+
if (separator.token === key || name.toLowerCase() === key.toLowerCase()) {
|
|
119
|
+
return separator;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return undefined;
|
|
123
|
+
}
|
|
107
124
|
toString() {
|
|
108
125
|
return `Separator.${this.name}`;
|
|
109
126
|
}
|