randex 0.3.0 → 0.5.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.
@@ -14,6 +14,7 @@
14
14
  "json"
15
15
  ],
16
16
  "cSpell.words": [
17
+ "Randex",
17
18
  "tebot"
18
19
  ],
19
20
  }
package/README.md CHANGED
@@ -14,63 +14,110 @@ Installation:
14
14
  npm i randex
15
15
  ```
16
16
 
17
- - [`random` function](#random-function)
18
- - [randomFileName](#randomfilename)
19
- - [randomUsername](#randomfilename)
20
- - [randomEmail](#randomfilename)
21
- - [randomName](#randomfilename)
22
- - [randomFullName](#randomfilename)
17
+ Functions:
23
18
 
19
+ - [random](#random)
20
+ - [randomFileName](#randomfilename)
21
+ - [randomUsername](#randomusername)
22
+ - [randomEmail](#randomemail)
23
+ - [randomName](#randomname)
24
+ - [randomFullName](#randomfullname)
24
25
 
25
- ## `random` function
26
+ ## random
26
27
 
27
28
  Basic function for construct random thing.
28
29
 
29
30
  ```ts
30
- import { random } from 'randex';
31
-
31
+ import { random } from "randex";
32
+
33
+ // 3 english chars
34
+ random({
35
+ set: "english",
36
+ length: 3,
37
+ });
38
+ // EFd
39
+
40
+ // 3 lower english chars
41
+ random({
42
+ set: ["english", "lower"],
43
+ length: 3,
44
+ });
45
+ // yhl
46
+
47
+ // min 1 and max 3 english chars
48
+ random({
49
+ set: "english",
50
+ length: [1, 3],
51
+ });
52
+ // eR
53
+
54
+ // 3 number chars
55
+ random({
56
+ set: "number",
57
+ length: 3,
58
+ });
59
+ // 643
60
+
61
+ // 3 chars from custom range
62
+ random({
63
+ range: "abc123",
64
+ length: 3,
65
+ });
66
+ // a21
67
+
68
+ // 3 english or number chars
69
+ random({
70
+ set: ["english", "number"],
71
+ length: 3,
72
+ });
73
+
74
+ // 4 chars: 2 english and 2 english or number
32
75
  random(
33
- {
34
- set: "alphabet",
35
- length: 2,
36
- },
37
- {
38
- set: ["alphabet", "number"],
39
- length: 2,
40
- }
76
+ {
77
+ set: "english",
78
+ length: 2,
79
+ },
80
+ {
81
+ set: ["english", "number"],
82
+ length: 2,
83
+ }
41
84
  );
42
-
43
- // gTo3
85
+ // Kb3b
44
86
  ```
45
87
 
46
88
  Options:
47
- | Name | Type| Description|
89
+ | Name | Type| Description|
48
90
  | ------------------------ | ---- |------------ |
49
- |set| `hex`, `symbol`, `alphabet`, `alphabetLower`, `alphabetUpper`, `number`, `binary` | Set of predefined chars|
91
+ |set|[RandexSet](#randexset) | Defined chars|
50
92
  |range|`string`| Range or custom chars |
51
- |length|`number`, `[number, number]` | Length or min and max length of chars are needed to be generated |
52
-
53
-
93
+ |length|[RandexLength](#randexlength) | Length of chars |
54
94
 
55
95
  ## randomFileName
56
96
 
57
97
  Randoms file name.
58
98
 
59
99
  ```ts
60
- import { randomFileName } from 'randex';
100
+ import { randomFileName } from "randex";
61
101
 
62
- randomFileName(
63
- {
64
- extension: 'txt'
65
- },
66
- );
102
+ randomFileName({
103
+ extension: "txt",
104
+ });
67
105
 
68
- // FSCOuNN.txt
106
+ // td1TX31eOB.txt
69
107
  ```
70
108
 
71
- Equals to:
109
+ Equals to:
110
+
72
111
  ```ts
73
- random([["alphabet", "number"], "_.", [5, 10]]) + random(["alphabet", "number"]) + "." + (extension ? extension : random(["alphabetLower", [2, 5]]));
112
+ random([["english", "number"], "_.", [5, 10]]) +
113
+ random(["english", "number"]) +
114
+ "." +
115
+ (extension
116
+ ? extension
117
+ : random([
118
+ ["english", "l"],
119
+ [2, 5],
120
+ ]));
74
121
  ```
75
122
 
76
123
  ## randomUsername
@@ -78,20 +125,23 @@ random([["alphabet", "number"], "_.", [5, 10]]) + random(["alphabet", "number"])
78
125
  Randoms username.
79
126
 
80
127
  ```ts
81
- import { randomUsername } from 'randex';
82
-
128
+ import { randomUsername } from "randex";
83
129
 
84
130
  randomUsername();
85
131
 
86
132
  // icvv81d1j
87
133
  ```
88
134
 
89
- Equals to:
135
+ Equals to:
136
+
90
137
  ```ts
91
- random("alphabetLower", [
92
- ["alphabetLower", "number"],
138
+ random(
139
+ ["english", "l"],
140
+ [
141
+ [["english", "l"], "number"],
93
142
  [5, 10],
94
- ]);
143
+ ]
144
+ );
95
145
  ```
96
146
 
97
147
  ## randomEmail
@@ -99,21 +149,24 @@ random("alphabetLower", [
99
149
  Randoms username.
100
150
 
101
151
  ```ts
102
- import { randomEmail } from 'randex';
103
-
152
+ import { randomEmail } from "randex";
104
153
 
105
154
  randomEmail();
106
155
 
107
- // dwrq4wu54nzj@dhvy.dtk
156
+ // stv4ox27sevt@mqsyin.fil
108
157
  ```
109
158
 
110
- Equals to:
159
+ Equals to:
160
+
111
161
  ```ts
112
- random("alphabetLower", [["alphabetLower", "number"], 10], "alphabetLower") +
113
- "@" +
114
- random("alphabetLower", ["alphabetLower", "-", [0, 5]], "alphabetLower") +
115
- "." +
116
- random(["alphabetLower", [2, 4]]);
162
+ random(["english", "l"], [[["english", "l"], "number"], 10], ["english", "l"]) +
163
+ "@" +
164
+ random(["english", "l"], [["english", "l"], "-", [0, 5]], ["english", "l"]) +
165
+ "." +
166
+ random([
167
+ ["english", "l"],
168
+ [2, 4],
169
+ ]);
117
170
  ```
118
171
 
119
172
  ## randomName
@@ -121,39 +174,93 @@ random("alphabetLower", [["alphabetLower", "number"], 10], "alphabetLower") +
121
174
  Randoms a name of the person, city, place, restaurant, ect.
122
175
 
123
176
  ```ts
124
- import { randomName } from 'randex';
125
-
177
+ import { randomName } from "randex";
126
178
 
179
+ // default (min - 2, max - 10 chars)
127
180
  randomName();
181
+ // Ijb
182
+
183
+ // name from french alphabet
184
+ randomName({ alphabet: "french" });
185
+ // Dbïœ
128
186
 
129
- // Txotsaw
187
+ // name with length 5 chars
188
+ randomName({ length: 5 });
189
+ // Okmpj
190
+
191
+ // name with 2 min and 5 max chars
192
+ randomName({ length: [2, 5] });
193
+ // Wslg
130
194
  ```
131
195
 
132
- Equals to:
196
+ Equals to:
197
+
133
198
  ```ts
134
- random("alphabetUpper", ["alphabetLower", [1, 10]]);
199
+ random(
200
+ ["english", "u"],
201
+ [
202
+ ["english", "l"],
203
+ [1, 10],
204
+ ]
205
+ );
135
206
  ```
136
207
 
137
208
  Options:
138
- | Name | Type| Description|
209
+ | Name | Type| Description|
139
210
  | ------------------------ | ---- |------------ |
140
- |length|`number`, `[number, number]` | Length or min and max length of chars are needed to be generated |
211
+ |length|[RandexLength](#randexlength) | Length of chars |
212
+ |alphabet|[RandexAlphabet](#randexalphabet) | Defined alphabet |
141
213
 
142
214
  ## randomFullName
143
215
 
144
216
  Randoms a full name of the person.
145
217
 
146
218
  ```ts
147
- import { randomFullName } from 'randex';
148
-
219
+ import { randomFullName } from "randex";
149
220
 
150
221
  randomFullName();
151
-
152
222
  // Eqaa Bfmotnq
153
223
  ```
154
224
 
155
- Equals to:
156
- ```ts
157
- random("alphabetUpper", ["alphabetLower", [1, 10]]) + " " + random("alphabetUpper", ["alphabetLower", [1, 10]]);
158
- ```
225
+ Options:
226
+ | Name | Type| Description|
227
+ | ------------------------ | ---- |------------ |
228
+ |firstLength|[RandexLength](#randexlength) | First name length of chars |
229
+ |secondLength|[RandexLength](#randexlength) | Second name length of chars |
230
+ |alphabet|[RandexAlphabet](#randexalphabet) | Defined alphabet |
231
+
232
+ ## Types
233
+
234
+ ## RandexLength
235
+
236
+ Possible types:
237
+
238
+ `number`: strict length
239
+
240
+ `[number, number]`: an array of min and max length.
241
+
242
+ ## RandexSet
243
+
244
+ Possible types:
245
+
246
+ `string`: an alphabet - [RandexAlphabet](#randexalphabet) or a set of chars - [RandexKit](#randexkit)
247
+
248
+ `[string, string]`: the first item is - [RandexAlphabet](#randexalphabet), the second item is - [RandexCase](#randexcase)
249
+
250
+ ## RandexAlphabet
251
+
252
+ An alphabet of chars
253
+
254
+ `string` values: `english`, `french`, `spanish`, `russian`.
255
+
256
+ ## RandexKit
257
+
258
+ A kit of chars
259
+
260
+ `string` values: `hex`, `symbol`, `number`, `binary`.
261
+
262
+ ## RandexCase
263
+
264
+ A case of alphabet
159
265
 
266
+ `string` values: `upper`, `u`,`lower`, `l`.
@@ -1,2 +1,2 @@
1
- import { RandomPartOptions } from "../interfaces";
2
- export declare function random(...options: RandomPartOptions[]): string;
1
+ import { RandexContentOptions } from "../interfaces";
2
+ export declare function random(...options: RandexContentOptions[]): string;
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.random = void 0;
4
4
  var set_1 = require("./set");
5
+ var type_1 = require("./type");
5
6
  function randomInt(max) {
6
7
  return Math.floor(Math.random() * max);
7
8
  }
@@ -18,56 +19,50 @@ function randomString(params) {
18
19
  }
19
20
  for (var i = 0; i < currentLength; i++) {
20
21
  var index = randomInt(range.length - 1);
21
- result += range[index];
22
+ if (range.length >= index || range.length <= index) {
23
+ result += range[index];
24
+ }
22
25
  }
23
26
  return result;
24
27
  }
25
- function ifLengthType(value) {
26
- return typeof value === "number" || Array.isArray(value);
27
- }
28
- function toFullOption(options) {
29
- if (typeof options === "string") {
28
+ function toContent(options) {
29
+ if (type_1.RandexTypeParser.isSet(options)) {
30
30
  return {
31
31
  set: options,
32
- length: 1,
33
32
  };
34
33
  }
35
- else if (Array.isArray(options)) {
34
+ else if (type_1.RandexTypeParser.isPartArray(options)) {
36
35
  if (options.length === 2) {
37
36
  var set = options[0], rangeOrLength = options[1];
38
- var result = {
39
- set: set,
40
- };
41
- if (ifLengthType(rangeOrLength)) {
42
- result.length = rangeOrLength;
43
- }
44
- else {
45
- result.range = rangeOrLength;
46
- }
47
- return result;
37
+ return type_1.RandexTypeParser.isLength(rangeOrLength)
38
+ ? {
39
+ set: set,
40
+ length: rangeOrLength,
41
+ }
42
+ : {
43
+ set: set,
44
+ range: rangeOrLength,
45
+ };
48
46
  }
49
- if (options.length === 3 && ifLengthType(options[2])) {
47
+ else if (options.length === 3) {
48
+ var set = options[0], range = options[1], length_1 = options[2];
50
49
  return {
51
- set: options[0],
52
- range: options[1],
53
- length: options[2],
50
+ set: set,
51
+ range: range,
52
+ length: length_1,
54
53
  };
55
54
  }
56
- return {
57
- set: options,
58
- length: 1,
59
- };
60
55
  }
61
56
  return options;
62
57
  }
63
- function toParam(options) {
58
+ function toRangeOptions(options) {
64
59
  var fullRange = "";
65
60
  var set = options.set, range = options.range, _a = options.length, length = _a === void 0 ? 1 : _a;
66
61
  if (range) {
67
62
  fullRange += range;
68
63
  }
69
64
  if (set) {
70
- fullRange += Array.isArray(set) ? set.map(function (x) { return set_1.RtSetUtil.toRange(x); }).join("") : set_1.RtSetUtil.toRange(set);
65
+ fullRange += set_1.RtSetUtil.toRange(set);
71
66
  }
72
67
  return {
73
68
  range: fullRange,
@@ -82,8 +77,8 @@ function random() {
82
77
  var result = "";
83
78
  for (var _a = 0, options_1 = options; _a < options_1.length; _a++) {
84
79
  var option = options_1[_a];
85
- var o = toFullOption(option);
86
- var p = toParam(o);
80
+ var content = toContent(option);
81
+ var p = toRangeOptions(content);
87
82
  result += randomString(p);
88
83
  }
89
84
  return result;
@@ -1,6 +1,7 @@
1
- import { RandomSet } from "../interfaces";
2
- export declare const alphabet: string;
1
+ import { RandexSet } from "../interfaces";
3
2
  export declare class RtSetUtil {
4
3
  private static toSingleRange;
5
- static toRange(set: RandomSet): string;
4
+ private static toAlphabetCase;
5
+ private static toAlphabetRange;
6
+ static toRange(set: RandexSet): string;
6
7
  }
package/lib/basic/set.js CHANGED
@@ -1,24 +1,30 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.RtSetUtil = exports.alphabet = void 0;
4
- var alphabetUpper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
5
- var alphabetLower = "abcdefghijklmnopqrstuvwxyz";
3
+ exports.RtSetUtil = void 0;
4
+ var type_1 = require("./type");
6
5
  var hex = "0123456789ABCDEFabcdef";
7
6
  var symbol = '[-!$%^&*()_+|~=`{}[]:";<>?,./]' + "'";
8
- exports.alphabet = alphabetLower + alphabetUpper;
9
7
  var number = "0123456789";
10
8
  var binary = "01";
9
+ var englishLower = "abcdefghijklmnopqrstuvwxyz";
10
+ var englishUpper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
11
+ var frenchLower = "abcdefghijklmnopqrstuvwxyzàâæçèéêëîïôœùûü";
12
+ var frenchUpper = "ABCDEFGHIJKLMNOPQRSTUVWXYZÀÂÆÇÈÉÊËÎÏÔŒÙÛÜ";
13
+ var spanishLower = "abcdefghijklmnopqrstuvwxyzñ";
14
+ var spanishUpper = "ABCDEFGHIJKLMNOPQRSTUVWXYZÑ";
15
+ var russianLower = "абвгдеёжзийклмнопрстуфхцчшщъыьэюя";
16
+ var russianUpper = "АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ";
11
17
  var RtSetUtil = /** @class */ (function () {
12
18
  function RtSetUtil() {
13
19
  }
14
- RtSetUtil.toSingleRange = function (set) {
15
- switch (set) {
16
- case "alphabet":
17
- return exports.alphabet;
18
- case "alphabetUpper":
19
- return alphabetUpper;
20
- case "alphabetLower":
21
- return alphabetLower;
20
+ RtSetUtil.toSingleRange = function (itemSet) {
21
+ if (Array.isArray(itemSet)) {
22
+ return RtSetUtil.toAlphabetRange(itemSet);
23
+ }
24
+ else if (type_1.RandexTypeParser.inAlphabet(itemSet)) {
25
+ return RtSetUtil.toAlphabetRange([itemSet, "default"]);
26
+ }
27
+ switch (itemSet) {
22
28
  case "hex":
23
29
  return hex;
24
30
  case "symbol":
@@ -31,8 +37,42 @@ var RtSetUtil = /** @class */ (function () {
31
37
  return "";
32
38
  }
33
39
  };
40
+ RtSetUtil.toAlphabetCase = function (cas, lowerWord, upperWord) {
41
+ switch (cas) {
42
+ case "lower":
43
+ case "l":
44
+ return lowerWord;
45
+ case "upper":
46
+ case "u":
47
+ return upperWord;
48
+ default:
49
+ return lowerWord + upperWord;
50
+ }
51
+ };
52
+ RtSetUtil.toAlphabetRange = function (set) {
53
+ var alphabet = set[0], aCase = set[1];
54
+ switch (alphabet) {
55
+ case "english":
56
+ return RtSetUtil.toAlphabetCase(aCase, englishLower, englishUpper);
57
+ case "french":
58
+ return RtSetUtil.toAlphabetCase(aCase, frenchLower, frenchUpper);
59
+ case "spanish":
60
+ return RtSetUtil.toAlphabetCase(aCase, spanishLower, spanishUpper);
61
+ // case "german":
62
+ // return RtSetUtil.toAlphabetCase(aCase, englishLower, englishUpper);
63
+ case "russian":
64
+ return RtSetUtil.toAlphabetCase(aCase, russianLower, russianUpper);
65
+ }
66
+ };
34
67
  RtSetUtil.toRange = function (set) {
35
- return Array.isArray(set) ? set.map(function (x) { return RtSetUtil.toSingleRange(x); }).join("") : RtSetUtil.toSingleRange(set);
68
+ var result = "";
69
+ if (type_1.RandexTypeParser.isSetSingle(set)) {
70
+ result += RtSetUtil.toSingleRange(set);
71
+ }
72
+ else if (type_1.RandexTypeParser.isSet(set)) {
73
+ result += set.map(RtSetUtil.toSingleRange).join("");
74
+ }
75
+ return result;
36
76
  };
37
77
  return RtSetUtil;
38
78
  }());
@@ -0,0 +1,12 @@
1
+ import { RandexCase, RandexAlphabet, RandexItemSet, RandexContentArrayOptions, RandexSet, RandexSingleSet, RandexLength } from "../interfaces";
2
+ export declare class RandexTypeParser {
3
+ private static inRange;
4
+ private static inLiteral;
5
+ static inCase(value: string): value is RandexCase;
6
+ static inAlphabet(value: string): value is RandexAlphabet;
7
+ static inSet(value: string): value is RandexSingleSet;
8
+ static isLength(value: any): value is RandexLength;
9
+ static isSetSingle(value: any): value is RandexItemSet;
10
+ static isSet(value: any): value is RandexSet;
11
+ static isPartArray(value: any): value is RandexContentArrayOptions;
12
+ }
@@ -0,0 +1,78 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RandexTypeParser = void 0;
4
+ var RandexTypeParser = /** @class */ (function () {
5
+ function RandexTypeParser() {
6
+ }
7
+ RandexTypeParser.inRange = function (value, range) {
8
+ var min = range[0], max = range[1];
9
+ return value >= min && value <= max;
10
+ };
11
+ RandexTypeParser.inLiteral = function (value) {
12
+ var expects = [];
13
+ for (var _i = 1; _i < arguments.length; _i++) {
14
+ expects[_i - 1] = arguments[_i];
15
+ }
16
+ return !!expects.find(function (x) { return x === value; });
17
+ };
18
+ RandexTypeParser.inCase = function (value) {
19
+ return RandexTypeParser.inLiteral(value, "default", "lower", "upper", "l", "u");
20
+ };
21
+ RandexTypeParser.inAlphabet = function (value) {
22
+ return RandexTypeParser.inLiteral(value, "english", "french", "spanish", // "german",
23
+ "russian");
24
+ };
25
+ RandexTypeParser.inSet = function (value) {
26
+ return RandexTypeParser.inAlphabet(value) || RandexTypeParser.inLiteral(value, "hex", "symbol", "number", "binary");
27
+ };
28
+ // RandomLength
29
+ RandexTypeParser.isLength = function (value) {
30
+ if (typeof value === "number") {
31
+ return true;
32
+ }
33
+ else if (Array.isArray(value) && value.length === 2) {
34
+ return typeof value[0] === "number" && typeof value[1] === "number";
35
+ }
36
+ return false;
37
+ };
38
+ // RandomItemSet
39
+ RandexTypeParser.isSetSingle = function (value) {
40
+ if (typeof value === "string") {
41
+ return RandexTypeParser.inSet(value);
42
+ }
43
+ else if (Array.isArray(value) && value.length === 2) {
44
+ var _a = value, alphabet = _a[0], aCase = _a[1];
45
+ if (typeof alphabet === "string" && typeof aCase === "string") {
46
+ if (RandexTypeParser.inAlphabet(alphabet) && RandexTypeParser.inCase(aCase)) {
47
+ return true;
48
+ }
49
+ }
50
+ }
51
+ return false;
52
+ };
53
+ RandexTypeParser.isSet = function (value) {
54
+ if (RandexTypeParser.isSetSingle(value)) {
55
+ return true;
56
+ }
57
+ else if (Array.isArray(value)) {
58
+ var sets = value.filter(function (x) { return RandexTypeParser.isSetSingle(x); });
59
+ return sets.length === value.length;
60
+ }
61
+ return false;
62
+ };
63
+ RandexTypeParser.isPartArray = function (value) {
64
+ if (Array.isArray(value) && RandexTypeParser.inRange(value.length, [2, 3])) {
65
+ if (value.length === 2) {
66
+ var set = value[0], rangeOrLength = value[1];
67
+ return RandexTypeParser.isSet(set) && (RandexTypeParser.isLength(rangeOrLength) || typeof rangeOrLength === "string");
68
+ }
69
+ else if (value.length === 3) {
70
+ var set = value[0], range = value[1], length_1 = value[2];
71
+ return RandexTypeParser.isSet(set) && typeof range === "string" && RandexTypeParser.isLength(length_1);
72
+ }
73
+ }
74
+ return false;
75
+ };
76
+ return RandexTypeParser;
77
+ }());
78
+ exports.RandexTypeParser = RandexTypeParser;
@@ -3,10 +3,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.randomEmail = void 0;
4
4
  var basic_1 = require("../basic");
5
5
  function randomEmail() {
6
- return ((0, basic_1.random)("alphabetLower", [["alphabetLower", "number"], 10], "alphabetLower") +
6
+ return ((0, basic_1.random)(["english", "l"], [[["english", "l"], "number"], 10], ["english", "l"]) +
7
7
  "@" +
8
- (0, basic_1.random)("alphabetLower", ["alphabetLower", "-", [0, 5]], "alphabetLower") +
8
+ (0, basic_1.random)(["english", "l"], [["english", "l"], "-", [0, 5]], ["english", "l"]) +
9
9
  "." +
10
- (0, basic_1.random)(["alphabetLower", [2, 4]]));
10
+ (0, basic_1.random)([
11
+ ["english", "l"],
12
+ [2, 4],
13
+ ]));
11
14
  }
12
15
  exports.randomEmail = randomEmail;
@@ -4,6 +4,14 @@ exports.randomFileName = void 0;
4
4
  var basic_1 = require("../basic");
5
5
  function randomFileName(options) {
6
6
  var extension = (options || {}).extension;
7
- return (0, basic_1.random)([["alphabet", "number"], "_.", [5, 10]]) + (0, basic_1.random)(["alphabet", "number"]) + "." + (extension ? extension : (0, basic_1.random)(["alphabetLower", [2, 5]]));
7
+ return ((0, basic_1.random)([["english", "number"], "_.", [5, 10]]) +
8
+ (0, basic_1.random)(["english", "number"]) +
9
+ "." +
10
+ (extension
11
+ ? extension
12
+ : (0, basic_1.random)([
13
+ ["english", "l"],
14
+ [2, 5],
15
+ ])));
8
16
  }
9
17
  exports.randomFileName = randomFileName;
@@ -1,6 +1,12 @@
1
- import { RandomLength } from "../interfaces";
2
- export interface RandomNameOptions {
3
- length: RandomLength;
1
+ import { RandexAlphabet, RandexLength } from "../interfaces";
2
+ export interface RandexNameOptions {
3
+ length?: RandexLength;
4
+ alphabet?: RandexAlphabet;
4
5
  }
5
- export declare function randomName(options?: RandomNameOptions): string;
6
- export declare function randomFullName(): string;
6
+ export interface RandexFullNameOptions {
7
+ firstLength?: RandexLength;
8
+ secondLength?: RandexLength;
9
+ alphabet?: RandexAlphabet;
10
+ }
11
+ export declare function randomName(options?: RandexNameOptions): string;
12
+ export declare function randomFullName(options?: RandexFullNameOptions): string;
@@ -23,12 +23,13 @@ function getLength(reservedChars, length, defaultLength) {
23
23
  return result;
24
24
  }
25
25
  function randomName(options) {
26
- var length = (options || {}).length;
26
+ var _a = options || {}, length = _a.length, _b = _a.alphabet, alphabet = _b === void 0 ? "english" : _b;
27
27
  var currentLength = getLength(1, length, [1, 9]);
28
- return (0, basic_1.random)("alphabetUpper", ["alphabetLower", currentLength]);
28
+ return (0, basic_1.random)([alphabet, "u"], [[alphabet, "l"], currentLength]);
29
29
  }
30
30
  exports.randomName = randomName;
31
- function randomFullName() {
32
- return randomName() + " " + randomName();
31
+ function randomFullName(options) {
32
+ var _a = options || {}, firstLength = _a.firstLength, secondLength = _a.secondLength, alphabet = _a.alphabet;
33
+ return randomName({ length: firstLength, alphabet: alphabet }) + " " + randomName({ length: secondLength, alphabet: alphabet });
33
34
  }
34
35
  exports.randomFullName = randomFullName;
@@ -3,8 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.randomUsername = void 0;
4
4
  var basic_1 = require("../basic");
5
5
  function randomUsername() {
6
- return (0, basic_1.random)("alphabetLower", [
7
- ["alphabetLower", "number"],
6
+ return (0, basic_1.random)(["english", "l"], [
7
+ [["english", "l"], "number"],
8
8
  [5, 10],
9
9
  ]);
10
10
  }
@@ -1,17 +1,21 @@
1
- export type RandomSingleSet = "hex" | "symbol" | "alphabet" | "alphabetLower" | "alphabetUpper" | "number" | "binary";
2
- export type RandomSet = RandomSingleSet | RandomSingleSet[];
3
- export type RandomLength = number | [number, number];
4
- interface RandomPartFullOptions {
5
- set?: RandomSet;
1
+ export type RandexCase = "default" | "lower" | "upper" | "l" | "u";
2
+ export type RandexAlphabet = "english" | "french" | "spanish" | "russian";
3
+ export type RandexSingleSet = RandexAlphabet | "hex" | "symbol" | "number" | "binary";
4
+ export type RandexAlphabetCase = [RandexAlphabet, RandexCase];
5
+ export type RandexItemSet = RandexSingleSet | RandexAlphabetCase;
6
+ export type RandexSet = RandexItemSet | RandexItemSet[];
7
+ export type RandexLength = number | [number, number];
8
+ interface RandexItemBasicOptions {
9
+ set?: RandexSet;
6
10
  range?: string;
7
- length?: RandomLength;
11
+ length?: RandexLength;
8
12
  }
9
- export interface RandomPartSetOptions extends Omit<RandomPartFullOptions, "set"> {
10
- set: RandomSet;
13
+ export interface RandexContentSetOptions extends Omit<RandexItemBasicOptions, "set"> {
14
+ set: RandexSet;
11
15
  }
12
- export interface RandomPartRangeOptions extends Omit<RandomPartFullOptions, "range"> {
16
+ export interface RandexContentRangeOptions extends Omit<RandexItemBasicOptions, "range"> {
13
17
  range: string;
14
18
  }
15
- export type RandomPartArrayOptions = [RandomSet, string | RandomLength] | [RandomSet, string, RandomLength];
16
- export type RandomPartOptions = RandomPartSetOptions | RandomPartRangeOptions | RandomPartArrayOptions | RandomSet;
19
+ export type RandexContentArrayOptions = [RandexSet, string | RandexLength] | [RandexSet, string, RandexLength];
20
+ export type RandexContentOptions = RandexContentSetOptions | RandexContentRangeOptions | RandexContentArrayOptions | RandexSet;
17
21
  export {};
@@ -5,8 +5,8 @@ var test_util_1 = require("./test-util");
5
5
  describe("randomFileName", function () {
6
6
  it("simple", function () {
7
7
  for (var i = 0; i < 100; i++) {
8
- var email = (0, filename_1.randomFileName)();
9
- expect(test_util_1.TestUtil.isFileNameValid(email)).toBeTruthy();
8
+ var value = (0, filename_1.randomFileName)();
9
+ expect(test_util_1.TestUtil.isFileNameValid(value)).toBeTruthy();
10
10
  }
11
11
  });
12
12
  });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,74 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var basic_1 = require("../basic");
4
+ var custom_1 = require("../custom");
5
+ describe("markdown", function () {
6
+ it("random", function () {
7
+ var value = (0, basic_1.random)({
8
+ set: "english",
9
+ length: 3,
10
+ });
11
+ expect(value.length).toBe(3);
12
+ value = (0, basic_1.random)({
13
+ set: ["english", "lower"],
14
+ length: 3,
15
+ });
16
+ expect(value.length).toBe(3);
17
+ value = (0, basic_1.random)({
18
+ set: "english",
19
+ length: [1, 3],
20
+ });
21
+ expect(value.length).toBeLessThanOrEqual(3);
22
+ value = (0, basic_1.random)({
23
+ set: "number",
24
+ length: 3,
25
+ });
26
+ expect(value.length).toBe(3);
27
+ value = (0, basic_1.random)({
28
+ range: "abc123",
29
+ length: 3,
30
+ });
31
+ expect(value.length).toBe(3);
32
+ value = (0, basic_1.random)({
33
+ set: ["english", "number"],
34
+ length: 3,
35
+ });
36
+ expect(value.length).toBe(3);
37
+ value = (0, basic_1.random)({
38
+ set: "english",
39
+ length: 2,
40
+ }, {
41
+ set: ["english", "number"],
42
+ length: 2,
43
+ });
44
+ expect(value.length).toBe(4);
45
+ });
46
+ it("randomFileName", function () {
47
+ var value = (0, custom_1.randomFileName)({
48
+ extension: "txt",
49
+ });
50
+ expect(value).toBeTruthy();
51
+ });
52
+ it("randomUsername", function () {
53
+ var value = (0, custom_1.randomUsername)();
54
+ expect(value).toBeTruthy();
55
+ });
56
+ it("randomEmail", function () {
57
+ var value = (0, custom_1.randomEmail)();
58
+ expect(value).toBeTruthy();
59
+ });
60
+ it("randomName", function () {
61
+ var value = (0, custom_1.randomName)();
62
+ expect(value).toBeTruthy();
63
+ value = (0, custom_1.randomName)({ alphabet: "french" });
64
+ expect(value).toBeTruthy();
65
+ value = (0, custom_1.randomName)({ length: 5 });
66
+ expect(value).toBeTruthy();
67
+ value = (0, custom_1.randomName)({ length: [2, 5] });
68
+ expect(value).toBeTruthy();
69
+ });
70
+ it("randomFullName", function () {
71
+ var value = (0, custom_1.randomFullName)();
72
+ expect(value).toBeTruthy();
73
+ });
74
+ });
@@ -4,21 +4,21 @@ var basic_1 = require("../basic");
4
4
  var test_util_1 = require("./test-util");
5
5
  describe("random params", function () {
6
6
  it("array 1", function () {
7
- var set = "alphabetLower";
7
+ var set = "english";
8
8
  var result1 = (0, basic_1.random)({ set: set });
9
9
  var result2 = (0, basic_1.random)(set);
10
10
  test_util_1.TestUtil.testInSet(result1, set);
11
11
  test_util_1.TestUtil.testInSet(result2, set);
12
12
  });
13
13
  it("single: array", function () {
14
- var set = ["number", "alphabetLower"];
14
+ var set = ["number", "english"];
15
15
  var result1 = (0, basic_1.random)({ set: set });
16
16
  var result2 = (0, basic_1.random)(set);
17
17
  test_util_1.TestUtil.testInSet(result1, set);
18
18
  test_util_1.TestUtil.testInSet(result2, set);
19
19
  });
20
20
  it("single: array, length", function () {
21
- var set = ["number", "alphabetLower"];
21
+ var set = ["number", "english"];
22
22
  var length = 3;
23
23
  var result1 = (0, basic_1.random)({ set: set, length: length });
24
24
  var result2 = (0, basic_1.random)([set, length]);
@@ -26,7 +26,7 @@ describe("random params", function () {
26
26
  test_util_1.TestUtil.testInSet(result2, set, length);
27
27
  });
28
28
  it("single: array, length as array", function () {
29
- var set = ["number", "alphabetLower"];
29
+ var set = ["number", "english"];
30
30
  var length = [1, 4];
31
31
  var result1 = (0, basic_1.random)({ set: set, length: length });
32
32
  var result2 = (0, basic_1.random)([set, length]);
@@ -42,7 +42,7 @@ describe("random params", function () {
42
42
  test_util_1.TestUtil.testInBoth(result2, set, range);
43
43
  });
44
44
  it("array 2 (length)", function () {
45
- var set = "alphabet";
45
+ var set = "english";
46
46
  var length = 3;
47
47
  var result1 = (0, basic_1.random)({ set: set, length: length });
48
48
  var result2 = (0, basic_1.random)([set, length]);
@@ -14,9 +14,9 @@ function inRange(range, length) {
14
14
  }
15
15
  describe("random", function () {
16
16
  it("set", function () {
17
- inSet("alphabet", 3);
18
- inSet("alphabetLower", 3);
19
- inSet("alphabetUpper", 3);
17
+ inSet("english", 3);
18
+ inSet(["english", "l"], 3);
19
+ inSet(["english", "u"], 3);
20
20
  inSet("hex", 3);
21
21
  inSet("symbol", 3);
22
22
  inSet("number", 3);
@@ -27,9 +27,31 @@ describe("random", function () {
27
27
  inRange("1234abc!@#", 3);
28
28
  });
29
29
  it("length: alphabet", function () {
30
- inSet("alphabet", 0);
31
- inSet("alphabet", [0, 0]);
32
- inSet("alphabet", [1, 3]);
33
- inSet("alphabet", [0, 1]);
30
+ inSet("english", 0);
31
+ inSet("english", [0, 0]);
32
+ inSet("english", [1, 3]);
33
+ inSet("english", [0, 1]);
34
+ });
35
+ it("alphabet: upper", function () {
36
+ for (var i = 0; i < 100; i++) {
37
+ var value = (0, basic_1.random)(["english", "u"]);
38
+ expect(test_util_1.TestUtil.isUpperCharValid(value, 1)).toBeTruthy();
39
+ value = (0, basic_1.random)([
40
+ ["english", "u"],
41
+ [1, 3],
42
+ ]);
43
+ expect(test_util_1.TestUtil.isUpperCharValid(value, [1, 3])).toBeTruthy();
44
+ }
45
+ });
46
+ it("alphabet: lower", function () {
47
+ for (var i = 0; i < 100; i++) {
48
+ var value = (0, basic_1.random)(["english", "l"]);
49
+ expect(test_util_1.TestUtil.isLowerCharValid(value, 1)).toBeTruthy();
50
+ value = (0, basic_1.random)([
51
+ ["english", "lower"],
52
+ [1, 3],
53
+ ]);
54
+ expect(test_util_1.TestUtil.isLowerCharValid(value, [1, 3])).toBeTruthy();
55
+ }
34
56
  });
35
57
  });
@@ -1,15 +1,17 @@
1
- import { RandomLength, RandomSet } from "../interfaces";
1
+ import { RandexLength, RandexSet } from "../interfaces";
2
2
  export declare class TestUtil {
3
- static testIn(value: string, length: RandomLength | undefined, action: () => void): void;
4
- static testInSet(value: string, set: RandomSet, length?: RandomLength): void;
5
- static testInRange(value: string, range: string, length?: RandomLength): void;
6
- static testInBoth(value: string, set: RandomSet, range: string, length?: RandomLength): void;
3
+ static testIn(value: string, length: RandexLength | undefined, action: () => void): void;
4
+ static testInSet(value: string, set: RandexSet, length?: RandexLength): void;
5
+ static testInRange(value: string, range: string, length?: RandexLength): void;
6
+ static testInBoth(value: string, set: RandexSet, range: string, length?: RandexLength): void;
7
7
  static inRange(value: string, range: string): boolean;
8
- static inSet(value: string, set: RandomSet): boolean;
9
- static inBoth(value: string, set: RandomSet, range: string): boolean;
8
+ static inSet(value: string, set: RandexSet): boolean;
9
+ static inBoth(value: string, set: RandexSet, range: string): boolean;
10
10
  static isEmailValid(email: string): RegExpMatchArray | null;
11
11
  static isUsernameValid(email: string): RegExpMatchArray | null;
12
12
  static isNameValid(email: string): RegExpMatchArray | null;
13
+ static isUpperCharValid(value: string, length?: RandexLength): RegExpMatchArray | null;
14
+ static isLowerCharValid(value: string, length?: RandexLength): RegExpMatchArray | null;
13
15
  static isFullNameValid(email: string): RegExpMatchArray | null;
14
16
  static isFileNameValid(email: string): RegExpMatchArray | null;
15
17
  static inNumberRange(value: number, range: [number, number]): boolean;
@@ -66,6 +66,16 @@ var TestUtil = /** @class */ (function () {
66
66
  TestUtil.isNameValid = function (email) {
67
67
  return String(email).match(/^[A-Z][a-z]{1,}$/);
68
68
  };
69
+ TestUtil.isUpperCharValid = function (value, length) {
70
+ if (length === void 0) { length = 1; }
71
+ var l = Array.isArray(length) ? "".concat(length[0], ",").concat(length[1]) : length;
72
+ return String(value).match(new RegExp("^[A-Z]{".concat(l, "}$")));
73
+ };
74
+ TestUtil.isLowerCharValid = function (value, length) {
75
+ if (length === void 0) { length = 1; }
76
+ var l = Array.isArray(length) ? "".concat(length[0], ",").concat(length[1]) : length;
77
+ return String(value).match(new RegExp("^[a-z]{".concat(l, "}$")));
78
+ };
69
79
  TestUtil.isFullNameValid = function (email) {
70
80
  return String(email).match(/^[A-Z][a-z]{1,}[ ][A-Z][a-z]{1,}$/);
71
81
  };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var type_1 = require("../basic/type");
4
+ function isLength(value, expectedResult) {
5
+ if (expectedResult === void 0) { expectedResult = true; }
6
+ var result = type_1.RandexTypeParser.isLength(value);
7
+ expect(expectedResult).toBe(result);
8
+ }
9
+ function isSetSingle(value, expectedResult) {
10
+ if (expectedResult === void 0) { expectedResult = true; }
11
+ var result = type_1.RandexTypeParser.isSetSingle(value);
12
+ expect(expectedResult).toBe(result);
13
+ }
14
+ function isSet(value, expectedResult) {
15
+ if (expectedResult === void 0) { expectedResult = true; }
16
+ var result = type_1.RandexTypeParser.isSet(value);
17
+ expect(expectedResult).toBe(result);
18
+ }
19
+ describe("type", function () {
20
+ it("isLength", function () {
21
+ // true
22
+ isLength(1);
23
+ isLength([1, 2]);
24
+ // false
25
+ isLength([1, 2, 3], false);
26
+ isLength("hi", false);
27
+ isLength(null, false);
28
+ });
29
+ it("isSetSingle", function () {
30
+ // true
31
+ isSetSingle("english");
32
+ isSetSingle("hex");
33
+ isSetSingle(["english", "lower"]);
34
+ // false
35
+ isSetSingle("bric", false);
36
+ isSetSingle("hexeron", false);
37
+ isSetSingle(["bric", "lower"], false);
38
+ isSetSingle(1, false);
39
+ isSetSingle(null, false);
40
+ isSetSingle(["lower", "english"], false);
41
+ });
42
+ it("RandomSet", function () {
43
+ // true single
44
+ isSet("english");
45
+ isSet("hex");
46
+ isSet(["english", "lower"]);
47
+ // true array
48
+ isSet(["english", "hex", ["english", "u"]]);
49
+ // false single
50
+ isSet("bric", false);
51
+ isSet("hexeron", false);
52
+ isSet(["bric", "lower"], false);
53
+ isSet(1, false);
54
+ isSet(null, false);
55
+ // false array
56
+ isSet(["hex", 2], false);
57
+ isSet(["english", "hex", ["u", "english"]], false);
58
+ });
59
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "randex",
3
- "version": "0.3.0",
3
+ "version": "0.5.0",
4
4
  "description": "",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",