namefully 1.2.0 → 1.3.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/builder.js +80 -0
- 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 +12 -15
- package/dist/lib/name.js +57 -149
- package/dist/lib/namefully.js +132 -438
- package/dist/lib/parser.js +42 -71
- package/dist/lib/types.js +6 -80
- package/dist/lib/utils.js +26 -60
- package/dist/lib/validator.js +54 -101
- package/dist/types/builder.d.ts +73 -0
- 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 +6 -1
- package/dist/{lib/src → types}/name.d.ts +30 -81
- package/dist/{lib → types}/namefully.d.ts +45 -101
- package/dist/{lib → types}/parser.d.ts +5 -9
- package/dist/{lib/src → types}/types.d.ts +1 -1
- package/dist/{lib/src → types}/utils.d.ts +8 -16
- package/dist/{lib → types}/validator.d.ts +7 -16
- package/dist/umd/namefully.js +955 -1671
- package/dist/umd/namefully.min.js +1 -3
- package/package.json +46 -49
- package/readme.md +71 -42
- 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/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.d.ts +0 -63
- 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
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.NameBuilder = void 0;
|
|
4
|
+
const namefully_1 = require("./namefully");
|
|
5
|
+
const validator_1 = require("./validator");
|
|
6
|
+
class Builder {
|
|
7
|
+
constructor(prebuild, postbuild, preclear, postclear) {
|
|
8
|
+
this.prebuild = prebuild;
|
|
9
|
+
this.postbuild = postbuild;
|
|
10
|
+
this.preclear = preclear;
|
|
11
|
+
this.postclear = postclear;
|
|
12
|
+
this.queue = [];
|
|
13
|
+
this.instance = null;
|
|
14
|
+
}
|
|
15
|
+
get size() {
|
|
16
|
+
return this.queue.length;
|
|
17
|
+
}
|
|
18
|
+
removeFirst() {
|
|
19
|
+
return this.queue.length > 0 ? this.queue.shift() : undefined;
|
|
20
|
+
}
|
|
21
|
+
removeLast() {
|
|
22
|
+
return this.queue.length > 0 ? this.queue.pop() : undefined;
|
|
23
|
+
}
|
|
24
|
+
addFirst(value) {
|
|
25
|
+
this.queue.unshift(value);
|
|
26
|
+
}
|
|
27
|
+
addLast(value) {
|
|
28
|
+
this.queue.push(value);
|
|
29
|
+
}
|
|
30
|
+
add(...values) {
|
|
31
|
+
this.queue.push(...values);
|
|
32
|
+
}
|
|
33
|
+
remove(value) {
|
|
34
|
+
const index = this.queue.indexOf(value);
|
|
35
|
+
if (index !== -1) {
|
|
36
|
+
this.queue.splice(index, 1);
|
|
37
|
+
return true;
|
|
38
|
+
}
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
removeWhere(callback) {
|
|
42
|
+
this.queue = this.queue.filter((item) => !callback(item));
|
|
43
|
+
}
|
|
44
|
+
retainWhere(callback) {
|
|
45
|
+
this.queue = this.queue.filter(callback);
|
|
46
|
+
}
|
|
47
|
+
clear() {
|
|
48
|
+
var _a, _b;
|
|
49
|
+
if (this.instance !== null)
|
|
50
|
+
(_a = this.preclear) === null || _a === void 0 ? void 0 : _a.call(this, this.instance);
|
|
51
|
+
this.queue = [];
|
|
52
|
+
(_b = this.postclear) === null || _b === void 0 ? void 0 : _b.call(this);
|
|
53
|
+
this.instance = null;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
class NameBuilder extends Builder {
|
|
57
|
+
constructor(names, prebuild, postbuild, preclear, postclear) {
|
|
58
|
+
super(prebuild, postbuild, preclear, postclear);
|
|
59
|
+
this.add(...names);
|
|
60
|
+
}
|
|
61
|
+
static create(name) {
|
|
62
|
+
return new NameBuilder(name ? [name] : []);
|
|
63
|
+
}
|
|
64
|
+
static of(...initialNames) {
|
|
65
|
+
return new NameBuilder(initialNames);
|
|
66
|
+
}
|
|
67
|
+
static use({ names, prebuild, postbuild, preclear, postclear, }) {
|
|
68
|
+
return new NameBuilder(names !== null && names !== void 0 ? names : [], prebuild, postbuild, preclear, postclear);
|
|
69
|
+
}
|
|
70
|
+
build(config) {
|
|
71
|
+
var _a, _b;
|
|
72
|
+
(_a = this.prebuild) === null || _a === void 0 ? void 0 : _a.call(this);
|
|
73
|
+
const names = [...this.queue];
|
|
74
|
+
validator_1.ArrayNameValidator.create().validate(names);
|
|
75
|
+
this.instance = new namefully_1.Namefully(names, config);
|
|
76
|
+
(_b = this.postbuild) === null || _b === void 0 ? void 0 : _b.call(this, this.instance);
|
|
77
|
+
return this.instance;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
exports.NameBuilder = NameBuilder;
|
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.3.0';
|
|
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
|