Skip to content

Commit 46585ef

Browse files
authored
feat(isBtcAddress): add isBtcAddress validator (#1163)
1 parent b331b5a commit 46585ef

File tree

8 files changed

+63
-1
lines changed

8 files changed

+63
-1
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ Validator | Description
9595
**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}`.
9696
**isCreditCard(str)** | check if the string is a credit card.
9797
**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].
98+
**isBtcAddress(str)** | check if the string is a valid BTC address.
9899
**isDataURI(str)** | check if the string is a [data uri format](https://developer.mozilla.org/en-US/docs/Web/HTTP/data_URIs).
99100
**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,'.
100101
**isDivisibleBy(str, number)** | check if the string is a number that's divisible by another.

index.js

+3
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ var _isMobilePhone = _interopRequireWildcard(require("./lib/isMobilePhone"));
117117

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

120+
var _isBtcAddress = _interopRequireDefault(require("./lib/isBtcAddress"));
121+
120122
var _isISO = _interopRequireDefault(require("./lib/isISO8601"));
121123

122124
var _isRFC = _interopRequireDefault(require("./lib/isRFC3339"));
@@ -231,6 +233,7 @@ var validator = {
231233
isPostalCode: _isPostalCode.default,
232234
isPostalCodeLocales: _isPostalCode.locales,
233235
isCurrency: _isCurrency.default,
236+
isBtcAddress: _isBtcAddress.default,
234237
isISO8601: _isISO.default,
235238
isRFC3339: _isRFC.default,
236239
isISO31661Alpha2: _isISO31661Alpha.default,

lib/isBtcAddress.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"use strict";
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
exports.default = isBtcAddress;
7+
8+
var _assertString = _interopRequireDefault(require("./util/assertString"));
9+
10+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11+
12+
// supports Bech32 addresses
13+
var btc = /^(bc1|[13])[a-zA-HJ-NP-Z0-9]{25,39}$/;
14+
15+
function isBtcAddress(str) {
16+
(0, _assertString.default)(str);
17+
return btc.test(str);
18+
}
19+
20+
module.exports = exports.default;
21+
module.exports.default = exports.default;

src/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ import isMobilePhone, { locales as isMobilePhoneLocales } from './lib/isMobilePh
7272

7373
import isCurrency from './lib/isCurrency';
7474

75+
import isBtcAddress from './lib/isBtcAddress';
76+
7577
import isISO8601 from './lib/isISO8601';
7678
import isRFC3339 from './lib/isRFC3339';
7779
import isISO31661Alpha2 from './lib/isISO31661Alpha2';
@@ -166,6 +168,7 @@ const validator = {
166168
isPostalCode,
167169
isPostalCodeLocales,
168170
isCurrency,
171+
isBtcAddress,
169172
isISO8601,
170173
isRFC3339,
171174
isISO31661Alpha2,

src/lib/isBtcAddress.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import assertString from './util/assertString';
2+
3+
// supports Bech32 addresses
4+
const btc = /^(bc1|[13])[a-zA-HJ-NP-Z0-9]{25,39}$/;
5+
6+
export default function isBtcAddress(str) {
7+
assertString(str);
8+
return btc.test(str);
9+
}

test/validators.js

+16
Original file line numberDiff line numberDiff line change
@@ -6506,6 +6506,22 @@ describe('Validators', () => {
65066506
});
65076507
});
65086508

6509+
it('should validate Bitcoin addresses', () => {
6510+
test({
6511+
validator: 'isBtcAddress',
6512+
valid: [
6513+
'1MUz4VMYui5qY1mxUiG8BQ1Luv6tqkvaiL',
6514+
'3J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy',
6515+
'bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq',
6516+
],
6517+
invalid: [
6518+
'4J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy',
6519+
'0x56F0B8A998425c53c75C4A303D4eF987533c5597',
6520+
'pp8skudq3x5hzw8ew7vzsw8tn4k8wxsqsv0lt0mf3g',
6521+
],
6522+
});
6523+
});
6524+
65096525
it('should validate booleans', () => {
65106526
test({
65116527
validator: 'isBoolean',

validator.js

+9
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
}(this, (function () { 'use strict';
2828

2929
function _typeof(obj) {
30+
"@babel/helpers - typeof";
31+
3032
if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
3133
_typeof = function (obj) {
3234
return typeof obj;
@@ -1769,6 +1771,12 @@ function isCurrency(str, options) {
17691771
return currencyRegex(options).test(str);
17701772
}
17711773

1774+
var btc = /^(bc1|[13])[a-zA-HJ-NP-Z0-9]{25,39}$/;
1775+
function isBtcAddress(str) {
1776+
assertString(str);
1777+
return btc.test(str);
1778+
}
1779+
17721780
/* eslint-disable max-len */
17731781
// from http://goo.gl/0ejHHW
17741782

@@ -2313,6 +2321,7 @@ var validator = {
23132321
isPostalCode: isPostalCode,
23142322
isPostalCodeLocales: locales$4,
23152323
isCurrency: isCurrency,
2324+
isBtcAddress: isBtcAddress,
23162325
isISO8601: isISO8601,
23172326
isRFC3339: isRFC3339,
23182327
isISO31661Alpha2: isISO31661Alpha2,

validator.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)