namefully 1.3.0 → 1.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/lib/config.js +10 -4
- package/dist/lib/constants.js +3 -26
- package/dist/lib/error.js +5 -10
- package/dist/lib/full-name.js +39 -14
- package/dist/lib/name.js +28 -29
- package/dist/lib/namefully.js +76 -62
- package/dist/lib/parser.js +25 -41
- package/dist/lib/types.js +19 -1
- package/dist/lib/utils.js +19 -31
- package/dist/lib/validator.js +34 -48
- package/dist/types/builder.d.ts +4 -2
- package/dist/types/config.d.ts +2 -5
- package/dist/types/constants.d.ts +2 -2
- package/dist/types/error.d.ts +11 -16
- package/dist/types/full-name.d.ts +16 -13
- package/dist/types/name.d.ts +25 -26
- package/dist/types/namefully.d.ts +115 -91
- package/dist/types/parser.d.ts +7 -10
- package/dist/types/types.d.ts +19 -48
- package/dist/types/utils.d.ts +4 -6
- package/dist/types/validator.d.ts +1 -1
- package/dist/umd/namefully.js +258 -266
- package/dist/umd/namefully.min.js +1 -1
- package/package.json +2 -2
package/dist/lib/config.js
CHANGED
|
@@ -99,10 +99,16 @@ class Config {
|
|
|
99
99
|
__classPrivateFieldSet(this, _Config_surname, types_1.Surname.FATHER, "f");
|
|
100
100
|
_a.cache.set(this.name, this);
|
|
101
101
|
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
102
|
+
update({ orderedBy, title, ending }) {
|
|
103
|
+
const config = _a.cache.get(this.name);
|
|
104
|
+
if (!config)
|
|
105
|
+
return;
|
|
106
|
+
if (orderedBy !== __classPrivateFieldGet(this, _Config_orderedBy, "f"))
|
|
107
|
+
__classPrivateFieldSet(config, _Config_orderedBy, orderedBy, "f");
|
|
108
|
+
if (title !== __classPrivateFieldGet(this, _Config_title, "f"))
|
|
109
|
+
__classPrivateFieldSet(config, _Config_title, title, "f");
|
|
110
|
+
if (ending !== __classPrivateFieldGet(this, _Config_ending, "f"))
|
|
111
|
+
__classPrivateFieldSet(config, _Config_ending, ending, "f");
|
|
106
112
|
}
|
|
107
113
|
}
|
|
108
114
|
exports.Config = Config;
|
package/dist/lib/constants.js
CHANGED
|
@@ -1,30 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
exports.VERSION = '1.3.
|
|
3
|
+
exports.ALLOWED_FORMAT_TOKENS = exports.MAX_NUMBER_OF_NAME_PARTS = exports.MIN_NUMBER_OF_NAME_PARTS = exports.VERSION = void 0;
|
|
4
|
+
exports.VERSION = '1.3.1';
|
|
5
5
|
exports.MIN_NUMBER_OF_NAME_PARTS = 2;
|
|
6
6
|
exports.MAX_NUMBER_OF_NAME_PARTS = 5;
|
|
7
|
-
exports.
|
|
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$`;
|
package/dist/lib/error.js
CHANGED
|
@@ -17,19 +17,14 @@ class NameError extends Error {
|
|
|
17
17
|
this.name = 'NameError';
|
|
18
18
|
}
|
|
19
19
|
get sourceAsString() {
|
|
20
|
-
let input = '';
|
|
21
|
-
if (!this.source)
|
|
22
|
-
input = '<undefined>';
|
|
23
20
|
if (typeof this.source === 'string')
|
|
24
|
-
|
|
25
|
-
if ((0, utils_1.isNameArray)(this.source))
|
|
26
|
-
input = this.source.map((n) => n.toString()).join(' ');
|
|
21
|
+
return this.source;
|
|
27
22
|
if ((0, utils_1.isStringArray)(this.source))
|
|
28
|
-
|
|
29
|
-
return
|
|
23
|
+
return this.source.join(' ');
|
|
24
|
+
return '<undefined>';
|
|
30
25
|
}
|
|
31
26
|
get hasMessage() {
|
|
32
|
-
return this.message
|
|
27
|
+
return this.message.trim().length > 0;
|
|
33
28
|
}
|
|
34
29
|
toString() {
|
|
35
30
|
let report = `${this.name} (${this.sourceAsString})`;
|
|
@@ -79,7 +74,7 @@ exports.NotAllowedError = NotAllowedError;
|
|
|
79
74
|
class UnknownError extends NameError {
|
|
80
75
|
constructor(error) {
|
|
81
76
|
super(error.source, error.message, NameErrorType.UNKNOWN);
|
|
82
|
-
this.origin = error.
|
|
77
|
+
this.origin = error.origin;
|
|
83
78
|
this.name = 'UnknownError';
|
|
84
79
|
}
|
|
85
80
|
toString() {
|
package/dist/lib/full-name.js
CHANGED
|
@@ -14,10 +14,10 @@ var _FullName_prefix, _FullName_firstName, _FullName_middleName, _FullName_lastN
|
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
15
|
exports.FullName = void 0;
|
|
16
16
|
const config_1 = require("./config");
|
|
17
|
+
const validator_1 = require("./validator");
|
|
18
|
+
const types_1 = require("./types");
|
|
17
19
|
const error_1 = require("./error");
|
|
18
20
|
const name_1 = require("./name");
|
|
19
|
-
const types_1 = require("./types");
|
|
20
|
-
const validator_1 = require("./validator");
|
|
21
21
|
class FullName {
|
|
22
22
|
constructor(options) {
|
|
23
23
|
_FullName_prefix.set(this, void 0);
|
|
@@ -47,14 +47,15 @@ class FullName {
|
|
|
47
47
|
return __classPrivateFieldGet(this, _FullName_suffix, "f");
|
|
48
48
|
}
|
|
49
49
|
static parse(json, config) {
|
|
50
|
+
var _a;
|
|
50
51
|
try {
|
|
51
|
-
const
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
52
|
+
const { prefix, firstName: fn, middleName: mn, lastName: ln, suffix } = json;
|
|
53
|
+
return new FullName(config)
|
|
54
|
+
.setPrefix(prefix)
|
|
55
|
+
.setFirstName(typeof fn === 'string' ? fn : new name_1.FirstName(fn.value, ...((_a = fn.more) !== null && _a !== void 0 ? _a : [])))
|
|
56
|
+
.setMiddleName(typeof mn === 'string' ? [mn] : (mn !== null && mn !== void 0 ? mn : []))
|
|
57
|
+
.setLastName(typeof ln === 'string' ? ln : new name_1.LastName(ln.father, ln.mother))
|
|
58
|
+
.setSuffix(suffix);
|
|
58
59
|
}
|
|
59
60
|
catch (error) {
|
|
60
61
|
if (error instanceof error_1.NameError)
|
|
@@ -62,7 +63,7 @@ class FullName {
|
|
|
62
63
|
throw new error_1.UnknownError({
|
|
63
64
|
source: Object.values(json).join(' '),
|
|
64
65
|
message: 'could not parse JSON content',
|
|
65
|
-
error,
|
|
66
|
+
origin: error instanceof Error ? error : new Error(String(error)),
|
|
66
67
|
});
|
|
67
68
|
}
|
|
68
69
|
}
|
|
@@ -72,7 +73,7 @@ class FullName {
|
|
|
72
73
|
if (!__classPrivateFieldGet(this, _FullName_config, "f").bypass)
|
|
73
74
|
validator_1.Validators.prefix.validate(name);
|
|
74
75
|
const prefix = name instanceof name_1.Name ? name.value : name;
|
|
75
|
-
__classPrivateFieldSet(this, _FullName_prefix, name_1.Name.prefix(__classPrivateFieldGet(this, _FullName_config, "f").title === types_1.Title.US ? `${prefix}.` : prefix), "f");
|
|
76
|
+
__classPrivateFieldSet(this, _FullName_prefix, name_1.Name.prefix(__classPrivateFieldGet(this, _FullName_config, "f").title === types_1.Title.US && !prefix.endsWith('.') ? `${prefix}.` : prefix), "f");
|
|
76
77
|
return this;
|
|
77
78
|
}
|
|
78
79
|
setFirstName(name) {
|
|
@@ -92,7 +93,7 @@ class FullName {
|
|
|
92
93
|
return this;
|
|
93
94
|
if (!__classPrivateFieldGet(this, _FullName_config, "f").bypass)
|
|
94
95
|
validator_1.Validators.middleName.validate(names);
|
|
95
|
-
__classPrivateFieldSet(this, _FullName_middleName, names.map((
|
|
96
|
+
__classPrivateFieldSet(this, _FullName_middleName, names.map((n) => (n instanceof name_1.Name ? n : name_1.Name.middle(n))), "f");
|
|
96
97
|
return this;
|
|
97
98
|
}
|
|
98
99
|
setSuffix(name) {
|
|
@@ -103,13 +104,37 @@ class FullName {
|
|
|
103
104
|
__classPrivateFieldSet(this, _FullName_suffix, name_1.Name.suffix(name instanceof name_1.Name ? name.value : name), "f");
|
|
104
105
|
return this;
|
|
105
106
|
}
|
|
106
|
-
has(
|
|
107
|
+
has(key) {
|
|
108
|
+
const namon = typeof key === 'string' ? types_1.Namon.cast(key) : key;
|
|
109
|
+
if (!namon)
|
|
110
|
+
return false;
|
|
107
111
|
if (namon.equal(types_1.Namon.PREFIX))
|
|
108
112
|
return !!__classPrivateFieldGet(this, _FullName_prefix, "f");
|
|
109
113
|
if (namon.equal(types_1.Namon.SUFFIX))
|
|
110
114
|
return !!__classPrivateFieldGet(this, _FullName_suffix, "f");
|
|
111
115
|
return namon.equal(types_1.Namon.MIDDLE_NAME) ? __classPrivateFieldGet(this, _FullName_middleName, "f").length > 0 : true;
|
|
112
116
|
}
|
|
117
|
+
toString() {
|
|
118
|
+
return Array.from(this.toIterable(true)).join(' ');
|
|
119
|
+
}
|
|
120
|
+
*toIterable(flat = false) {
|
|
121
|
+
if (__classPrivateFieldGet(this, _FullName_prefix, "f"))
|
|
122
|
+
yield __classPrivateFieldGet(this, _FullName_prefix, "f");
|
|
123
|
+
if (flat) {
|
|
124
|
+
yield* __classPrivateFieldGet(this, _FullName_firstName, "f").asNames;
|
|
125
|
+
yield* __classPrivateFieldGet(this, _FullName_middleName, "f");
|
|
126
|
+
yield* __classPrivateFieldGet(this, _FullName_lastName, "f").asNames;
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
yield __classPrivateFieldGet(this, _FullName_firstName, "f");
|
|
130
|
+
yield* __classPrivateFieldGet(this, _FullName_middleName, "f");
|
|
131
|
+
yield __classPrivateFieldGet(this, _FullName_lastName, "f");
|
|
132
|
+
}
|
|
133
|
+
if (__classPrivateFieldGet(this, _FullName_suffix, "f"))
|
|
134
|
+
yield __classPrivateFieldGet(this, _FullName_suffix, "f");
|
|
135
|
+
}
|
|
136
|
+
*[(_FullName_prefix = new WeakMap(), _FullName_firstName = new WeakMap(), _FullName_middleName = new WeakMap(), _FullName_lastName = new WeakMap(), _FullName_suffix = new WeakMap(), _FullName_config = new WeakMap(), Symbol.iterator)]() {
|
|
137
|
+
yield* this.toIterable(true);
|
|
138
|
+
}
|
|
113
139
|
}
|
|
114
140
|
exports.FullName = FullName;
|
|
115
|
-
_FullName_prefix = new WeakMap(), _FullName_firstName = new WeakMap(), _FullName_middleName = new WeakMap(), _FullName_lastName = new WeakMap(), _FullName_suffix = new WeakMap(), _FullName_config = new WeakMap();
|
package/dist/lib/name.js
CHANGED
|
@@ -12,7 +12,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
12
12
|
};
|
|
13
13
|
var _Name_namon, _FirstName_more, _LastName_mother;
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.LastName = exports.FirstName = exports.Name = void 0;
|
|
15
|
+
exports.isNameArray = exports.LastName = exports.FirstName = exports.Name = void 0;
|
|
16
16
|
const error_1 = require("./error");
|
|
17
17
|
const types_1 = require("./types");
|
|
18
18
|
const utils_1 = require("./utils");
|
|
@@ -52,19 +52,19 @@ class Name {
|
|
|
52
52
|
return this.type === types_1.Namon.SUFFIX;
|
|
53
53
|
}
|
|
54
54
|
static prefix(value) {
|
|
55
|
-
return new
|
|
55
|
+
return new Name(value, types_1.Namon.PREFIX);
|
|
56
56
|
}
|
|
57
57
|
static first(value) {
|
|
58
|
-
return new
|
|
58
|
+
return new Name(value, types_1.Namon.FIRST_NAME);
|
|
59
59
|
}
|
|
60
60
|
static middle(value) {
|
|
61
|
-
return new
|
|
61
|
+
return new Name(value, types_1.Namon.MIDDLE_NAME);
|
|
62
62
|
}
|
|
63
63
|
static last(value) {
|
|
64
|
-
return new
|
|
64
|
+
return new Name(value, types_1.Namon.LAST_NAME);
|
|
65
65
|
}
|
|
66
66
|
static suffix(value) {
|
|
67
|
-
return new
|
|
67
|
+
return new Name(value, types_1.Namon.SUFFIX);
|
|
68
68
|
}
|
|
69
69
|
initials() {
|
|
70
70
|
return [this.initial];
|
|
@@ -84,8 +84,8 @@ class Name {
|
|
|
84
84
|
return this;
|
|
85
85
|
}
|
|
86
86
|
validate(name) {
|
|
87
|
-
if (
|
|
88
|
-
throw new error_1.InputError({ source: name, message: 'must be
|
|
87
|
+
if (typeof name === 'string' && name.trim().length < 1) {
|
|
88
|
+
throw new error_1.InputError({ source: name, message: 'must be 1+ characters' });
|
|
89
89
|
}
|
|
90
90
|
}
|
|
91
91
|
}
|
|
@@ -95,7 +95,7 @@ class FirstName extends Name {
|
|
|
95
95
|
constructor(value, ...more) {
|
|
96
96
|
super(value, types_1.Namon.FIRST_NAME);
|
|
97
97
|
_FirstName_more.set(this, void 0);
|
|
98
|
-
more.forEach(
|
|
98
|
+
more.forEach(this.validate);
|
|
99
99
|
__classPrivateFieldSet(this, _FirstName_more, more, "f");
|
|
100
100
|
}
|
|
101
101
|
get hasMore() {
|
|
@@ -106,9 +106,8 @@ class FirstName extends Name {
|
|
|
106
106
|
}
|
|
107
107
|
get asNames() {
|
|
108
108
|
const names = [Name.first(this.value)];
|
|
109
|
-
if (this.hasMore)
|
|
110
|
-
names.push(...__classPrivateFieldGet(this, _FirstName_more, "f").map(
|
|
111
|
-
}
|
|
109
|
+
if (this.hasMore)
|
|
110
|
+
names.push(...__classPrivateFieldGet(this, _FirstName_more, "f").map(Name.first));
|
|
112
111
|
return names;
|
|
113
112
|
}
|
|
114
113
|
get more() {
|
|
@@ -119,9 +118,8 @@ class FirstName extends Name {
|
|
|
119
118
|
}
|
|
120
119
|
initials(withMore = false) {
|
|
121
120
|
const inits = [this.initial];
|
|
122
|
-
if (withMore && this.hasMore)
|
|
121
|
+
if (withMore && this.hasMore)
|
|
123
122
|
inits.push(...__classPrivateFieldGet(this, _FirstName_more, "f").map((n) => n[0]));
|
|
124
|
-
}
|
|
125
123
|
return inits;
|
|
126
124
|
}
|
|
127
125
|
caps(range) {
|
|
@@ -140,7 +138,7 @@ class FirstName extends Name {
|
|
|
140
138
|
}
|
|
141
139
|
copyWith(values) {
|
|
142
140
|
var _a, _b;
|
|
143
|
-
return new FirstName((_a = values.first) !== null && _a !== void 0 ? _a : this.value, ...((_b = values.more) !== null && _b !== void 0 ? _b : __classPrivateFieldGet(this, _FirstName_more, "f")));
|
|
141
|
+
return new FirstName((_a = values === null || values === void 0 ? void 0 : values.first) !== null && _a !== void 0 ? _a : this.value, ...((_b = values === null || values === void 0 ? void 0 : values.more) !== null && _b !== void 0 ? _b : __classPrivateFieldGet(this, _FirstName_more, "f")));
|
|
144
142
|
}
|
|
145
143
|
}
|
|
146
144
|
exports.FirstName = FirstName;
|
|
@@ -168,9 +166,8 @@ class LastName extends Name {
|
|
|
168
166
|
}
|
|
169
167
|
get asNames() {
|
|
170
168
|
const names = [Name.last(this.value)];
|
|
171
|
-
if (this
|
|
169
|
+
if (__classPrivateFieldGet(this, _LastName_mother, "f"))
|
|
172
170
|
names.push(Name.last(__classPrivateFieldGet(this, _LastName_mother, "f")));
|
|
173
|
-
}
|
|
174
171
|
return names;
|
|
175
172
|
}
|
|
176
173
|
toString(format) {
|
|
@@ -183,39 +180,37 @@ class LastName extends Name {
|
|
|
183
180
|
return (_a = this.mother) !== null && _a !== void 0 ? _a : '';
|
|
184
181
|
case types_1.Surname.HYPHENATED:
|
|
185
182
|
return this.hasMother ? `${this.value}-${__classPrivateFieldGet(this, _LastName_mother, "f")}` : this.value;
|
|
186
|
-
|
|
183
|
+
default:
|
|
187
184
|
return this.hasMother ? `${this.value} ${__classPrivateFieldGet(this, _LastName_mother, "f")}` : this.value;
|
|
188
185
|
}
|
|
189
186
|
}
|
|
190
187
|
initials(format) {
|
|
191
|
-
format = format || this.format;
|
|
192
188
|
const inits = [];
|
|
193
|
-
switch (format) {
|
|
194
|
-
case types_1.Surname.MOTHER:
|
|
195
|
-
if (this.hasMother)
|
|
196
|
-
inits.push(__classPrivateFieldGet(this, _LastName_mother, "f")[0]);
|
|
197
|
-
break;
|
|
189
|
+
switch (format !== null && format !== void 0 ? format : this.format) {
|
|
198
190
|
case types_1.Surname.HYPHENATED:
|
|
199
191
|
case types_1.Surname.ALL:
|
|
200
192
|
inits.push(this.initial);
|
|
201
|
-
if (this
|
|
193
|
+
if (__classPrivateFieldGet(this, _LastName_mother, "f"))
|
|
194
|
+
inits.push(__classPrivateFieldGet(this, _LastName_mother, "f")[0]);
|
|
195
|
+
break;
|
|
196
|
+
case types_1.Surname.MOTHER:
|
|
197
|
+
if (__classPrivateFieldGet(this, _LastName_mother, "f"))
|
|
202
198
|
inits.push(__classPrivateFieldGet(this, _LastName_mother, "f")[0]);
|
|
203
199
|
break;
|
|
204
|
-
case types_1.Surname.FATHER:
|
|
205
200
|
default:
|
|
206
201
|
inits.push(this.initial);
|
|
207
202
|
}
|
|
208
203
|
return inits;
|
|
209
204
|
}
|
|
210
205
|
caps(range) {
|
|
211
|
-
range
|
|
206
|
+
range !== null && range !== void 0 ? range : (range = this.capsRange);
|
|
212
207
|
this.value = (0, utils_1.capitalize)(this.value, range);
|
|
213
208
|
if (this.hasMother)
|
|
214
209
|
__classPrivateFieldSet(this, _LastName_mother, (0, utils_1.capitalize)(__classPrivateFieldGet(this, _LastName_mother, "f"), range), "f");
|
|
215
210
|
return this;
|
|
216
211
|
}
|
|
217
212
|
decaps(range) {
|
|
218
|
-
range
|
|
213
|
+
range !== null && range !== void 0 ? range : (range = this.capsRange);
|
|
219
214
|
this.value = (0, utils_1.decapitalize)(this.value, range);
|
|
220
215
|
if (this.hasMother)
|
|
221
216
|
__classPrivateFieldSet(this, _LastName_mother, (0, utils_1.decapitalize)(__classPrivateFieldGet(this, _LastName_mother, "f"), range), "f");
|
|
@@ -223,8 +218,12 @@ class LastName extends Name {
|
|
|
223
218
|
}
|
|
224
219
|
copyWith(values) {
|
|
225
220
|
var _a, _b, _c;
|
|
226
|
-
return new LastName((_a = values.father) !== null && _a !== void 0 ? _a : this.value, (_b = values.mother) !== null && _b !== void 0 ? _b : this.mother, (_c = values.format) !== null && _c !== void 0 ? _c : this.format);
|
|
221
|
+
return new LastName((_a = values === null || values === void 0 ? void 0 : values.father) !== null && _a !== void 0 ? _a : this.value, (_b = values === null || values === void 0 ? void 0 : values.mother) !== null && _b !== void 0 ? _b : this.mother, (_c = values === null || values === void 0 ? void 0 : values.format) !== null && _c !== void 0 ? _c : this.format);
|
|
227
222
|
}
|
|
228
223
|
}
|
|
229
224
|
exports.LastName = LastName;
|
|
230
225
|
_LastName_mother = new WeakMap();
|
|
226
|
+
function isNameArray(value) {
|
|
227
|
+
return Array.isArray(value) && value.length > 0 && value.every((e) => e instanceof Name);
|
|
228
|
+
}
|
|
229
|
+
exports.isNameArray = isNameArray;
|
package/dist/lib/namefully.js
CHANGED
|
@@ -23,14 +23,16 @@ var _Namefully_instances, _Namefully_fullName, _Namefully_toParser, _Namefully_m
|
|
|
23
23
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
24
24
|
exports.Namefully = void 0;
|
|
25
25
|
const constants_1 = require("./constants");
|
|
26
|
+
const name_1 = require("./name");
|
|
26
27
|
const error_1 = require("./error");
|
|
27
|
-
const parser_1 = require("./parser");
|
|
28
|
-
const types_1 = require("./types");
|
|
29
28
|
const utils_1 = require("./utils");
|
|
29
|
+
const types_1 = require("./types");
|
|
30
|
+
const parser_1 = require("./parser");
|
|
30
31
|
class Namefully {
|
|
31
32
|
constructor(names, options) {
|
|
32
33
|
_Namefully_instances.add(this);
|
|
33
34
|
_Namefully_fullName.set(this, void 0);
|
|
35
|
+
this.json = this.toJson;
|
|
34
36
|
__classPrivateFieldSet(this, _Namefully_fullName, __classPrivateFieldGet(this, _Namefully_instances, "m", _Namefully_toParser).call(this, names).parse(options), "f");
|
|
35
37
|
}
|
|
36
38
|
static tryParse(text, index) {
|
|
@@ -90,25 +92,43 @@ class Namefully {
|
|
|
90
92
|
get salutation() {
|
|
91
93
|
return this.format('p l');
|
|
92
94
|
}
|
|
95
|
+
get parts() {
|
|
96
|
+
return __classPrivateFieldGet(this, _Namefully_fullName, "f").toIterable();
|
|
97
|
+
}
|
|
98
|
+
get size() {
|
|
99
|
+
return Array.from(this.parts).length;
|
|
100
|
+
}
|
|
101
|
+
*[(_Namefully_fullName = new WeakMap(), _Namefully_instances = new WeakSet(), Symbol.iterator)]() {
|
|
102
|
+
yield* __classPrivateFieldGet(this, _Namefully_fullName, "f").toIterable(true);
|
|
103
|
+
}
|
|
93
104
|
toString() {
|
|
94
105
|
return this.full;
|
|
95
106
|
}
|
|
96
|
-
get(
|
|
97
|
-
|
|
107
|
+
get(key) {
|
|
108
|
+
const namon = typeof key === 'string' ? types_1.Namon.cast(key) : key;
|
|
109
|
+
if (namon === null || namon === void 0 ? void 0 : namon.equal(types_1.Namon.PREFIX))
|
|
98
110
|
return __classPrivateFieldGet(this, _Namefully_fullName, "f").prefix;
|
|
99
|
-
if (namon.equal(types_1.Namon.FIRST_NAME))
|
|
111
|
+
if (namon === null || namon === void 0 ? void 0 : namon.equal(types_1.Namon.FIRST_NAME))
|
|
100
112
|
return __classPrivateFieldGet(this, _Namefully_fullName, "f").firstName;
|
|
101
|
-
if (namon.equal(types_1.Namon.MIDDLE_NAME))
|
|
113
|
+
if (namon === null || namon === void 0 ? void 0 : namon.equal(types_1.Namon.MIDDLE_NAME))
|
|
102
114
|
return __classPrivateFieldGet(this, _Namefully_fullName, "f").middleName;
|
|
103
|
-
if (namon.equal(types_1.Namon.LAST_NAME))
|
|
115
|
+
if (namon === null || namon === void 0 ? void 0 : namon.equal(types_1.Namon.LAST_NAME))
|
|
104
116
|
return __classPrivateFieldGet(this, _Namefully_fullName, "f").lastName;
|
|
105
|
-
if (namon.equal(types_1.Namon.SUFFIX))
|
|
117
|
+
if (namon === null || namon === void 0 ? void 0 : namon.equal(types_1.Namon.SUFFIX))
|
|
106
118
|
return __classPrivateFieldGet(this, _Namefully_fullName, "f").suffix;
|
|
107
119
|
return undefined;
|
|
108
120
|
}
|
|
109
121
|
equal(other) {
|
|
110
122
|
return this.toString() === other.toString();
|
|
111
123
|
}
|
|
124
|
+
deepEqual(other) {
|
|
125
|
+
const others = Array.from(other.parts);
|
|
126
|
+
for (const part of this.parts) {
|
|
127
|
+
if (!others.some((name) => name.equal(part)))
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
return true;
|
|
131
|
+
}
|
|
112
132
|
toJson() {
|
|
113
133
|
return {
|
|
114
134
|
prefix: this.prefix,
|
|
@@ -124,21 +144,26 @@ class Namefully {
|
|
|
124
144
|
fullName(orderedBy) {
|
|
125
145
|
const sep = this.config.ending ? ',' : '';
|
|
126
146
|
const names = [];
|
|
127
|
-
orderedBy = orderedBy || this.config.orderedBy;
|
|
128
147
|
if (this.prefix)
|
|
129
148
|
names.push(this.prefix);
|
|
130
|
-
if (orderedBy === types_1.NameOrder.FIRST_NAME) {
|
|
149
|
+
if ((orderedBy !== null && orderedBy !== void 0 ? orderedBy : this.config.orderedBy) === types_1.NameOrder.FIRST_NAME) {
|
|
131
150
|
names.push(this.first, ...this.middleName(), this.last + sep);
|
|
132
151
|
}
|
|
133
152
|
else {
|
|
134
|
-
names.push(this.last
|
|
153
|
+
names.push(this.last);
|
|
154
|
+
if (this.hasMiddle) {
|
|
155
|
+
names.push(this.first, this.middleName().join(' ') + sep);
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
names.push(this.first + sep);
|
|
159
|
+
}
|
|
135
160
|
}
|
|
136
161
|
if (this.suffix)
|
|
137
162
|
names.push(this.suffix);
|
|
138
163
|
return names.join(' ').trim();
|
|
139
164
|
}
|
|
140
165
|
birthName(orderedBy) {
|
|
141
|
-
orderedBy
|
|
166
|
+
orderedBy !== null && orderedBy !== void 0 ? orderedBy : (orderedBy = this.config.orderedBy);
|
|
142
167
|
return orderedBy === types_1.NameOrder.FIRST_NAME
|
|
143
168
|
? [this.first, ...this.middleName(), this.last].join(' ')
|
|
144
169
|
: [this.last, this.first, ...this.middleName()].join(' ');
|
|
@@ -153,50 +178,42 @@ class Namefully {
|
|
|
153
178
|
return __classPrivateFieldGet(this, _Namefully_fullName, "f").lastName.toString(format);
|
|
154
179
|
}
|
|
155
180
|
initials(options) {
|
|
181
|
+
const { orderedBy = this.config.orderedBy, only = types_1.NameType.BIRTH_NAME, asJson } = options !== null && options !== void 0 ? options : {};
|
|
156
182
|
const firstInits = __classPrivateFieldGet(this, _Namefully_fullName, "f").firstName.initials();
|
|
157
|
-
const midInits = __classPrivateFieldGet(this, _Namefully_fullName, "f").middleName.map((n) => n.
|
|
183
|
+
const midInits = __classPrivateFieldGet(this, _Namefully_fullName, "f").middleName.map((n) => n.value[0]);
|
|
158
184
|
const lastInits = __classPrivateFieldGet(this, _Namefully_fullName, "f").lastName.initials();
|
|
159
|
-
if (
|
|
185
|
+
if (asJson)
|
|
160
186
|
return { firstName: firstInits, middleName: midInits, lastName: lastInits };
|
|
161
|
-
const initials = [];
|
|
162
|
-
const { orderedBy = this.config.orderedBy, only = types_1.NameType.BIRTH_NAME } = options !== null && options !== void 0 ? options : {};
|
|
163
187
|
if (only !== types_1.NameType.BIRTH_NAME) {
|
|
164
|
-
|
|
165
|
-
initials.push(...firstInits);
|
|
166
|
-
}
|
|
167
|
-
else if (only === types_1.NameType.MIDDLE_NAME) {
|
|
168
|
-
initials.push(...midInits);
|
|
169
|
-
}
|
|
170
|
-
else {
|
|
171
|
-
initials.push(...lastInits);
|
|
172
|
-
}
|
|
188
|
+
return only === types_1.NameType.FIRST_NAME ? firstInits : only === types_1.NameType.MIDDLE_NAME ? midInits : lastInits;
|
|
173
189
|
}
|
|
174
190
|
else if (orderedBy === types_1.NameOrder.FIRST_NAME) {
|
|
175
|
-
|
|
191
|
+
return [...firstInits, ...midInits, ...lastInits];
|
|
176
192
|
}
|
|
177
193
|
else {
|
|
178
|
-
|
|
194
|
+
return [...lastInits, ...firstInits, ...midInits];
|
|
179
195
|
}
|
|
180
|
-
return initials;
|
|
181
196
|
}
|
|
182
197
|
shorten(orderedBy) {
|
|
183
|
-
orderedBy
|
|
198
|
+
orderedBy !== null && orderedBy !== void 0 ? orderedBy : (orderedBy = this.config.orderedBy);
|
|
199
|
+
const { firstName, lastName } = __classPrivateFieldGet(this, _Namefully_fullName, "f");
|
|
184
200
|
return orderedBy === types_1.NameOrder.FIRST_NAME
|
|
185
|
-
? [
|
|
186
|
-
: [
|
|
201
|
+
? [firstName.value, lastName.toString()].join(' ')
|
|
202
|
+
: [lastName.toString(), firstName.value].join(' ');
|
|
187
203
|
}
|
|
188
204
|
flatten(options) {
|
|
189
|
-
if (this.length <= options.limit)
|
|
190
|
-
return this.full;
|
|
191
205
|
const { by = types_1.Flat.MIDDLE_NAME, limit = 20, recursive = false, withMore = false, withPeriod = true, surname, } = options;
|
|
206
|
+
if (this.length <= limit)
|
|
207
|
+
return this.full;
|
|
208
|
+
const { firstName, lastName, middleName } = __classPrivateFieldGet(this, _Namefully_fullName, "f");
|
|
192
209
|
const sep = withPeriod ? '.' : '';
|
|
193
|
-
const fn = __classPrivateFieldGet(this, _Namefully_fullName, "f").firstName.toString();
|
|
194
|
-
const mn = this.middleName().join(' ');
|
|
195
|
-
const ln = __classPrivateFieldGet(this, _Namefully_fullName, "f").lastName.toString();
|
|
196
210
|
const hasMid = this.hasMiddle;
|
|
197
|
-
const
|
|
198
|
-
const
|
|
199
|
-
const
|
|
211
|
+
const fn = firstName.toString();
|
|
212
|
+
const mn = this.middleName().join(' ');
|
|
213
|
+
const ln = lastName.toString();
|
|
214
|
+
const f = firstName.initials(withMore).join(sep + ' ') + sep;
|
|
215
|
+
const l = lastName.initials(surname).join(sep + ' ') + sep;
|
|
216
|
+
const m = hasMid ? middleName.map((n) => n.value[0]).join(sep + ' ') + sep : '';
|
|
200
217
|
let name = [];
|
|
201
218
|
if (this.config.orderedBy === types_1.NameOrder.FIRST_NAME) {
|
|
202
219
|
switch (by) {
|
|
@@ -215,7 +232,7 @@ class Namefully {
|
|
|
215
232
|
case types_1.Flat.MID_LAST:
|
|
216
233
|
name = hasMid ? [fn, m, l] : [fn, l];
|
|
217
234
|
break;
|
|
218
|
-
|
|
235
|
+
default:
|
|
219
236
|
name = hasMid ? [f, m, l] : [f, l];
|
|
220
237
|
break;
|
|
221
238
|
}
|
|
@@ -237,7 +254,7 @@ class Namefully {
|
|
|
237
254
|
case types_1.Flat.MID_LAST:
|
|
238
255
|
name = hasMid ? [l, fn, m] : [l, fn];
|
|
239
256
|
break;
|
|
240
|
-
|
|
257
|
+
default:
|
|
241
258
|
name = hasMid ? [l, f, m] : [l, f];
|
|
242
259
|
break;
|
|
243
260
|
}
|
|
@@ -278,8 +295,8 @@ class Namefully {
|
|
|
278
295
|
pattern = 'o';
|
|
279
296
|
let group = '';
|
|
280
297
|
const formatted = [];
|
|
281
|
-
for (const char of pattern
|
|
282
|
-
if (constants_1.
|
|
298
|
+
for (const char of pattern) {
|
|
299
|
+
if (!constants_1.ALLOWED_FORMAT_TOKENS.includes(char)) {
|
|
283
300
|
throw new error_1.NotAllowedError({
|
|
284
301
|
source: this.full,
|
|
285
302
|
operation: 'format',
|
|
@@ -295,7 +312,8 @@ class Namefully {
|
|
|
295
312
|
return formatted.join('').trim();
|
|
296
313
|
}
|
|
297
314
|
flip() {
|
|
298
|
-
this.config.
|
|
315
|
+
const order = this.config.orderedBy === types_1.NameOrder.FIRST_NAME ? types_1.NameOrder.LAST_NAME : types_1.NameOrder.FIRST_NAME;
|
|
316
|
+
this.config.update({ orderedBy: order });
|
|
299
317
|
}
|
|
300
318
|
split(separator = /[' -]/g) {
|
|
301
319
|
return this.birth.replace(separator, ' ').split(' ');
|
|
@@ -337,27 +355,21 @@ class Namefully {
|
|
|
337
355
|
}
|
|
338
356
|
}
|
|
339
357
|
exports.Namefully = Namefully;
|
|
340
|
-
|
|
358
|
+
_Namefully_toParser = function _Namefully_toParser(raw) {
|
|
341
359
|
if (raw instanceof parser_1.Parser)
|
|
342
360
|
return raw;
|
|
343
361
|
if (typeof raw === 'string')
|
|
344
362
|
return new parser_1.StringParser(raw);
|
|
345
363
|
if ((0, utils_1.isStringArray)(raw))
|
|
346
364
|
return new parser_1.ArrayStringParser(raw);
|
|
347
|
-
if ((0,
|
|
365
|
+
if ((0, name_1.isNameArray)(raw))
|
|
348
366
|
return new parser_1.ArrayNameParser(raw);
|
|
349
367
|
if (typeof raw === 'object')
|
|
350
368
|
return new parser_1.NamaParser(raw);
|
|
351
|
-
throw new error_1.InputError({ source: raw, message: 'Cannot parse raw data
|
|
369
|
+
throw new error_1.InputError({ source: raw, message: 'Cannot parse raw data; review expected data types.' });
|
|
352
370
|
}, _Namefully_map = function _Namefully_map(char) {
|
|
353
371
|
var _a, _b;
|
|
354
372
|
switch (char) {
|
|
355
|
-
case '.':
|
|
356
|
-
case ',':
|
|
357
|
-
case ' ':
|
|
358
|
-
case '-':
|
|
359
|
-
case '_':
|
|
360
|
-
return char;
|
|
361
373
|
case 'b':
|
|
362
374
|
return this.birth;
|
|
363
375
|
case 'B':
|
|
@@ -376,16 +388,15 @@ _Namefully_fullName = new WeakMap(), _Namefully_instances = new WeakSet(), _Name
|
|
|
376
388
|
case 'o':
|
|
377
389
|
case 'O':
|
|
378
390
|
return ((character) => {
|
|
379
|
-
const sep = this.config.ending ? ',' : ''
|
|
391
|
+
const sep = this.config.ending ? ',' : '';
|
|
392
|
+
const names = [];
|
|
380
393
|
if (this.prefix)
|
|
381
394
|
names.push(this.prefix);
|
|
382
395
|
names.push(`${this.last},`.toUpperCase());
|
|
383
|
-
if (this.hasMiddle)
|
|
396
|
+
if (this.hasMiddle)
|
|
384
397
|
names.push(this.first, this.middleName().join(' ') + sep);
|
|
385
|
-
|
|
386
|
-
else {
|
|
398
|
+
else
|
|
387
399
|
names.push(this.first + sep);
|
|
388
|
-
}
|
|
389
400
|
if (this.suffix)
|
|
390
401
|
names.push(this.suffix);
|
|
391
402
|
const nama = names.join(' ').trim();
|
|
@@ -400,16 +411,19 @@ _Namefully_fullName = new WeakMap(), _Namefully_instances = new WeakSet(), _Name
|
|
|
400
411
|
case 'S':
|
|
401
412
|
return (_b = this.suffix) === null || _b === void 0 ? void 0 : _b.toUpperCase();
|
|
402
413
|
case '$f':
|
|
414
|
+
return __classPrivateFieldGet(this, _Namefully_fullName, "f").firstName.value[0];
|
|
403
415
|
case '$F':
|
|
404
|
-
return __classPrivateFieldGet(this, _Namefully_fullName, "f").firstName.initials()
|
|
416
|
+
return __classPrivateFieldGet(this, _Namefully_fullName, "f").firstName.initials(true).join('');
|
|
405
417
|
case '$l':
|
|
418
|
+
return __classPrivateFieldGet(this, _Namefully_fullName, "f").lastName.value[0];
|
|
406
419
|
case '$L':
|
|
407
|
-
return __classPrivateFieldGet(this, _Namefully_fullName, "f").lastName.initials()
|
|
420
|
+
return __classPrivateFieldGet(this, _Namefully_fullName, "f").lastName.initials().join('');
|
|
408
421
|
case '$m':
|
|
409
|
-
case '$M':
|
|
410
422
|
return this.hasMiddle ? this.middle[0] : undefined;
|
|
423
|
+
case '$M':
|
|
424
|
+
return this.hasMiddle ? __classPrivateFieldGet(this, _Namefully_fullName, "f").middleName.map((n) => n.value[0]).join('') : undefined;
|
|
411
425
|
default:
|
|
412
|
-
return undefined;
|
|
426
|
+
return constants_1.ALLOWED_FORMAT_TOKENS.includes(char) ? char : undefined;
|
|
413
427
|
}
|
|
414
428
|
};
|
|
415
429
|
exports.default = (names, options) => {
|