-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
44 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* text=auto eol=lf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
# Global | ||
node_modules/ | ||
coverage | ||
|
||
# OS Generated | ||
.DS_Store* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(''); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |