Skip to content

Modify isBase32 for option to use Crockford Base32 #1857

@sunwukonga

Description

@sunwukonga

Seeking willingness to incorporate a PR based on the following:

Crockford Base32

Add option crockford: bool to isBase32

isBase32(str [, options]) where options is {crockford: false} by default.

Usage: isBase32("CD89FGE", {crockford: true})

Example implementation:

import assertString from './util/assertString';
import merge from './util/merge';

const base32 = /^[A-Z2-7]+=*$/;
const crockfordBase32 = /^[A-HJKMNP-UW-Z0-9]+=*$/;

const defaultBase32Options = {
  crockford: false,
};

export default function isBase32(str, options) {
  assertString(str);
  options = merge(options, defaultBase32Options);
  const len = str.length;

  if (options.crockford) {
    return crockfordBase32.test(str);
  }

  if (len % 8 === 0 && base32.test(str)) {
    return true;
  }
  return false;
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions