namefully 1.2.1 → 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.
@@ -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;
@@ -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
- updateOrder(order) {
103
- if (order && order !== __classPrivateFieldGet(this, _Config_orderedBy, "f")) {
104
- __classPrivateFieldSet(_a.cache.get(this.name), _Config_orderedBy, order, "f");
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;
@@ -1,30 +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.VERSION = void 0;
4
- exports.VERSION = '1.2.1';
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.ALLOWED_TOKENS = [
8
- '.',
9
- ',',
10
- ' ',
11
- '-',
12
- '_',
13
- 'b',
14
- 'B',
15
- 'f',
16
- 'F',
17
- 'l',
18
- 'L',
19
- 'm',
20
- 'M',
21
- 'n',
22
- 'N',
23
- 'o',
24
- 'O',
25
- 'p',
26
- 'P',
27
- 's',
28
- 'S',
29
- '$',
30
- ];
7
+ exports.ALLOWED_FORMAT_TOKENS = ` .,_-()[]<>'"bBfFlLmMnNoOpPsS$`;
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
- input = this.source;
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
- input = this.source.join(' ');
29
- return input;
23
+ return this.source.join(' ');
24
+ return '<undefined>';
30
25
  }
31
26
  get hasMessage() {
32
- return this.message && this.message.trim().length > 0;
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.error;
77
+ this.origin = error.origin;
83
78
  this.name = 'UnknownError';
84
79
  }
85
80
  toString() {
@@ -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 fullName = new FullName(config);
52
- fullName.setPrefix(json.prefix);
53
- fullName.setFirstName(json.firstName);
54
- fullName.setMiddleName(json.middleName);
55
- fullName.setLastName(json.lastName);
56
- fullName.setSuffix(json.suffix);
57
- return fullName;
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((name) => (name instanceof name_1.Name ? name : name_1.Name.middle(name))), "f");
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(namon) {
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/index.js CHANGED
@@ -15,6 +15,8 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  exports.NameIndex = exports.Parser = exports.version = void 0;
18
+ const namefully_1 = require("./namefully");
19
+ __exportStar(require("./builder"), exports);
18
20
  __exportStar(require("./config"), exports);
19
21
  var constants_1 = require("./constants");
20
22
  Object.defineProperty(exports, "version", { enumerable: true, get: function () { return constants_1.VERSION; } });
@@ -27,3 +29,4 @@ Object.defineProperty(exports, "Parser", { enumerable: true, get: function () {
27
29
  __exportStar(require("./types"), exports);
28
30
  var utils_1 = require("./utils");
29
31
  Object.defineProperty(exports, "NameIndex", { enumerable: true, get: function () { return utils_1.NameIndex; } });
32
+ exports.default = namefully_1.default;
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 this(value, types_1.Namon.PREFIX);
55
+ return new Name(value, types_1.Namon.PREFIX);
56
56
  }
57
57
  static first(value) {
58
- return new this(value, types_1.Namon.FIRST_NAME);
58
+ return new Name(value, types_1.Namon.FIRST_NAME);
59
59
  }
60
60
  static middle(value) {
61
- return new this(value, types_1.Namon.MIDDLE_NAME);
61
+ return new Name(value, types_1.Namon.MIDDLE_NAME);
62
62
  }
63
63
  static last(value) {
64
- return new this(value, types_1.Namon.LAST_NAME);
64
+ return new Name(value, types_1.Namon.LAST_NAME);
65
65
  }
66
66
  static suffix(value) {
67
- return new this(value, types_1.Namon.SUFFIX);
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 ((name === null || name === void 0 ? void 0 : name.trim().length) < 2) {
88
- throw new error_1.InputError({ source: name, message: 'must be 2+ characters' });
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((n) => this.validate(n));
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((n) => Name.first(n)));
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.hasMother) {
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
- case types_1.Surname.ALL:
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.hasMother)
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 = range || this.capsRange;
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 = range || this.capsRange;
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;