Skip to content

Commit

Permalink
String 2.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Awilum committed Jan 12, 2022
1 parent 266891c commit a676040
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 21 deletions.
13 changes: 6 additions & 7 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
# EditorConfig: http://EditorConfig.org
# EditorConfig is awesome: https://EditorConfig.org

# top-most EditorConfig file
root = true

# Unix-style newlines with a newline ending every file
[*]
charset = utf-8
end_of_line = lf
trim_trailing_whitespace = true
insert_final_newline = true

[*.{js,d.ts,ts}]
charset = utf-8
trim_trailing_whitespace = true
indent_style = space
indent_size = 4

# 2 space indentation
[*.yaml, *.yml]
[package.json,*.yaml]
indent_style = space
indent_size = 2
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto eol=lf
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Global
node_modules/
coverage

# OS Generated
.DS_Store*
Expand Down
5 changes: 5 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
interface Options {
length?: number;
keyspace?: string;
}
export default function string(options?: Options): string;
12 changes: 7 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
export default function string(options) {
options = options || {};
let length = options.length || 64;
let keyspace = options.keyspace || '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
let pieces = [];
let length = options.length === undefined ? 64 : options.length;
const keyspace = options.keyspace || '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
const pieces = [];
if (length < 0) {
length = 1;
}
for (var i = 0; i < length; i++) {

for (let i = 0; i < length; i++) {
pieces.push(keyspace.charAt(Math.floor(Math.random() * keyspace.length)));
}

return pieces.join('');
};
}
7 changes: 7 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {expectType} from 'tsd';
import string from './index.js';

expectType<string>(string());
expectType<string>(string({length: -1}));
expectType<string>(string({length: 10}));
expectType<string>(string({length: 10, keyspace: '0123456789'}));
12 changes: 8 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@fakerjs/string",
"version": "2.0.1",
"version": "2.1.0",
"description": "String package provides functionality to generate a fake string value.",
"license": "MIT",
"repository": "faker-javascript/string",
Expand All @@ -15,13 +15,17 @@
"node": ">=12"
},
"scripts": {
"test": "ava"
"test": "c8 ava; xo --space 4; tsd;"
},
"devDependencies": {
"ava": "^3.15.0"
"ava": "^4.0.0",
"c8": "^7.11.0",
"tsd": "^0.19.1",
"xo": "^0.47.0"
},
"files": [
"index.js"
"index.js",
"index.d.ts"
],
"keywords": [
"fakerjs",
Expand Down
14 changes: 9 additions & 5 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import string from './index.js';
import test from 'ava';
import string from './index.js';

test('string return type to be string', t => {
t.is(typeof string(), 'string');
t.is(typeof string(), 'string');
});

test('string length is 10', t => {
t.is(string({length: 10}).length, 10);
t.is(string({length: 10}).length, 10);
});

test('string length is -1', t => {
t.is(string({length: -1}).length, 1);
});

test('string length is 10 with keyspace 0123456789', t => {
t.is(string({length: 10, keyspace: '0123456789'}).length, 10);
});
t.is(string({length: 10, keyspace: '0123456789'}).length, 10);
});

0 comments on commit a676040

Please sign in to comment.