Skip to content

Commit

Permalink
[mixte] 修改 randomLetter 工具方法逻辑, 当传参不是布尔值时, 随机一个小写或大写英文字母
Browse files Browse the repository at this point in the history
之前是随机一个小写字母
  • Loading branch information
Zhang-Wei-666 committed Aug 4, 2023
1 parent a1a2467 commit 7d880e8
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## [Unreleased]
- 🌟 [mixte] 新增 `isBoolean` 工具方法
- 💄 [mixte] 优化 `random`, `randomString` 工具方法类型定义
- ⚠️ [mixte] 修改 `randomLetter` 工具方法逻辑, 当传参不是布尔值时, 随机一个小写或大写英文字母 ( 之前是随机一个小写字母 )

## [v0.0.1-beta.13]
- 📅 2023-07-22
Expand Down
47 changes: 43 additions & 4 deletions packages/mixte/src/random/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { random, randomBoolean, randomLetter, randomLowercaseLetter, randomNatural, randomString, randomUppercaseLetter } from 'mixte';
import { isBoolean, random, randomBoolean, randomLetter, randomLowercaseLetter, randomNatural, randomString, randomUppercaseLetter } from 'mixte';
import { types } from '../is/testTypes';

describe('randomNatural', () => {
test('在传入的两个自然数之间随机生成一个自然数', () => {
Expand Down Expand Up @@ -122,15 +123,20 @@ describe('randomUppercaseLetter', () => {
});

describe('randomLetter', () => {
test('默认随机一个小写英文字母', () => {
test('默认随机一个小写或大写英文字母', () => {
const letters = new Set<string>();

for (let i = 0; i < 2600; i++)
for (let i = 0; i < 5200; i++)
letters.add(randomLetter());

expect(
Array.from(letters).sort(),
).toEqual(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']);
).toEqual(
[
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
].sort(),
);
});

test('传入 true, 则随机一个大写英文字母', () => {
Expand All @@ -154,6 +160,39 @@ describe('randomLetter', () => {
Array.from(letters).sort(),
).toEqual(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']);
});

test('传入非布尔值, 则随机一个小写或大写英文字母', () => {
Object.values(types).forEach((values) => {
values.forEach((value) => {
const letters = new Set<string>();
const count = isBoolean(value) ? 2600 : 5200;

for (let i = 0; i < count; i++)
letters.add(randomLetter(value));

if (value === true) {
expect(
Array.from(letters).sort(),
).toEqual(['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']);
}
else if (value === false) {
expect(
Array.from(letters).sort(),
).toEqual(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']);
}
else {
expect(
Array.from(letters).sort(),
).toEqual(
[
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
].sort(),
);
}
});
});
});
});

describe('randomString', () => {
Expand Down
21 changes: 15 additions & 6 deletions packages/mixte/src/random/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { isBoolean } from 'mixte';

/**
* 在传入的两个自然数之间随机生成一个自然数
* @param from 最小的自然数
Expand Down Expand Up @@ -65,16 +67,23 @@ export function randomUppercaseLetter() {

/**
* 随机一个英文字母
* @param uppercase 是否大写 ( default: false )
* @param uppercase 是否大写
* @example
*
* randomLetter(); // -> a ~ z
* randomLetter(); // -> a ~ z, A ~ Z
* randomLetter(true); // -> A ~ Z
* randomLetter(false); // -> a ~ z
*/
export function randomLetter(uppercase = false) {
return String.fromCharCode(
uppercase ? randomNatural(65, 90) : randomNatural(97, 122),
);
export function randomLetter(uppercase?: boolean) {
if (isBoolean(uppercase)) {
return uppercase
? randomUppercaseLetter()
: randomLowercaseLetter();
}

return randomNatural(0, 1)
? randomLowercaseLetter()
: randomUppercaseLetter();
}

/** 生成字符串的选项 */
Expand Down

0 comments on commit 7d880e8

Please sign in to comment.