Add is.numericString() (#67)

This commit is contained in:
Itai Steinherz 2018-11-01 13:21:49 +02:00 committed by Sindre Sorhus
parent 9ac56f1be7
commit 7317226c80
3 changed files with 23 additions and 2 deletions

View file

@ -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)

View file

@ -135,10 +135,13 @@ namespace is { // tslint:disable-line:no-namespace
export const symbol = isOfType<Symbol>('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<any> => !nullOrUndefined(value) && function_(value[Symbol.iterator]);
export const asyncIterable = (value: any): value is AsyncIterableIterator<any> => !nullOrUndefined(value) && function_(value[Symbol.asyncIterator]);

View file

@ -76,6 +76,14 @@ const types = new Map<string, Test>([
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']);
});