diff --git a/readme.md b/readme.md index e5adf94..b934d05 100644 --- a/readme.md +++ b/readme.md @@ -96,6 +96,10 @@ All the below methods accept a value and returns a boolean for whether the value Keep in mind that [functions are objects too](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions). +##### .numericString(value) + +Returns `true` for a string that represents a number. For example, `'42'` and `'-8'`. + ##### .regExp(value) ##### .date(value) ##### .error(value) diff --git a/source/index.ts b/source/index.ts index 3ae4dd1..b8f397c 100644 --- a/source/index.ts +++ b/source/index.ts @@ -135,10 +135,13 @@ namespace is { // tslint:disable-line:no-namespace export const symbol = isOfType('symbol'); // tslint:enable:variable-name + export const nullOrUndefined = (value: any): value is null | undefined => null_(value) || undefined(value); + export const numericString = (value: any): boolean => + string(value) && value.length > 0 && !Number.isNaN(Number(value)); + export const array = Array.isArray; export const buffer = isBuffer; - export const nullOrUndefined = (value: any): value is null | undefined => null_(value) || undefined(value); export const object = (value: any): value is object => !nullOrUndefined(value) && (function_(value) || isObject(value)); export const iterable = (value: any): value is IterableIterator => !nullOrUndefined(value) && function_(value[Symbol.iterator]); export const asyncIterable = (value: any): value is AsyncIterableIterator => !nullOrUndefined(value) && function_(value[Symbol.asyncIterator]); diff --git a/source/tests/test.ts b/source/tests/test.ts index af7eda9..a210807 100644 --- a/source/tests/test.ts +++ b/source/tests/test.ts @@ -76,6 +76,14 @@ const types = new Map([ Symbol('🦄') ] }], + ['numericString', { + is: m.numericString, + fixtures: [ + '5', + '-3.2', + 'Infinity' + ] + }], ['array', { is: is.array, fixtures: [ @@ -411,7 +419,7 @@ test('is.null', t => { }); test('is.string', t => { - testType(t, 'string', ['emptyString']); + testType(t, 'string', ['emptyString', 'numericString']); }); test('is.number', t => { @@ -426,6 +434,12 @@ test('is.symbol', t => { testType(t, 'symbol'); }); +test('is.numericString', t => { + testType(t, 'numericString'); + t.false(m.numericString('')); + t.false(m.numericString(1)); +}); + test('is.array', t => { testType(t, 'array', ['emptyArray']); });