Skip to content

Commit

Permalink
feat(isBtcAddress): add isBtcAddress validator
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelcerne committed Feb 3, 2020
1 parent b331b5a commit 317512d
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ Validator | Description
**isByteLength(str [, options])** | check if the string's length (in UTF-8 bytes) falls in a range.<br/><br/>`options` is an object which defaults to `{min:0, max: undefined}`.
**isCreditCard(str)** | check if the string is a credit card.
**isCurrency(str [, options])** | check if the string is a valid currency amount.<br/><br/>`options` is an object which defaults to `{symbol: '$', require_symbol: false, allow_space_after_symbol: false, symbol_after_digits: false, allow_negatives: true, parens_for_negatives: false, negative_sign_before_digits: false, negative_sign_after_digits: false, allow_negative_sign_placeholder: false, thousands_separator: ',', decimal_separator: '.', allow_decimal: true, require_decimal: false, digits_after_decimal: [2], allow_space_after_digits: false}`.<br/>**Note:** The array `digits_after_decimal` is filled with the exact number of digits allowed not a range, for example a range 1 to 3 will be given as [1, 2, 3].
**isBtcAddress(str)** | check if the string is a valid BTC address.
**isDataURI(str)** | check if the string is a [data uri format](https://developer.mozilla.org/en-US/docs/Web/HTTP/data_URIs).
**isDecimal(str [, options])** | check if the string represents a decimal number, such as 0.1, .3, 1.1, 1.00003, 4.0, etc.<br/><br/>`options` is an object which defaults to `{force_decimal: false, decimal_digits: '1,', locale: 'en-US'}`<br/><br/>`locale` determine the decimal separator and 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', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'es-ES', 'fr-FR', 'hu-HU', 'it-IT', 'ku-IQ', nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'sl-SI', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'tr-TR', 'uk-UA']`.<br/>**Note:** `decimal_digits` is given as a range like '1,3', a specific value like '3' or min like '1,'.
**isDivisibleBy(str, number)** | check if the string is a number that's divisible by another.
Expand Down
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ var _isMobilePhone = _interopRequireWildcard(require("./lib/isMobilePhone"));

var _isCurrency = _interopRequireDefault(require("./lib/isCurrency"));

var _isBtcAddress = _interopRequireDefault(require("./lib/isBtcAddress"));

var _isISO = _interopRequireDefault(require("./lib/isISO8601"));

var _isRFC = _interopRequireDefault(require("./lib/isRFC3339"));
Expand Down Expand Up @@ -231,6 +233,7 @@ var validator = {
isPostalCode: _isPostalCode.default,
isPostalCodeLocales: _isPostalCode.locales,
isCurrency: _isCurrency.default,
isBtcAddress: _isBtcAddress.default,
isISO8601: _isISO.default,
isRFC3339: _isRFC.default,
isISO31661Alpha2: _isISO31661Alpha.default,
Expand Down
21 changes: 21 additions & 0 deletions lib/isBtcAddress.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = isBtcAddress;

var _assertString = _interopRequireDefault(require("./util/assertString"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

// supports Bech32 addresses
var btc = /^(bc1|[13])[a-zA-HJ-NP-Z0-9]{25,39}$/;

function isBtcAddress(str) {
(0, _assertString.default)(str);
return btc.test(str);
}

module.exports = exports.default;
module.exports.default = exports.default;
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ import isMobilePhone, { locales as isMobilePhoneLocales } from './lib/isMobilePh

import isCurrency from './lib/isCurrency';

import isBtcAddress from './lib/isBtcAddress';

import isISO8601 from './lib/isISO8601';
import isRFC3339 from './lib/isRFC3339';
import isISO31661Alpha2 from './lib/isISO31661Alpha2';
Expand Down Expand Up @@ -166,6 +168,7 @@ const validator = {
isPostalCode,
isPostalCodeLocales,
isCurrency,
isBtcAddress,
isISO8601,
isRFC3339,
isISO31661Alpha2,
Expand Down
9 changes: 9 additions & 0 deletions src/lib/isBtcAddress.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import assertString from './util/assertString';

// supports Bech32 addresses
const btc = /^(bc1|[13])[a-zA-HJ-NP-Z0-9]{25,39}$/;

export default function isBtcAddress(str) {
assertString(str);
return btc.test(str);
}
16 changes: 16 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -6506,6 +6506,22 @@ describe('Validators', () => {
});
});

it('should validate Bitcoin addresses', () => {
test({
validator: 'isBtcAddress',
valid: [
'1MUz4VMYui5qY1mxUiG8BQ1Luv6tqkvaiL',
'3J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy',
'bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq',
],
invalid: [
'4J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy',
'0x56F0B8A998425c53c75C4A303D4eF987533c5597',
'pp8skudq3x5hzw8ew7vzsw8tn4k8wxsqsv0lt0mf3g',
],
});
});

it('should validate booleans', () => {
test({
validator: 'isBoolean',
Expand Down
9 changes: 9 additions & 0 deletions validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
}(this, (function () { 'use strict';

function _typeof(obj) {
"@babel/helpers - typeof";

if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
_typeof = function (obj) {
return typeof obj;
Expand Down Expand Up @@ -1769,6 +1771,12 @@ function isCurrency(str, options) {
return currencyRegex(options).test(str);
}

var btc = /^(bc1|[13])[a-zA-HJ-NP-Z0-9]{25,39}$/;
function isBtcAddress(str) {
assertString(str);
return btc.test(str);
}

/* eslint-disable max-len */
// from http://goo.gl/0ejHHW

Expand Down Expand Up @@ -2313,6 +2321,7 @@ var validator = {
isPostalCode: isPostalCode,
isPostalCodeLocales: locales$4,
isCurrency: isCurrency,
isBtcAddress: isBtcAddress,
isISO8601: isISO8601,
isRFC3339: isRFC3339,
isISO31661Alpha2: isISO31661Alpha2,
Expand Down
2 changes: 1 addition & 1 deletion validator.min.js

Large diffs are not rendered by default.

0 comments on commit 317512d

Please sign in to comment.