namefully 1.2.0 → 1.2.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 +67 -144
- package/dist/lib/constants.js +2 -3
- package/dist/lib/error.js +3 -121
- package/dist/lib/full-name.js +46 -78
- package/dist/lib/index.js +9 -15
- package/dist/lib/name.js +57 -149
- package/dist/lib/namefully.js +128 -441
- package/dist/lib/parser.js +33 -68
- package/dist/lib/types.js +6 -80
- package/dist/lib/utils.js +14 -60
- package/dist/lib/validator.js +46 -77
- package/dist/{lib/src → types}/config.d.ts +9 -33
- package/dist/{lib/src → types}/constants.d.ts +1 -1
- package/dist/{lib → types}/error.d.ts +8 -22
- package/dist/{lib → types}/full-name.d.ts +7 -24
- package/dist/{lib → types}/index.d.ts +1 -0
- package/dist/{lib/src → types}/name.d.ts +30 -81
- package/dist/{lib → types}/namefully.d.ts +29 -95
- package/dist/{lib → types}/parser.d.ts +1 -6
- package/dist/{lib/src → types}/types.d.ts +1 -1
- package/dist/{lib → types}/utils.d.ts +5 -15
- package/dist/{lib → types}/validator.d.ts +7 -16
- package/dist/umd/namefully.js +455 -1270
- package/dist/umd/namefully.min.js +1 -3
- package/package.json +44 -50
- package/readme.md +11 -10
- package/dist/example/index.js +0 -2734
- package/dist/lib/config.d.ts +0 -121
- package/dist/lib/config.js.map +0 -1
- package/dist/lib/constants.d.ts +0 -4
- package/dist/lib/constants.js.map +0 -1
- package/dist/lib/error.js.map +0 -1
- package/dist/lib/example/example.d.ts +0 -1
- package/dist/lib/full-name.js.map +0 -1
- package/dist/lib/index.js.map +0 -1
- package/dist/lib/name.d.ts +0 -177
- package/dist/lib/name.js.map +0 -1
- package/dist/lib/namefully.js.map +0 -1
- package/dist/lib/parser.js.map +0 -1
- package/dist/lib/src/error.d.ts +0 -172
- package/dist/lib/src/full-name.d.ts +0 -71
- package/dist/lib/src/index.d.ts +0 -20
- package/dist/lib/src/namefully.d.ts +0 -379
- package/dist/lib/src/parser.d.ts +0 -46
- package/dist/lib/src/utils.d.ts +0 -63
- package/dist/lib/src/validator.d.ts +0 -66
- package/dist/lib/types.d.ts +0 -127
- package/dist/lib/types.js.map +0 -1
- package/dist/lib/utils.js.map +0 -1
- package/dist/lib/validator.js.map +0 -1
- package/dist/umd/namefully.js.map +0 -1
- package/dist/umd/namefully.min.js.LICENSE.txt +0 -12
- package/dist/umd/namefully.min.js.map +0 -1
package/dist/lib/config.js
CHANGED
|
@@ -1,189 +1,112 @@
|
|
|
1
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 __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
8
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
9
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
10
|
+
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");
|
|
11
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
12
|
+
};
|
|
13
|
+
var _Config_instances, _a, _Config_name, _Config_orderedBy, _Config_separator, _Config_title, _Config_ending, _Config_bypass, _Config_surname, _Config_genNewName;
|
|
2
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
15
|
exports.Config = void 0;
|
|
4
16
|
const types_1 = require("./types");
|
|
5
17
|
const defaultName = 'default';
|
|
6
18
|
const copyAlias = '_copy';
|
|
7
|
-
/**
|
|
8
|
-
* The Configuration to use across the other components.
|
|
9
|
-
*
|
|
10
|
-
* The multiton pattern is used to handle configurations across the `namefully`
|
|
11
|
-
* setup. This adds consistency when building other components such as `FirstName`,
|
|
12
|
-
* `LastName`, or `Name` of distinct types that may be of particular shapes.
|
|
13
|
-
*
|
|
14
|
-
* For example, a person's `FullName` may appear by:
|
|
15
|
-
* - NameOrder.FIRST_NAME: `Jon Snow` or
|
|
16
|
-
* - NameOrder.LAST_NAME: `Snow Jon`.
|
|
17
|
-
*
|
|
18
|
-
* `Config` makes it easy to set up a specific configuration for `Namefully`
|
|
19
|
-
* and reuse it through other instances or components along the way. If a new
|
|
20
|
-
* `Config` is needed, a named configuration may be created. It is actually
|
|
21
|
-
* advised to use named `Config.create(name)` instead as it may help mitigate issues
|
|
22
|
-
* and avoid confusion and ambiguity in the future. Plus, a named configuration
|
|
23
|
-
* explains its purpose.
|
|
24
|
-
*
|
|
25
|
-
* ```ts
|
|
26
|
-
* const defaultConfig = Config.create();
|
|
27
|
-
* const mergedConfig = Config.merge({ name: 'other', title: Title.US });
|
|
28
|
-
* const copyConfig = mergedConfig.copyWith({ ending: true });
|
|
29
|
-
* ```
|
|
30
|
-
*
|
|
31
|
-
* Additionally, a configuration may be merged with or copied from an existing
|
|
32
|
-
* configuration, prioritizing the new one's values, as shown in the example
|
|
33
|
-
* above.
|
|
34
|
-
*/
|
|
35
19
|
class Config {
|
|
36
|
-
constructor(name, orderedBy = types_1.NameOrder.FIRST_NAME, separator = types_1.Separator.SPACE, title = types_1.Title.UK, ending = false, bypass = true, surname = types_1.Surname.FATHER) {
|
|
37
|
-
this._name = name;
|
|
38
|
-
this._orderedBy = orderedBy;
|
|
39
|
-
this._separator = separator;
|
|
40
|
-
this._title = title;
|
|
41
|
-
this._ending = ending;
|
|
42
|
-
this._bypass = bypass;
|
|
43
|
-
this._surname = surname;
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* The order of appearance of a full name.
|
|
47
|
-
*/
|
|
48
20
|
get orderedBy() {
|
|
49
|
-
return this
|
|
21
|
+
return __classPrivateFieldGet(this, _Config_orderedBy, "f");
|
|
50
22
|
}
|
|
51
|
-
/**
|
|
52
|
-
* The token used to indicate how to split string values.
|
|
53
|
-
*/
|
|
54
23
|
get separator() {
|
|
55
|
-
return this
|
|
24
|
+
return __classPrivateFieldGet(this, _Config_separator, "f");
|
|
56
25
|
}
|
|
57
|
-
/**
|
|
58
|
-
* The abbreviation type to indicate whether or not to add period to a prefix
|
|
59
|
-
* using the American or British way.
|
|
60
|
-
*/
|
|
61
26
|
get title() {
|
|
62
|
-
return this
|
|
27
|
+
return __classPrivateFieldGet(this, _Config_title, "f");
|
|
63
28
|
}
|
|
64
|
-
/**
|
|
65
|
-
* The option indicating if an ending suffix is used in a formal way.
|
|
66
|
-
*/
|
|
67
29
|
get ending() {
|
|
68
|
-
return this
|
|
30
|
+
return __classPrivateFieldGet(this, _Config_ending, "f");
|
|
69
31
|
}
|
|
70
|
-
/**
|
|
71
|
-
* A bypass of the validation rules with this option. This option is ideal
|
|
72
|
-
* to avoid checking their validity.
|
|
73
|
-
*/
|
|
74
32
|
get bypass() {
|
|
75
|
-
return this
|
|
33
|
+
return __classPrivateFieldGet(this, _Config_bypass, "f");
|
|
76
34
|
}
|
|
77
|
-
/**
|
|
78
|
-
* An option indicating how to format a surname.
|
|
79
|
-
*
|
|
80
|
-
* The supported formats are:
|
|
81
|
-
* - `FATHER` name only
|
|
82
|
-
* - `MOTHER` name only
|
|
83
|
-
* - `HYPHENATED`, joining both father and mother names with a hyphen
|
|
84
|
-
* - `ALL`, joining both father and mother names with a space.
|
|
85
|
-
*
|
|
86
|
-
* Note that this option can be set when creating a `LastName`. As this can
|
|
87
|
-
* become ambiguous at the time of handling it, the value set in this is
|
|
88
|
-
* prioritized and viewed as the source of truth for future considerations.
|
|
89
|
-
*/
|
|
90
35
|
get surname() {
|
|
91
|
-
return this
|
|
36
|
+
return __classPrivateFieldGet(this, _Config_surname, "f");
|
|
92
37
|
}
|
|
93
|
-
/**
|
|
94
|
-
* The name of the cached configuration.
|
|
95
|
-
*/
|
|
96
38
|
get name() {
|
|
97
|
-
return this
|
|
39
|
+
return __classPrivateFieldGet(this, _Config_name, "f");
|
|
40
|
+
}
|
|
41
|
+
constructor(name, orderedBy = types_1.NameOrder.FIRST_NAME, separator = types_1.Separator.SPACE, title = types_1.Title.UK, ending = false, bypass = true, surname = types_1.Surname.FATHER) {
|
|
42
|
+
_Config_instances.add(this);
|
|
43
|
+
_Config_name.set(this, void 0);
|
|
44
|
+
_Config_orderedBy.set(this, void 0);
|
|
45
|
+
_Config_separator.set(this, void 0);
|
|
46
|
+
_Config_title.set(this, void 0);
|
|
47
|
+
_Config_ending.set(this, void 0);
|
|
48
|
+
_Config_bypass.set(this, void 0);
|
|
49
|
+
_Config_surname.set(this, void 0);
|
|
50
|
+
__classPrivateFieldSet(this, _Config_name, name, "f");
|
|
51
|
+
__classPrivateFieldSet(this, _Config_orderedBy, orderedBy, "f");
|
|
52
|
+
__classPrivateFieldSet(this, _Config_separator, separator, "f");
|
|
53
|
+
__classPrivateFieldSet(this, _Config_title, title, "f");
|
|
54
|
+
__classPrivateFieldSet(this, _Config_ending, ending, "f");
|
|
55
|
+
__classPrivateFieldSet(this, _Config_bypass, bypass, "f");
|
|
56
|
+
__classPrivateFieldSet(this, _Config_surname, surname, "f");
|
|
98
57
|
}
|
|
99
|
-
/**
|
|
100
|
-
* Returns a named configuration with default values.
|
|
101
|
-
* @param name describing its purpose.
|
|
102
|
-
*/
|
|
103
58
|
static create(name = defaultName) {
|
|
104
|
-
if (!
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
return Config.cache.get(name);
|
|
59
|
+
if (!_a.cache.has(name))
|
|
60
|
+
_a.cache.set(name, new this(name));
|
|
61
|
+
return _a.cache.get(name);
|
|
108
62
|
}
|
|
109
|
-
/**
|
|
110
|
-
* Returns a combined version of the existing values of the default configuration
|
|
111
|
-
* and the provided optional values of another configuration.
|
|
112
|
-
* @param other partial config to be combined with.
|
|
113
|
-
*/
|
|
114
63
|
static merge(other) {
|
|
115
|
-
var
|
|
64
|
+
var _b, _c, _d, _e, _f, _g;
|
|
116
65
|
if (!other) {
|
|
117
|
-
return
|
|
66
|
+
return _a.create();
|
|
118
67
|
}
|
|
119
68
|
else {
|
|
120
|
-
const config =
|
|
121
|
-
config
|
|
122
|
-
config
|
|
123
|
-
config
|
|
124
|
-
config
|
|
125
|
-
config
|
|
126
|
-
config
|
|
69
|
+
const config = _a.create(other.name);
|
|
70
|
+
__classPrivateFieldSet(config, _Config_orderedBy, (_b = other.orderedBy) !== null && _b !== void 0 ? _b : config.orderedBy, "f");
|
|
71
|
+
__classPrivateFieldSet(config, _Config_separator, (_c = other.separator) !== null && _c !== void 0 ? _c : config.separator, "f");
|
|
72
|
+
__classPrivateFieldSet(config, _Config_title, (_d = other.title) !== null && _d !== void 0 ? _d : config.title, "f");
|
|
73
|
+
__classPrivateFieldSet(config, _Config_ending, (_e = other.ending) !== null && _e !== void 0 ? _e : config.ending, "f");
|
|
74
|
+
__classPrivateFieldSet(config, _Config_bypass, (_f = other.bypass) !== null && _f !== void 0 ? _f : config.bypass, "f");
|
|
75
|
+
__classPrivateFieldSet(config, _Config_surname, (_g = other.surname) !== null && _g !== void 0 ? _g : config.surname, "f");
|
|
127
76
|
return config;
|
|
128
77
|
}
|
|
129
78
|
}
|
|
130
|
-
/**
|
|
131
|
-
* Returns a copy of this configuration merged with the provided values.
|
|
132
|
-
*
|
|
133
|
-
* The word `_copy` is added to the existing config's name to create the new
|
|
134
|
-
* config's name if the name already exists for previous configurations. This
|
|
135
|
-
* is useful to maintain the uniqueness of each configuration. For example,
|
|
136
|
-
* if the new copy is made from the default configuration, this new copy will
|
|
137
|
-
* be named `default_copy`.
|
|
138
|
-
*/
|
|
139
79
|
copyWith(options = {}) {
|
|
140
80
|
const { name, orderedBy, separator, title, ending, bypass, surname } = options;
|
|
141
|
-
const config =
|
|
142
|
-
config
|
|
143
|
-
config
|
|
144
|
-
config
|
|
145
|
-
config
|
|
146
|
-
config
|
|
147
|
-
config
|
|
81
|
+
const config = _a.create(__classPrivateFieldGet(this, _Config_instances, "m", _Config_genNewName).call(this, name !== null && name !== void 0 ? name : this.name + copyAlias));
|
|
82
|
+
__classPrivateFieldSet(config, _Config_orderedBy, orderedBy !== null && orderedBy !== void 0 ? orderedBy : this.orderedBy, "f");
|
|
83
|
+
__classPrivateFieldSet(config, _Config_separator, separator !== null && separator !== void 0 ? separator : this.separator, "f");
|
|
84
|
+
__classPrivateFieldSet(config, _Config_title, title !== null && title !== void 0 ? title : this.title, "f");
|
|
85
|
+
__classPrivateFieldSet(config, _Config_ending, ending !== null && ending !== void 0 ? ending : this.ending, "f");
|
|
86
|
+
__classPrivateFieldSet(config, _Config_bypass, bypass !== null && bypass !== void 0 ? bypass : this.bypass, "f");
|
|
87
|
+
__classPrivateFieldSet(config, _Config_surname, surname !== null && surname !== void 0 ? surname : this.surname, "f");
|
|
148
88
|
return config;
|
|
149
89
|
}
|
|
150
|
-
/**
|
|
151
|
-
* Makes an exact copy of the current configuration.
|
|
152
|
-
*/
|
|
153
90
|
clone() {
|
|
154
91
|
return this.copyWith();
|
|
155
92
|
}
|
|
156
|
-
/**
|
|
157
|
-
* Resets the configuration by setting it back to its default values.
|
|
158
|
-
*/
|
|
159
93
|
reset() {
|
|
160
|
-
this
|
|
161
|
-
this
|
|
162
|
-
this
|
|
163
|
-
this
|
|
164
|
-
this
|
|
165
|
-
this
|
|
166
|
-
|
|
94
|
+
__classPrivateFieldSet(this, _Config_orderedBy, types_1.NameOrder.FIRST_NAME, "f");
|
|
95
|
+
__classPrivateFieldSet(this, _Config_separator, types_1.Separator.SPACE, "f");
|
|
96
|
+
__classPrivateFieldSet(this, _Config_title, types_1.Title.UK, "f");
|
|
97
|
+
__classPrivateFieldSet(this, _Config_ending, false, "f");
|
|
98
|
+
__classPrivateFieldSet(this, _Config_bypass, true, "f");
|
|
99
|
+
__classPrivateFieldSet(this, _Config_surname, types_1.Surname.FATHER, "f");
|
|
100
|
+
_a.cache.set(this.name, this);
|
|
167
101
|
}
|
|
168
|
-
/**
|
|
169
|
-
* Alters the name order between the first and last name, and rearrange the
|
|
170
|
-
* order of appearance of a name set.
|
|
171
|
-
*/
|
|
172
102
|
updateOrder(order) {
|
|
173
|
-
if (order && order !== this
|
|
174
|
-
|
|
103
|
+
if (order && order !== __classPrivateFieldGet(this, _Config_orderedBy, "f")) {
|
|
104
|
+
__classPrivateFieldSet(_a.cache.get(this.name), _Config_orderedBy, order, "f");
|
|
175
105
|
}
|
|
176
106
|
}
|
|
177
|
-
/**
|
|
178
|
-
* Generates a unique new name.
|
|
179
|
-
*/
|
|
180
|
-
genNewName(name) {
|
|
181
|
-
return name === this.name || Config.cache.has(name) ? this.genNewName(name + copyAlias) : name;
|
|
182
|
-
}
|
|
183
107
|
}
|
|
184
108
|
exports.Config = Config;
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
109
|
+
_a = Config, _Config_name = new WeakMap(), _Config_orderedBy = new WeakMap(), _Config_separator = new WeakMap(), _Config_title = new WeakMap(), _Config_ending = new WeakMap(), _Config_bypass = new WeakMap(), _Config_surname = new WeakMap(), _Config_instances = new WeakSet(), _Config_genNewName = function _Config_genNewName(name) {
|
|
110
|
+
return name === this.name || _a.cache.has(name) ? __classPrivateFieldGet(this, _Config_instances, "m", _Config_genNewName).call(this, name + copyAlias) : name;
|
|
111
|
+
};
|
|
188
112
|
Config.cache = new Map();
|
|
189
|
-
//# sourceMappingURL=config.js.map
|
package/dist/lib/constants.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ALLOWED_TOKENS = exports.MAX_NUMBER_OF_NAME_PARTS = exports.MIN_NUMBER_OF_NAME_PARTS = exports.
|
|
4
|
-
exports.
|
|
3
|
+
exports.ALLOWED_TOKENS = exports.MAX_NUMBER_OF_NAME_PARTS = exports.MIN_NUMBER_OF_NAME_PARTS = exports.VERSION = void 0;
|
|
4
|
+
exports.VERSION = '1.2.1';
|
|
5
5
|
exports.MIN_NUMBER_OF_NAME_PARTS = 2;
|
|
6
6
|
exports.MAX_NUMBER_OF_NAME_PARTS = 5;
|
|
7
7
|
exports.ALLOWED_TOKENS = [
|
|
@@ -28,4 +28,3 @@ exports.ALLOWED_TOKENS = [
|
|
|
28
28
|
'S',
|
|
29
29
|
'$',
|
|
30
30
|
];
|
|
31
|
-
//# sourceMappingURL=constants.js.map
|
package/dist/lib/error.js
CHANGED
|
@@ -2,96 +2,35 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.UnknownError = exports.NotAllowedError = exports.ValidationError = exports.InputError = exports.NameError = exports.NameErrorType = void 0;
|
|
4
4
|
const utils_1 = require("./utils");
|
|
5
|
-
/**
|
|
6
|
-
* The error types supported by `Namefully`.
|
|
7
|
-
*/
|
|
8
5
|
var NameErrorType;
|
|
9
6
|
(function (NameErrorType) {
|
|
10
|
-
/**
|
|
11
|
-
* Thrown when a name entry/argument is incorrect.
|
|
12
|
-
*
|
|
13
|
-
* For example, a name should have a minimum of 2 characters, so an empty
|
|
14
|
-
* string or a string of one character would cause this kind of error.
|
|
15
|
-
*/
|
|
16
7
|
NameErrorType[NameErrorType["INPUT"] = 0] = "INPUT";
|
|
17
|
-
/**
|
|
18
|
-
* Thrown when the name components do not match the validation rules if the
|
|
19
|
-
* `Config.bypass` is not flagged up. This bypass option skips the validation
|
|
20
|
-
* rules.
|
|
21
|
-
*
|
|
22
|
-
* See also: `ValidationError`
|
|
23
|
-
*/
|
|
24
8
|
NameErrorType[NameErrorType["VALIDATION"] = 1] = "VALIDATION";
|
|
25
|
-
/**
|
|
26
|
-
* Thrown by not allowed operations such as in NameBuilder or name formatting.
|
|
27
|
-
*
|
|
28
|
-
* See also: `NotAllowedError`, `Namefully.format`.
|
|
29
|
-
*/
|
|
30
9
|
NameErrorType[NameErrorType["NOT_ALLOWED"] = 2] = "NOT_ALLOWED";
|
|
31
|
-
/**
|
|
32
|
-
* Thrown by any other unknown sources or unexpected situation.
|
|
33
|
-
*/
|
|
34
10
|
NameErrorType[NameErrorType["UNKNOWN"] = 3] = "UNKNOWN";
|
|
35
|
-
})(NameErrorType
|
|
36
|
-
/**
|
|
37
|
-
* Base class for all name-related errors.
|
|
38
|
-
*
|
|
39
|
-
* A custom error is intended to convey information to the user about a failure,
|
|
40
|
-
* so that it can be addressed programmatically.
|
|
41
|
-
*
|
|
42
|
-
* A name handling failure is not considered a native error that should cause a
|
|
43
|
-
* program failure. Au contraire, it is expected that a programmer using this utility
|
|
44
|
-
* would consider validating a name using its own business rules. That is not
|
|
45
|
-
* this utility's job to guess those rules. So, the predefined `ValidationRules`
|
|
46
|
-
* obey some common validation techniques when it comes to sanitizing a person
|
|
47
|
-
* name. For this reason, the [Config.bypass] is set to `true` by default,
|
|
48
|
-
* indicating that those predefined rules should be skipped for the sake of the
|
|
49
|
-
* program.
|
|
50
|
-
*
|
|
51
|
-
* A programmer may leverage `Parser` to indicate business-tailored rules if he
|
|
52
|
-
* or she wants this utility to perform those safety checks behind the scenes.
|
|
53
|
-
*
|
|
54
|
-
* A name error intends to provide useful information about what causes the error
|
|
55
|
-
* and let the user take initiative on what happens next to the given name:
|
|
56
|
-
* reconstructing it or skipping it.
|
|
57
|
-
*/
|
|
11
|
+
})(NameErrorType || (exports.NameErrorType = NameErrorType = {}));
|
|
58
12
|
class NameError extends Error {
|
|
59
|
-
/**
|
|
60
|
-
* Creates an error with a message describing the issue for a name source.
|
|
61
|
-
* @param source name input that caused the error
|
|
62
|
-
* @param message a message describing the failure.
|
|
63
|
-
* @param type of `NameErrorType`
|
|
64
|
-
*/
|
|
65
13
|
constructor(source, message, type = NameErrorType.UNKNOWN) {
|
|
66
14
|
super(message);
|
|
67
15
|
this.source = source;
|
|
68
16
|
this.type = type;
|
|
69
17
|
this.name = 'NameError';
|
|
70
18
|
}
|
|
71
|
-
/**
|
|
72
|
-
* The actual source input which caused the error.
|
|
73
|
-
*/
|
|
74
19
|
get sourceAsString() {
|
|
75
20
|
let input = '';
|
|
76
21
|
if (!this.source)
|
|
77
22
|
input = '<undefined>';
|
|
78
23
|
if (typeof this.source === 'string')
|
|
79
24
|
input = this.source;
|
|
80
|
-
if (utils_1.isNameArray(this.source))
|
|
25
|
+
if ((0, utils_1.isNameArray)(this.source))
|
|
81
26
|
input = this.source.map((n) => n.toString()).join(' ');
|
|
82
|
-
if (utils_1.isStringArray(this.source))
|
|
27
|
+
if ((0, utils_1.isStringArray)(this.source))
|
|
83
28
|
input = this.source.join(' ');
|
|
84
29
|
return input;
|
|
85
30
|
}
|
|
86
|
-
/**
|
|
87
|
-
* Whether a message describing the failure exists.
|
|
88
|
-
*/
|
|
89
31
|
get hasMessage() {
|
|
90
32
|
return this.message && this.message.trim().length > 0;
|
|
91
33
|
}
|
|
92
|
-
/**
|
|
93
|
-
* Returns a string representation of the error.
|
|
94
|
-
*/
|
|
95
34
|
toString() {
|
|
96
35
|
let report = `${this.name} (${this.sourceAsString})`;
|
|
97
36
|
if (this.hasMessage)
|
|
@@ -100,42 +39,14 @@ class NameError extends Error {
|
|
|
100
39
|
}
|
|
101
40
|
}
|
|
102
41
|
exports.NameError = NameError;
|
|
103
|
-
/**
|
|
104
|
-
* An error thrown when a name source input is incorrect.
|
|
105
|
-
*
|
|
106
|
-
* A `Name` is a name for this utility under certain criteria (i.e., 2+ chars),
|
|
107
|
-
* hence, a wrong input will cause this kind of error. Another common reason
|
|
108
|
-
* may be a wrong key in a Json name parsing mechanism.
|
|
109
|
-
*
|
|
110
|
-
* Keep in mind that this error is different from a `ValidationError`.
|
|
111
|
-
*/
|
|
112
42
|
class InputError extends NameError {
|
|
113
|
-
/**
|
|
114
|
-
* Creates a new `InputError` with an optional error `message`.
|
|
115
|
-
*
|
|
116
|
-
* The name source is by nature a string content, maybe wrapped up in a different
|
|
117
|
-
* type. This string value may be extracted to form the following output:
|
|
118
|
-
* "InputError (stringName)",
|
|
119
|
-
* "InputError (stringName): message".
|
|
120
|
-
*/
|
|
121
43
|
constructor(error) {
|
|
122
44
|
super(error.source, error.message, NameErrorType.INPUT);
|
|
123
45
|
this.name = 'InputError';
|
|
124
46
|
}
|
|
125
47
|
}
|
|
126
48
|
exports.InputError = InputError;
|
|
127
|
-
/**
|
|
128
|
-
* An error thrown to indicate that a name fails the validation rules.
|
|
129
|
-
*/
|
|
130
49
|
class ValidationError extends NameError {
|
|
131
|
-
/**
|
|
132
|
-
* Creates error containing the invalid `nameType` and a `message` that
|
|
133
|
-
* briefly describes the problem if provided.
|
|
134
|
-
*
|
|
135
|
-
* For example, a validation error can be interpreted as:
|
|
136
|
-
* "ValidationError (nameType='stringName')",
|
|
137
|
-
* "ValidationError (nameType='stringName'): message"
|
|
138
|
-
*/
|
|
139
50
|
constructor(error) {
|
|
140
51
|
super(error.source, error.message, NameErrorType.VALIDATION);
|
|
141
52
|
this.nameType = error.nameType;
|
|
@@ -149,22 +60,7 @@ class ValidationError extends NameError {
|
|
|
149
60
|
}
|
|
150
61
|
}
|
|
151
62
|
exports.ValidationError = ValidationError;
|
|
152
|
-
/**
|
|
153
|
-
* Thrown by not allowed operations such as in name formatting.
|
|
154
|
-
*
|
|
155
|
-
* For example, this will occur when trying to format a name accordingly using
|
|
156
|
-
* a non-supported key.
|
|
157
|
-
*/
|
|
158
63
|
class NotAllowedError extends NameError {
|
|
159
|
-
/**
|
|
160
|
-
* Creates a new `NotAllowedError` with an optional error `message` and the
|
|
161
|
-
* `operation` name.
|
|
162
|
-
*
|
|
163
|
-
* For example, an error of this kind can be interpreted as:
|
|
164
|
-
* "NotAllowedError (stringName)",
|
|
165
|
-
* "NotAllowedError (stringName) - operationName",
|
|
166
|
-
* "NotAllowedError (stringName) - operationName: message"
|
|
167
|
-
*/
|
|
168
64
|
constructor(error) {
|
|
169
65
|
super(error.source, error.message, NameErrorType.NOT_ALLOWED);
|
|
170
66
|
this.operation = error.operation;
|
|
@@ -180,20 +76,7 @@ class NotAllowedError extends NameError {
|
|
|
180
76
|
}
|
|
181
77
|
}
|
|
182
78
|
exports.NotAllowedError = NotAllowedError;
|
|
183
|
-
/**
|
|
184
|
-
* A fallback error thrown by any unknown sources or unexpected failure that are
|
|
185
|
-
* not of `NameError`.
|
|
186
|
-
*
|
|
187
|
-
* In this particular case, an `origin` remains useful as it provides details
|
|
188
|
-
* on the sources and the true nature of the unexpected error.
|
|
189
|
-
* At this point, deciding whether to exit the program or not depends on the
|
|
190
|
-
* programmer.
|
|
191
|
-
*/
|
|
192
79
|
class UnknownError extends NameError {
|
|
193
|
-
/**
|
|
194
|
-
* Creates a new `UnknownError` with an optional error `message`.
|
|
195
|
-
* Optionally, the original error revealing the true nature of the failure.
|
|
196
|
-
*/
|
|
197
80
|
constructor(error) {
|
|
198
81
|
super(error.source, error.message, NameErrorType.UNKNOWN);
|
|
199
82
|
this.origin = error.error;
|
|
@@ -207,4 +90,3 @@ class UnknownError extends NameError {
|
|
|
207
90
|
}
|
|
208
91
|
}
|
|
209
92
|
exports.UnknownError = UnknownError;
|
|
210
|
-
//# sourceMappingURL=error.js.map
|
package/dist/lib/full-name.js
CHANGED
|
@@ -1,4 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
3
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
4
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
5
|
+
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");
|
|
6
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
7
|
+
};
|
|
8
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
9
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
10
|
+
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");
|
|
11
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
12
|
+
};
|
|
13
|
+
var _FullName_prefix, _FullName_firstName, _FullName_middleName, _FullName_lastName, _FullName_suffix, _FullName_config;
|
|
2
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
15
|
exports.FullName = void 0;
|
|
4
16
|
const config_1 = require("./config");
|
|
@@ -6,72 +18,34 @@ const error_1 = require("./error");
|
|
|
6
18
|
const name_1 = require("./name");
|
|
7
19
|
const types_1 = require("./types");
|
|
8
20
|
const validator_1 = require("./validator");
|
|
9
|
-
/**
|
|
10
|
-
* The core component of this utility.
|
|
11
|
-
*
|
|
12
|
-
* This component is comprised of five entities that make it easy to handle a
|
|
13
|
-
* full name set: prefix, first name, middle name, last name, and suffix.
|
|
14
|
-
* This class is intended for internal processes. However, it is understandable
|
|
15
|
-
* that it might be needed at some point for additional purposes. For this reason,
|
|
16
|
-
* it's made available.
|
|
17
|
-
*
|
|
18
|
-
* It is recommended to avoid using this class unless it is highly necessary or
|
|
19
|
-
* a custom parser is used for uncommon use cases. This utility tries to cover
|
|
20
|
-
* as many use cases as possible.
|
|
21
|
-
*
|
|
22
|
-
* Additionally, an optional configuration can be used to indicate some specific
|
|
23
|
-
* behaviors related to that name handling.
|
|
24
|
-
*/
|
|
25
21
|
class FullName {
|
|
26
|
-
/**
|
|
27
|
-
* Creates a full name as it goes
|
|
28
|
-
* @param options optional configuration for additional features.
|
|
29
|
-
*/
|
|
30
22
|
constructor(options) {
|
|
31
|
-
this
|
|
32
|
-
this
|
|
23
|
+
_FullName_prefix.set(this, void 0);
|
|
24
|
+
_FullName_firstName.set(this, void 0);
|
|
25
|
+
_FullName_middleName.set(this, []);
|
|
26
|
+
_FullName_lastName.set(this, void 0);
|
|
27
|
+
_FullName_suffix.set(this, void 0);
|
|
28
|
+
_FullName_config.set(this, void 0);
|
|
29
|
+
__classPrivateFieldSet(this, _FullName_config, config_1.Config.merge(options), "f");
|
|
33
30
|
}
|
|
34
|
-
/**
|
|
35
|
-
* A snapshot of the configuration used to set up this full name.
|
|
36
|
-
*/
|
|
37
31
|
get config() {
|
|
38
|
-
return this
|
|
32
|
+
return __classPrivateFieldGet(this, _FullName_config, "f");
|
|
39
33
|
}
|
|
40
|
-
/**
|
|
41
|
-
* The prefix part of the full name.
|
|
42
|
-
*/
|
|
43
34
|
get prefix() {
|
|
44
|
-
return this
|
|
35
|
+
return __classPrivateFieldGet(this, _FullName_prefix, "f");
|
|
45
36
|
}
|
|
46
|
-
/**
|
|
47
|
-
* The first name part of the full name.
|
|
48
|
-
*/
|
|
49
37
|
get firstName() {
|
|
50
|
-
return this
|
|
38
|
+
return __classPrivateFieldGet(this, _FullName_firstName, "f");
|
|
51
39
|
}
|
|
52
|
-
/**
|
|
53
|
-
* The last name part of the full name.
|
|
54
|
-
*/
|
|
55
40
|
get lastName() {
|
|
56
|
-
return this
|
|
41
|
+
return __classPrivateFieldGet(this, _FullName_lastName, "f");
|
|
57
42
|
}
|
|
58
|
-
/**
|
|
59
|
-
* The middle name part of the full name.
|
|
60
|
-
*/
|
|
61
43
|
get middleName() {
|
|
62
|
-
return this
|
|
44
|
+
return __classPrivateFieldGet(this, _FullName_middleName, "f");
|
|
63
45
|
}
|
|
64
|
-
/**
|
|
65
|
-
* The suffix part of the full name.
|
|
66
|
-
*/
|
|
67
46
|
get suffix() {
|
|
68
|
-
return this
|
|
47
|
+
return __classPrivateFieldGet(this, _FullName_suffix, "f");
|
|
69
48
|
}
|
|
70
|
-
/**
|
|
71
|
-
* Parses a json name into a full name.
|
|
72
|
-
* @param json parsable name element
|
|
73
|
-
* @param config optional configuration for additional features.
|
|
74
|
-
*/
|
|
75
49
|
static parse(json, config) {
|
|
76
50
|
try {
|
|
77
51
|
const fullName = new FullName(config);
|
|
@@ -83,65 +57,59 @@ class FullName {
|
|
|
83
57
|
return fullName;
|
|
84
58
|
}
|
|
85
59
|
catch (error) {
|
|
86
|
-
if (error instanceof error_1.NameError)
|
|
60
|
+
if (error instanceof error_1.NameError)
|
|
87
61
|
throw error;
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
error,
|
|
94
|
-
});
|
|
95
|
-
}
|
|
62
|
+
throw new error_1.UnknownError({
|
|
63
|
+
source: Object.values(json).join(' '),
|
|
64
|
+
message: 'could not parse JSON content',
|
|
65
|
+
error,
|
|
66
|
+
});
|
|
96
67
|
}
|
|
97
68
|
}
|
|
98
69
|
setPrefix(name) {
|
|
99
70
|
if (!name)
|
|
100
71
|
return this;
|
|
101
|
-
if (!this.
|
|
72
|
+
if (!__classPrivateFieldGet(this, _FullName_config, "f").bypass)
|
|
102
73
|
validator_1.Validators.prefix.validate(name);
|
|
103
74
|
const prefix = name instanceof name_1.Name ? name.value : name;
|
|
104
|
-
this
|
|
75
|
+
__classPrivateFieldSet(this, _FullName_prefix, name_1.Name.prefix(__classPrivateFieldGet(this, _FullName_config, "f").title === types_1.Title.US ? `${prefix}.` : prefix), "f");
|
|
105
76
|
return this;
|
|
106
77
|
}
|
|
107
78
|
setFirstName(name) {
|
|
108
|
-
if (!this.
|
|
79
|
+
if (!__classPrivateFieldGet(this, _FullName_config, "f").bypass)
|
|
109
80
|
validator_1.Validators.firstName.validate(name);
|
|
110
|
-
this
|
|
81
|
+
__classPrivateFieldSet(this, _FullName_firstName, name instanceof name_1.FirstName ? name : new name_1.FirstName(name), "f");
|
|
111
82
|
return this;
|
|
112
83
|
}
|
|
113
84
|
setLastName(name) {
|
|
114
|
-
if (!this.
|
|
85
|
+
if (!__classPrivateFieldGet(this, _FullName_config, "f").bypass)
|
|
115
86
|
validator_1.Validators.lastName.validate(name);
|
|
116
|
-
this
|
|
87
|
+
__classPrivateFieldSet(this, _FullName_lastName, name instanceof name_1.LastName ? name : new name_1.LastName(name), "f");
|
|
117
88
|
return this;
|
|
118
89
|
}
|
|
119
90
|
setMiddleName(names) {
|
|
120
91
|
if (!Array.isArray(names))
|
|
121
|
-
return;
|
|
122
|
-
if (!this.
|
|
92
|
+
return this;
|
|
93
|
+
if (!__classPrivateFieldGet(this, _FullName_config, "f").bypass)
|
|
123
94
|
validator_1.Validators.middleName.validate(names);
|
|
124
|
-
this
|
|
95
|
+
__classPrivateFieldSet(this, _FullName_middleName, names.map((name) => (name instanceof name_1.Name ? name : name_1.Name.middle(name))), "f");
|
|
125
96
|
return this;
|
|
126
97
|
}
|
|
127
98
|
setSuffix(name) {
|
|
128
99
|
if (!name)
|
|
129
100
|
return this;
|
|
130
|
-
if (!this.
|
|
101
|
+
if (!__classPrivateFieldGet(this, _FullName_config, "f").bypass)
|
|
131
102
|
validator_1.Validators.suffix.validate(name);
|
|
132
|
-
this
|
|
103
|
+
__classPrivateFieldSet(this, _FullName_suffix, name_1.Name.suffix(name instanceof name_1.Name ? name.value : name), "f");
|
|
133
104
|
return this;
|
|
134
105
|
}
|
|
135
|
-
/**
|
|
136
|
-
* Returns true if a namon has been set.
|
|
137
|
-
*/
|
|
138
106
|
has(namon) {
|
|
139
107
|
if (namon.equal(types_1.Namon.PREFIX))
|
|
140
|
-
return !!this
|
|
108
|
+
return !!__classPrivateFieldGet(this, _FullName_prefix, "f");
|
|
141
109
|
if (namon.equal(types_1.Namon.SUFFIX))
|
|
142
|
-
return !!this
|
|
143
|
-
return namon.equal(types_1.Namon.MIDDLE_NAME) ? this.
|
|
110
|
+
return !!__classPrivateFieldGet(this, _FullName_suffix, "f");
|
|
111
|
+
return namon.equal(types_1.Namon.MIDDLE_NAME) ? __classPrivateFieldGet(this, _FullName_middleName, "f").length > 0 : true;
|
|
144
112
|
}
|
|
145
113
|
}
|
|
146
114
|
exports.FullName = FullName;
|
|
147
|
-
|
|
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();
|