Skip to content

Commit beeea72

Browse files
Added readme and updated webpack configuration to build correctly. Moved web and node files to Web and Node directories. Added a few comments.
1 parent de83575 commit beeea72

11 files changed

+180
-70
lines changed

.eslintrc.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"parser": "babel-eslint",
3+
"extends": "@jitesoft"
4+
}

README.md

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# @jitesoft/random-string
2+
3+
Random string generator with a shared api for both node and web.
4+
5+
Usage:
6+
7+
```javascript
8+
import random from '@jitesoft/random-string';
9+
const randomString = random(128, { minSpecial: 5 });
10+
console.log(randomString);
11+
// random string with a minimum of 5 special characters and a full length of 128 characters.
12+
```
13+
14+
If you wish to access the specific files by absolute paths, they are located at `@jitesoft/random-string/dist/index.node.js` and `@jitesoft/random-string/dist/index.web.js`.
15+
16+
## Information
17+
18+
### Randomness
19+
20+
The random generator for the node version uses the `crypto` library to generate random characters while the web version
21+
uses the web `crypto` api. If web crypto api is not available, it will fallback to using the `Math.random` function.
22+
The `Math.random` function is NOT a cryptographically secure algorithm, the randomness should be good enough, but do not
23+
use the fallback in cases where high security is a must.
24+
25+
### Characters
26+
27+
The following characters are used (char codes):
28+
29+
Alpha: `65-90` & `97-122`
30+
Numbers: `48-57`
31+
Special: `33-47` & `58-64` & `91-96` & `123-126`
32+
33+
## Options
34+
35+
The following options (with defaults shown) can be passed as an object as the second argument of the function:
36+
37+
```json
38+
{
39+
"special": true,
40+
"numbers": true,
41+
"alpha": true,
42+
"minSpecial": 0,
43+
"minNumbers": 0,
44+
"minAlpha": 0
45+
}
46+
```
47+
48+
This allows for a bit more specific generation of strings, default values are used for anything not set when parsing the options.
49+
50+
## Source
51+
52+
The source code is kept intact under the `src` directory in the package. This is so that you may use the code directly if you wish to convert
53+
it with your own bundler or converter.

package-lock.json

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+10-11
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
{
2-
"name": "@jitesoft/rand",
2+
"name": "@jitesoft/random-string",
33
"version": "1.0.0",
4-
"description": "Random string generator.",
4+
"description": "Cross environment random string generator.",
55
"main": "dist/index.js",
6-
"module": "dist/index.mjs",
7-
"browser": "dist/index-web.js",
6+
"browser": "dist/index.web.js",
87
"scripts": {
9-
"test": "eslint src/ && jest"
8+
"test": "eslint src/ && jest",
9+
"build:prod": "cross-env NODE_ENV=production webpack --config webpack.config.js"
1010
},
1111
"keywords": [
12-
"random"
12+
"random",
13+
"cross-env",
14+
"web",
15+
"node",
16+
"random string"
1317
],
1418
"author": "Johannes Tegnér <johannes@jitesoft.com>",
1519
"license": "MIT",
16-
"dependencies": {},
1720
"devDependencies": {
1821
"@babel/core": "^7.8.4",
1922
"@jitesoft/babel-preset-main": "^2.1.3",
@@ -27,9 +30,5 @@
2730
"jest": "^25.1.0",
2831
"webpack": "^5.0.0-beta.13",
2932
"webpack-cli": "^4.0.0-beta.3"
30-
},
31-
"eslintConfig": {
32-
"parser": "babel-eslint",
33-
"extends": "@jitesoft"
3433
}
3534
}

src/Generator.js

-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ export default class Generator {
2727
return this.getRandom(len).map(i => (Math.round(i % 10) + 48));
2828
}
2929

