namefully 1.1.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.
Files changed (136) hide show
  1. package/dist/lib/config.js +112 -0
  2. package/dist/lib/constants.js +30 -0
  3. package/dist/lib/error.js +92 -0
  4. package/dist/lib/full-name.js +115 -0
  5. package/dist/lib/index.js +27 -12
  6. package/dist/lib/name.js +230 -0
  7. package/dist/lib/namefully.js +368 -681
  8. package/dist/lib/parser.js +138 -0
  9. package/dist/lib/types.js +107 -0
  10. package/dist/lib/utils.js +92 -0
  11. package/dist/lib/validator.js +301 -0
  12. package/dist/types/config.d.ts +97 -0
  13. package/dist/types/constants.d.ts +4 -0
  14. package/dist/types/error.d.ts +158 -0
  15. package/dist/types/full-name.d.ts +54 -0
  16. package/dist/types/index.d.ts +21 -0
  17. package/dist/types/name.d.ts +126 -0
  18. package/dist/types/namefully.d.ts +313 -0
  19. package/dist/types/parser.d.ts +41 -0
  20. package/dist/types/types.d.ts +127 -0
  21. package/dist/types/utils.d.ts +53 -0
  22. package/dist/types/validator.d.ts +57 -0
  23. package/dist/umd/namefully.js +1503 -2900
  24. package/dist/umd/namefully.min.js +1 -3
  25. package/package.json +28 -38
  26. package/readme.md +138 -167
  27. package/changelog.md +0 -134
  28. package/dist/lib/core/constants.d.ts +0 -48
  29. package/dist/lib/core/constants.js +0 -101
  30. package/dist/lib/core/constants.js.map +0 -1
  31. package/dist/lib/core/core.d.ts +0 -17
  32. package/dist/lib/core/core.js +0 -24
  33. package/dist/lib/core/core.js.map +0 -1
  34. package/dist/lib/core/index.d.ts +0 -9
  35. package/dist/lib/core/index.js +0 -13
  36. package/dist/lib/core/index.js.map +0 -1
  37. package/dist/lib/core/parsers/array-name.parser.d.ts +0 -41
  38. package/dist/lib/core/parsers/array-name.parser.js +0 -86
  39. package/dist/lib/core/parsers/array-name.parser.js.map +0 -1
  40. package/dist/lib/core/parsers/array-string.parser.d.ts +0 -47
  41. package/dist/lib/core/parsers/array-string.parser.js +0 -95
  42. package/dist/lib/core/parsers/array-string.parser.js.map +0 -1
  43. package/dist/lib/core/parsers/index.d.ts +0 -11
  44. package/dist/lib/core/parsers/index.js +0 -11
  45. package/dist/lib/core/parsers/index.js.map +0 -1
  46. package/dist/lib/core/parsers/nama.parser.d.ts +0 -33
  47. package/dist/lib/core/parsers/nama.parser.js +0 -74
  48. package/dist/lib/core/parsers/nama.parser.js.map +0 -1
  49. package/dist/lib/core/parsers/parser.d.ts +0 -28
  50. package/dist/lib/core/parsers/parser.js +0 -3
  51. package/dist/lib/core/parsers/parser.js.map +0 -1
  52. package/dist/lib/core/parsers/string.parser.d.ts +0 -60
  53. package/dist/lib/core/parsers/string.parser.js +0 -62
  54. package/dist/lib/core/parsers/string.parser.js.map +0 -1
  55. package/dist/lib/core/utils.d.ts +0 -52
  56. package/dist/lib/core/utils.js +0 -178
  57. package/dist/lib/core/utils.js.map +0 -1
  58. package/dist/lib/index.d.ts +0 -10
  59. package/dist/lib/index.js.map +0 -1
  60. package/dist/lib/models/enums.d.ts +0 -106
  61. package/dist/lib/models/enums.js +0 -114
  62. package/dist/lib/models/enums.js.map +0 -1
  63. package/dist/lib/models/firstname.d.ts +0 -77
  64. package/dist/lib/models/firstname.js +0 -131
  65. package/dist/lib/models/firstname.js.map +0 -1
  66. package/dist/lib/models/fullname.d.ts +0 -73
  67. package/dist/lib/models/fullname.js +0 -99
  68. package/dist/lib/models/fullname.js.map +0 -1
  69. package/dist/lib/models/index.d.ts +0 -13
  70. package/dist/lib/models/index.js +0 -16
  71. package/dist/lib/models/index.js.map +0 -1
  72. package/dist/lib/models/lastname.d.ts +0 -80
  73. package/dist/lib/models/lastname.js +0 -157
  74. package/dist/lib/models/lastname.js.map +0 -1
  75. package/dist/lib/models/misc.d.ts +0 -108
  76. package/dist/lib/models/misc.js +0 -3
  77. package/dist/lib/models/misc.js.map +0 -1
  78. package/dist/lib/models/name.d.ts +0 -76
  79. package/dist/lib/models/name.js +0 -115
  80. package/dist/lib/models/name.js.map +0 -1
  81. package/dist/lib/models/summary.d.ts +0 -26
  82. package/dist/lib/models/summary.js +0 -61
  83. package/dist/lib/models/summary.js.map +0 -1
  84. package/dist/lib/namefully.d.ts +0 -302
  85. package/dist/lib/namefully.js.map +0 -1
  86. package/dist/lib/validators/array-name.validator.d.ts +0 -25
  87. package/dist/lib/validators/array-name.validator.js +0 -75
  88. package/dist/lib/validators/array-name.validator.js.map +0 -1
  89. package/dist/lib/validators/array-string.validator.d.ts +0 -43
  90. package/dist/lib/validators/array-string.validator.js +0 -76
  91. package/dist/lib/validators/array-string.validator.js.map +0 -1
  92. package/dist/lib/validators/common/validation-error.d.ts +0 -19
  93. package/dist/lib/validators/common/validation-error.js +0 -26
  94. package/dist/lib/validators/common/validation-error.js.map +0 -1
  95. package/dist/lib/validators/common/validation-rule.d.ts +0 -72
  96. package/dist/lib/validators/common/validation-rule.js +0 -76
  97. package/dist/lib/validators/common/validation-rule.js.map +0 -1
  98. package/dist/lib/validators/common/validation-type.d.ts +0 -24
  99. package/dist/lib/validators/common/validation-type.js +0 -28
  100. package/dist/lib/validators/common/validation-type.js.map +0 -1
  101. package/dist/lib/validators/firstname.validator.d.ts +0 -20
  102. package/dist/lib/validators/firstname.validator.js +0 -29
  103. package/dist/lib/validators/firstname.validator.js.map +0 -1
  104. package/dist/lib/validators/fullname.validator.d.ts +0 -21
  105. package/dist/lib/validators/fullname.validator.js +0 -38
  106. package/dist/lib/validators/fullname.validator.js.map +0 -1
  107. package/dist/lib/validators/index.d.ts +0 -21
  108. package/dist/lib/validators/index.js +0 -35
  109. package/dist/lib/validators/index.js.map +0 -1
  110. package/dist/lib/validators/lastname.validator.d.ts +0 -20
  111. package/dist/lib/validators/lastname.validator.js +0 -29
  112. package/dist/lib/validators/lastname.validator.js.map +0 -1
  113. package/dist/lib/validators/middlename.validator.d.ts +0 -20
  114. package/dist/lib/validators/middlename.validator.js +0 -38
  115. package/dist/lib/validators/middlename.validator.js.map +0 -1
  116. package/dist/lib/validators/nama.validator.d.ts +0 -21
  117. package/dist/lib/validators/nama.validator.js +0 -44
  118. package/dist/lib/validators/nama.validator.js.map +0 -1
  119. package/dist/lib/validators/namon.validator.d.ts +0 -20
  120. package/dist/lib/validators/namon.validator.js +0 -29
  121. package/dist/lib/validators/namon.validator.js.map +0 -1
  122. package/dist/lib/validators/prefix.validator.d.ts +0 -14
  123. package/dist/lib/validators/prefix.validator.js +0 -31
  124. package/dist/lib/validators/prefix.validator.js.map +0 -1
  125. package/dist/lib/validators/string-name.validator.d.ts +0 -20
  126. package/dist/lib/validators/string-name.validator.js +0 -29
  127. package/dist/lib/validators/string-name.validator.js.map +0 -1
  128. package/dist/lib/validators/suffix.validator.d.ts +0 -14
  129. package/dist/lib/validators/suffix.validator.js +0 -31
  130. package/dist/lib/validators/suffix.validator.js.map +0 -1
  131. package/dist/lib/validators/validator.d.ts +0 -13
  132. package/dist/lib/validators/validator.js +0 -9
  133. package/dist/lib/validators/validator.js.map +0 -1
  134. package/dist/umd/namefully.js.map +0 -1
  135. package/dist/umd/namefully.min.js.LICENSE.txt +0 -29
  136. package/dist/umd/namefully.min.js.map +0 -1
@@ -1,723 +1,410 @@
1
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;
2
23
  Object.defineProperty(exports, "__esModule", { value: true });
3
- /**
4
- * Welcome to namefully!
5
- *
6
- * namefully is a JS utility for handing person names.
7
- *
8
- * Sources
9
- * - repo: https://github.com/ralflorent/namefully
10
- * - docs: https://namefully.netlify.app
11
- * - npm: https://npmjs.com/package/namefully
12
- *
13
- * Created on March 03, 2020
14
- * @author Ralph Florent <ralflornt@gmail.com>
15
- * @license MIT
16
- */
17
- const core_1 = require("./core");
18
- const models_1 = require("./models");
19
- const validators_1 = require("./validators");
20
- /**
21
- * Person name handler
22
- * @class
23
- *
24
- * `Namefully` does not magically guess which part of the name is what. It relies
25
- * actually on how the developer indicates the roles of the name parts so that
26
- * it, internally, can perform certain operations and saves the developer some
27
- * calculations/processings. Nevertheless, Namefully can be constructed using
28
- * distinct raw data shape. This is intended to give some flexibility to the
29
- * developer so that he or she is not bound to a particular data format. Please,
30
- * do follow closely the APIs to know how to properly use it in order to avoid
31
- * some errors (mainly validation's).
32
- *
33
- * `Namefully` also works like a trap door. Once a raw data is provided and
34
- * validated, a developer can only ACCESS in a vast amount of, yet effective ways
35
- * the name info. NO EDITING is possible. If the name is mistaken, a new instance
36
- * of `Namefully` must be created. Remember, this utility's primary objective is
37
- * to help to **handle** a person name.
38
- *
39
- * Note that the name standards used for the current version of this library are
40
- * as follows:
41
- * [Prefix] Firstname [Middlename] Lastname [Suffix]
42
- * The opening `[` and closing `]` brackets mean that these parts are optional.
43
- * In other words, the most basic and typical case is a name that looks like this:
44
- * `John Smith`, where `John` is the first name and `Smith`, the last name.
45
- * @see https://departments.weber.edu/qsupport&training/Data_Standards/Name.htm
46
- * for more info on name standards.
47
- *
48
- * **IMPORTANT**: Keep in mind that the order of appearance matters and can be
49
- * altered through configured parameters, which we will be seeing later on. By
50
- * default, the order of appearance is as shown above and will be used as a basis
51
- * for future examples and use cases.
52
- *
53
- * Once imported, all that is required to do is to create an instance of
54
- * `Namefully` and the rest will follow.
55
- *
56
- * Some terminologies used across the library are:
57
- * - namon: 1 piece of a name (e.g., firstname)
58
- * - nama: 2+ pieces of a name (e.g., firstname + lastname)
59
- *
60
- * Happy handling!
61
- */
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");
62
30
  class Namefully {
63
- /**
64
- * Constructs an instance of the utility and helps to benefit from many helpers
65
- * @param {string | string[] | Name[] | Nama | Fullname} raw element to parse or
66
- * construct the pieces of the name
67
- * @param {Config} options to configure how to run the utility
68
- */
69
- constructor(raw, options) {
70
- // well, first thing first
71
- this.configure(options);
72
- // let's try to parse this, baby!
73
- this.build(raw);
74
- }
75
- /**
76
- * Gets the full name ordered as configured
77
- * @param {NameOrder} orderedBy force to order by first or last
78
- * name by overriding the preset configuration
79
- *
80
- * @see {format} to alter manually the order of appearance of the full name.
81
- * For example, ::format('l f m') outputs `lastname firstname middlename`.
82
- */
83
- getFullname(orderedBy) {
84
- orderedBy = this.parseNameOrder(orderedBy);
85
- const { titling, ending } = this.config;
86
- const pxSep = titling === 'us' ? models_1.Separator.PERIOD : models_1.Separator.EMPTY; // Mr[.]
87
- const sxSep = ending ? ',' : models_1.Separator.EMPTY; // [,] PhD
88
- const nama = [];
89
- if (this.fullname.prefix)
90
- nama.push(models_1.Separator.EMPTY.concat(this.fullname.prefix, pxSep));
91
- if (orderedBy === 'firstname') {
92
- nama.push(this.getFirstname());
93
- nama.push(...this.getMiddlenames());
94
- nama.push(models_1.Separator.EMPTY.concat(this.getLastname(), sxSep));
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) {
37
+ try {
38
+ return new this(parser_1.Parser.build(text));
95
39
  }
96
- else {
97
- nama.push(this.getLastname());
98
- nama.push(this.getFirstname());
99
- nama.push(this.getMiddlenames().join(models_1.Separator.SPACE).concat(sxSep));
40
+ catch (error) {
41
+ return undefined;
100
42
  }
101
- if (this.fullname.suffix)
102
- nama.push(this.fullname.suffix);
103
- return nama.join(models_1.Separator.SPACE);
104
- }
105
- /**
106
- * Gets the birth name ordered as configured, no prefix or suffix
107
- * @param {NameOrder} orderedBy force to order by first or last
108
- * name by overriding the preset configuration
109
- */
110
- getBirthname(orderedBy) {
111
- orderedBy = this.parseNameOrder(orderedBy);
112
- const nama = [];
113
- if (orderedBy === 'firstname') {
114
- nama.push(this.getFirstname());
115
- nama.push(...this.getMiddlenames());
116
- nama.push(this.getLastname());
43
+ }
44
+ static parse(text) {
45
+ return __awaiter(this, void 0, void 0, function* () {
46
+ return parser_1.Parser.buildAsync(text).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
+ toString() {
91
+ return this.full;
92
+ }
93
+ get(namon) {
94
+ if (namon.equal(types_1.Namon.PREFIX))
95
+ return __classPrivateFieldGet(this, _Namefully_fullName, "f").prefix;
96
+ if (namon.equal(types_1.Namon.FIRST_NAME))
97
+ return __classPrivateFieldGet(this, _Namefully_fullName, "f").firstName;
98
+ if (namon.equal(types_1.Namon.MIDDLE_NAME))
99
+ return __classPrivateFieldGet(this, _Namefully_fullName, "f").middleName;
100
+ if (namon.equal(types_1.Namon.LAST_NAME))
101
+ return __classPrivateFieldGet(this, _Namefully_fullName, "f").lastName;
102
+ if (namon.equal(types_1.Namon.SUFFIX))
103
+ return __classPrivateFieldGet(this, _Namefully_fullName, "f").suffix;
104
+ return undefined;
105
+ }
106
+ equal(other) {
107
+ return this.toString() === other.toString();
108
+ }
109
+ toJson() {
110
+ return {
111
+ prefix: this.prefix,
112
+ firstName: this.first,
113
+ middleName: this.middleName(),
114
+ lastName: this.last,
115
+ suffix: this.suffix,
116
+ };
117
+ }
118
+ has(namon) {
119
+ return __classPrivateFieldGet(this, _Namefully_fullName, "f").has(namon);
120
+ }
121
+ fullName(orderedBy) {
122
+ const sep = this.config.ending ? ',' : '';
123
+ const names = [];
124
+ orderedBy = orderedBy || this.config.orderedBy;
125
+ if (this.prefix)
126
+ names.push(this.prefix);
127
+ if (orderedBy === types_1.NameOrder.FIRST_NAME) {
128
+ names.push(this.first, ...this.middleName(), this.last + sep);
117
129
  }
118
130
  else {
119
- nama.push(this.getLastname());
120
- nama.push(this.getFirstname());
121
- nama.push(...this.getMiddlenames());
122
- }
123
- return nama.join(models_1.Separator.SPACE);
124
- }
125
- /**
126
- * Gets the first name part of the full name
127
- * @param {boolean} includeAll whether to include other pieces of the first
128
- * name
129
- */
130
- getFirstname(includeAll = true) {
131
- return this.fullname.firstname.tostring(includeAll);
132
- }
133
- /**
134
- * Gets the last name part of the full name
135
- * @param {LastnameFormat} [format] overrides the how-to format of a surname
136
- * output, considering its subparts.
137
- */
138
- getLastname(format) {
139
- return this.fullname.lastname.tostring(format);
140
- }
141
- /**
142
- * Gets the middle names part of the full name
143
- */
144
- getMiddlenames() {
145
- return this.hasMiddlename() ? this.fullname.middlename.map(n => n.namon) : [];
146
- }
147
- /**
148
- * Gets the prefix part of the full name
149
- */
150
- getPrefix() {
151
- return this.fullname.prefix
152
- ? this.fullname.prefix.concat(this.config.titling === 'us'
153
- ? models_1.Separator.PERIOD
154
- : models_1.Separator.EMPTY) : models_1.Separator.EMPTY;
155
- }
156
- /**
157
- * Gets the suffix part of the full name
158
- */
159
- getSuffix() {
160
- return this.fullname.suffix || models_1.Separator.EMPTY;
161
- }
162
- /**
163
- * Gets the initials of the full name
164
- * @param {NameOrder} orderedBy force to order by first or last name by
165
- * overriding the preset configuration
166
- * @param {boolean} [withMid] whether to include middle names's
167
- *
168
- * @example
169
- * Given the names:
170
- * - `John Smith` => ['J', 'S']
171
- * - `John Ben Smith` => ['J', 'S']
172
- * when `withMid` is set to true:
173
- * - `John Ben Smith` => ['J', 'B', 'S']
174
- *
175
- * **NOTE**:
176
- * Ordered by last name obeys the following format:
177
- * `lastname firstname [middlename]`
178
- * which means that if no middle name was set, setting `withMid` to true
179
- * will output nothing and warn the end user about it.
180
- */
181
- getInitials(orderedBy, withMid = false) {
182
- orderedBy = this.parseNameOrder(orderedBy);
183
- const midInits = this.fullname.middlename ?
184
- this.fullname.middlename.map(n => n.getInitials()) : [];
185
- if (withMid && !this.hasMiddlename()) {
186
- console.warn('No initials for middle names since none was set.');
131
+ names.push(this.last, this.first, this.middleName().join(' ') + sep);
187
132
  }
133
+ if (this.suffix)
134
+ names.push(this.suffix);
135
+ return names.join(' ').trim();
136
+ }
137
+ birthName(orderedBy) {
138
+ orderedBy = orderedBy || this.config.orderedBy;
139
+ return orderedBy === types_1.NameOrder.FIRST_NAME
140
+ ? [this.first, ...this.middleName(), this.last].join(' ')
141
+ : [this.last, this.first, ...this.middleName()].join(' ');
142
+ }
143
+ firstName(withMore = true) {
144
+ return __classPrivateFieldGet(this, _Namefully_fullName, "f").firstName.toString(withMore);
145
+ }
146
+ middleName() {
147
+ return __classPrivateFieldGet(this, _Namefully_fullName, "f").middleName.map((n) => n.value);
148
+ }
149
+ lastName(format) {
150
+ return __classPrivateFieldGet(this, _Namefully_fullName, "f").lastName.toString(format);
151
+ }
152
+ initials(options) {
188
153
  const initials = [];
189
- if (orderedBy === 'firstname') {
190
- initials.push(...this.fullname.firstname.getInitials());
191
- if (withMid)
192
- midInits.forEach(m => initials.push(...m));
193
- initials.push(...this.fullname.lastname.getInitials());
154
+ const firstInits = __classPrivateFieldGet(this, _Namefully_fullName, "f").firstName.initials();
155
+ const midInits = __classPrivateFieldGet(this, _Namefully_fullName, "f").middleName.map((n) => n.initials()[0]);
156
+ const lastInits = __classPrivateFieldGet(this, _Namefully_fullName, "f").lastName.initials();
157
+ const mergedOptions = Object.assign({ orderedBy: this.config.orderedBy, only: types_1.NameType.BIRTH_NAME }, options);
158
+ const { orderedBy, only } = mergedOptions;
159
+ if (only !== types_1.NameType.BIRTH_NAME) {
160
+ if (only === types_1.NameType.FIRST_NAME) {
161
+ initials.push(...firstInits);
162
+ }
163
+ else if (only === types_1.NameType.MIDDLE_NAME) {
164
+ initials.push(...midInits);
165
+ }
166
+ else {
167
+ initials.push(...lastInits);
168
+ }
169
+ }
170
+ else if (orderedBy === types_1.NameOrder.FIRST_NAME) {
171
+ initials.push(...firstInits, ...midInits, ...lastInits);
194
172
  }
195
173
  else {
196
- initials.push(...this.fullname.lastname.getInitials());
197
- initials.push(...this.fullname.firstname.getInitials());
198
- if (withMid)
199
- midInits.forEach(m => initials.push(...m));
174
+ initials.push(...lastInits, ...firstInits, ...midInits);
200
175
  }
201
176
  return initials;
202
177
  }
203
- /**
204
- * Gives some descriptive statistics that summarize the central tendency,
205
- * dispersion and shape of the characters' distribution.
206
- * @param what which variant to use when describe a name part
207
- *
208
- * Treated as a categorical dataset, the summary contains the following info:
209
- * `count` : the number of *unrestricted* characters of the name;
210
- * `frequency` : the highest frequency within the characters;
211
- * `top` : the character with the highest frequency;
212
- * `unique` : the count of unique characters of the name;
213
- * `distribution` : the characters' distribution.
214
- *
215
- * @example
216
- * Given the name "Thomas Alva Edison", the summary will output as follows:
217
- *
218
- * Descriptive statistics for "Thomas Alva Edison"
219
- * count : 16
220
- * frequency: 3
221
- * top : A
222
- * unique : 12
223
- * distribution: { T: 1, H: 1, O: 2, M: 1, A: 2, S: 2, ' ': 2, L: 1, V: 1,
224
- * E: 1, D: 1, I: 1, N: 1 }
225
- *
226
- * **NOTE:**
227
- * During the setup, a set of restricted characters can be defined to be removed
228
- * from the stats. By default, the only restricted character is the `space`.
229
- * That is why the `count` for the example below result in `16` instead of
230
- * `16`.
231
- * Another thing to consider is that the summary is case *insensitive*. Note
232
- * that the letter `a` has the top frequency, be it `3`.
233
- */
234
- describe(what) {
235
- what = core_1.allowShortNameType(what);
236
- switch (what) {
237
- case 'firstname':
238
- return this.fullname.firstname.describe();
239
- case 'lastname':
240
- return this.fullname.lastname.describe();
241
- case 'middlename':
242
- if (!this.hasMiddlename()) {
243
- console.warn('No Summary for middle names since none was set.');
244
- return null;
245
- }
246
- return new models_1.Summary(this.fullname.middlename
247
- .map(n => n.namon)
248
- .join(models_1.Separator.SPACE));
249
- default:
250
- return this.summary;
251
- }
252
- }
253
- /**
254
- * Shortens a complex full name to a simple typical name, a combination of
255
- * first name and last name.
256
- * @param {NameOrder} orderedBy force to order by first or last
257
- * name by overriding the preset configuration
258
- *
259
- * @example
260
- * For a given name such as `Mr Keanu Charles Reeves`, shortening this name
261
- * is equivalent to making it `Keanu Reeves`.
262
- *
263
- * As a shortened name, the namon of the first name is favored over the other
264
- * names forming part of the entire first names, if any. Meanwhile, for
265
- * the last name, the configured `lastnameFormat` is prioritized.
266
- *
267
- * @example
268
- * For a given `Firstname Fathername Mothername`, shortening this name when
269
- * the lastnameFormat is set as `mother` is equivalent to making it:
270
- * `Firstname Mothername`.
271
- */
272
178
  shorten(orderedBy) {
273
- orderedBy = orderedBy || this.config.orderedBy; // override config
274
- const { firstname, lastname } = this.fullname;
275
- return orderedBy === 'firstname'
276
- ? [firstname.namon, lastname.tostring()].join(models_1.Separator.SPACE)
277
- : [lastname.tostring(), firstname.namon].join(models_1.Separator.SPACE);
278
- }
279
- /**
280
- * Compresses a name by using different forms of variants
281
- * @param {number} [limit] a threshold to limit the number of characters
282
- * @param {'firstname'|'lastname'|'middlename'|'firstmid'|'midlast'} [by]
283
- * a variant to use when compressing the long name. The last two variants
284
- * represent respectively the combination of `firstname + middlename` and
285
- * `middlename + lastname`.
286
- * @param {boolean} [warning] should warn when the set limit is violated
287
- *
288
- * @example
289
- * The compressing operation is only executed iff there is valid entry and it
290
- * surpasses the limit set. In the examples below, let us assume that the
291
- * name goes beyond the limit value.
292
- *
293
- * Compressing a long name refers to reducing the name to the following forms:
294
- * 1. by firstname: 'John Moe Beau Lennon' => 'J. Moe Beau Lennon'
295
- * 2. by middlename: 'John Moe Beau Lennon' => 'John M. B. Lennon'
296
- * 3. by lastname: 'John Moe Beau Lennon' => 'John Moe Beau L.'
297
- * 4. by firstmid: 'John Moe Beau Lennon' => 'J. M. B. Lennon'
298
- * 5. by midlast: 'John Moe Beau Lennon' => 'John M. B. L.'
299
- *
300
- * By default, it compresses by 'firstmid' variant: 'J. M. B. Lennon'.
301
- */
302
- compress(limit = 20, by = 'mn', warning = true) {
303
- if (this.getFullname().length <= limit) // no need to compress
304
- return this.getFullname();
305
- const { firstname: fn, lastname: ln, middlename } = this.fullname;
306
- const mn = this.getMiddlenames().join(models_1.Separator.SPACE);
307
- const hasmid = Array.isArray(middlename) && middlename.length > 0;
308
- const sep = models_1.Separator.PERIOD;
309
- const firsts = fn.getInitials().join(sep).concat(sep);
310
- const lasts = ln.getInitials().join(sep).concat(sep);
311
- const mids = hasmid
312
- ? middlename.map(n => n.getInitials()).join(sep).concat(sep)
313
- : models_1.Separator.EMPTY;
314
- let cname = '';
315
- if (this.config.orderedBy === 'firstname') {
179
+ orderedBy = orderedBy || this.config.orderedBy;
180
+ return orderedBy === types_1.NameOrder.FIRST_NAME
181
+ ? [__classPrivateFieldGet(this, _Namefully_fullName, "f").firstName.value, __classPrivateFieldGet(this, _Namefully_fullName, "f").lastName.toString()].join(' ')
182
+ : [__classPrivateFieldGet(this, _Namefully_fullName, "f").lastName.toString(), __classPrivateFieldGet(this, _Namefully_fullName, "f").firstName.value].join(' ');
183
+ }
184
+ flatten(options) {
185
+ if (this.length <= options.limit)
186
+ return this.full;
187
+ const mergedOptions = Object.assign({ limit: 20, by: types_1.Flat.MIDDLE_NAME, withPeriod: true, recursive: false, withMore: false }, options);
188
+ const { by, limit, recursive, withMore, withPeriod, surname } = mergedOptions;
189
+ const sep = withPeriod ? '.' : '';
190
+ const fn = __classPrivateFieldGet(this, _Namefully_fullName, "f").firstName.toString();
191
+ const mn = this.middleName().join(' ');
192
+ const ln = __classPrivateFieldGet(this, _Namefully_fullName, "f").lastName.toString();
193
+ const hasMid = this.hasMiddle;
194
+ const f = __classPrivateFieldGet(this, _Namefully_fullName, "f").firstName.initials(withMore).join(sep + ' ') + sep;
195
+ const l = __classPrivateFieldGet(this, _Namefully_fullName, "f").lastName.initials(surname).join(sep + ' ') + sep;
196
+ const m = hasMid ? __classPrivateFieldGet(this, _Namefully_fullName, "f").middleName.map((n) => n.initials()[0]).join(sep + ' ') + sep : '';
197
+ let name = [];
198
+ if (this.config.orderedBy === types_1.NameOrder.FIRST_NAME) {
316
199
  switch (by) {
317
- case 'firstname':
318
- case 'fn':
319
- cname = hasmid ?
320
- [firsts, mn, ln.tostring()].join(models_1.Separator.SPACE) :
321
- [firsts, ln.tostring()].join(models_1.Separator.SPACE);
200
+ case types_1.Flat.FIRST_NAME:
201
+ name = hasMid ? [f, mn, ln] : [f, ln];
322
202
  break;
323
- case 'lastname':
324
- case 'ln':
325
- cname = hasmid ?
326
- [fn.tostring(), mn, lasts].join(models_1.Separator.SPACE) :
327
- [fn.tostring(), lasts].join(models_1.Separator.SPACE);
203
+ case types_1.Flat.LAST_NAME:
204
+ name = hasMid ? [fn, mn, l] : [fn, l];
328
205
  break;
329
- case 'middlename':
330
- case 'mn':
331
- cname = hasmid ?
332
- [fn.tostring(), mids, ln.tostring()].join(models_1.Separator.SPACE) :
333
- [fn.tostring(), ln.tostring()].join(models_1.Separator.SPACE);
206
+ case types_1.Flat.MIDDLE_NAME:
207
+ name = hasMid ? [fn, m, ln] : [fn, ln];
334
208
  break;
335
- case 'firstmid':
336
- case 'fm':
337
- cname = hasmid ?
338
- [firsts, mids, ln.tostring()].join(models_1.Separator.SPACE) :
339
- [firsts, ln.tostring()].join(models_1.Separator.SPACE);
209
+ case types_1.Flat.FIRST_MID:
210
+ name = hasMid ? [f, m, ln] : [f, ln];
340
211
  break;
341
- case 'midlast':
342
- case 'ml':
343
- cname = hasmid ?
344
- [fn.tostring(), mids, lasts].join(models_1.Separator.SPACE) :
345
- [fn.tostring(), lasts].join(models_1.Separator.SPACE);
212
+ case types_1.Flat.MID_LAST:
213
+ name = hasMid ? [fn, m, l] : [fn, l];
214
+ break;
215
+ case types_1.Flat.ALL:
216
+ name = hasMid ? [f, m, l] : [f, l];
346
217
  break;
347
218
  }
348
219
  }
349
220
  else {
350
221
  switch (by) {
351
- case 'firstname':
352
- case 'fn':
353
- cname = hasmid ?
354
- [ln.tostring(), firsts, mn].join(models_1.Separator.SPACE) :
355
- [ln.tostring(), firsts].join(models_1.Separator.SPACE);
222
+ case types_1.Flat.FIRST_NAME:
223
+ name = hasMid ? [ln, f, mn] : [ln, f];
224
+ break;
225
+ case types_1.Flat.LAST_NAME:
226
+ name = hasMid ? [l, fn, mn] : [l, fn];
356
227
  break;
357
- case 'lastname':
358
- case 'ln':
359
- cname = hasmid ?
360
- [lasts, fn.tostring(), mn].join(models_1.Separator.SPACE) :
361
- [lasts, fn.tostring()].join(models_1.Separator.SPACE);
228
+ case types_1.Flat.MIDDLE_NAME:
229
+ name = hasMid ? [ln, fn, m] : [ln, fn];
362
230
  break;
363
- case 'middlename':
364
- case 'mn':
365
- cname = hasmid ?
366
- [ln.tostring(), fn.tostring(), mids].join(models_1.Separator.SPACE) :
367
- [ln.tostring(), fn.tostring()].join(models_1.Separator.SPACE);
231
+ case types_1.Flat.FIRST_MID:
232
+ name = hasMid ? [ln, f, m] : [ln, f];
368
233
  break;
369
- case 'firstmid':
370
- case 'fm':
371
- cname = hasmid ?
372
- [ln.tostring(), firsts, mids].join(models_1.Separator.SPACE) :
373
- [ln.tostring(), firsts].join(models_1.Separator.SPACE);
234
+ case types_1.Flat.MID_LAST:
235
+ name = hasMid ? [l, fn, m] : [l, fn];
374
236
  break;
375
- case 'midlast':
376
- case 'ml':
377
- cname = hasmid ?
378
- [lasts, fn.tostring(), mids].join(models_1.Separator.SPACE) :
379
- [lasts, fn.tostring()].join(models_1.Separator.SPACE);
237
+ case types_1.Flat.ALL:
238
+ name = hasMid ? [l, f, m] : [l, f];
380
239
  break;
381
240
  }
382
241
  }
383
- if (warning && cname.length > limit)
384
- console.warn(`The compressed name <${cname}> still surpasses the set limit ${limit}`);
385
- return cname;
386
- }
387
- /**
388
- * Zips or compresses a name by using different forms of variants
389
- * @param by a variant to use when compressing the long name. The last two
390
- * variants represent respectively the combination of `firstname + middlename`
391
- * and `middlename + lastname`.
392
- */
393
- zip(by = 'mn') {
394
- let v;
395
- if (by === 'fn' || by === 'firstname')
396
- v = 'firstname';
397
- if (by === 'mn' || by === 'middlename')
398
- v = 'middlename';
399
- if (by === 'ln' || by === 'lastname')
400
- v = 'lastname';
401
- if (by === 'fm' || by === 'firstmid')
402
- v = 'firstmid';
403
- if (by === 'ml' || by === 'midlast')
404
- v = 'midlast';
405
- return this.compress(0, v, false);
406
- }
407
- /**
408
- * Suggests possible (randomly) usernames closest to the name
409
- *
410
- * **NOTE**
411
- * The validity of these usernames are not checked against any social media
412
- * or web app online.
413
- */
414
- username() {
415
- const unames = [];
416
- const { firstname, lastname } = this.fullname;
417
- const p = models_1.Separator.PERIOD;
418
- const fn = firstname.tostring().toLowerCase();
419
- const ln = lastname.father.toLowerCase();
420
- // Given `John Smith`
421
- unames.push(fn + ln); // johnsmith
422
- unames.push(ln + fn); // smithjohn
423
- unames.push(fn[0] + ln); // jsmith
424
- unames.push(ln[0] + fn); // sjohn
425
- unames.push(fn[0] + p + ln); // j.smith
426
- unames.push(ln[0] + p + fn); // s.john
427
- unames.push(fn.slice(0, 2) + ln); // josmith
428
- unames.push(ln.slice(0, 2) + fn); // smjohn
429
- unames.push(fn.slice(0, 2) + p + ln); // jo.smith
430
- unames.push(ln.slice(0, 2) + p + fn); // sm.john
431
- return unames;
432
- }
433
- /**
434
- * Formats the name as desired
435
- * @param {string} how to format the full name
436
- *
437
- * How to format it?
438
- * string format
439
- * -------------
440
- * 'short': typical first + last name
441
- * 'long': birth name (without prefix and suffix)
442
- *
443
- * char format
444
- * -----------
445
- * 'b': birth name
446
- * 'B': capitalized birth name
447
- * 'f': first name
448
- * 'F': capitalized first name
449
- * 'l': last name (official)
450
- * 'L': capitalized last name
451
- * 'm': middle names
452
- * 'M': capitalized middle names
453
- * 'o': official document format
454
- * 'O': official document format in capital letters
455
- * 'p': prefix
456
- * 'P': capitalized prefix
457
- * 's': suffix
458
- * 'S': capitalized suffix
459
- *
460
- * punctuations
461
- * ------------
462
- * '.': period
463
- * ',': comma
464
- * ' ': space
465
- * '-': hyphen
466
- * '_': underscore
467
- *
468
- * @example
469
- * Given the name `Joe Jim Smith`, call the `format` with the how string.
470
- * - format('l f') => 'Smith Joe'
471
- * - format('L, f') => 'SMITH, Joe'
472
- * - format('short') => 'Joe Smith'
473
- * - format() => 'SMITH, Joe Jim'
474
- */
475
- format(how = 'official') {
476
- if (how === 'short')
477
- return this.shorten();
478
- if (how === 'long')
479
- return this.getBirthname();
480
- if (how === 'official')
481
- how = 'o';
482
- const formatted = [];
483
- const tokens = [
484
- '.', ',', ' ', '-', '_', 'b', 'B', 'f', 'F', 'l', 'L', 'm', 'M',
485
- 'n', 'N', 'o', 'O', 'p', 'P', 's', 'S'
486
- ];
487
- for (const c of how) {
488
- if (tokens.indexOf(c) === -1)
489
- throw new Error(`<${c}> is an invalid character for the formatting.`);
490
- formatted.push(this.map(c));
491
- }
492
- return formatted.join(models_1.Separator.EMPTY).trim();
493
- }
494
- /**
495
- * Returns the count of characters of the birth name, excluding punctuations
496
- */
497
- size() {
498
- return new models_1.Summary(this.getBirthname(), [...core_1.RESTRICTED_CHARS]).count;
499
- }
500
- /**
501
- * Returns an ascii representation of each characters of a name as specified
502
- * @param options use specifics to shape conversion
503
- */
504
- ascii(options = {}) {
505
- const { exceptions: restrictions } = options;
506
- const nameType = core_1.allowShortNameType(options.nameType);
507
- const { firstname, lastname, middlename } = this.fullname;
508
- switch (nameType) {
509
- case 'firstname':
510
- return firstname.ascii(restrictions);
511
- case 'lastname':
512
- return lastname.ascii(restrictions);
513
- case 'middlename':
514
- if (!this.hasMiddlename())
515
- console.warn(`No ASCII conversion for middle names since none was set.`);
516
- return middlename
517
- .map(n => n.ascii(restrictions)) // convert
518
- .reduce((acc, value) => acc.concat(value), []); // then flatten
519
- default:
520
- const firsts = firstname.ascii(restrictions);
521
- const mids = middlename
522
- .map(n => n.ascii(restrictions))
523
- .reduce((acc, value) => acc.concat(value), []);
524
- const lasts = lastname.ascii(restrictions);
525
- if (this.config.orderedBy === 'firstname') {
526
- return firsts.concat(mids, lasts);
527
- }
528
- return lasts.concat(firsts, mids);
242
+ const flat = name.join(' ');
243
+ if (recursive && flat.length > limit) {
244
+ const next = by === types_1.Flat.FIRST_NAME
245
+ ? types_1.Flat.MIDDLE_NAME
246
+ : by === types_1.Flat.MIDDLE_NAME
247
+ ? types_1.Flat.LAST_NAME
248
+ : by === types_1.Flat.LAST_NAME
249
+ ? types_1.Flat.FIRST_MID
250
+ : by === types_1.Flat.FIRST_MID
251
+ ? types_1.Flat.MID_LAST
252
+ : by === types_1.Flat.MID_LAST
253
+ ? types_1.Flat.ALL
254
+ : by === types_1.Flat.ALL
255
+ ? types_1.Flat.ALL
256
+ : by;
257
+ if (next === by)
258
+ return flat;
259
+ return this.flatten(Object.assign(Object.assign({}, options), { by: next }));
529
260
  }
261
+ return flat;
530
262
  }
531
- /**
532
- * Transforms a birth name to a specific case
533
- * @param case which case to convert a birth name to
534
- */
535
- to(_case) {
536
- const birthname = this.getBirthname();
537
- const nama = birthname
538
- .replace(/[' -]/g, models_1.Separator.SPACE)
539
- .split(models_1.Separator.SPACE);
540
- switch (_case) {
541
- case 'upper':
542
- return birthname.toUpperCase();
543
- case 'lower':
544
- return birthname.toLowerCase();
545
- case 'camel':
546
- case 'pascal':
547
- const pascalCase = nama.map(n => core_1.capitalize(n)).join(models_1.Separator.EMPTY);
548
- return _case === 'camel' ? core_1.decapitalize(pascalCase) : pascalCase;
549
- case 'snake':
550
- return nama.map(n => n.toLowerCase()).join(models_1.Separator.UNDERSCORE);
551
- case 'hyphen':
552
- return nama.map(n => n.toLowerCase()).join(models_1.Separator.HYPHEN);
553
- case 'dot':
554
- return nama.map(n => n.toLowerCase()).join(models_1.Separator.PERIOD);
555
- case 'toggle':
556
- return core_1.toggleCase(birthname);
557
- default:
558
- return models_1.Separator.EMPTY;
559
- }
263
+ zip(by = types_1.Flat.MID_LAST, withPeriod = true) {
264
+ return this.flatten({ limit: 0, by, withPeriod });
560
265
  }
561
- /**
562
- * Returns a password-like representation of a name
563
- * @param {NameType} [what] which name part
564
- */
565
- passwd(what) {
566
- what = core_1.allowShortNameType(what);
567
- switch (what) {
568
- case 'firstname':
569
- return this.fullname.firstname.passwd();
570
- case 'lastname':
571
- return this.fullname.lastname.passwd();
572
- case 'middlename':
573
- if (!this.hasMiddlename())
574
- console.warn('No password for middle names since none was set.');
575
- return this.fullname.middlename
576
- .map(n => n.passwd())
577
- .join(models_1.Separator.EMPTY);
578
- default:
579
- return core_1.generatePassword(this.getBirthname());
266
+ format(pattern) {
267
+ var _a;
268
+ if (pattern === 'short')
269
+ return this.short;
270
+ if (pattern === 'long')
271
+ return this.long;
272
+ if (pattern === 'public')
273
+ return this.public;
274
+ if (pattern === 'official')
275
+ pattern = 'o';
276
+ let group = '';
277
+ const formatted = [];
278
+ for (const char of pattern.split('')) {
279
+ if (constants_1.ALLOWED_TOKENS.indexOf(char) === -1) {
280
+ throw new error_1.NotAllowedError({
281
+ source: this.full,
282
+ operation: 'format',
283
+ message: `unsupported character <${char}> from ${pattern}.`,
284
+ });
285
+ }
286
+ group += char;
287
+ if (char === '$')
288
+ continue;
289
+ formatted.push((_a = __classPrivateFieldGet(this, _Namefully_instances, "m", _Namefully_map).call(this, group)) !== null && _a !== void 0 ? _a : '');
290
+ group = '';
580
291
  }
292
+ return formatted.join('').trim();
581
293
  }
582
- hasMiddlename() {
583
- return Array.isArray(this.fullname.middlename) && this.fullname.middlename.length > 0;
584
- }
585
- configure(options) {
586
- // consider using deepmerge if objects no longer stay shallow
587
- this.config = Object.assign(Object.assign({}, core_1.CONFIG), options); // if options, it overrides CONFIG
588
- this.config.orderedBy = core_1.allowShortNameOrder(this.config.orderedBy);
589
- }
590
- initialize(parser) {
591
- const { orderedBy, separator, bypass, lastnameFormat } = this.config;
592
- this.fullname = parser.parse({ orderedBy, separator, bypass, lastnameFormat });
593
- }
594
- parseNameOrder(orderedBy) {
595
- orderedBy = orderedBy || this.config.orderedBy; // override config
596
- return core_1.allowShortNameOrder(orderedBy);
597
- }
598
- map(c) {
599
- const { firstname, lastname, middlename, prefix, suffix } = this.fullname;
600
- switch (c) {
601
- case '.':
602
- return models_1.Separator.PERIOD;
603
- case ',':
604
- return models_1.Separator.COMMA;
605
- case ' ':
606
- return models_1.Separator.SPACE;
607
- case '-':
608
- return models_1.Separator.HYPHEN;
609
- case '_':
610
- return models_1.Separator.UNDERSCORE;
611
- case 'b':
612
- return this.getBirthname();
613
- case 'B':
614
- return this.getBirthname().toUpperCase();
615
- case 'f':
616
- return firstname.tostring();
617
- case 'F':
618
- return firstname.tostring().toUpperCase();
619
- case 'l':
620
- return lastname.tostring();
621
- case 'L':
622
- return lastname.tostring().toUpperCase();
623
- case 'm':
624
- if (!this.hasMiddlename()) {
625
- console.warn('No formatting for middle names since none was set.');
626
- return models_1.Separator.EMPTY;
627
- }
628
- return this.fullname.middlename.map(n => n.namon).join(models_1.Separator.SPACE);
629
- case 'M':
630
- if (!this.hasMiddlename()) {
631
- console.warn('No formatting for middle names since none was set.');
632
- return models_1.Separator.EMPTY;
633
- }
634
- return middlename.map(n => n.namon.toUpperCase()).join(models_1.Separator.SPACE);
635
- case 'o':
636
- case 'O':
637
- const { titling, ending } = this.config;
638
- const pxSep = titling === 'us' ? models_1.Separator.PERIOD : models_1.Separator.EMPTY;
639
- const sxSep = ending ? ',' : models_1.Separator.EMPTY;
640
- const nama = [];
641
- if (prefix)
642
- nama.push(prefix.concat(pxSep));
643
- nama.push(lastname.tostring().concat(models_1.Separator.COMMA).toUpperCase());
644
- if (this.hasMiddlename()) {
645
- nama.push(firstname.tostring());
646
- nama.push(middlename.map(n => n.namon).join(models_1.Separator.SPACE).concat(sxSep));
647
- }
648
- else {
649
- nama.push(firstname.tostring().concat(sxSep));
650
- }
651
- nama.push(suffix || models_1.Separator.EMPTY);
652
- const official = nama.join(models_1.Separator.SPACE).trim();
653
- return c === 'o' ? official : official.toUpperCase();
654
- case 'p':
655
- return prefix || models_1.Separator.EMPTY;
656
- case 'P':
657
- return prefix ? prefix.toUpperCase() : models_1.Separator.EMPTY;
658
- case 's':
659
- return suffix || models_1.Separator.EMPTY;
660
- case 'S':
661
- return suffix ? suffix.toUpperCase() : models_1.Separator.EMPTY;
662
- }
294
+ flip() {
295
+ this.config.updateOrder(this.config.orderedBy === types_1.NameOrder.FIRST_NAME ? types_1.NameOrder.LAST_NAME : types_1.NameOrder.FIRST_NAME);
663
296
  }
664
- build(raw) {
665
- if (this.config.parser) {
666
- this.initialize(this.config.parser);
667
- }
668
- else if (typeof raw === 'string') { // check for string type
669
- this.initialize(new core_1.StringParser(raw));
670
- }
671
- else if (Array.isArray(raw) && raw.length) { // check for T[]
672
- if (typeof raw[0] === 'string') { // check for string[]
673
- for (const key of raw)
674
- if (typeof key !== 'string')
675
- throw new Error(`Cannot parse raw data as array of 'string'`);
676
- this.initialize(new core_1.ArrayStringParser(raw));
677
- }
678
- else if (raw[0] instanceof models_1.Name) { // check for Name[]
679
- for (const obj of raw)
680
- if (!(obj instanceof models_1.Name))
681
- throw new Error(`Cannot parse raw data as array of 'Name'`);
682
- this.initialize(new core_1.ArrayNameParser(raw));
297
+ split(separator = /[' -]/g) {
298
+ return this.birth.replace(separator, ' ').split(' ');
299
+ }
300
+ join(separator = '') {
301
+ return this.split().join(separator);
302
+ }
303
+ toUpperCase() {
304
+ return this.birth.toUpperCase();
305
+ }
306
+ toLowerCase() {
307
+ return this.birth.toLowerCase();
308
+ }
309
+ toCamelCase() {
310
+ return (0, utils_1.decapitalize)(this.toPascalCase());
311
+ }
312
+ toPascalCase() {
313
+ return this.split()
314
+ .map((n) => (0, utils_1.capitalize)(n))
315
+ .join('');
316
+ }
317
+ toSnakeCase() {
318
+ return this.split()
319
+ .map((n) => n.toLowerCase())
320
+ .join('_');
321
+ }
322
+ toHyphenCase() {
323
+ return this.split()
324
+ .map((n) => n.toLowerCase())
325
+ .join('-');
326
+ }
327
+ toDotCase() {
328
+ return this.split()
329
+ .map((n) => n.toLowerCase())
330
+ .join('.');
331
+ }
332
+ toToggleCase() {
333
+ return (0, utils_1.toggleCase)(this.birth);
334
+ }
335
+ }
336
+ exports.Namefully = Namefully;
337
+ _Namefully_fullName = new WeakMap(), _Namefully_instances = new WeakSet(), _Namefully_toParser = function _Namefully_toParser(raw) {
338
+ if (raw instanceof parser_1.Parser)
339
+ return raw;
340
+ if (typeof raw === 'string')
341
+ return new parser_1.StringParser(raw);
342
+ if ((0, utils_1.isStringArray)(raw))
343
+ return new parser_1.ArrayStringParser(raw);
344
+ if ((0, utils_1.isNameArray)(raw))
345
+ return new parser_1.ArrayNameParser(raw);
346
+ if (typeof raw === 'object')
347
+ return new parser_1.NamaParser(raw);
348
+ throw new error_1.InputError({ source: raw, message: 'Cannot parse raw data. Review expected data types.' });
349
+ }, _Namefully_map = function _Namefully_map(char) {
350
+ var _a, _b;
351
+ switch (char) {
352
+ case '.':
353
+ case ',':
354
+ case ' ':
355
+ case '-':
356
+ case '_':
357
+ return char;
358
+ case 'b':
359
+ return this.birth;
360
+ case 'B':
361
+ return this.birth.toUpperCase();
362
+ case 'f':
363
+ return this.first;
364
+ case 'F':
365
+ return this.first.toUpperCase();
366
+ case 'l':
367
+ return this.last;
368
+ case 'L':
369
+ return this.last.toUpperCase();
370
+ case 'm':
371
+ case 'M':
372
+ return char === 'm' ? this.middleName().join(' ') : this.middleName().join(' ').toUpperCase();
373
+ case 'o':
374
+ case 'O':
375
+ const sep = this.config.ending ? ',' : '';
376
+ const names = [];
377
+ if (this.prefix)
378
+ names.push(this.prefix);
379
+ names.push(`${this.last},`.toUpperCase());
380
+ if (this.hasMiddle) {
381
+ names.push(this.first, this.middleName().join(' ') + sep);
683
382
  }
684
383
  else {
685
- // typescript should stop them, but let's be paranoid (for JS developers)
686
- throw new Error(`Cannot parse raw data as arrays that are not of 'Name' or string`);
687
- }
688
- }
689
- else if (raw instanceof Object) { // check for json object
690
- for (const [key, value] of Object.entries(raw)) { // make sure keys are correct
691
- if (['firstname', 'lastname', 'middlename', 'prefix', 'suffix'].indexOf(key) === -1)
692
- throw new Error(`Cannot parse raw data as json object that does not contains keys of` +
693
- `'${Object.keys(models_1.Namon)}'`);
694
- // make sure the values are proper string or object
695
- if (typeof value !== 'string' && typeof value !== 'object')
696
- throw new Error(`Cannot parse raw data. The key <${key}> should be a 'string|object' type`);
384
+ names.push(this.first + sep);
697
385
  }
698
- if (typeof raw['firstname'] === 'string') // this key must always exist
699
- this.initialize(new core_1.NamaParser(raw));
700
- else
701
- this.fullname = raw;
702
- }
703
- else {
704
- // typescript should stop them, but let's be paranoid again (for JS developers)
705
- throw new Error(`Cannot parse raw data. Review the data type expected.`);
706
- }
707
- // paranoid coder mode: on :P
708
- if (!this.config.bypass)
709
- new validators_1.FullnameValidator().validate(this.fullname);
710
- this.summary = new models_1.Summary(this.getFullname());
386
+ if (this.suffix)
387
+ names.push(this.suffix);
388
+ const nama = names.join(' ').trim();
389
+ return char === 'o' ? nama : nama.toUpperCase();
390
+ case 'p':
391
+ return this.prefix;
392
+ case 'P':
393
+ return (_a = this.prefix) === null || _a === void 0 ? void 0 : _a.toUpperCase();
394
+ case 's':
395
+ return this.suffix;
396
+ case 'S':
397
+ return (_b = this.suffix) === null || _b === void 0 ? void 0 : _b.toUpperCase();
398
+ case '$f':
399
+ case '$F':
400
+ return __classPrivateFieldGet(this, _Namefully_fullName, "f").firstName.initials()[0];
401
+ case '$l':
402
+ case '$L':
403
+ return __classPrivateFieldGet(this, _Namefully_fullName, "f").lastName.initials()[0];
404
+ case '$m':
405
+ case '$M':
406
+ return this.hasMiddle ? this.middle[0] : undefined;
407
+ default:
408
+ return undefined;
711
409
  }
712
- }
713
- exports.Namefully = Namefully;
714
- Namefully.prototype.full = Namefully.prototype.getFullname;
715
- Namefully.prototype.birth = Namefully.prototype.getBirthname;
716
- Namefully.prototype.fn = Namefully.prototype.getFirstname;
717
- Namefully.prototype.ln = Namefully.prototype.getLastname;
718
- Namefully.prototype.mn = Namefully.prototype.getMiddlenames;
719
- Namefully.prototype.px = Namefully.prototype.getPrefix;
720
- Namefully.prototype.sx = Namefully.prototype.getSuffix;
721
- Namefully.prototype.inits = Namefully.prototype.getInitials;
722
- Namefully.prototype.stats = Namefully.prototype.describe;
723
- //# sourceMappingURL=namefully.js.map
410
+ };