Skip to content

Commit 50fb72c

Browse files
authored
feat(string): add special() method (#1634)
1 parent f1948bd commit 50fb72c

File tree

3 files changed

+141
-0
lines changed

3 files changed

+141
-0
lines changed

src/modules/string/index.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,4 +442,65 @@ export class StringModule {
442442
};
443443
return RFC4122_TEMPLATE.replace(/[xy]/g, replacePlaceholders);
444444
}
445+
446+
/**
447+
* Returns a string containing only special characters.
448+
*
449+
* @param length Length of the generated string. Defaults to `1`.
450+
* @param length.min The minimum number of special characters to generate.
451+
* @param length.max The maximum number of special characters to generate.
452+
*
453+
* @example
454+
* faker.string.special() // '$'
455+
* faker.string.special(5) // '#*!.~'
456+
* faker.string.special({ min: 5, max: 10 }) // ')|@*>^+'
457+
*
458+
* @since 8.0.0
459+
*/
460+
special(length: number | { min: number; max: number } = 1): string {
461+
length = this.faker.helpers.rangeToNumber(length);
462+
if (length <= 0) {
463+
return '';
464+
}
465+
466+
let specialString = '';
467+
for (let i = 0; i < length; i++) {
468+
specialString += this.faker.helpers.arrayElement([
469+
'!',
470+
'"',
471+
'#',
472+
'$',
473+
'%',
474+
'&',
475+
"'",
476+
'(',
477+
')',
478+
'*',
479+
'+',
480+
',',
481+
'-',
482+
'.',
483+
'/',
484+
':',
485+
';',
486+
'<',
487+
'=',
488+
'>',
489+
'?',
490+
'@',
491+
'[',
492+
'\\',
493+
']',
494+
'^',
495+
'_',
496+
'`',
497+
'{',
498+
'|',
499+
'}',
500+
'~',
501+
]);
502+
}
503+
504+
return specialString;
505+
}
445506
}

test/__snapshots__/string.spec.ts.snap

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,20 @@ exports[`string > 42 > sample > with length parameter 5`] = `"\\"&{dn"`;
104104
105105
exports[`string > 42 > sample > with length range 1`] = `"ky2eiXX/J/*&Kq"`;
106106
107+
exports[`string > 42 > special > noArgs 1`] = `","`;
108+
109+
exports[`string > 42 > special > with length parameter 1`] = `",^}&\\\\"`;
110+
111+
exports[`string > 42 > special > with length parameter 2`] = `"]>>%/"`;
112+
113+
exports[`string > 42 > special > with length parameter 3`] = `"%$\\"/\`"`;
114+
115+
exports[`string > 42 > special > with length parameter 4`] = `"+>%[?"`;
116+
117+
exports[`string > 42 > special > with length parameter 5`] = `"!\\"~\\\\_"`;
118+
119+
exports[`string > 42 > special > with length range 1`] = `"^}&\\\\]>>%/%$\\"/\`"`;
120+
107121
exports[`string > 42 > uuid 1`] = `"5cf2bc99-2721-407d-992b-a00fbdf302f2"`;
108122
109123
exports[`string > 42 > uuid 2`] = `"94980604-8962-404f-9371-c9368f970d9a"`;
@@ -218,6 +232,20 @@ exports[`string > 1211 > sample > with length parameter 5`] = `"h^]dn"`;
218232
219233
exports[`string > 1211 > sample > with length range 1`] = `"Kti5-}$_/\`4hHA0afl\\"h"`;
220234
235+
exports[`string > 1211 > special > noArgs 1`] = `"|"`;
236+
237+
exports[`string > 1211 > special > with length parameter 1`] = `"|/{]("`;
238+
239+
exports[`string > 1211 > special > with length parameter 2`] = `"%~\\"@&"`;
240+
241+
exports[`string > 1211 > special > with length parameter 3`] = `"@'].,"`;
242+
243+
exports[`string > 1211 > special > with length parameter 4`] = `"&[\\\\_!"`;
244+
245+
exports[`string > 1211 > special > with length parameter 5`] = `"]@?\\\\_"`;
246+
247+
exports[`string > 1211 > special > with length range 1`] = `"/{](%~\\"@&@'].,&[\\\\_!]"`;
248+
221249
exports[`string > 1211 > uuid 1`] = `"e7ec32f0-a2a3-4c65-abbd-0caabde64dfd"`;
222250
223251
exports[`string > 1211 > uuid 2`] = `"f379e325-9f7c-4064-a086-f23942b68e5f"`;
@@ -332,6 +360,20 @@ exports[`string > 1337 > sample > with length parameter 5`] = `"D)[B,"`;
332360
333361
exports[`string > 1337 > sample > with length range 1`] = `"U/4:SK$>6QX9"`;
334362
363+
exports[`string > 1337 > special > noArgs 1`] = `")"`;
364+
365+
exports[`string > 1337 > special > with length parameter 1`] = `")<&')"`;
366+
367+
exports[`string > 1337 > special > with length parameter 2`] = `"</\\"+("`;
368+
369+
exports[`string > 1337 > special > with length parameter 3`] = `";=)+~"`;
370+
371+
exports[`string > 1337 > special > with length parameter 4`] = `")\\\\*$^"`;
372+
373+
exports[`string > 1337 > special > with length parameter 5`] = `"-$?,%"`;
374+
375+
exports[`string > 1337 > special > with length range 1`] = `"<&')</\\"+(;=)"`;
376+
335377
exports[`string > 1337 > uuid 1`] = `"48234870-5389-445f-8b41-c61a52bf27dc"`;
336378
337379
exports[`string > 1337 > uuid 2`] = `"cc057669-8c53-474d-a677-226d3e8ed92f"`;

test/string.spec.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ describe('string', () => {
7575
});
7676

7777
t.itRepeated('uuid', 5);
78+
79+
t.describe('special', (t) => {
80+
t.it('noArgs')
81+
.itRepeated('with length parameter', 5, 5)
82+
.it('with length range', { min: 10, max: 20 });
83+
});
7884
});
7985

8086
describe(`random seeded tests for seed ${faker.seed()}`, () => {
@@ -550,6 +556,38 @@ describe('string', () => {
550556
expect(UUID).toMatch(RFC4122);
551557
});
552558
});
559+
560+
describe('special', () => {
561+
it('should return a value of type string with default length of 1', () => {
562+
const actual = faker.string.special();
563+
564+
expect(actual).toBeTypeOf('string');
565+
expect(actual).toHaveLength(1);
566+
});
567+
568+
it('should return an empty string when length is negative', () => {
569+
const actual = faker.string.special(
570+
faker.number.int({ min: -1000, max: -1 })
571+
);
572+
573+
expect(actual).toBe('');
574+
expect(actual).toHaveLength(0);
575+
});
576+
577+
it('should return string of designated length', () => {
578+
const length = 87;
579+
const actual = faker.string.special(length);
580+
581+
expect(actual).toHaveLength(length);
582+
});
583+
584+
it('should return string with a length within a given range', () => {
585+
const actual = faker.string.special({ min: 10, max: 20 });
586+
587+
expect(actual.length).toBeGreaterThanOrEqual(10);
588+
expect(actual.length).toBeLessThanOrEqual(20);
589+
});
590+
});
553591
}
554592
});
555593
});

0 commit comments

Comments
 (0)