getRandomStr

说明

获取随机乱序字符串

源码

https://github.com/ddzy/ts-utility-plugins/tree/master/src/ddzy/utility/string/getRandomStr

用法

getRandomStr.test.ts
import { getRandomStr } from "../../utility/string/getRandomStr";

describe('getRandomStr', () => {
  test('getRandomStr should return default string when not received parameters', () => {
    const expected: [string, number] = [
      'string',
      16,
    ];
    const result: string = getRandomStr();

    expect(typeof result).toBe(expected[0]);
    expect(result.length).toBe(expected[1]);
  });

  test('getRandomStr should return default string when received a empty array', () => {
    const received: string[] = [];
    const expected: [string, number] = [
      'string',
      16,
    ];
    const result: string = getRandomStr(received);

    expect(typeof result).toBe(expected[0]);
    expect(result.length).toBe(expected[1]);
  });

  test('getRandomStr should return assignment length of string when received the length parameter', () => {
    const received: [string[], number] = [
      ['a', 'b', 'c', '1', '2', '3', '_', '-'],
      8,
    ];
    const expected: [string, number] = [
      'string',
      8,
    ];
    const result: string = getRandomStr(...received);

    expect(typeof result).toBe(expected[0]);
    expect(result.length).toBe(expected[1]);
  });

  test('getRandomStr should return a string that not contains special characters when received the truthy parameter names `enableSpecialCharacter`', () => {
    const received: [
      undefined,
      undefined,
      boolean,
    ] = [undefined, undefined, false];
    const expected: RegExp = /[_\-&$@^]+/g;
    const result: string = getRandomStr(...received);

    expect(expected.test(result)).toBeFalsy();
  });
});

最后更新于