randex 0.1.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.
Files changed (43) hide show
  1. package/.eslintignore +3 -0
  2. package/.eslintrc.js +3 -0
  3. package/.github/workflows/publish.yml +23 -0
  4. package/.vscode/settings.json +19 -0
  5. package/LICENSE +21 -0
  6. package/README.md +154 -0
  7. package/jest.config.js +13 -0
  8. package/jestconfig.json +7 -0
  9. package/lib/basic/index.d.ts +2 -0
  10. package/lib/basic/index.js +91 -0
  11. package/lib/basic/set.d.ts +6 -0
  12. package/lib/basic/set.js +39 -0
  13. package/lib/custom/email.d.ts +1 -0
  14. package/lib/custom/email.js +12 -0
  15. package/lib/custom/filename.d.ts +4 -0
  16. package/lib/custom/filename.js +9 -0
  17. package/lib/custom/index.d.ts +4 -0
  18. package/lib/custom/index.js +20 -0
  19. package/lib/custom/name.d.ts +2 -0
  20. package/lib/custom/name.js +12 -0
  21. package/lib/custom/username.d.ts +1 -0
  22. package/lib/custom/username.js +11 -0
  23. package/lib/index.d.ts +2 -0
  24. package/lib/index.js +18 -0
  25. package/lib/interfaces/index.d.ts +17 -0
  26. package/lib/interfaces/index.js +2 -0
  27. package/lib/test/email.test.d.ts +1 -0
  28. package/lib/test/email.test.js +12 -0
  29. package/lib/test/filename.test.d.ts +1 -0
  30. package/lib/test/filename.test.js +12 -0
  31. package/lib/test/fullname.test.d.ts +1 -0
  32. package/lib/test/fullname.test.js +12 -0
  33. package/lib/test/name.test.d.ts +1 -0
  34. package/lib/test/name.test.js +12 -0
  35. package/lib/test/params.test.d.ts +1 -0
  36. package/lib/test/params.test.js +61 -0
  37. package/lib/test/random.test.d.ts +1 -0
  38. package/lib/test/random.test.js +35 -0
  39. package/lib/test/test-util.d.ts +15 -0
  40. package/lib/test/test-util.js +79 -0
  41. package/lib/test/username.test.d.ts +1 -0
  42. package/lib/test/username.test.js +12 -0
  43. package/package.json +34 -0