30-
3130
/**
3231
* Fetch a list of random char codes of special characters.
3332
*

src/Node/index.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import NodeGenerator from '../NodeGenerator';
2+
import rand from '../rand';
3+
4+
const generator = new NodeGenerator();
5+
const random = rand(generator);
6+
7+
export default random;

src/Web/index.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import WebGenerator from '../WebGenerator';
2+
import rand from '../rand';
3+
4+
const generator = new WebGenerator();
5+
const random = rand(generator);
6+
7+
export default random;
8+
export {
9+
random,
10+
rand
11+
};

src/node.js

-8
This file was deleted.

src/rand.js

+65-35
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,71 @@
1-
import { shuffle } from './Util'
1+
import { shuffle } from './Util';
22

3-
export default (generator) => (length, options = {
3+
/**
4+
* @typedef Option
5+
* @type {{special: boolean, minAlpha: number, alpha: boolean, numbers: boolean, minNumbers: number, minSpecial: number}}
6+
*/
7+
8+
const defaultOptions = {
49
alpha: true,
510
numbers: true,
611
special: true,
7-
minAlpha: 1,
8-
minNumbers: 1,
9-
minSpecial: 1,
10-
}) => {
11-
12-
const lists = {
13-
alpha: (options.alpha ? generator.alpha(length) : []),
14-
special: (options.special ? generator.special(length) : []),
15-
numbers: (options.numbers ? generator.numbers(length) : []),
16-
combined: []
17-
};
12+
minAlpha: 0,
13+
minNumbers: 0,
14+
minSpecial: 0
15+
};
16+
17+
/**
18+
* Factory method for the random string generator.
19+
* @param generator
20+
* @return {function(*=, *=): string}
21+
*/
22+
export default (generator) =>
23+
/**
24+
* Random string generator.
25+
*
26+
* @param {number} [length] Length of the string to generate.
27+
* @param {Option} [options] Options object to specify character types and minimums.
28+
* @return {string} Generated random string.
29+
*/
30+
(length = 32, options = {}) => {
31+
options = Object.assign({}, defaultOptions, options);
32+
33+
if (options.minNumbers + options.minAlpha + options.minSpecial > length) {
34+
throw new Error(
35+
'You have specified a lower length than possible with the minimum count of specific character types:\n' +
36+
`\tLength ${length}.\n` +
37+
`\tMin Alpha: ${options.minAlpha}\n` +
38+
`\tMin Numeric: ${options.minNumbers}\n` +
39+
`\tMin Special: ${options.minSpecial}`
40+
);
41+
}
42+
43+
const lists = {
44+
alpha: Array.from(options.alpha ? generator.alpha(length) : []),
45+
special: Array.from(options.special ? generator.special(length) : []),
46+
numbers: Array.from(options.numbers ? generator.numbers(length) : []),
47+
combined: []
48+
};
1849

19-
const result = [];
20-
result.push(...lists.alpha.splice(0, options.minAlpha));
21-
result.push(...lists.special.splice(0, options.minSpecial));
22-
result.push(...lists.numbers.splice(0, options.numbers));
23-
24-
lists.combined = shuffle([].concat(
25-
...lists.alpha,
26-
...lists.special,
27-
...lists.numbers)
28-
);
29-
30-
// Concat rest.
31-
result.push(lists.combined.splice(0, length - (
32-
(options.special ? options.minSpecial : 0)
33-
+
34-
(options.alpha ? options.minAlpha : 0)
35-
+
36-
(options.numbers ? options.minNumbers : 0)
37-
)));
38-
39-
return shuffle(result);
40-
}
50+
const result = [];
51+
result.push(...(options.alpha ? lists.alpha.splice(0, options.minAlpha) : []));
52+
result.push(...(options.numbers ? lists.numbers.splice(0, options.minNumbers) : []));
53+
result.push(...(options.special ? lists.special.splice(0, options.minSpecial) : []));
4154

55+
lists.combined.push(
56+
...lists.alpha,
57+
...lists.numbers,
58+
...lists.special
59+
);
60+
61+
lists.combined = shuffle(lists.combined);
62+
63+
// Concat rest.
64+
result.push(...lists.combined.splice(0, length - (
65+
(options.special ? options.minSpecial : 0) +
66+
(options.alpha ? options.minAlpha : 0) +
67+
(options.numbers ? options.minNumbers : 0)
68+
)));
69+
70+
return String.fromCharCode(...shuffle(result));
71+
};

src/web.js

Whitespace-only changes.

webpack.config.js

+26-11
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
const Path = require('path');
22

3-
module.exports = {
3+
const shared = {
44
mode: process.env.NODE_ENV,
55
optimization: {
6-
minimize: process.env.NODE_ENV === 'production'
6+
minimize: process.env.NODE_ENV === 'production',
77
},
88
module: {
99
rules: [
@@ -13,16 +13,31 @@ module.exports = {
1313
loader: 'babel-loader'
1414
}
1515
]
16-
},
17-
entry: {
18-
index: [
19-
Path.join(__dirname, 'src', 'index.js')
20-
]
21-
},
16+
}
17+
};
18+
19+
const web = Object.assign({}, shared, {
20+
target: 'web',
21+
entry: Path.join(__dirname, 'src', 'Web', 'index.js'),
2222
output: {
23-
filename: 'index.js',
24-
library: '@jitesoft/rand',
23+
filename: 'index.web.js',
24+
library: '@jitesoft/random-string',
2525
libraryTarget: 'umd',
2626
globalObject: 'this'
2727
}
28-
};
28+
});
29+
30+
const node = Object.assign({}, shared, {
31+
target: 'node',
32+
entry: Path.join(__dirname, 'src', 'Node', 'index.js'),
33+
output: {
34+
filename: 'index.node.js',
35+
library: '@jitesoft/random-string',
36+
libraryTarget: 'umd'
37+
}
38+
});
39+
40+
module.exports = [
41+
web,
42+
node
43+
];

0 commit comments

Comments
 (0)