grpc-js-core: simplify regular expression usage

This commit moves two regular expressions out of the functions
they are used in, and defines them as constants. This commit
also switches from match() to test(), as a Boolean result is
all that's needed.
This commit is contained in:
cjihrig 2018-08-25 11:42:02 -04:00
parent d777e9312b
commit 797bcbaffe
No known key found for this signature in database
GPG Key ID: 7434390BDBE9B9C5

View File

@ -1,5 +1,7 @@
import * as http2 from 'http2';
import {forOwn} from 'lodash';
const LEGAL_KEY_REGEX = /^[0-9a-z_.-]+$/;
const LEGAL_NON_BINARY_VALUE_REGEX = /^[ -~]*$/;
export type MetadataValue = string|Buffer;
@ -24,11 +26,11 @@ function cloneMetadataObject(repr: MetadataObject): MetadataObject {
}
function isLegalKey(key: string): boolean {
return !!key.match(/^[0-9a-z_.-]+$/);
return LEGAL_KEY_REGEX.test(key);
}
function isLegalNonBinaryValue(value: string): boolean {
return !!value.match(/^[ -~]*$/);
return LEGAL_NON_BINARY_VALUE_REGEX.test(value);
}
function isBinaryKey(key: string): boolean {