validateNonRepeatedConsecutiveCharsInString
Description
It resolves the str parameter if the characters don't repeat consecutively.
If you want to allow a character to repeat to a certain number of times, you can assign a value to tolerance. The tolerance is concerned with the least amount of times it must repeat (times -1), and not least amount of times it appears.
In other words, if you call this function passing 'aabbbbba' and tolerance of 2, this function will reject RepeatedConsecutiveCharsInStringValidationError, since b is allowed to repeat only twice, however it's repeating 3 times.
The usage of this permission is when you want to avoid the input to have repeated characters, for example, when you are validating a password.
Parameter List | Returns | Rejection Errors |
---|---|---|
| Promise<string || Array> | |
Related Functions | Function Signature | |
function validateNonRepeatedConsecutiveCharsInString(str, tolerance[, tuple]): Promise<string || Array> |
Examples
1
2
3
4
5
import { validateNonRepeatedConsecutiveCharsInString } from "puddy-m/lib/validators/complexValidators";
validateNonRepeatedConsecutiveCharsInString("nonrepeat").then(
(nonRepeatChars) => console.log(nonRepeatChars) // prints 'nonrepeat'
);
1
2
3
4
5
6
7
import { validateNonRepeatedConsecutiveCharsInString } from "puddy-m/lib/validators/complexValidators";
import { validateString } from "puddy-m/lib/validators";
// 1 is allowed to repeat only once, so "11" is valid.
validateNonRepeatedConsecutiveCharsInString("11", 1).then(
(repeatedWithTolerance) => console.log(repeatedWithTolerance)
);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import { validateNonRepeatedConsecutiveCharsInString } from "puddy-m/lib/validators/complexValidators";
validateNonRepeatedConsecutiveCharsInString("ab", 0, [])
.then((tuple) => validateString("Foo", tuple))
.then((tuple) => console.log(tuple)); // prints ['aab', 'foo']
const run = async () => {
try {
const validatedNonRepeatChars =
await validateNonRepeatedConsecutiveCharsInString("foo", 1); // 'o' is allowed to repeat only once, which is correct.
console.log(validatedNonRepeatChars);
} catch (e) {
// Throws an IntegerValidationErrorToValue
console.log(e);
}
};
run();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import { validateNonRepeatedConsecutiveCharsInString } from "puddy-m/lib/validators/complexValidators";
const run = async () => {
try {
const validatedNonRepeatChars =
await validateNonRepeatedConsecutiveCharsInString("fooo", 1); // 'o' is allowed to repeat only once, so, it will throw
console.log(validatedNonRepeatChars);
} catch (e) {
// Throws an NonRepeatedConsecutiveCharsInStringValidationError
console.log(e);
}
};
run();