package/.eslintignore ADDED
@@ -0,0 +1,3 @@
1
+ .*
2
+ /lib
3
+ node_modules
package/.eslintrc.js ADDED
@@ -0,0 +1,3 @@
1
+ module.exports = {
2
+ extends: ['varp-common']
3
+ };
@@ -0,0 +1,23 @@
1
+ name: NPM
2
+
3
+ on:
4
+ push:
5
+ branches: [master]
6
+
7
+ jobs:
8
+ publish:
9
+ runs-on: ubuntu-latest
10
+ steps:
11
+ - uses: actions/checkout@v2
12
+ # Setup .npmrc file to publish to npm
13
+ - uses: actions/setup-node@v2
14
+ with:
15
+ node-version: '18.x'
16
+ registry-url: 'https://registry.npmjs.org'
17
+ - run: npm install
18
+ - run: npm run lint
19
+ - run: npm run test
20
+ - run: npm run build
21
+ - run: npm publish
22
+ env:
23
+ NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
@@ -0,0 +1,19 @@
1
+ {
2
+ "editor.tabSize": 2,
3
+ "editor.defaultFormatter": "dbaeumer.vscode-eslint",
4
+ "eslint.format.enable": true,
5
+ "editor.codeActionsOnSave": {
6
+ "source.fixAll.eslint": "always"
7
+ },
8
+ "eslint.validate": [
9
+ "typescript",
10
+ "typescriptreact",
11
+ "javascript",
12
+ "javascriptreact",
13
+ "markdown",
14
+ "json"
15
+ ],
16
+ "cSpell.words": [
17
+ "tebot"
18
+ ],
19
+ }
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 varp
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,154 @@
1
+ # randex
2
+
3
+ <a href="https://www.npmjs.com/package/randex">
4
+ <img src="https://nodei.co/npm/randex.png?mini=true">
5
+ </a>
6
+
7
+ TypeScript library for random thing like string, username, email, name, etc.
8
+
9
+ ## Usage
10
+
11
+ Installation:
12
+
13
+ ```js
14
+ npm i randex
15
+ ```
16
+
17
+ - [`random` function](#random-function)
18
+ - [randomFileName](#randomfilename)
19
+ - [randomUsername](#randomfilename)
20
+ - [randomEmail](#randomfilename)
21
+ - [randomName](#randomfilename)
22
+ - [randomFullName](#randomfilename)
23
+
24
+
25
+ ## `random` function
26
+
27
+ Basic function for construct random thing.
28
+
29
+ ```ts
30
+ import { random } from 'randex';
31
+
32
+ random(
33
+ {
34
+ set: "alphabet",
35
+ length: 2,
36
+ },
37
+ {
38
+ set: ["alphabet", "number"],
39
+ length: 2,
40
+ }
41
+ );
42
+
43
+ // gTo3
44
+ ```
45
+
46
+ Options:
47
+ | Name | Type| Description|
48
+ | ------------------------ | ---- |------------ |
49
+ |set| `hex`, `symbol`, `alphabet`, `alphabetLower`, `alphabetUpper`, `number`, `binary` | Set of predefined chars|
50
+ |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
+
54
+
55
+ ## randomFileName
56
+
57
+ Randoms file name.
58
+
59
+ ```ts
60
+ import { randomFileName } from 'randex';
61
+
62
+ randomFileName(
63
+ {
64
+ extension: 'txt'
65
+ },
66
+ );
67
+
68
+ // FSCOuNN.txt
69
+ ```
70
+
71
+ Equals to:
72
+ ```ts
73
+ random([["alphabet", "number"], "_.", [5, 10]]) + random(["alphabet", "number"]) + "." + (extension ? extension : random(["alphabetLower", [2, 5]]));
74
+ ```
75
+
76
+ ## randomUsername
77
+
78
+ Randoms username.
79
+
80
+ ```ts
81
+ import { randomUsername } from 'randex';
82
+
83
+
84
+ randomUsername();
85
+
86
+ // icvv81d1j
87
+ ```
88
+
89
+ Equals to:
90
+ ```ts
91
+ random("alphabetLower", [
92
+ ["alphabetLower", "number"],
93
+ [5, 10],
94
+ ]);
95
+ ```
96
+
97
+ ## randomEmail
98
+
99
+ Randoms username.
100
+
101
+ ```ts
102
+ import { randomEmail } from 'randex';
103
+
104
+
105
+ randomEmail();
106
+
107
+ // dwrq4wu54nzj@dhvy.dtk
108
+ ```
109
+
110
+ Equals to:
111
+ ```ts
112
+ random("alphabetLower", [["alphabetLower", "number"], 10], "alphabetLower") +
113
+ "@" +
114
+ random("alphabetLower", ["alphabetLower", "-", [0, 5]], "alphabetLower") +
115
+ "." +
116
+ random(["alphabetLower", [2, 4]]);
117
+ ```
118
+
119
+ ## randomName
120
+
121
+ Randoms a name of the person, city, place, restaurant, ect.
122
+
123
+ ```ts
124
+ import { randomName } from 'randex';
125
+
126
+
127
+ randomName();
128
+
129
+ // Txotsaw
130
+ ```
131
+
132
+ Equals to:
133
+ ```ts
134
+ random("alphabetUpper", ["alphabetLower", [1, 10]]);
135
+ ```
136
+
137
+ ## randomFullName
138
+
139
+ Randoms a full name of the person.
140
+
141
+ ```ts
142
+ import { randomFullName } from 'randex';
143
+
144
+
145
+ randomFullName();
146
+
147
+ // Eqaa Bfmotnq
148
+ ```
149
+
150
+ Equals to:
151
+ ```ts
152
+ random("alphabetUpper", ["alphabetLower", [1, 10]]) + " " + random("alphabetUpper", ["alphabetLower", [1, 10]]);
153
+ ```
154
+
package/jest.config.js ADDED
@@ -0,0 +1,13 @@
1
+ module.exports = {
2
+ transform: {
3
+ "^.+\\.(t|j)sx?$": "ts-jest",
4
+ },
5
+ testRegex: "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
6
+ moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"],
7
+ globals: {
8
+ "ts-jest": {
9
+ // tsConfigFile: 'tsconfig.json',
10
+ diagnostics: false,
11
+ },
12
+ },
13
+ };
@@ -0,0 +1,7 @@
1
+ {
2
+ "transform": {
3
+ "^.+\\.(t|j)sx?$": "ts-jest"
4
+ },
5
+ "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
6
+ "moduleFileExtensions": ["ts", "tsx", "js", "jsx", "json", "node"]
7
+ }
@@ -0,0 +1,2 @@
1
+ import { RandomPartOptions } from "../interfaces";
2
+ export declare function random(...options: RandomPartOptions[]): string;
@@ -0,0 +1,91 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.random = void 0;
4
+ var set_1 = require("./set");
5
+ function randomInt(max) {
6
+ return Math.floor(Math.random() * max);
7
+ }
8
+ function randomString(params) {
9
+ var length = params.length, range = params.range;
10
+ var result = "";
11
+ var currentLength = 0;
12
+ if (typeof length === "number") {
13
+ currentLength = length;
14
+ }
15
+ else if (Array.isArray(length)) {
16
+ var min = length[0], max = length[1];
17
+ currentLength = min + randomInt(max - min);
18
+ }
19
+ for (var i = 0; i < currentLength; i++) {
20
+ var index = randomInt(range.length - 1);
21
+ result += range[index];
22
+ }
23
+ return result;
24
+ }
25
+ function ifLengthType(value) {
26
+ return typeof value === "number" || Array.isArray(value);
27
+ }
28
+ function toFullOption(options) {
29
+ if (typeof options === "string") {
30
+ return {
31
+ set: options,
32
+ length: 1,
33
+ };
34
+ }
35
+ else if (Array.isArray(options)) {
36
+ if (options.length === 2) {
37
+ 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;
48
+ }
49
+ if (options.length === 3 && ifLengthType(options[2])) {
50
+ return {
51
+ set: options[0],
52
+ range: options[1],
53
+ length: options[2],
54
+ };
55
+ }
56
+ return {
57
+ set: options,
58
+ length: 1,
59
+ };
60
+ }
61
+ return options;
62
+ }
63
+ function toParam(options) {
64
+ var fullRange = "";
65
+ var set = options.set, range = options.range, _a = options.length, length = _a === void 0 ? 1 : _a;
66
+ if (range) {
67
+ fullRange += range;
68
+ }
69
+ if (set) {
70
+ fullRange += Array.isArray(set) ? set.map(function (x) { return set_1.RtSetUtil.toRange(x); }).join("") : set_1.RtSetUtil.toRange(set);
71
+ }
72
+ return {
73
+ range: fullRange,
74
+ length: length,
75
+ };
76
+ }
77
+ function random() {
78
+ var options = [];
79
+ for (var _i = 0; _i < arguments.length; _i++) {
80
+ options[_i] = arguments[_i];
81
+ }
82
+ var result = "";
83
+ for (var _a = 0, options_1 = options; _a < options_1.length; _a++) {
84
+ var option = options_1[_a];
85
+ var o = toFullOption(option);
86
+ var p = toParam(o);
87
+ result += randomString(p);
88
+ }
89
+ return result;
90
+ }
91
+ exports.random = random;
@@ -0,0 +1,6 @@
1
+ import { RandomSet } from "../interfaces";
2
+ export declare const alphabet: string;
3
+ export declare class RtSetUtil {
4
+ private static toSingleRange;
5
+ static toRange(set: RandomSet): string;
6
+ }
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RtSetUtil = exports.alphabet = void 0;
4
+ var alphabetUpper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
5
+ var alphabetLower = "abcdefghijklmnopqrstuvwxyz";
6
+ var hex = "0123456789ABCDEFabcdef";
7
+ var symbol = '[-!$%^&*()_+|~=`{}[]:";<>?,./]' + "'";
8
+ exports.alphabet = alphabetLower + alphabetUpper;
9
+ var number = "0123456789";
10
+ var binary = "01";
11
+ var RtSetUtil = /** @class */ (function () {
12
+ function RtSetUtil() {
13
+ }
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;
22
+ case "hex":
23
+ return hex;
24
+ case "symbol":
25
+ return symbol;
26
+ case "number":
27
+ return number;
28
+ case "binary":
29
+ return binary;
30
+ default:
31
+ return "";
32
+ }
33
+ };
34
+ RtSetUtil.toRange = function (set) {
35
+ return Array.isArray(set) ? set.map(function (x) { return RtSetUtil.toSingleRange(x); }).join("") : RtSetUtil.toSingleRange(set);
36
+ };
37
+ return RtSetUtil;
38
+ }());
39
+ exports.RtSetUtil = RtSetUtil;
@@ -0,0 +1 @@
1
+ export declare function randomEmail(): string;
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.randomEmail = void 0;
4
+ var basic_1 = require("../basic");
5
+ function randomEmail() {
6
+ return ((0, basic_1.random)("alphabetLower", [["alphabetLower", "number"], 10], "alphabetLower") +
7
+ "@" +
8
+ (0, basic_1.random)("alphabetLower", ["alphabetLower", "-", [0, 5]], "alphabetLower") +
9
+ "." +
10
+ (0, basic_1.random)(["alphabetLower", [2, 4]]));
11
+ }
12
+ exports.randomEmail = randomEmail;
@@ -0,0 +1,4 @@
1
+ export interface RandomFileNameOptions {
2
+ extension?: string;
3
+ }
4
+ export declare function randomFileName(options?: RandomFileNameOptions): string;
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.randomFileName = void 0;
4
+ var basic_1 = require("../basic");
5
+ function randomFileName(options) {
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]]));
8
+ }
9
+ exports.randomFileName = randomFileName;
@@ -0,0 +1,4 @@
1
+ export * from "./email";
2
+ export * from "./filename";
3
+ export * from "./name";
4
+ export * from "./username";
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./email"), exports);
18
+ __exportStar(require("./filename"), exports);
19
+ __exportStar(require("./name"), exports);
20
+ __exportStar(require("./username"), exports);
@@ -0,0 +1,2 @@
1
+ export declare function randomName(): string;
2
+ export declare function randomFullName(): string;
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.randomFullName = exports.randomName = void 0;
4
+ var basic_1 = require("../basic");
5
+ function randomName() {
6
+ return (0, basic_1.random)("alphabetUpper", ["alphabetLower", [1, 10]]);
7
+ }
8
+ exports.randomName = randomName;
9
+ function randomFullName() {
10
+ return randomName() + " " + randomName();
11
+ }
12
+ exports.randomFullName = randomFullName;
@@ -0,0 +1 @@
1
+ export declare function randomUsername(): string;
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.randomUsername = void 0;
4
+ var basic_1 = require("../basic");
5
+ function randomUsername() {
6
+ return (0, basic_1.random)("alphabetLower", [
7
+ ["alphabetLower", "number"],
8
+ [5, 10],
9
+ ]);
10
+ }
11
+ exports.randomUsername = randomUsername;
package/lib/index.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from "./basic";
2
+ export * from "./custom";
package/lib/index.js ADDED
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./basic"), exports);
18
+ __exportStar(require("./custom"), exports);
@@ -0,0 +1,17 @@
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;
6
+ range?: string;
7
+ length?: RandomLength;
8
+ }
9
+ export interface RandomPartSetOptions extends Omit<RandomPartFullOptions, "set"> {
10
+ set: RandomSet;
11
+ }
12
+ export interface RandomPartRangeOptions extends Omit<RandomPartFullOptions, "range"> {
13
+ range: string;
14
+ }
15
+ export type RandomPartArrayOptions = [RandomSet, string | RandomLength] | [RandomSet, string, RandomLength];
16
+ export type RandomPartOptions = RandomPartSetOptions | RandomPartRangeOptions | RandomPartArrayOptions | RandomSet;
17
+ export {};
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var email_1 = require("../custom/email");
4
+ var test_util_1 = require("./test-util");
5
+ describe("randomEmail", function () {
6
+ it("simple", function () {
7
+ for (var i = 0; i < 100; i++) {
8
+ var value = (0, email_1.randomEmail)();
9
+ expect(test_util_1.TestUtil.isEmailValid(value)).toBeTruthy();
10
+ }
11
+ });
12
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var filename_1 = require("../custom/filename");
4
+ var test_util_1 = require("./test-util");
5
+ describe("randomFileName", function () {
6
+ it("simple", function () {
7
+ for (var i = 0; i < 100; i++) {
8
+ var email = (0, filename_1.randomFileName)();
9
+ expect(test_util_1.TestUtil.isFileNameValid(email)).toBeTruthy();
10
+ }
11
+ });
12
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var name_1 = require("../custom/name");
4
+ var test_util_1 = require("./test-util");
5
+ describe("randomFullName", function () {
6
+ it("simple", function () {
7
+ for (var i = 0; i < 100; i++) {
8
+ var value = (0, name_1.randomFullName)();
9
+ expect(test_util_1.TestUtil.isFullNameValid(value)).toBeTruthy();
10
+ }
11
+ });
12
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var name_1 = require("../custom/name");
4
+ var test_util_1 = require("./test-util");
5
+ describe("randomName", function () {
6
+ it("simple", function () {
7
+ for (var i = 0; i < 100; i++) {
8
+ var value = (0, name_1.randomName)();
9
+ expect(test_util_1.TestUtil.isNameValid(value)).toBeTruthy();
10
+ }
11
+ });
12
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var basic_1 = require("../basic");
4
+ var test_util_1 = require("./test-util");
5
+ describe("random params", function () {
6
+ it("array 1", function () {
7
+ var set = "alphabetLower";
8
+ var result1 = (0, basic_1.random)({ set: set });
9
+ var result2 = (0, basic_1.random)(set);
10
+ test_util_1.TestUtil.testInSet(result1, set);
11
+ test_util_1.TestUtil.testInSet(result2, set);
12
+ });
13
+ it("single: array", function () {
14
+ var set = ["number", "alphabetLower"];
15
+ var result1 = (0, basic_1.random)({ set: set });
16
+ var result2 = (0, basic_1.random)(set);
17
+ test_util_1.TestUtil.testInSet(result1, set);
18
+ test_util_1.TestUtil.testInSet(result2, set);
19
+ });
20
+ it("single: array, length", function () {
21
+ var set = ["number", "alphabetLower"];
22
+ var length = 3;
23
+ var result1 = (0, basic_1.random)({ set: set, length: length });
24
+ var result2 = (0, basic_1.random)([set, length]);
25
+ test_util_1.TestUtil.testInSet(result1, set, length);
26
+ test_util_1.TestUtil.testInSet(result2, set, length);
27
+ });
28
+ it("single: array, length as array", function () {
29
+ var set = ["number", "alphabetLower"];
30
+ var length = [1, 4];
31
+ var result1 = (0, basic_1.random)({ set: set, length: length });
32
+ var result2 = (0, basic_1.random)([set, length]);
33
+ test_util_1.TestUtil.testInSet(result1, set, length);
34
+ test_util_1.TestUtil.testInSet(result2, set, length);
35
+ });
36
+ it("array 2 (range)", function () {
37
+ var set = "number";
38
+ var range = "abcdefghijklmnopqrstuvwxyz";
39
+ var result1 = (0, basic_1.random)({ set: set, range: range });
40
+ var result2 = (0, basic_1.random)([set, range]);
41
+ test_util_1.TestUtil.testInBoth(result1, set, range);
42
+ test_util_1.TestUtil.testInBoth(result2, set, range);
43
+ });
44
+ it("array 2 (length)", function () {
45
+ var set = "alphabet";
46
+ var length = 3;
47
+ var result1 = (0, basic_1.random)({ set: set, length: length });
48
+ var result2 = (0, basic_1.random)([set, length]);
49
+ test_util_1.TestUtil.testInSet(result1, set, length);
50
+ test_util_1.TestUtil.testInSet(result2, set, length);
51
+ });
52
+ it("array 3", function () {
53
+ var set = "number";
54
+ var range = "abcdefghijklmnopqrstuvwxyz";
55
+ var length = 3;
56
+ var result1 = (0, basic_1.random)({ set: set, range: range, length: length });
57
+ var result2 = (0, basic_1.random)([set, range, length]);
58
+ test_util_1.TestUtil.testInBoth(result1, set, range, length);
59
+ test_util_1.TestUtil.testInBoth(result2, set, range, length);
60
+ });
61
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var basic_1 = require("../basic");
4
+ var test_util_1 = require("./test-util");
5
+ function inSet(set, length) {
6
+ if (length === void 0) { length = 1; }
7
+ var result = (0, basic_1.random)({ set: set, length: length });
8
+ test_util_1.TestUtil.testInSet(result, set, length);
9
+ }
10
+ function inRange(range, length) {
11
+ if (length === void 0) { length = 1; }
12
+ var result = (0, basic_1.random)({ range: range, length: length });
13
+ test_util_1.TestUtil.testInRange(result, range, length);
14
+ }
15
+ describe("random", function () {
16
+ it("set", function () {
17
+ inSet("alphabet", 3);
18
+ inSet("alphabetLower", 3);
19
+ inSet("alphabetUpper", 3);
20
+ inSet("hex", 3);
21
+ inSet("symbol", 3);
22
+ inSet("number", 3);
23
+ inSet("binary", 3);
24
+ });
25
+ it("range", function () {
26
+ inRange("abc", 3);
27
+ inRange("1234abc!@#", 3);
28
+ });
29
+ it("length: alphabet", function () {
30
+ inSet("alphabet", 0);
31
+ inSet("alphabet", [0, 0]);
32
+ inSet("alphabet", [1, 3]);
33
+ inSet("alphabet", [0, 1]);
34
+ });
35
+ });
@@ -0,0 +1,15 @@
1
+ import { RandomLength, RandomSet } from "../interfaces";
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;
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;
10
+ static isEmailValid(email: string): RegExpMatchArray | null;
11
+ static isUsernameValid(email: string): RegExpMatchArray | null;
12
+ static isNameValid(email: string): RegExpMatchArray | null;
13
+ static isFullNameValid(email: string): RegExpMatchArray | null;
14
+ static isFileNameValid(email: string): RegExpMatchArray | null;
15
+ }
@@ -0,0 +1,79 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TestUtil = void 0;
4
+ var set_1 = require("../basic/set");
5
+ var TestUtil = /** @class */ (function () {
6
+ function TestUtil() {
7
+ }
8
+ TestUtil.testIn = function (value, length, action) {
9
+ if (length === void 0) { length = 1; }
10
+ if (typeof length === "number") {
11
+ expect(value.length).toBe(length);
12
+ }
13
+ else if (Array.isArray(length)) {
14
+ var min = length[0], max = length[1];
15
+ expect(value.length).toBeGreaterThanOrEqual(min);
16
+ expect(value.length).toBeLessThanOrEqual(max);
17
+ }
18
+ if (value) {
19
+ action();
20
+ }
21
+ };
22
+ TestUtil.testInSet = function (value, set, length) {
23
+ if (length === void 0) { length = 1; }
24
+ TestUtil.testIn(value, length, function () {
25
+ expect(TestUtil.inSet(value, set)).toBeTruthy();
26
+ });
27
+ };
28
+ TestUtil.testInRange = function (value, range, length) {
29
+ if (length === void 0) { length = 1; }
30
+ TestUtil.testIn(value, length, function () {
31
+ expect(TestUtil.inRange(value, range)).toBeTruthy();
32
+ });
33
+ };
34
+ TestUtil.testInBoth = function (value, set, range, length) {
35
+ if (length === void 0) { length = 1; }
36
+ TestUtil.testIn(value, length, function () {
37
+ expect(TestUtil.inBoth(value, set, range)).toBeTruthy();
38
+ });
39
+ };
40
+ TestUtil.inRange = function (value, range) {
41
+ for (var i = 0; i < value.length; i++) {
42
+ if (!range.includes(value[i])) {
43
+ return false;
44
+ }
45
+ }
46
+ return value.length > 0;
47
+ };
48
+ TestUtil.inSet = function (value, set) {
49
+ var range = set_1.RtSetUtil.toRange(set);
50
+ return TestUtil.inRange(value, range);
51
+ };
52
+ TestUtil.inBoth = function (value, set, range) {
53
+ var fullRange = set_1.RtSetUtil.toRange(set) + range;
54
+ return TestUtil.inRange(value, fullRange);
55
+ };
56
+ TestUtil.isEmailValid = function (email) {
57
+ return String(email)
58
+ .toLowerCase()
59
+ .match(/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|.(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/);
60
+ };
61
+ TestUtil.isUsernameValid = function (email) {
62
+ return String(email)
63
+ .toLowerCase()
64
+ .match(/^[a-z][a-z0-9]{5,}$/);
65
+ };
66
+ TestUtil.isNameValid = function (email) {
67
+ return String(email).match(/^[A-Z][a-z]{1,}$/);
68
+ };
69
+ TestUtil.isFullNameValid = function (email) {
70
+ return String(email).match(/^[A-Z][a-z]{1,}[ ][A-Z][a-z]{1,}$/);
71
+ };
72
+ TestUtil.isFileNameValid = function (email) {
73
+ return String(email)
74
+ .toLowerCase()
75
+ .match(/^[a-z0-9_\.]{2,}$/);
76
+ };
77
+ return TestUtil;
78
+ }());
79
+ exports.TestUtil = TestUtil;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var username_1 = require("../custom/username");
4
+ var test_util_1 = require("./test-util");
5
+ describe("randomEmail", function () {
6
+ it("simple", function () {
7
+ for (var i = 0; i < 100; i++) {
8
+ var value = (0, username_1.randomUsername)();
9
+ expect(test_util_1.TestUtil.isUsernameValid(value)).toBeTruthy();
10
+ }
11
+ });
12
+ });
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "randex",
3
+ "version": "0.1.0",
4
+ "description": "",
5
+ "main": "lib/index.js",
6
+ "types": "lib/index.d.ts",
7
+ "scripts": {
8
+ "test": "jest --config jest.config.js",
9
+ "build": "tsc",
10
+ "lint": "eslint .",
11
+ "lint:fix": "eslint . --fix"
12
+ },
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "git+https://github.com/berezh/randex.git"
16
+ },
17
+ "author": "",
18
+ "license": "ISC",
19
+ "bugs": {},
20
+ "devDependencies": {
21
+ "@types/jest": "^24.9.1",
22
+ "eslint-config-varp-common": "^1.0.0",
23
+ "eslint-plugin-tsc": "^2.0.0",
24
+ "jest": "^24.9.0",
25
+ "ts-jest": "^24.1.0",
26
+ "typescript": "^4.2.4"
27
+ },
28
+ "dependencies": {
29
+ "reflect-metadata": "^0.2.1"
30
+ },
31
+ "keywords": [
32
+ "random","random filename", "random email", "random username", "random full name", "random string"
33
+ ]
34
+ }