Skip to content

Commit

Permalink
Added options.length and options.chunkSize
Browse files Browse the repository at this point in the history
  • Loading branch information
art-emini committed Jan 27, 2021
1 parent 641c884 commit a76e0f2
Show file tree
Hide file tree
Showing 763 changed files with 59 additions and 68,204 deletions.
37 changes: 30 additions & 7 deletions EasyToken.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@ String.prototype.insert = function(index, string) {
return string + this;
};

function chunkStr(str, size) {
const numChunks = Math.ceil(str.length / size);
const chunks = new Array(numChunks);

for (let i = 0, o = 0; i < numChunks; ++i, o += size) {
chunks[i] = str.substr(o, size);
};

return chunks;
};


/**
*
* @param {object} options An object that holds the options such as the "database" to write to, and the "characters". If none present it will generate a 16 character long (Letters and numbers) token not including hyphens. (OPTIONAL)
Expand All @@ -27,6 +39,8 @@ function createToken(options, callback) {
options.lifetime = options.lifetime || null;
options.base64Encode = options.base64Encode || false;
options.chunked = options.chunked || false;
options.chunkSize = options.chunkSize || null;
options.length = options.length || 16;

if(options.characters == "ABC") {
characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
Expand All @@ -39,16 +53,25 @@ function createToken(options, callback) {
};

callback = callback || function () {};
for(var i = 0; i < 16; i++ ) {
for(var i = 0; i < options.length; i++ ) {
result += characters.charAt(Math.floor(Math.random() * characters.length));
};


if (options.chunked == true) {
result = result.insert(4, "-");
result = result.insert(9, "-");
result = result.insert(14, "-");
}
// chunk
if (options.chunked == true && options.length % 2 == 0 && options.chunkSize >= 1) {
result = chunkStr(result, options.chunkSize);
// now an array
result.forEach(subStr => {
var index = result.indexOf(subStr);
var l = result.length;
var old = subStr;
if(index != result.length - 1) {
result[index] = old + "-";
};
});
result = result.toString();
result = result.replace(/,/g, "");
};


if(options.base64Encode == true) {
Expand Down
19 changes: 12 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,26 @@ const EasyToken = require("../EasyToken");

var options = {

// The file where the token will be written too. Starts from the root. (OPTIONAL)
// The file where the token will be written too. Starts from the root. Must b e a textfile. (OPTIONAL)
database: "test/test.txt",

// The characters that are allowed in the token. Put ABC for letters, 123 for numbers, or ABC123 for both.
characters: "ABC123",

// How long the token will last for in the database in milliseconds. Takes #, and "Infinite" (OPTIONAL, ONLY WORKS IF YOU LISTED A DATABASE)
// How long the token will last for in the database in milliseconds. Takes #, and "Infinite" (OPTIONAL, ONLY WORKS IF YOU LISTED A "DATABASE"(textfile))
lifetime: 5000,

// A boolean to determine if you want the result to be stored and return as a base64 encoded string.
base64Encode: true,
// A boolean to determine if you want the result to be stored and return as a base64 encoded string. (OPTIONAL)
base64Encode: false,

// A boolean to determine if you want the token to be broken up into 4 parts. (OPTIONAL)
chunked: true
// A boolean to determine if you want the token to be broken up into 4 parts, only works if length is an even number. (OPTIONAL)
chunked: true,

// A number to determine the size of the each chunk. Only works if chunked = true. (OPTIONAL)
chunkSize: 3,

// The length of your token, returns a 16 digit token if not defined. **Only works correctly if base64Encode is false** (OPTIONAL)
length: 12
};


Expand All @@ -45,5 +51,4 @@ EasyToken.createToken(options, (token) => {
console.log(EasyToken.createShort("ABC"));



```
15 changes: 0 additions & 15 deletions node_modules/.bin/is-ci

This file was deleted.

17 changes: 0 additions & 17 deletions node_modules/.bin/is-ci.cmd

This file was deleted.

18 changes: 0 additions & 18 deletions node_modules/.bin/is-ci.ps1

This file was deleted.

15 changes: 0 additions & 15 deletions node_modules/.bin/nodemon

This file was deleted.

17 changes: 0 additions & 17 deletions node_modules/.bin/nodemon.cmd

This file was deleted.

18 changes: 0 additions & 18 deletions node_modules/.bin/nodemon.ps1

This file was deleted.

15 changes: 0 additions & 15 deletions node_modules/.bin/nodetouch

This file was deleted.

17 changes: 0 additions & 17 deletions node_modules/.bin/nodetouch.cmd

This file was deleted.

18 changes: 0 additions & 18 deletions node_modules/.bin/nodetouch.ps1

This file was deleted.

15 changes: 0 additions & 15 deletions node_modules/.bin/nopt

This file was deleted.

17 changes: 0 additions & 17 deletions node_modules/.bin/nopt.cmd

This file was deleted.

18 changes: 0 additions & 18 deletions node_modules/.bin/nopt.ps1

This file was deleted.

15 changes: 0 additions & 15 deletions node_modules/.bin/rc

This file was deleted.

17 changes: 0 additions & 17 deletions node_modules/.bin/rc.cmd

This file was deleted.

18 changes: 0 additions & 18 deletions node_modules/.bin/rc.ps1

This file was deleted.

15 changes: 0 additions & 15 deletions node_modules/.bin/semver

This file was deleted.

17 changes: 0 additions & 17 deletions node_modules/.bin/semver.cmd

This file was deleted.

Loading

0 comments on commit a76e0f2

Please sign in to comment.