namefully 1.3.0 → 2.0.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/{lib → cjs}/builder.js +15 -13
- package/dist/cjs/config.js +100 -0
- package/dist/{lib → cjs}/constants.js +1 -1
- package/dist/{lib → cjs}/error.js +8 -5
- package/dist/cjs/fullname.js +102 -0
- package/dist/{lib → cjs}/index.js +18 -15
- package/dist/cjs/name.js +218 -0
- package/dist/cjs/namefully.js +391 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/parser.js +135 -0
- package/dist/{lib → cjs}/types.js +40 -36
- package/dist/{lib → cjs}/utils.js +17 -17
- package/dist/cjs/validator.js +266 -0
- package/dist/{types → esm}/builder.d.ts +8 -8
- package/dist/esm/builder.js +78 -0
- package/dist/{types → esm}/config.d.ts +1 -1
- package/dist/esm/config.js +96 -0
- package/dist/{types → esm}/constants.d.ts +1 -1
- package/dist/esm/constants.js +27 -0
- package/dist/{types → esm}/error.d.ts +2 -3
- package/dist/esm/error.js +87 -0
- package/dist/{types/full-name.d.ts → esm/fullname.d.ts} +3 -3
- package/dist/esm/fullname.js +98 -0
- package/dist/esm/index.d.ts +25 -0
- package/dist/esm/index.js +12 -0
- package/dist/{types → esm}/name.d.ts +2 -1
- package/dist/esm/name.js +211 -0
- package/dist/{types → esm}/namefully.d.ts +8 -8
- package/dist/esm/namefully.js +387 -0
- package/dist/esm/package.json +1 -0
- package/dist/{types → esm}/parser.d.ts +4 -4
- package/dist/esm/parser.js +127 -0
- package/dist/esm/types.js +106 -0
- package/dist/{types → esm}/utils.d.ts +1 -2
- package/dist/esm/utils.js +96 -0
- package/dist/{types → esm}/validator.d.ts +3 -3
- package/dist/esm/validator.js +259 -0
- package/dist/namefully.js +1580 -0
- package/dist/namefully.min.js +1 -0
- package/package.json +44 -27
- package/readme.md +1 -1
- package/dist/lib/config.js +0 -112
- package/dist/lib/full-name.js +0 -115
- package/dist/lib/name.js +0 -230
- package/dist/lib/namefully.js +0 -417
- package/dist/lib/parser.js +0 -144
- package/dist/lib/validator.js +0 -285
- package/dist/types/index.d.ts +0 -25
- package/dist/umd/namefully.js +0 -1931
- package/dist/umd/namefully.min.js +0 -1
- /package/dist/{types → esm}/types.d.ts +0 -0
package/dist/lib/namefully.js
DELETED
|
@@ -1,417 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
12
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
13
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
14
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
15
|
-
};
|
|
16
|
-
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
17
|
-
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
18
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
19
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
20
|
-
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
21
|
-
};
|
|
22
|
-
var _Namefully_instances, _Namefully_fullName, _Namefully_toParser, _Namefully_map;
|
|
23
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
24
|
-
exports.Namefully = void 0;
|
|
25
|
-
const constants_1 = require("./constants");
|
|
26
|
-
const error_1 = require("./error");
|
|
27
|
-
const parser_1 = require("./parser");
|
|
28
|
-
const types_1 = require("./types");
|
|
29
|
-
const utils_1 = require("./utils");
|
|
30
|
-
class Namefully {
|
|
31
|
-
constructor(names, options) {
|
|
32
|
-
_Namefully_instances.add(this);
|
|
33
|
-
_Namefully_fullName.set(this, void 0);
|
|
34
|
-
__classPrivateFieldSet(this, _Namefully_fullName, __classPrivateFieldGet(this, _Namefully_instances, "m", _Namefully_toParser).call(this, names).parse(options), "f");
|
|
35
|
-
}
|
|
36
|
-
static tryParse(text, index) {
|
|
37
|
-
try {
|
|
38
|
-
return new Namefully(parser_1.Parser.build(text, index));
|
|
39
|
-
}
|
|
40
|
-
catch (_a) {
|
|
41
|
-
return undefined;
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
static parse(text, index) {
|
|
45
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
46
|
-
return parser_1.Parser.buildAsync(text, index).then((parser) => new Namefully(parser));
|
|
47
|
-
});
|
|
48
|
-
}
|
|
49
|
-
get config() {
|
|
50
|
-
return __classPrivateFieldGet(this, _Namefully_fullName, "f").config;
|
|
51
|
-
}
|
|
52
|
-
get length() {
|
|
53
|
-
return this.birth.length;
|
|
54
|
-
}
|
|
55
|
-
get prefix() {
|
|
56
|
-
var _a;
|
|
57
|
-
return (_a = __classPrivateFieldGet(this, _Namefully_fullName, "f").prefix) === null || _a === void 0 ? void 0 : _a.toString();
|
|
58
|
-
}
|
|
59
|
-
get first() {
|
|
60
|
-
return this.firstName();
|
|
61
|
-
}
|
|
62
|
-
get middle() {
|
|
63
|
-
return this.hasMiddle ? this.middleName()[0] : undefined;
|
|
64
|
-
}
|
|
65
|
-
get hasMiddle() {
|
|
66
|
-
return __classPrivateFieldGet(this, _Namefully_fullName, "f").has(types_1.Namon.MIDDLE_NAME);
|
|
67
|
-
}
|
|
68
|
-
get last() {
|
|
69
|
-
return this.lastName();
|
|
70
|
-
}
|
|
71
|
-
get suffix() {
|
|
72
|
-
var _a;
|
|
73
|
-
return (_a = __classPrivateFieldGet(this, _Namefully_fullName, "f").suffix) === null || _a === void 0 ? void 0 : _a.toString();
|
|
74
|
-
}
|
|
75
|
-
get birth() {
|
|
76
|
-
return this.birthName();
|
|
77
|
-
}
|
|
78
|
-
get short() {
|
|
79
|
-
return this.shorten();
|
|
80
|
-
}
|
|
81
|
-
get long() {
|
|
82
|
-
return this.birth;
|
|
83
|
-
}
|
|
84
|
-
get full() {
|
|
85
|
-
return this.fullName();
|
|
86
|
-
}
|
|
87
|
-
get public() {
|
|
88
|
-
return this.format('f $l');
|
|
89
|
-
}
|
|
90
|
-
get salutation() {
|
|
91
|
-
return this.format('p l');
|
|
92
|
-
}
|
|
93
|
-
toString() {
|
|
94
|
-
return this.full;
|
|
95
|
-
}
|
|
96
|
-
get(namon) {
|
|
97
|
-
if (namon.equal(types_1.Namon.PREFIX))
|
|
98
|
-
return __classPrivateFieldGet(this, _Namefully_fullName, "f").prefix;
|
|
99
|
-
if (namon.equal(types_1.Namon.FIRST_NAME))
|
|
100
|
-
return __classPrivateFieldGet(this, _Namefully_fullName, "f").firstName;
|
|
101
|
-
if (namon.equal(types_1.Namon.MIDDLE_NAME))
|
|
102
|
-
return __classPrivateFieldGet(this, _Namefully_fullName, "f").middleName;
|
|
103
|
-
if (namon.equal(types_1.Namon.LAST_NAME))
|
|
104
|
-
return __classPrivateFieldGet(this, _Namefully_fullName, "f").lastName;
|
|
105
|
-
if (namon.equal(types_1.Namon.SUFFIX))
|
|
106
|
-
return __classPrivateFieldGet(this, _Namefully_fullName, "f").suffix;
|
|
107
|
-
return undefined;
|
|
108
|
-
}
|
|
109
|
-
equal(other) {
|
|
110
|
-
return this.toString() === other.toString();
|
|
111
|
-
}
|
|
112
|
-
toJson() {
|
|
113
|
-
return {
|
|
114
|
-
prefix: this.prefix,
|
|
115
|
-
firstName: this.first,
|
|
116
|
-
middleName: this.middleName(),
|
|
117
|
-
lastName: this.last,
|
|
118
|
-
suffix: this.suffix,
|
|
119
|
-
};
|
|
120
|
-
}
|
|
121
|
-
has(namon) {
|
|
122
|
-
return __classPrivateFieldGet(this, _Namefully_fullName, "f").has(namon);
|
|
123
|
-
}
|
|
124
|
-
fullName(orderedBy) {
|
|
125
|
-
const sep = this.config.ending ? ',' : '';
|
|
126
|
-
const names = [];
|
|
127
|
-
orderedBy = orderedBy || this.config.orderedBy;
|
|
128
|
-
if (this.prefix)
|
|
129
|
-
names.push(this.prefix);
|
|
130
|
-
if (orderedBy === types_1.NameOrder.FIRST_NAME) {
|
|
131
|
-
names.push(this.first, ...this.middleName(), this.last + sep);
|
|
132
|
-
}
|
|
133
|
-
else {
|
|
134
|
-
names.push(this.last, this.first, this.middleName().join(' ') + sep);
|
|
135
|
-
}
|
|
136
|
-
if (this.suffix)
|
|
137
|
-
names.push(this.suffix);
|
|
138
|
-
return names.join(' ').trim();
|
|
139
|
-
}
|
|
140
|
-
birthName(orderedBy) {
|
|
141
|
-
orderedBy = orderedBy || this.config.orderedBy;
|
|
142
|
-
return orderedBy === types_1.NameOrder.FIRST_NAME
|
|
143
|
-
? [this.first, ...this.middleName(), this.last].join(' ')
|
|
144
|
-
: [this.last, this.first, ...this.middleName()].join(' ');
|
|
145
|
-
}
|
|
146
|
-
firstName(withMore = true) {
|
|
147
|
-
return __classPrivateFieldGet(this, _Namefully_fullName, "f").firstName.toString(withMore);
|
|
148
|
-
}
|
|
149
|
-
middleName() {
|
|
150
|
-
return __classPrivateFieldGet(this, _Namefully_fullName, "f").middleName.map((n) => n.value);
|
|
151
|
-
}
|
|
152
|
-
lastName(format) {
|
|
153
|
-
return __classPrivateFieldGet(this, _Namefully_fullName, "f").lastName.toString(format);
|
|
154
|
-
}
|
|
155
|
-
initials(options) {
|
|
156
|
-
const firstInits = __classPrivateFieldGet(this, _Namefully_fullName, "f").firstName.initials();
|
|
157
|
-
const midInits = __classPrivateFieldGet(this, _Namefully_fullName, "f").middleName.map((n) => n.initials()[0]);
|
|
158
|
-
const lastInits = __classPrivateFieldGet(this, _Namefully_fullName, "f").lastName.initials();
|
|
159
|
-
if (options === null || options === void 0 ? void 0 : options.asJson)
|
|
160
|
-
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
|
-
if (only !== types_1.NameType.BIRTH_NAME) {
|
|
164
|
-
if (only === types_1.NameType.FIRST_NAME) {
|
|
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
|
-
}
|
|
173
|
-
}
|
|
174
|
-
else if (orderedBy === types_1.NameOrder.FIRST_NAME) {
|
|
175
|
-
initials.push(...firstInits, ...midInits, ...lastInits);
|
|
176
|
-
}
|
|
177
|
-
else {
|
|
178
|
-
initials.push(...lastInits, ...firstInits, ...midInits);
|
|
179
|
-
}
|
|
180
|
-
return initials;
|
|
181
|
-
}
|
|
182
|
-
shorten(orderedBy) {
|
|
183
|
-
orderedBy = orderedBy || this.config.orderedBy;
|
|
184
|
-
return orderedBy === types_1.NameOrder.FIRST_NAME
|
|
185
|
-
? [__classPrivateFieldGet(this, _Namefully_fullName, "f").firstName.value, __classPrivateFieldGet(this, _Namefully_fullName, "f").lastName.toString()].join(' ')
|
|
186
|
-
: [__classPrivateFieldGet(this, _Namefully_fullName, "f").lastName.toString(), __classPrivateFieldGet(this, _Namefully_fullName, "f").firstName.value].join(' ');
|
|
187
|
-
}
|
|
188
|
-
flatten(options) {
|
|
189
|
-
if (this.length <= options.limit)
|
|
190
|
-
return this.full;
|
|
191
|
-
const { by = types_1.Flat.MIDDLE_NAME, limit = 20, recursive = false, withMore = false, withPeriod = true, surname, } = options;
|
|
192
|
-
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
|
-
const hasMid = this.hasMiddle;
|
|
197
|
-
const f = __classPrivateFieldGet(this, _Namefully_fullName, "f").firstName.initials(withMore).join(sep + ' ') + sep;
|
|
198
|
-
const l = __classPrivateFieldGet(this, _Namefully_fullName, "f").lastName.initials(surname).join(sep + ' ') + sep;
|
|
199
|
-
const m = hasMid ? __classPrivateFieldGet(this, _Namefully_fullName, "f").middleName.map((n) => n.initials()[0]).join(sep + ' ') + sep : '';
|
|
200
|
-
let name = [];
|
|
201
|
-
if (this.config.orderedBy === types_1.NameOrder.FIRST_NAME) {
|
|
202
|
-
switch (by) {
|
|
203
|
-
case types_1.Flat.FIRST_NAME:
|
|
204
|
-
name = hasMid ? [f, mn, ln] : [f, ln];
|
|
205
|
-
break;
|
|
206
|
-
case types_1.Flat.LAST_NAME:
|
|
207
|
-
name = hasMid ? [fn, mn, l] : [fn, l];
|
|
208
|
-
break;
|
|
209
|
-
case types_1.Flat.MIDDLE_NAME:
|
|
210
|
-
name = hasMid ? [fn, m, ln] : [fn, ln];
|
|
211
|
-
break;
|
|
212
|
-
case types_1.Flat.FIRST_MID:
|
|
213
|
-
name = hasMid ? [f, m, ln] : [f, ln];
|
|
214
|
-
break;
|
|
215
|
-
case types_1.Flat.MID_LAST:
|
|
216
|
-
name = hasMid ? [fn, m, l] : [fn, l];
|
|
217
|
-
break;
|
|
218
|
-
case types_1.Flat.ALL:
|
|
219
|
-
name = hasMid ? [f, m, l] : [f, l];
|
|
220
|
-
break;
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
else {
|
|
224
|
-
switch (by) {
|
|
225
|
-
case types_1.Flat.FIRST_NAME:
|
|
226
|
-
name = hasMid ? [ln, f, mn] : [ln, f];
|
|
227
|
-
break;
|
|
228
|
-
case types_1.Flat.LAST_NAME:
|
|
229
|
-
name = hasMid ? [l, fn, mn] : [l, fn];
|
|
230
|
-
break;
|
|
231
|
-
case types_1.Flat.MIDDLE_NAME:
|
|
232
|
-
name = hasMid ? [ln, fn, m] : [ln, fn];
|
|
233
|
-
break;
|
|
234
|
-
case types_1.Flat.FIRST_MID:
|
|
235
|
-
name = hasMid ? [ln, f, m] : [ln, f];
|
|
236
|
-
break;
|
|
237
|
-
case types_1.Flat.MID_LAST:
|
|
238
|
-
name = hasMid ? [l, fn, m] : [l, fn];
|
|
239
|
-
break;
|
|
240
|
-
case types_1.Flat.ALL:
|
|
241
|
-
name = hasMid ? [l, f, m] : [l, f];
|
|
242
|
-
break;
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
const flat = name.join(' ');
|
|
246
|
-
if (recursive && flat.length > limit) {
|
|
247
|
-
const next = by === types_1.Flat.FIRST_NAME
|
|
248
|
-
? types_1.Flat.MIDDLE_NAME
|
|
249
|
-
: by === types_1.Flat.MIDDLE_NAME
|
|
250
|
-
? types_1.Flat.LAST_NAME
|
|
251
|
-
: by === types_1.Flat.LAST_NAME
|
|
252
|
-
? types_1.Flat.FIRST_MID
|
|
253
|
-
: by === types_1.Flat.FIRST_MID
|
|
254
|
-
? types_1.Flat.MID_LAST
|
|
255
|
-
: by === types_1.Flat.MID_LAST
|
|
256
|
-
? types_1.Flat.ALL
|
|
257
|
-
: by === types_1.Flat.ALL
|
|
258
|
-
? types_1.Flat.ALL
|
|
259
|
-
: by;
|
|
260
|
-
if (next === by)
|
|
261
|
-
return flat;
|
|
262
|
-
return this.flatten(Object.assign(Object.assign({}, options), { by: next }));
|
|
263
|
-
}
|
|
264
|
-
return flat;
|
|
265
|
-
}
|
|
266
|
-
zip(by = types_1.Flat.MID_LAST, withPeriod = true) {
|
|
267
|
-
return this.flatten({ limit: 0, by, withPeriod });
|
|
268
|
-
}
|
|
269
|
-
format(pattern) {
|
|
270
|
-
var _a;
|
|
271
|
-
if (pattern === 'short')
|
|
272
|
-
return this.short;
|
|
273
|
-
if (pattern === 'long')
|
|
274
|
-
return this.long;
|
|
275
|
-
if (pattern === 'public')
|
|
276
|
-
return this.public;
|
|
277
|
-
if (pattern === 'official')
|
|
278
|
-
pattern = 'o';
|
|
279
|
-
let group = '';
|
|
280
|
-
const formatted = [];
|
|
281
|
-
for (const char of pattern.split('')) {
|
|
282
|
-
if (constants_1.ALLOWED_TOKENS.indexOf(char) === -1) {
|
|
283
|
-
throw new error_1.NotAllowedError({
|
|
284
|
-
source: this.full,
|
|
285
|
-
operation: 'format',
|
|
286
|
-
message: `unsupported character <${char}> from ${pattern}.`,
|
|
287
|
-
});
|
|
288
|
-
}
|
|
289
|
-
group += char;
|
|
290
|
-
if (char === '$')
|
|
291
|
-
continue;
|
|
292
|
-
formatted.push((_a = __classPrivateFieldGet(this, _Namefully_instances, "m", _Namefully_map).call(this, group)) !== null && _a !== void 0 ? _a : '');
|
|
293
|
-
group = '';
|
|
294
|
-
}
|
|
295
|
-
return formatted.join('').trim();
|
|
296
|
-
}
|
|
297
|
-
flip() {
|
|
298
|
-
this.config.updateOrder(this.config.orderedBy === types_1.NameOrder.FIRST_NAME ? types_1.NameOrder.LAST_NAME : types_1.NameOrder.FIRST_NAME);
|
|
299
|
-
}
|
|
300
|
-
split(separator = /[' -]/g) {
|
|
301
|
-
return this.birth.replace(separator, ' ').split(' ');
|
|
302
|
-
}
|
|
303
|
-
join(separator = '') {
|
|
304
|
-
return this.split().join(separator);
|
|
305
|
-
}
|
|
306
|
-
toUpperCase() {
|
|
307
|
-
return this.birth.toUpperCase();
|
|
308
|
-
}
|
|
309
|
-
toLowerCase() {
|
|
310
|
-
return this.birth.toLowerCase();
|
|
311
|
-
}
|
|
312
|
-
toCamelCase() {
|
|
313
|
-
return (0, utils_1.decapitalize)(this.toPascalCase());
|
|
314
|
-
}
|
|
315
|
-
toPascalCase() {
|
|
316
|
-
return this.split()
|
|
317
|
-
.map((n) => (0, utils_1.capitalize)(n))
|
|
318
|
-
.join('');
|
|
319
|
-
}
|
|
320
|
-
toSnakeCase() {
|
|
321
|
-
return this.split()
|
|
322
|
-
.map((n) => n.toLowerCase())
|
|
323
|
-
.join('_');
|
|
324
|
-
}
|
|
325
|
-
toHyphenCase() {
|
|
326
|
-
return this.split()
|
|
327
|
-
.map((n) => n.toLowerCase())
|
|
328
|
-
.join('-');
|
|
329
|
-
}
|
|
330
|
-
toDotCase() {
|
|
331
|
-
return this.split()
|
|
332
|
-
.map((n) => n.toLowerCase())
|
|
333
|
-
.join('.');
|
|
334
|
-
}
|
|
335
|
-
toToggleCase() {
|
|
336
|
-
return (0, utils_1.toggleCase)(this.birth);
|
|
337
|
-
}
|
|
338
|
-
}
|
|
339
|
-
exports.Namefully = Namefully;
|
|
340
|
-
_Namefully_fullName = new WeakMap(), _Namefully_instances = new WeakSet(), _Namefully_toParser = function _Namefully_toParser(raw) {
|
|
341
|
-
if (raw instanceof parser_1.Parser)
|
|
342
|
-
return raw;
|
|
343
|
-
if (typeof raw === 'string')
|
|
344
|
-
return new parser_1.StringParser(raw);
|
|
345
|
-
if ((0, utils_1.isStringArray)(raw))
|
|
346
|
-
return new parser_1.ArrayStringParser(raw);
|
|
347
|
-
if ((0, utils_1.isNameArray)(raw))
|
|
348
|
-
return new parser_1.ArrayNameParser(raw);
|
|
349
|
-
if (typeof raw === 'object')
|
|
350
|
-
return new parser_1.NamaParser(raw);
|
|
351
|
-
throw new error_1.InputError({ source: raw, message: 'Cannot parse raw data. Review expected data types.' });
|
|
352
|
-
}, _Namefully_map = function _Namefully_map(char) {
|
|
353
|
-
var _a, _b;
|
|
354
|
-
switch (char) {
|
|
355
|
-
case '.':
|
|
356
|
-
case ',':
|
|
357
|
-
case ' ':
|
|
358
|
-
case '-':
|
|
359
|
-
case '_':
|
|
360
|
-
return char;
|
|
361
|
-
case 'b':
|
|
362
|
-
return this.birth;
|
|
363
|
-
case 'B':
|
|
364
|
-
return this.birth.toUpperCase();
|
|
365
|
-
case 'f':
|
|
366
|
-
return this.first;
|
|
367
|
-
case 'F':
|
|
368
|
-
return this.first.toUpperCase();
|
|
369
|
-
case 'l':
|
|
370
|
-
return this.last;
|
|
371
|
-
case 'L':
|
|
372
|
-
return this.last.toUpperCase();
|
|
373
|
-
case 'm':
|
|
374
|
-
case 'M':
|
|
375
|
-
return char === 'm' ? this.middleName().join(' ') : this.middleName().join(' ').toUpperCase();
|
|
376
|
-
case 'o':
|
|
377
|
-
case 'O':
|
|
378
|
-
return ((character) => {
|
|
379
|
-
const sep = this.config.ending ? ',' : '', names = [];
|
|
380
|
-
if (this.prefix)
|
|
381
|
-
names.push(this.prefix);
|
|
382
|
-
names.push(`${this.last},`.toUpperCase());
|
|
383
|
-
if (this.hasMiddle) {
|
|
384
|
-
names.push(this.first, this.middleName().join(' ') + sep);
|
|
385
|
-
}
|
|
386
|
-
else {
|
|
387
|
-
names.push(this.first + sep);
|
|
388
|
-
}
|
|
389
|
-
if (this.suffix)
|
|
390
|
-
names.push(this.suffix);
|
|
391
|
-
const nama = names.join(' ').trim();
|
|
392
|
-
return character === 'o' ? nama : nama.toUpperCase();
|
|
393
|
-
})(char);
|
|
394
|
-
case 'p':
|
|
395
|
-
return this.prefix;
|
|
396
|
-
case 'P':
|
|
397
|
-
return (_a = this.prefix) === null || _a === void 0 ? void 0 : _a.toUpperCase();
|
|
398
|
-
case 's':
|
|
399
|
-
return this.suffix;
|
|
400
|
-
case 'S':
|
|
401
|
-
return (_b = this.suffix) === null || _b === void 0 ? void 0 : _b.toUpperCase();
|
|
402
|
-
case '$f':
|
|
403
|
-
case '$F':
|
|
404
|
-
return __classPrivateFieldGet(this, _Namefully_fullName, "f").firstName.initials()[0];
|
|
405
|
-
case '$l':
|
|
406
|
-
case '$L':
|
|
407
|
-
return __classPrivateFieldGet(this, _Namefully_fullName, "f").lastName.initials()[0];
|
|
408
|
-
case '$m':
|
|
409
|
-
case '$M':
|
|
410
|
-
return this.hasMiddle ? this.middle[0] : undefined;
|
|
411
|
-
default:
|
|
412
|
-
return undefined;
|
|
413
|
-
}
|
|
414
|
-
};
|
|
415
|
-
exports.default = (names, options) => {
|
|
416
|
-
return new Namefully(names, options);
|
|
417
|
-
};
|
package/dist/lib/parser.js
DELETED
|
@@ -1,144 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
3
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
4
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
5
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
6
|
-
};
|
|
7
|
-
var _NamaParser_instances, _NamaParser_asNama;
|
|
8
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
-
exports.ArrayNameParser = exports.NamaParser = exports.ArrayStringParser = exports.StringParser = exports.Parser = void 0;
|
|
10
|
-
const full_name_1 = require("./full-name");
|
|
11
|
-
const config_1 = require("./config");
|
|
12
|
-
const utils_1 = require("./utils");
|
|
13
|
-
const validator_1 = require("./validator");
|
|
14
|
-
const name_1 = require("./name");
|
|
15
|
-
const types_1 = require("./types");
|
|
16
|
-
const error_1 = require("./error");
|
|
17
|
-
class Parser {
|
|
18
|
-
constructor(raw) {
|
|
19
|
-
this.raw = raw;
|
|
20
|
-
}
|
|
21
|
-
static build(text, index) {
|
|
22
|
-
const parts = text.trim().split(types_1.Separator.SPACE.token);
|
|
23
|
-
const length = parts.length;
|
|
24
|
-
if (index instanceof utils_1.NameIndex) {
|
|
25
|
-
const names = Object.entries(index.toJson())
|
|
26
|
-
.filter(([, position]) => position > -1 && position < length)
|
|
27
|
-
.map(([key, position]) => new name_1.Name(parts[position], types_1.Namon.all.get(key)));
|
|
28
|
-
return new ArrayNameParser(names);
|
|
29
|
-
}
|
|
30
|
-
if (length < 2) {
|
|
31
|
-
throw new error_1.InputError({
|
|
32
|
-
source: text,
|
|
33
|
-
message: 'cannot build from invalid input',
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
else if (length === 2 || length === 3) {
|
|
37
|
-
return new StringParser(text);
|
|
38
|
-
}
|
|
39
|
-
else {
|
|
40
|
-
const last = parts.pop();
|
|
41
|
-
const [first, ...middles] = parts;
|
|
42
|
-
return new ArrayStringParser([first, middles.join(' '), last]);
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
static buildAsync(text, index) {
|
|
46
|
-
try {
|
|
47
|
-
return Promise.resolve(Parser.build(text, index));
|
|
48
|
-
}
|
|
49
|
-
catch (error) {
|
|
50
|
-
return Promise.reject(error);
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
exports.Parser = Parser;
|
|
55
|
-
class StringParser extends Parser {
|
|
56
|
-
parse(options) {
|
|
57
|
-
const config = config_1.Config.merge(options);
|
|
58
|
-
const names = this.raw.split(config.separator.token);
|
|
59
|
-
return new ArrayStringParser(names).parse(options);
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
exports.StringParser = StringParser;
|
|
63
|
-
class ArrayStringParser extends Parser {
|
|
64
|
-
parse(options) {
|
|
65
|
-
const config = config_1.Config.merge(options);
|
|
66
|
-
const fullName = new full_name_1.FullName(config);
|
|
67
|
-
const raw = this.raw.map((n) => n.trim());
|
|
68
|
-
const index = utils_1.NameIndex.when(config.orderedBy, raw.length);
|
|
69
|
-
const validator = new validator_1.ArrayStringValidator(index);
|
|
70
|
-
if (config.bypass) {
|
|
71
|
-
validator.validateIndex(raw);
|
|
72
|
-
}
|
|
73
|
-
else {
|
|
74
|
-
validator.validate(raw);
|
|
75
|
-
}
|
|
76
|
-
const { firstName, lastName, middleName, prefix, suffix } = index;
|
|
77
|
-
fullName.setFirstName(new name_1.FirstName(raw[firstName]));
|
|
78
|
-
fullName.setLastName(new name_1.LastName(raw[lastName]));
|
|
79
|
-
if (raw.length >= 3)
|
|
80
|
-
fullName.setMiddleName(raw[middleName].split(config.separator.token));
|
|
81
|
-
if (raw.length >= 4)
|
|
82
|
-
fullName.setPrefix(name_1.Name.prefix(raw[prefix]));
|
|
83
|
-
if (raw.length === 5)
|
|
84
|
-
fullName.setSuffix(name_1.Name.suffix(raw[suffix]));
|
|
85
|
-
return fullName;
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
exports.ArrayStringParser = ArrayStringParser;
|
|
89
|
-
class NamaParser extends Parser {
|
|
90
|
-
constructor() {
|
|
91
|
-
super(...arguments);
|
|
92
|
-
_NamaParser_instances.add(this);
|
|
93
|
-
}
|
|
94
|
-
parse(options) {
|
|
95
|
-
const config = config_1.Config.merge(options);
|
|
96
|
-
if (config.bypass) {
|
|
97
|
-
validator_1.NamaValidator.create().validateKeys(__classPrivateFieldGet(this, _NamaParser_instances, "m", _NamaParser_asNama).call(this));
|
|
98
|
-
}
|
|
99
|
-
else {
|
|
100
|
-
validator_1.NamaValidator.create().validate(__classPrivateFieldGet(this, _NamaParser_instances, "m", _NamaParser_asNama).call(this));
|
|
101
|
-
}
|
|
102
|
-
return full_name_1.FullName.parse(this.raw, config);
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
exports.NamaParser = NamaParser;
|
|
106
|
-
_NamaParser_instances = new WeakSet(), _NamaParser_asNama = function _NamaParser_asNama() {
|
|
107
|
-
return new Map(Object.entries(this.raw).map(([key, value]) => {
|
|
108
|
-
const namon = types_1.Namon.cast(key);
|
|
109
|
-
if (!namon) {
|
|
110
|
-
throw new error_1.InputError({
|
|
111
|
-
source: Object.values(this.raw).join(' '),
|
|
112
|
-
message: `unsupported key "${key}"`,
|
|
113
|
-
});
|
|
114
|
-
}
|
|
115
|
-
return [namon, value];
|
|
116
|
-
}));
|
|
117
|
-
};
|
|
118
|
-
class ArrayNameParser extends Parser {
|
|
119
|
-
parse(options) {
|
|
120
|
-
const config = config_1.Config.merge(options);
|
|
121
|
-
const fullName = new full_name_1.FullName(config);
|
|
122
|
-
validator_1.ArrayNameValidator.create().validate(this.raw);
|
|
123
|
-
for (const name of this.raw) {
|
|
124
|
-
if (name.isPrefix) {
|
|
125
|
-
fullName.setPrefix(name);
|
|
126
|
-
}
|
|
127
|
-
else if (name.isSuffix) {
|
|
128
|
-
fullName.setSuffix(name);
|
|
129
|
-
}
|
|
130
|
-
else if (name.isFirstName) {
|
|
131
|
-
fullName.setFirstName(name instanceof name_1.FirstName ? name : new name_1.FirstName(name.value));
|
|
132
|
-
}
|
|
133
|
-
else if (name.isMiddleName) {
|
|
134
|
-
fullName.middleName.push(name);
|
|
135
|
-
}
|
|
136
|
-
else if (name.isLastName) {
|
|
137
|
-
const lastName = new name_1.LastName(name.value, name instanceof name_1.LastName ? name.mother : undefined, config.surname);
|
|
138
|
-
fullName.setLastName(lastName);
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
return fullName;
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
exports.ArrayNameParser = ArrayNameParser;
|