Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

randomstring

Package Overview
Dependencies
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

randomstring - npm Package Compare versions

Comparing version 1.0.8 to 1.1.0

lib/charset.js

5

CHANGELOG.md

@@ -0,1 +1,6 @@

1.1.0 / Sep 06, 2015
==================
* Added support for custom character sets
* Added option for excluding poorly readable characters
1.0.8 / Sep 01, 2015

@@ -2,0 +7,0 @@ ==================

47

lib/randomstring.js
"use strict";
var charsNumbers = '0123456789';
var charsLower = 'abcdefghijklmnopqrstuvwxyz';
var charsUpper = charsLower.toUpperCase();
var crypto = require('crypto');
var charset = require('./charset.js');
var crypto = require('crypto');
var chars = charsNumbers + charsLower + charsUpper;
exports.generate = function(length) {
length = length || 32;
var string = '';
while(string.length < length){
exports.generate = function(options) {
var length, chars, string = '';
// Handle options
if (typeof options === 'object') {
length = options.length || 32;
if (options.charset) {
chars = charset.generate(options.charset, options.readable);
}
else {
chars = charset.generate('alphanumeric', options.readable);
}
}
else if (typeof options === 'number') {
length = options;
chars = charset.generate('alphanumeric');
}
else {
length = 32;
chars = charset.generate('alphanumeric');
}
// Generate the string
while (string.length < length) {
var bf;
try {
bf = crypto.randomBytes(length);
} catch (e){
}
catch (e) {
continue;
}
for(var i = 0; i < bf.length; i++){
for (var i = 0; i < bf.length; i++) {
var index = bf.readUInt8(i) % chars.length;

@@ -26,0 +41,0 @@ string += chars.charAt(index);

{
"name": "randomstring",
"version": "1.0.8",
"version": "1.1.0",
"author": "Elias Klughammer <elias@klughammer.com> (http://www.klughammer.com)",

@@ -5,0 +5,0 @@ "description": "A module for generating random strings",

@@ -5,2 +5,4 @@ # node-randomstring

Library to help you create random strings.
## Installation

@@ -24,4 +26,29 @@

// >> "xqm5wXX"
randomstring.generate({
length: 12,
charset: 'alphabetic'
});
// >> "AqoTIzKurxJi"
randomstring.generate({
charset: 'abc'
});
// >> "accbaabbbbcccbccccaacacbbcbbcbbc"
```
## API
`randomstring.`
- `generate(options)`
- `length` - the length of the random string. (default: 32) [OPTIONAL]
- `readable` - exclude poorly readable chars: 0OIl. (default: false) [OPTIONAL]
- `charset` - define the character set for the string. (default: 'alphanumeric') [OPTIONAL]
- `alphanumeric` - [0-9 a-z A-Z]
- `alphabetic` - [a-z A-Z]
- `numeric` - [0-9]
- `hex` - [0-9 a-f]
- `custom` - any given characters
## Tests

@@ -28,0 +55,0 @@

"use strict";
var assert = require("assert")
var random = require("..").generate
var assert = require("assert");
var random = require("..").generate;
describe("randomstring.generate()", function() {
describe("randomstring.generate(options)", function() {

@@ -11,13 +11,47 @@ it("returns a string", function() {

assert.equal(typeof(rds), "string");
console.log(" String return: " + rds);
})
});
it("defaults to 32 characters in length", function() {
assert.equal(random().length, 32)
})
assert.equal(random().length, 32);
});
it("accepts length as an optional first argument", function() {
assert.equal(random(10).length, 10)
})
assert.equal(random(10).length, 10);
});
it("accepts length as an option param", function() {
assert.equal(random({ length: 7 }).length, 7);
});
it("accepts 'numeric' as charset option", function() {
var testData = random({ length: 24700, charset: 'numeric' });
var search = testData.search(/\D/ig);
assert.equal(search, -1);
});
it("accepts 'alphabetic' as charset option", function() {
var testData = random({ length: 24700, charset: 'alphabetic' });
var search = testData.search(/\d/ig);
assert.equal(search, -1);
});
it("accepts 'hex' as charset option", function() {
var testData = random({ length: 24700, charset: 'hex' });
var search = testData.search(/[^0-9a-f]/ig);
assert.equal(search, -1);
});
it("accepts custom charset", function() {
var charset = "abc";
var testData = random({ length: 24700, charset: charset });
var search = testData.search(/[^abc]/ig);
assert.equal(search, -1);
});
it("accepts readable option", function() {
var testData = random({ length: 24700, readable: true });
var search = testData.search(/[0OIl]/ig);
assert.equal(search, -1);
});
})
});

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc