diff options
| author | Brayton Rude <[email protected]> | 2022-12-07 04:13:40 -0600 |
|---|---|---|
| committer | GitHub <[email protected]> | 2022-12-07 10:13:40 +0000 |
| commit | 50fb72ce3d7a911564ad5ff9f929ca5567a83757 (patch) | |
| tree | 75a9f702192e707faedb5bad5e81e580f83bd73b /src/modules | |
| parent | f1948bd5efac0acb286db398b665bb581fcaec27 (diff) | |
| download | faker-50fb72ce3d7a911564ad5ff9f929ca5567a83757.tar.xz faker-50fb72ce3d7a911564ad5ff9f929ca5567a83757.zip | |
feat(string): add special() method (#1634)
Diffstat (limited to 'src/modules')
| -rw-r--r-- | src/modules/string/index.ts | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/src/modules/string/index.ts b/src/modules/string/index.ts index 827c1f45..32f76b72 100644 --- a/src/modules/string/index.ts +++ b/src/modules/string/index.ts @@ -442,4 +442,65 @@ export class StringModule { }; return RFC4122_TEMPLATE.replace(/[xy]/g, replacePlaceholders); } + + /** + * Returns a string containing only special characters. + * + * @param length Length of the generated string. Defaults to `1`. + * @param length.min The minimum number of special characters to generate. + * @param length.max The maximum number of special characters to generate. + * + * @example + * faker.string.special() // '$' + * faker.string.special(5) // '#*!.~' + * faker.string.special({ min: 5, max: 10 }) // ')|@*>^+' + * + * @since 8.0.0 + */ + special(length: number | { min: number; max: number } = 1): string { + length = this.faker.helpers.rangeToNumber(length); + if (length <= 0) { + return ''; + } + + let specialString = ''; + for (let i = 0; i < length; i++) { + specialString += this.faker.helpers.arrayElement([ + '!', + '"', + '#', + '$', + '%', + '&', + "'", + '(', + ')', + '*', + '+', + ',', + '-', + '.', + '/', + ':', + ';', + '<', + '=', + '>', + '?', + '@', + '[', + '\\', + ']', + '^', + '_', + '`', + '{', + '|', + '}', + '~', + ]); + } + + return specialString; + } } |
