randex
v0.21.0
Published
Generates random filename, username, email, name, full name, etc for test purposes.
Downloads
557
Maintainers
Readme
randex
Generates random filename, username, email, name, full name, etc for test purposes.
Usage
Installation:
npm i randex
Strings:
Numbers:
Generic Type:
Deeps:
random
Basic function to construct random things.
import { random } from "randex";
// 3 english chars
random({
set: "english",
length: 3,
});
// or short:
random(["english", 3]);
// EFd
// 3 lower english chars
random({
set: ["english", "lower"],
length: 3,
});
// or short:
random([["english", "lower"], 3]);
// yhl
// min 1 and max 3 english chars
random({
set: "english",
length: [1, 3],
});
// or short:
random(["english", [1, 3]]);
// eR
// 3 number chars
random({
set: "number",
length: 3,
});
// or short:
random(["number", 3]);
// 643
// 3 chars from custom range
random({
range: "abc123",
length: 3,
});
// a21
// 3 english or number chars
random({
set: ["english", "number"],
length: 3,
});
// or short:
random([["english", "number"], 3]);
// x4d
// 4 chars: 2 english and 2 english or number
random(
{
set: "english",
length: 2,
},
{
set: ["english", "number"],
length: 2,
}
);
// or short:
random(["english", 2], [["english", "number"], 2]);
// Kb3b
Options:
| Name | Type| Description|
| ------------------------ | ---- |------------ |
|set|RandexSet | Defined chars|
|range|string
| Range or custom chars |
|length|RandexLength | Length of chars |
randomFileName
Randoms file name.
import { randomFileName } from "randex";
// default
randomFileName({
extension: "txt",
});
// td1TX31eOB.txt
// defined extension
randomFileName({
extension: "txt",
});
// or short:
randomFileName("txt");
// HkmOjqHC6.txt
// defined file name length and extension length
randomFileName({
fileNameLength: [7, 10],
extensionLength: 5,
});
// or short:
randomFileName([7, 10], 5);
// 1SJVkHSBjq.tejuw
// defined file name length and extension
randomFileName({
fileNameLength: 8,
extension: "xml",
});
// or short:
randomFileName(8, "xml");
// JyCsuN5kD.xml
Options:
| Name | Type| Description|
| ------------------------ | ---- |------------ |
|fileNameLength|RandexLength | Length of file name (not including extension). Default: [3,10] |
|extensionLength|RandexLength | Length of extension (not including file name) chars. Default: [2,5] |
|extension| string
| File extension. |
randomUsername
Randoms username.
import { randomUsername } from "randex";
// default
randomUsername();
// icvv81d1j
// with length 5 chars
randomUsername({ length: 5 });
// or short:
randomUsername(5);
// okmle
// with 2 min and 5 max chars
randomName({ length: [2, 5] });
// or short:
randomUsername([2, 5]);
// lkhs
Options: | Name | Type| Description| | ------------------------ | ---- |------------ | |length|RandexLength | Length of chars. Default: [6,10] |
randomEmail
Randoms an email.
import { randomEmail } from "randex";
// default
randomEmail();
// [email protected]
// defined prefix length
randomEmail({
prefixLength: 8,
});
// or shot:
randomEmail(8);
// [email protected]
// defined prefix, low domain and hight domain length
randomEmail({
prefixLength: 8,
lowDomainLength: 4,
hightDomainLength: 2,
});
// or shot:
randomEmail(8, 4, 2);
// [email protected]
// defined domain
randomEmail({
domain: "test.com",
});
// or shot:
randomEmail("test.com");
// [email protected]
Options:
| Name | Type| Description|
| ------------------------ | ---- |------------ |
|prefixLength|RandexLength | Length of email prefix (chars before @
). Default: [6, 10] |
|hightDomainLength|RandexLength | Length of hight domain part (example: test.com). Default: [1,6] |
|lowDomainLength|RandexLength | Length of low domain part (example: test.com). Default: [4,2] |
|domain| string
| Defined domain. |
randomName
Randoms a name of the person, city, place, restaurant, ect.
import { randomName } from "randex";
// default
randomName();
// Ijb
// with length 5 chars
randomName({ length: 5 });
// or short:
randomName(5);
// Okmpj
// with 2 min and 5 max chars
randomName({ length: [2, 5] });
// or short:
randomName([2, 5]);
// Wslg
// name from french alphabet
randomName({ alphabet: "french" });
// or short:
randomName("french");
// Dbïœ
// name from french alphabet with length
randomName({ alphabet: "french", length: 10 });
// or short:
randomName("french", 10);
// Rsîrjhjôôw
Options:
| Name | Type| Description|
| ------------------------ | ---- |------------ |
|length|RandexLength | Length of chars. Default: [2,10] |
|alphabet|RandexAlphabet | Defined alphabet. Default: english
|
randomFullName
Randoms a full name of the person.
import { randomFullName } from "randex";
// default
randomFullName();
// Eqaa Bfmotnq
Options:
| Name | Type| Description|
| ------------------------ | ---- |------------ |
|firstLength|RandexLength | First name length of chars. Default: [2, 10] |
|secondLength|RandexLength | Second name length of chars. Default: [2,10] |
|alphabet|RandexAlphabet | Defined alphabet. Default: english
|
randomNumber
Generates random number from a range of numbers.
import { randomNumber } from "randex";
// Generates a number from a range: [0, 3] (includes 0 and 3).
randomNumber(3);
// Generates a number from a specified range: [3, 7].
randomNumber([3, 7]);
Options:
| Name | Type| Description|
| ------------------------ | ---- |------------ |
|decimals| number
| Number of decimal digits |
randomNumberArray
Generates random array with values from a range.
import { randomNumberArray } from "randex";
// Generates an array (with length equals 2) with unique numbers from a range: [0, 3].
randomNumberArray(3, 2);
// Generates an array (with length equals 3) with unique numbers from a range: [3, 7].
randomNumber([3, 7], 3);
randomArray
Generates random array with values from defined array.
import { randomArray } from "randex";
// Generates an array (with length equals 2) with values picked from defined array as the first parameter.
randomArray([1, 2, 3, 4], 2);
// [2, 4]
randomArray(["1", "2", "3", "4"], 2);
// ["1", "3"]
Customization
random function is very flexible, there are many custom functions can be created.
There are examples how to create custom function for most common cases:
randomFileName
random([
["english", "number"],
[3, 10],
]) +
"." +
random([
["english", "l"],
[2, 5],
]);
randomUsername
random(
["english", "l"],
[
[["english", "l"], "number"],
[5, 10],
]
);
randomEmail
random([
[["english", "l"], "number"],
[6, 10],
]) +
"@" +
random([
["english", "l"],
[2, 4],
]) +
"." +
random([
["english", "l"],
[1, 6],
]);
randomName
random(
["english", "u"],
[
["english", "l"],
[1, 10],
]
);
randomFullName
random(
["english", "u"],
[
["english", "l"],
[1, 10],
]
) +
" " +
random(
["english", "u"],
[
["english", "l"],
[1, 10],
]
);
Types
RandexLength
Possible types:
number
: strict length
[number, number]
: an array of min and max length.
Example:
random({ set: "bit", length: 5 });
random({ set: "bit", length: [5, 10] });
// or short:
random(["bit", 5]);
random(["bit", [5, 10]]);
RandexSet
Possible types:
string
: an alphabet - RandexAlphabet or a set of chars - RandexKit
[string, string]
: the first item is - RandexAlphabet, the second item is - RandexCase
Example:
random({ set: "bit" });
random({ set: "spanish" });
random({ set: ["spanish", "lower"] });
// or short:
random("bit");
random("spanish");
random(["spanish", "l"]);
RandexAlphabet
An alphabet of chars
string
values: english
, french
, spanish
, russian
.
Example:
random({ set: "spanish" });
// or short:
random("spanish");
RandexKit
A kit of chars
string
values: hex
, symbol
, number
, binary
.
Example:
random({ set: "hex" });
// or short:
random("hex");
RandexCase
A case of alphabet
string
values: upper
, u
,lower
, l
.
Example:
random({ set: ["spanish", "lower"] });
// or short:
random(["spanish", "l"]);