Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@
*.log
node_modules
package-lock.json
test.js
test.js
.idea/
dist/
bun.lock
.claude/
5 changes: 4 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
.DS_Store
*.log
test.js
test.js
src/
tsconfig.json
tsup.config.ts
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ npm install randomstring
## Usage

```javascript
var randomstring = require("randomstring");
// CommonJS
const randomstring = require("randomstring");

// ES Modules
import randomstring from "randomstring";

randomstring.generate();
// >> "XwPp9xazJ0ku5CZnlmgAx2Dld8SHkAeT"
Expand Down Expand Up @@ -46,6 +50,8 @@ randomstring.generate({

```

TypeScript types are included out of the box — no need to install `@types` separately.

## API

`randomstring.`
Expand Down Expand Up @@ -81,10 +87,11 @@ randomstring.generate({
$ randomstring length=24 charset=github readable
> hthbtgiguihgbuttuutubugg

## Tests
## Development

```
npm install
npm run build
npm test
```

Expand Down
2 changes: 1 addition & 1 deletion bin/randomstring
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env node

var randomstring = require('..');
var randomstring = require('../dist/index.js');

var options = {};

Expand Down
1 change: 0 additions & 1 deletion index.js

This file was deleted.

71 changes: 0 additions & 71 deletions lib/charset.js

This file was deleted.

106 changes: 0 additions & 106 deletions lib/randomstring.js

This file was deleted.

33 changes: 28 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,49 @@
{
"name": "randomstring",
"version": "1.3.1",
"author": "Elias Klughammer <elias@klughammer.com> (http://www.klughammer.com)",
"version": "2.0.0",
"author": "Elias Klughammer <elias@klughammer.com> (http://www.klughammer.com), Nelie Taylor <nghi@huongda.com> (https://n96.dev)",
"description": "A module for generating random strings",
"homepage": "https://github.com/klughammer/node-randomstring",
"repository": {
"type": "git",
"url": "git://github.com/klughammer/node-randomstring.git"
},
"main": "./index",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"exports": {
".": {
"import": {
"types": "./dist/index.d.mts",
"default": "./dist/index.mjs"
},
"require": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
}
}
},
"engines": {
"node": "*"
},
"files": [
"dist",
"bin"
],
"dependencies": {
"randombytes": "2.1.0"
},
"devDependencies": {
"mocha": "^10.0.0"
"@types/node": "^25.2.3",
"mocha": "^11.7.5",
"tsup": "^8.4.0",
"typescript": "^5.7.0"
},
"license": "MIT",
"scripts": {
"test": "mocha"
"build": "tsup",
"test": "npm run build && mocha",
"prepublishOnly": "npm run build"
},
"bin": "bin/randomstring"
}
69 changes: 69 additions & 0 deletions src/charset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
export type CharsetType =
| 'alphanumeric'
| 'numeric'
| 'alphabetic'
| 'hex'
| 'binary'
| 'octal'
| (string & {});

export class Charset {
chars: string;

constructor() {
this.chars = '';
}

setType(type: CharsetType | CharsetType[]): void {
if (Array.isArray(type)) {
for (let i = 0; i < type.length; i++) {
this.chars += this.getCharacters(type[i]);
}
} else {
this.chars = this.getCharacters(type);
}
}

getCharacters(type: CharsetType): string {
const numbers = '0123456789';
const charsLower = 'abcdefghijklmnopqrstuvwxyz';
const charsUpper = charsLower.toUpperCase();
const hexChars = 'abcdef';
const binaryChars = '01';
const octalChars = '01234567';

if (type === 'alphanumeric') {
return numbers + charsLower + charsUpper;
} else if (type === 'numeric') {
return numbers;
} else if (type === 'alphabetic') {
return charsLower + charsUpper;
} else if (type === 'hex') {
return numbers + hexChars;
} else if (type === 'binary') {
return binaryChars;
} else if (type === 'octal') {
return octalChars;
} else {
return type;
}
}

removeUnreadable(): void {
const unreadableChars = /[0OIl]/g;
this.chars = this.chars.replace(unreadableChars, '');
}

setcapitalization(capitalization: string): void {
if (capitalization === 'uppercase') {
this.chars = this.chars.toUpperCase();
} else if (capitalization === 'lowercase') {
this.chars = this.chars.toLowerCase();
}
}

removeDuplicates(): void {
const charMap = this.chars.split('');
this.chars = [...new Set(charMap)].join('');
}
}
Loading