Skip to content

Commit cf8dcea

Browse files
BigOsvaapbraaar
andauthored
feat(isBase32): Add option for Crockford's base 32 alternative (#1888)
closes #1857 * feat(crockforbase32): Add option for Crockford's base 32 alternative to isBase32 method * test move out to its own it() statement * Update README.md Co-authored-by: Brage <[email protected]> Co-authored-by: Brage <[email protected]>
1 parent c1b21a9 commit cf8dcea

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ Validator | Description
9595
**isAlpha(str [, locale, options])** | check if the string contains only letters (a-zA-Z).<br/><br/>Locale is one of `['ar', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-LB', 'ar-LY', 'ar-MA', 'ar-QA', 'ar-QM', 'ar-SA', 'ar-SD', 'ar-SY', 'ar-TN', 'ar-YE', 'bg-BG', 'cs-CZ', 'da-DK', 'de-DE', 'el-GR', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'es-ES', 'fa-IR', 'fi-FI', 'fr-CA', 'fr-FR', 'he', 'hi-IN', 'hu-HU', 'it-IT', 'ku-IQ', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'sl-SI', 'sk-SK', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'tr-TR', 'uk-UA']`) and defaults to `en-US`. Locale list is `validator.isAlphaLocales`. options is an optional object that can be supplied with the following key(s): ignore which can either be a String or RegExp of characters to be ignored e.g. " -" will ignore spaces and -'s.
9696
**isAlphanumeric(str [, locale, options])** | check if the string contains only letters and numbers (a-zA-Z0-9).<br/><br/>Locale is one of `['ar', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-LB', 'ar-LY', 'ar-MA', 'ar-QA', 'ar-QM', 'ar-SA', 'ar-SD', 'ar-SY', 'ar-TN', 'ar-YE', 'bg-BG', 'cs-CZ', 'da-DK', 'de-DE', 'el-GR', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'es-ES', 'fa-IR', 'fi-FI', 'fr-CA', 'fr-FR', 'he', 'hi-IN', 'hu-HU', 'it-IT', 'ku-IQ', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'sl-SI', 'sk-SK', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'tr-TR', 'uk-UA']`) and defaults to `en-US`. Locale list is `validator.isAlphanumericLocales`. options is an optional object that can be supplied with the following key(s): ignore which can either be a String or RegExp of characters to be ignored e.g. " -" will ignore spaces and -'s.
9797
**isAscii(str)** | check if the string contains ASCII chars only.
98-
**isBase32(str)** | check if a string is base32 encoded.
98+
**isBase32(str [, options])** | check if a string is base32 encoded. `options` is optional and defaults to `{crockford: false}`.<br/> When `crockford` is true it tests the given base32 encoded string using [Crockford's base32 alternative](http://www.crockford.com/base32.html).
9999
**isBase58(str)** | check if a string is base58 encoded.
100100
**isBase64(str [, options])** | check if a string is base64 encoded. options is optional and defaults to `{urlSafe: false}`<br/> when `urlSafe` is true it tests the given base64 encoded string is [url safe](https://base64.guru/standards/base64url)
101101
**isBefore(str [, date])** | check if the string is a date that's before the specified date.

src/lib/isBase32.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
11
import assertString from './util/assertString';
2+
import merge from './util/merge';
23

34
const base32 = /^[A-Z2-7]+=*$/;
5+
const crockfordBase32 = /^[A-HJKMNP-TV-Z0-9]+$/;
46

5-
export default function isBase32(str) {
7+
const defaultBase32Options = {
8+
crockford: false,
9+
};
10+
11+
export default function isBase32(str, options) {
612
assertString(str);
13+
options = merge(options, defaultBase32Options);
14+
15+
if (options.crockford) {
16+
return crockfordBase32.test(str);
17+
}
18+
719
const len = str.length;
820
if (len % 8 === 0 && base32.test(str)) {
921
return true;

test/validators.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5753,6 +5753,25 @@ describe('Validators', () => {
57535753
});
57545754
});
57555755

5756+
it('should validate base32 strings with crockford alternative', () => {
5757+
test({
5758+
validator: 'isBase32',
5759+
args: [{ crockford: true }],
5760+
valid: [
5761+
'91JPRV3F41BPYWKCCGGG',
5762+
'60',
5763+
'64',
5764+
'B5QQA833C5Q20S3F41MQ8',
5765+
],
5766+
invalid: [
5767+
'91JPRV3F41BUPYWKCCGGG',
5768+
'B5QQA833C5Q20S3F41MQ8L',
5769+
'60I',
5770+
'B5QQA833OULIC5Q20S3F41MQ8',
5771+
],
5772+
});
5773+
});
5774+
57565775
it('should validate base58 strings', () => {
57575776
test({
57585777
validator: 'isBase58',

0 commit comments

Comments
 (0)