From a6760407c4e7cbefd116957d49adfa8ddc062abf Mon Sep 17 00:00:00 2001 From: Awilum Date: Wed, 12 Jan 2022 22:04:54 +0300 Subject: [PATCH] String 2.1.0 --- .editorconfig | 13 ++++++------- .gitattributes | 1 + .gitignore | 1 + index.d.ts | 5 +++++ index.js | 12 +++++++----- index.test-d.ts | 7 +++++++ package.json | 12 ++++++++---- test.js | 14 +++++++++----- 8 files changed, 44 insertions(+), 21 deletions(-) create mode 100644 .gitattributes create mode 100644 index.d.ts create mode 100644 index.test-d.ts diff --git a/.editorconfig b/.editorconfig index e98f58d..ea8f4ab 100644 --- a/.editorconfig +++ b/.editorconfig @@ -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 diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..94f480d --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +* text=auto eol=lf \ No newline at end of file diff --git a/.gitignore b/.gitignore index e7ef1ee..bac2cd5 100755 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ # Global node_modules/ +coverage # OS Generated .DS_Store* diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..d1ff45e --- /dev/null +++ b/index.d.ts @@ -0,0 +1,5 @@ +interface Options { + length?: number; + keyspace?: string; +} +export default function string(options?: Options): string; diff --git a/index.js b/index.js index fa7e0e9..eb2d2a1 100644 --- a/index.js +++ b/index.js @@ -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(''); -}; \ No newline at end of file +} diff --git a/index.test-d.ts b/index.test-d.ts new file mode 100644 index 0000000..a1d5cee --- /dev/null +++ b/index.test-d.ts @@ -0,0 +1,7 @@ +import {expectType} from 'tsd'; +import string from './index.js'; + +expectType(string()); +expectType(string({length: -1})); +expectType(string({length: 10})); +expectType(string({length: 10, keyspace: '0123456789'})); diff --git a/package.json b/package.json index b80d4ad..6fa8ed5 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", diff --git a/test.js b/test.js index 54ec2bf..38e5fa3 100644 --- a/test.js +++ b/test.js @@ -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); -}); \ No newline at end of file + t.is(string({length: 10, keyspace: '0123456789'}).length, 10); +});