isString true for String object (#245)

* Fix isString

* value is

* Fix isHex too

* Fix isHex

* Add hex test with String
This commit is contained in:
Amaury Martiny
2018-11-12 16:23:18 +01:00
committed by Jaco Greeff
parent 6c85979c3a
commit 7a43354ece
4 changed files with 17 additions and 5 deletions
+7
View File
@@ -19,6 +19,13 @@ describe('isHex', () => {
).toEqual(true);
});
it('returns true on hex values with String', () => {
expect(
isHex(new String(`0x${test}`))
).toEqual(true);
});
it('returns false on hex values (non % 2)', () => {
expect(
isHex(`0x${test}0`)
+2 -3
View File
@@ -22,9 +22,8 @@ const HEX_REGEX = /^0x[a-fA-F0-9]+$/;
* isHex('0x1234', 8); // => false
* ```
*/
export default function isHex (_value: any, bitLength: number = -1, ignoreLength: boolean = false): boolean {
const value = (_value as string);
const isValidHex = value === '0x' || (isString(value) && HEX_REGEX.test(value));
export default function isHex (value: any, bitLength: number = -1, ignoreLength: boolean = false): value is string | String {
const isValidHex = value === '0x' || (isString(value) && HEX_REGEX.test(value.toString()));
if (isValidHex && bitLength !== -1) {
return value.length === (2 + Math.ceil(bitLength / 4));
+6
View File
@@ -17,6 +17,12 @@ describe('isString', () => {
).toEqual(true);
});
it('returns true on String object', () => {
expect(
isString(new String('foo'))
).toEqual(true);
});
it('returns false on invalid numbers', () => {
expect(
isString(2)
+2 -2
View File
@@ -17,6 +17,6 @@
* console.log('isString', isString('test')); // => true
* ```
*/
export default function isString (value: any): value is string {
return typeof value === 'string';
export default function isString (value: any): value is string | String {
return typeof value === 'string' || value instanceof String;
}