From 50fb72ce3d7a911564ad5ff9f929ca5567a83757 Mon Sep 17 00:00:00 2001 From: Brayton Rude <59886631+notbrayton@users.noreply.github.com> Date: Wed, 7 Dec 2022 04:13:40 -0600 Subject: feat(string): add special() method (#1634) --- src/modules/string/index.ts | 61 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) (limited to 'src/modules/string') 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; + } } -- cgit v1.2.3