├── .gitignore ├── .jshintrc ├── .travis.yml ├── Gruntfile.js ├── LICENSE-MIT ├── README.md ├── lib └── random-string.js ├── package.json └── test └── random-string_test.js /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules/ 2 | -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "curly": true, 3 | "eqeqeq": true, 4 | "immed": true, 5 | "latedef": true, 6 | "newcap": true, 7 | "noarg": true, 8 | "sub": true, 9 | "undef": true, 10 | "unused": true, 11 | "boss": true, 12 | "eqnull": true, 13 | "node": true, 14 | "es5": true 15 | } 16 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "0.10" 4 | before_script: 5 | - npm install -g grunt-cli 6 | -------------------------------------------------------------------------------- /Gruntfile.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = function(grunt) { 4 | 5 | // Project configuration. 6 | grunt.initConfig({ 7 | nodeunit: { 8 | files: ['test/**/*_test.js'], 9 | }, 10 | jshint: { 11 | options: { 12 | jshintrc: '.jshintrc' 13 | }, 14 | gruntfile: { 15 | src: 'Gruntfile.js' 16 | }, 17 | lib: { 18 | src: ['lib/**/*.js'] 19 | }, 20 | test: { 21 | src: ['test/**/*.js'] 22 | }, 23 | }, 24 | watch: { 25 | gruntfile: { 26 | files: '<%= jshint.gruntfile.src %>', 27 | tasks: ['jshint:gruntfile'] 28 | }, 29 | lib: { 30 | files: '<%= jshint.lib.src %>', 31 | tasks: ['jshint:lib', 'nodeunit'] 32 | }, 33 | test: { 34 | files: '<%= jshint.test.src %>', 35 | tasks: ['jshint:test', 'nodeunit'] 36 | }, 37 | }, 38 | }); 39 | 40 | // These plugins provide necessary tasks. 41 | grunt.loadNpmTasks('grunt-contrib-nodeunit'); 42 | grunt.loadNpmTasks('grunt-contrib-jshint'); 43 | grunt.loadNpmTasks('grunt-contrib-watch'); 44 | 45 | // Default task. 46 | grunt.registerTask('default', ['jshint', 'nodeunit']); 47 | 48 | }; 49 | -------------------------------------------------------------------------------- /LICENSE-MIT: -------------------------------------------------------------------------------- 1 | Copyright (c) 2013 Valiton GmbH, Bastian 'hereandnow' Behrens 2 | 3 | Permission is hereby granted, free of charge, to any person 4 | obtaining a copy of this software and associated documentation 5 | files (the "Software"), to deal in the Software without 6 | restriction, including without limitation the rights to use, 7 | copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | copies of the Software, and to permit persons to whom the 9 | Software is furnished to do so, subject to the following 10 | conditions: 11 | 12 | The above copyright notice and this permission notice shall be 13 | included in all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 17 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 19 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 20 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # random-string 2 | 3 | Simple Module for generating Random Strings 4 | 5 | ![random-string](https://api.travis-ci.org/valiton/node-random-string.png "random-string") 6 | 7 | ## Getting Started 8 | Install the module with: `npm install random-string` 9 | 10 | ```javascript 11 | var randomString = require('random-string'); 12 | var x = randomString(); // x contains now a random String with the length of 8 13 | ``` 14 | 15 | ## Documentation 16 | 17 | You can call the randomString-Method with additional options for specifing how long your resulting string should be and which characters to include 18 | 19 | ```javascript 20 | // e.g. you want a string with a length of 20 21 | var x = randomString({length: 20}); 22 | ``` 23 | 24 | ### options 25 | 26 | #### options.length 27 | 28 | number - the length of your resulting string (DEFAULT: 8) 29 | 30 | #### options.numeric 31 | 32 | boolean - should your resulting string contain numbers (from 0-9) (DEFAULT: true) 33 | 34 | #### options.letters 35 | 36 | boolean - should your resulting string contain letters (from a-z, lower and uppercase) (DEFAULT: true) 37 | 38 | #### options.special 39 | 40 | boolean - should your resulting string contain any of these special characters (!$%^&*()_+|~-=\`{}[]:;<>?,./) (DEFAULT: false) 41 | 42 | #### options.exclude 43 | 44 | array - removes characters from resulting string 45 | *Note: Lowercase letters will not remove uppercase letters* 46 | 47 | 48 | ## Examples 49 | 50 | ```javascript 51 | // that would be a call with all options (hint: thats a call with all defaults, und the options wouldnt be necessary in that case!) 52 | var x = randomString({ 53 | length: 8, 54 | numeric: true, 55 | letters: true, 56 | special: false, 57 | exclude: ['a', 'b', '1'] 58 | }); 59 | ``` 60 | 61 | ## Contributing 62 | In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using [Grunt](http://gruntjs.com/). 63 | 64 | 65 | ## Release History 66 | 67 | - 0.2.0 introduce `exclude`-option 68 | 69 | - 0.1.2 fix characterset ([#2](https://github.com/valiton/node-random-string/issues/2)) 70 | 71 | - 0.1.1 do not contain special cars per default 72 | 73 | - 0.1.0 Initial Release 74 | 75 | 76 | ## Contributors 77 | 78 | - Bastian "hereandnow" Behrens 79 | - Baran "bjskistad" Skistad 80 | 81 | 82 | ## License 83 | Copyright (c) 2013 Valiton GmbH 84 | Licensed under the MIT license. 85 | -------------------------------------------------------------------------------- /lib/random-string.js: -------------------------------------------------------------------------------- 1 | /* 2 | * random-string 3 | * https://github.com/valiton/node-random-string 4 | * 5 | * Copyright (c) 2013 Valiton GmbH, Bastian 'hereandnow' Behrens 6 | * Licensed under the MIT license. 7 | */ 8 | 9 | 'use strict'; 10 | 11 | var numbers = '0123456789', 12 | letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz', 13 | specials = '!$%^&*()_+|~-=`{}[]:;<>?,./'; 14 | 15 | 16 | function _defaults (opts) { 17 | opts || (opts = {}); 18 | return { 19 | length: opts.length || 8, 20 | numeric: typeof opts.numeric === 'boolean' ? opts.numeric : true, 21 | letters: typeof opts.letters === 'boolean' ? opts.letters : true, 22 | special: typeof opts.special === 'boolean' ? opts.special : false, 23 | exclude: Array.isArray(opts.exclude) ? opts.exclude : [] 24 | }; 25 | } 26 | 27 | function _buildChars (opts) { 28 | var chars = ''; 29 | if (opts.numeric) { chars += numbers; } 30 | if (opts.letters) { chars += letters; } 31 | if (opts.special) { chars += specials; } 32 | for (var i = 0; i <= opts.exclude.length; i++){ 33 | chars = chars.replace(opts.exclude[i], ""); 34 | } 35 | return chars; 36 | } 37 | 38 | module.exports = function randomString(opts) { 39 | opts = _defaults(opts); 40 | var i, rn, 41 | rnd = '', 42 | len = opts.length, 43 | exclude = opts.exclude, 44 | randomChars = _buildChars(opts); 45 | for (i = 1; i <= len; i++) { 46 | rnd += randomChars.substring(rn = Math.floor(Math.random() * randomChars.length), rn + 1); 47 | } 48 | return rnd; 49 | }; 50 | 51 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "random-string", 3 | "description": "Simple Module for generating Random Strings", 4 | "version": "0.2.0", 5 | "homepage": "https://github.com/valiton/node-random-string", 6 | "author": { 7 | "name": "Valiton GmbH, Bastian 'hereandnow' Behrens", 8 | "email": "bastian.behrens@valiton.com" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "git://github.com/valiton/node-random-string.git" 13 | }, 14 | "bugs": { 15 | "url": "https://github.com/valiton/node-random-string/issues" 16 | }, 17 | "licenses": [ 18 | { 19 | "type": "MIT", 20 | "url": "https://github.com/valiton/node-random-string/blob/master/LICENSE-MIT" 21 | } 22 | ], 23 | "main": "lib/random-string", 24 | "engines": { 25 | "node": ">= 0.10.0" 26 | }, 27 | "scripts": { 28 | "test": "grunt nodeunit" 29 | }, 30 | "devDependencies": { 31 | "grunt-contrib-jshint": "~0.4.3", 32 | "grunt-contrib-nodeunit": "~0.1.2", 33 | "grunt-contrib-watch": "~0.4.3", 34 | "grunt": "~0.4.1" 35 | }, 36 | "keywords": [ 37 | "random", 38 | "string" 39 | ] 40 | } 41 | -------------------------------------------------------------------------------- /test/random-string_test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var randomString = require('../lib/random-string.js'); 4 | 5 | exports['randomString'] = { 6 | 7 | 'default_length': function(test) { 8 | test.expect(1); 9 | test.equal(randomString().length, 8, 'the random string should be excactly 8 characters long'); 10 | test.done(); 11 | }, 12 | 13 | 'change_length': function(test) { 14 | test.expect(1); 15 | test.equal(randomString({length: 5}).length, 5, 'the random string should be excactly 5 characters long now'); 16 | test.done(); 17 | }, 18 | 19 | 'include_only_numbers': function(test) { 20 | test.expect(3); 21 | var result = randomString({ 22 | numeric: true, 23 | letters: false, 24 | special: false 25 | }); 26 | test.ok(/^\d+$/.test(result), 'the random string should include only numbers'); 27 | test.equal(/^[a-zA-Z]+$/.test(result), false, 'the random string should not include letters'); 28 | test.equal(/^[-!$%^&*()_+|~=`{}\[\]:";'<>?,.\/]+$/.test(result), false, 'the random string should not include specials'); 29 | test.done(); 30 | }, 31 | 32 | 'include_only_letters': function(test) { 33 | test.expect(3); 34 | var result = randomString({ 35 | numeric: false, 36 | letters: true, 37 | special: false 38 | }); 39 | test.equal(/^\d+$/.test(result), false, 'the random string should not include numbers'); 40 | test.ok(/^[a-zA-Z]+$/.test(result), 'the random string should include only letters'); 41 | test.equal(/^[-!$%^&*()_+|~=`{}\[\]:";'<>?,.\/]+$/.test(result), false, 'the random string should not include specials'); 42 | test.done(); 43 | }, 44 | 45 | 'include_only_specials': function(test) { 46 | test.expect(3); 47 | var result = randomString({ 48 | numeric: false, 49 | letters: false, 50 | special: true 51 | }); 52 | test.equal(/^\d+$/.test(result), false, 'the random string should not include numbers'); 53 | test.equal(/^[a-zA-Z]+$/.test(result), false, 'the random string should not include letters'); 54 | test.ok(/^[-!$%^&*()_+|~=`{}\[\]:";'<>?,.\/]+$/.test(result), 'the random string should include only specials'); 55 | test.done(); 56 | } 57 | 58 | }; 59 | --------------------------------------------------------------------------------