Add support for explicit string character code ranges #6036
-
🚀 Feature RequestIt would be nice to have a way to specify more fine-grained character code ranges for string generation. MotivationI want to generate alphanumeric strings, but just adding a generator for just that exact case feels overly specific. If I could pass a more fine-grained way of selecting subvalues then I could easily construct whatever specific arbitrary I needed. Example// generates a string consisting of single characters from the provided string
const hexGenerator = fc.string({ fromChars: "1234567890ABCDEF" });
// generates a string consisting of characters selected from the union of the below ranges
const alphanumericGenerator = fc.string({
fromChars: fc.stringBuilders.charRange({
// numerals
encoding: "ascii",
start: 48,
end: 57
}).concat(fc.stringBuilders.charRange({
// uppercase letters
encoding: "ascii",
start: 65
end: 90
}).concat(fc.stringBuilders.charRange({
// lowercase letters
encoding: "ascii",
start: 97,
end: 122
});
The two could be combined, I think. Maybe a Side note, I think fast-check is awesome, and if a feature like this was desired I would be happy to try to implement it myself. I didn't want to attempt a drive-by PR for something that may not fit the overall design goals of the library. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
|
Do you know about fc.stringMatching? With that you can generate strings based on a regex. In your example, not sure if you mean union or concatenation of ranges, but both is possible: // concatenation:
fc.stringMatching(/^[0-9][a-z][A-Z]$/)
// union:
fc.stringMatching(/^[0-9a-zA-Z]$/) |
Beta Was this translation helpful? Give feedback.
-
|
I didn't realize there were more string functions than on the "primitives" docs page 🤦 |
Beta Was this translation helpful? Give feedback.
Do you know about fc.stringMatching? With that you can generate strings based on a regex. In your example, not sure if you mean union or concatenation of ranges, but both is possible: