Refactor module in TypeScript (#28)

This commit is contained in:
Lukas Tetzlaff 2017-11-06 16:26:59 +01:00 committed by Sindre Sorhus
parent 83adc096ef
commit 8d8fd2b7e0
7 changed files with 602 additions and 375 deletions

243
source/index.ts Normal file
View file

@ -0,0 +1,243 @@
import * as util from 'util';
const toString = Object.prototype.toString;
const getObjectType = (value: any) => toString.call(value).slice(8, -1) as string;
const isOfType = (type: string) => (value: any) => typeof value === type; // tslint:disable-line
const isObjectOfType = (type: string) => (value: any) => getObjectType(value) === type;
function is(value: any) { // tslint:disable-line:only-arrow-functions
if (value === null) {
return 'null';
}
if (value === true || value === false) {
return 'boolean';
}
const type = typeof value;
if (type === 'undefined') {
return 'undefined';
}
if (type === 'string') {
return 'string';
}
if (type === 'number') {
return 'number';
}
if (type === 'symbol') {
return 'symbol';
}
if (is.function_(value)) {
return 'Function';
}
if (Array.isArray(value)) {
return 'Array';
}
if (Buffer.isBuffer(value)) {
return 'Buffer';
}
const tagType = getObjectType(value);
if (tagType) {
return tagType;
}
if (value instanceof String || value instanceof Boolean || value instanceof Number) {
throw new TypeError('Please don\'t use object wrappers for primitive types');
}
return 'Object';
}
namespace is { // tslint:disable-line:no-namespace
const isObject = (value: any) => typeof value === 'object';
// tslint:disable:variable-name
export const undefined = isOfType('undefined');
export const string = isOfType('string');
export const number = isOfType('number');
export const function_ = isOfType('function');
export const null_ = (value: any) => value === null;
export const class_ = (value: any) => function_(value) && value.toString().startsWith('class ');
export const boolean = (value: any) => value === true || value === false;
// tslint:enable:variable-name
export const symbol = isOfType('symbol');
export const array = Array.isArray;
export const buffer = Buffer.isBuffer;
export const nullOrUndefined = (value: any) => null_(value) || undefined(value);
export const object = (value: any) => !nullOrUndefined(value) && (function_(value) || isObject(value));
export const iterable = (value: any) => !nullOrUndefined(value) && function_(value[Symbol.iterator]);
export const generator = (value: any) => iterable(value) && function_(value.next) && function_(value.throw);
export const nativePromise = isObjectOfType('Promise');
const hasPromiseAPI = (value: any) =>
!null_(value) &&
isObject(value) &&
function_(value.then) &&
function_(value.catch);
export const promise = (value: any) => nativePromise(value) || hasPromiseAPI(value);
// TODO: Change to use `isObjectOfType` once Node.js 6 or higher is targeted
const isFunctionOfType = (type: string) => (value: any) => function_(value) && function_(value.constructor) && value.constructor.name === type;
export const generatorFunction = isFunctionOfType('GeneratorFunction');
export const asyncFunction = isFunctionOfType('AsyncFunction');
export const regExp = isObjectOfType('RegExp');
export const date = isObjectOfType('Date');
export const error = isObjectOfType('Error');
export const map = isObjectOfType('Map');
export const set = isObjectOfType('Set');
export const weakMap = isObjectOfType('WeakMap');
export const weakSet = isObjectOfType('WeakSet');
export const int8Array = isObjectOfType('Int8Array');
export const uint8Array = isObjectOfType('Uint8Array');
export const uint8ClampedArray = isObjectOfType('Uint8ClampedArray');
export const int16Array = isObjectOfType('Int16Array');
export const uint16Array = isObjectOfType('Uint16Array');
export const int32Array = isObjectOfType('Int32Array');
export const uint32Array = isObjectOfType('Uint32Array');
export const float32Array = isObjectOfType('Float32Array');
export const float64Array = isObjectOfType('Float64Array');
export const arrayBuffer = isObjectOfType('ArrayBuffer');
export const sharedArrayBuffer = isObjectOfType('SharedArrayBuffer');
export const truthy = (value: any) => Boolean(value);
export const falsy = (value: any) => !value;
export const nan = (value: any) => Number.isNaN(value);
const primitiveTypes = new Set([
'undefined',
'string',
'number',
'boolean',
'symbol'
]);
export const primitive = (value: any) => null_(value) || primitiveTypes.has(typeof value);
export const integer = (value: any) => Number.isInteger(value);
export const safeInteger = (value: any) => Number.isSafeInteger(value);
export const plainObject = (value: any) => {
// From: https://github.com/sindresorhus/is-plain-obj/blob/master/index.js
let prototype;
return getObjectType(value) === 'Object' &&
(prototype = Object.getPrototypeOf(value), prototype === null || // tslint:disable-line:ban-comma-operator
prototype === Object.getPrototypeOf({}));
};
const typedArrayTypes = new Set([
'Int8Array',
'Uint8Array',
'Uint8ClampedArray',
'Int16Array',
'Uint16Array',
'Int32Array',
'Uint32Array',
'Float32Array',
'Float64Array'
]);
export const typedArray = (value: any) => typedArrayTypes.has(getObjectType(value));
const isValidLength = (value: any) => safeInteger(value) && value > -1;
export const arrayLike = (value: any) => !nullOrUndefined(value) && !function_(value) && isValidLength(value.length);
export const inRange = (value: number, range: number | number[]) => {
if (number(range)) {
return value >= Math.min(0, range as number) && value <= Math.max(range as number, 0);
}
if (array(range) && range.length === 2) {
// TODO: Use spread operator here when targeting Node.js 6 or higher
return value >= Math.min.apply(null, range) && value <= Math.max.apply(null, range);
}
throw new TypeError(`Invalid range: ${util.inspect(range)}`);
};
const NODE_TYPE_ELEMENT = 1;
const DOM_PROPERTIES_TO_CHECK = [
'innerHTML',
'ownerDocument',
'style',
'attributes',
'nodeValue'
];
export const domElement = (value: any) => object(value) && value.nodeType === NODE_TYPE_ELEMENT && string(value.nodeName) &&
!plainObject(value) && DOM_PROPERTIES_TO_CHECK.every(property => property in value);
export const infinite = (value: any) => value === Infinity || value === -Infinity;
const isAbsoluteMod2 = (value: number) => (rem: number) => integer(rem) && Math.abs(rem % 2) === value;
export const even = isAbsoluteMod2(0);
export const odd = isAbsoluteMod2(1);
const isWhiteSpaceString = (value: any) => string(value) && /\S/.test(value) === false;
const isEmptyStringOrArray = (value: any) => (string(value) || array(value)) && value.length === 0;
const isEmptyObject = (value: any) => !map(value) && !set(value) && object(value) && Object.keys(value).length === 0;
const isEmptyMapOrSet = (value: any) => (map(value) || set(value)) && value.size === 0;
export const empty = (value: any) => falsy(value) || isEmptyStringOrArray(value) || isEmptyObject(value) || isEmptyMapOrSet(value);
export const emptyOrWhitespace = (value: any) => empty(value) || isWhiteSpaceString(value);
type ArrayMethod = (fn: (value: any, index: number, arr: any[]) => boolean, thisArg?: any) => boolean;
const predicateOnArray = (method: ArrayMethod, predicate: any, args: IArguments) => {
// `args` is the calling function's "arguments object".
// We have to do it this way to keep node v4 support.
// So here we convert it to an array and slice off the first item.
const values = Array.prototype.slice.call(args, 1);
if (function_(predicate) === false) {
throw new TypeError(`Invalid predicate: ${util.inspect(predicate)}`);
}
if (values.length === 0) {
throw new TypeError('Invalid number of values');
}
return method.call(values, predicate);
};
// We can't use rest parameters in node v4 due to the lack of the spread operator.
// Therefore We have to use anonymous functions for the any() and all() methods
// tslint:disable:only-arrow-functions no-function-expression
export function any(...predicate: any[]): any; // tslint:disable-line:variable-name
export function any(predicate: any) {
return predicateOnArray(Array.prototype.some, predicate, arguments);
}
export function all(...predicate: any[]): any;
export function all(predicate: any) {
return predicateOnArray(Array.prototype.every, predicate, arguments);
}
// tslint:enable:only-arrow-functions no-function-expression
}
// Some few keywords are reserved, but we'll populate them for the node-folks
// See https://github.com/Microsoft/TypeScript/issues/2536
Object.defineProperties(is, {
class: {value: is.class_},
function: {value: is.function_},
null: {value: is.null_}
});
export default is; // tslint:disable-line:no-default-export

715
source/tests/test.ts Normal file
View file

@ -0,0 +1,715 @@
import * as util from 'util';
import test, {TestContext, Context} from 'ava';
import {jsdom} from 'jsdom';
import m from '..';
const isNode8orHigher = Number(process.versions.node.split('.')[0]) >= 8;
// Currently out of order, see https://github.com/Microsoft/TypeScript/issues/15202 class PromiseSubclassFixture<T> extends Promise<T> {}
class ErrorSubclassFixture extends Error {}
const document = jsdom();
const createDomElement = (el: string) => document.createElement(el);
interface Test {
is(value: any): boolean;
fixtures: any[];
}
const types = new Map<string, Test>([
['undefined', {
is: m.undefined,
fixtures: [
undefined
]
}],
['null', {
is: m.null_,
fixtures: [
null
]
}],
['string', {
is: m.string,
fixtures: [
'🦄',
'hello world',
''
]
}],
['number', {
is: m.number,
fixtures: [
6,
1.4,
0,
-0,
Infinity,
-Infinity
]
}],
['boolean', {
is: m.boolean,
fixtures: [
true, false
]
}],
['symbol', {
is: m.symbol,
fixtures: [
Symbol('🦄')
]
}],
['array', {
is: m.array,
fixtures: [
[1, 2],
new Array(2) // tslint:disable-line:prefer-array-literal
]
}],
['function', {
is: m.function_,
fixtures: [
// tslint:disable:no-empty no-unused-variable only-arrow-functions no-function-expression
function foo() {}, // tslint:disable-line:no-unused
function() {},
() => {},
async function() {},
function *(): any {}
// tslint:enable:no-empty no-unused-variable only-arrow-functions no-function-expression
]
}],
['buffer', {
is: m.buffer,
fixtures: [
Buffer.from('🦄')
]
}],
['object', {
is: m.object,
fixtures: [
{x: 1},
Object.create({x: 1})
]
}],
['regExp', {
is: m.regExp,
fixtures: [
/\w/,
new RegExp('\\w')
]
}],
['date', {
is: m.date,
fixtures: [
new Date()
]
}],
['error', {
is: m.error,
fixtures: [
new Error('🦄'),
new ErrorSubclassFixture()
]
}],
['nativePromise', {
is: m.nativePromise,
fixtures: [
Promise.resolve(),
// PromiseSubclassFixture.resolve()
]
}],
['promise', {
is: m.promise,
fixtures: [
{then() {}, catch() {}} // tslint:disable-line:no-empty
]
}],
['generator', {
is: m.generator,
fixtures: [
(function *() { yield 4; })() // tslint:disable-line
]
}],
['generatorFunction', {
is: m.generatorFunction,
fixtures: [
function *() { yield 4; } // tslint:disable-line
]
}],
['asyncFunction', {
is: m.asyncFunction,
fixtures: [
async function() {}, // tslint:disable-line:no-empty only-arrow-functions no-function-expression
async () => {} // tslint:disable-line:no-empty
]
}],
['map', {
is: m.map,
fixtures: [
new Map()
]
}],
['set', {
is: m.set,
fixtures: [
new Set()
]
}],
['weakSet', {
is: m.weakSet,
fixtures: [
new WeakSet()
]
}],
['weakMap', {
is: m.weakMap,
fixtures: [
new WeakMap()
]
}],
['int8Array', {
is: m.int8Array,
fixtures: [
new Int8Array(0)
]
}],
['uint8Array', {
is: m.uint8Array,
fixtures: [
new Uint8Array(0)
]
}],
['uint8ClampedArray', {
is: m.uint8ClampedArray,
fixtures: [
new Uint8ClampedArray(0)
]
}],
['int16Array', {
is: m.int16Array,
fixtures: [
new Int16Array(0)
]
}],
['uint16Array', {
is: m.uint16Array,
fixtures: [
new Uint16Array(0)
]
}],
['int32Array', {
is: m.int32Array,
fixtures: [
new Int32Array(0)
]
}],
['uint32Array', {
is: m.uint32Array,
fixtures: [
new Uint32Array(0)
]
}],
['float32Array', {
is: m.float32Array,
fixtures: [
new Float32Array(0)
]
}],
['float64Array', {
is: m.float64Array,
fixtures: [
new Float64Array(0)
]
}],
['arrayBuffer', {
is: m.arrayBuffer,
fixtures: [
new ArrayBuffer(10)
]
}],
['nan', {
is: m.nan,
fixtures: [
NaN,
Number.NaN
]
}],
['nullOrUndefined', {
is: m.nullOrUndefined,
fixtures: [
null,
undefined
]
}],
['plainObject', {
is: m.plainObject,
fixtures: [
{x: 1},
Object.create(null),
new Object()
]
}],
['integer', {
is: m.integer,
fixtures: [
6
]
}],
['safeInteger', {
is: m.safeInteger,
fixtures: [
Math.pow(2, 53) - 1,
-Math.pow(2, 53) + 1
]
}],
['domElement', {
is: m.domElement,
fixtures: [
'div',
'input',
'span',
'img',
'canvas',
'script'
].map(createDomElement) }
], ['non-domElements', {
is: value => !m.domElement(value),
fixtures: [
document.createTextNode('data'),
document.createProcessingInstruction('xml-stylesheet', 'href="mycss.css" type="text/css"'),
document.createComment('This is a comment'),
document,
document.implementation.createDocumentType('svg:svg', '-//W3C//DTD SVG 1.1//EN', 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'), // tslint:disable-line
document.createDocumentFragment()
]
}],
['infinite', {
is: m.infinite,
fixtures: [
Infinity,
-Infinity
]
}]
]);
// This ensures a certain method matches only the types it's supposed to and none of the other methods' types
const testType = (t: TestContext & Context<any>, type: string, exclude?: string[]) => {
const testData = types.get(type);
if (testData === undefined) {
t.fail(`is.${type} not defined`);
return;
}
const {is} = testData;
for (const [key, {fixtures}] of types) {
// TODO: Automatically exclude value types in other tests that we have in the current one.
// Could reduce the use of `exclude`.
if (exclude && exclude.indexOf(key) !== -1) {
continue;
}
const assert = key === type ? t.true.bind(t) : t.false.bind(t);
for (const fixture of fixtures) {
assert(is(fixture), `Value: ${util.inspect(fixture)}`);
}
}
};
test('is', t => {
t.is(m(null), 'null');
t.is(m(undefined), 'undefined');
// TODO: Expand this to all the supported types. Maybe reuse `testType()` somehow.
});
test('is.undefined', t => {
testType(t, 'undefined', ['nullOrUndefined']);
});
test('is.null', t => {
testType(t, 'null', ['nullOrUndefined']);
});
test('is.string', t => {
testType(t, 'string');
});
test('is.number', t => {
testType(t, 'number', ['nan', 'integer', 'safeInteger', 'infinite']);
});
test('is.boolean', t => {
testType(t, 'boolean');
});
test('is.symbol', t => {
testType(t, 'symbol');
});
test('is.array', t => {
testType(t, 'array');
});
test('is.function', t => {
testType(t, 'function', ['generatorFunction', 'asyncFunction']);
});
test('is.buffer', t => {
testType(t, 'buffer');
});
test('is.object', t => {
const testData = types.get('object');
if (testData === undefined) {
t.fail('is.object not defined');
return;
}
for (const el of testData.fixtures) {
t.true(m.object(el));
}
});
test('is.regExp', t => {
testType(t, 'regExp');
});
test('is.date', t => {
testType(t, 'date');
});
test('is.error', t => {
testType(t, 'error');
});
if (isNode8orHigher) {
test('is.nativePromise', t => {
testType(t, 'nativePromise');
});
test('is.promise', t => {
testType(t, 'promise', ['nativePromise']);
});
/*test('is.asyncFunction', t => {
testType(t, 'asyncFunction', ['function']);
});*/
}
test('is.generator', t => {
testType(t, 'generator');
});
test('is.generatorFunction', t => {
testType(t, 'generatorFunction', ['function']);
});
test('is.map', t => {
testType(t, 'map');
});
test('is.set', t => {
testType(t, 'set');
});
test('is.weakMap', t => {
testType(t, 'weakMap');
});
test('is.weakSet', t => {
testType(t, 'weakSet');
});
test('is.int8Array', t => {
testType(t, 'int8Array');
});
test('is.uint8Array', t => {
testType(t, 'uint8Array', ['buffer']);
});
test('is.uint8ClampedArray', t => {
testType(t, 'uint8ClampedArray');
});
test('is.int16Array', t => {
testType(t, 'int16Array');
});
test('is.uint16Array', t => {
testType(t, 'uint16Array');
});
test('is.int32Array', t => {
testType(t, 'int32Array');
});
test('is.uint32Array', t => {
testType(t, 'uint32Array');
});
test('is.float32Array', t => {
testType(t, 'float32Array');
});
test('is.float64Array', t => {
testType(t, 'float64Array');
});
test('is.arrayBuffer', t => {
testType(t, 'arrayBuffer');
});
test('is.dataView', t => {
testType(t, 'arrayBuffer');
});
test('is.truthy', t => {
t.true(m.truthy('unicorn'));
t.true(m.truthy('🦄'));
t.true(m.truthy(new Set()));
t.true(m.truthy(Symbol('🦄')));
t.true(m.truthy(true));
});
test('is.falsy', t => {
t.true(m.falsy(false));
t.true(m.falsy(0));
t.true(m.falsy(''));
t.true(m.falsy(null));
t.true(m.falsy(undefined));
t.true(m.falsy(NaN));
});
test('is.nan', t => {
testType(t, 'nan');
});
test('is.nullOrUndefined', t => {
testType(t, 'nullOrUndefined', ['undefined', 'null']);
});
test('is.primitive', t => {
const primitives = [
undefined,
null,
'🦄',
6,
Infinity,
-Infinity,
true,
false,
Symbol('🦄')
];
for (const el of primitives) {
t.true(m.primitive(el));
}
});
test('is.integer', t => {
testType(t, 'integer', ['number', 'safeInteger']);
t.false(m.integer(1.4));
});
test('is.safeInteger', t => {
testType(t, 'safeInteger', ['number', 'integer']);
t.false(m.safeInteger(Math.pow(2, 53)));
t.false(m.safeInteger(-Math.pow(2, 53)));
});
test('is.plainObject', t => {
testType(t, 'plainObject', ['object', 'promise']);
});
test('is.iterable', t => {
t.true(m.iterable(''));
t.true(m.iterable([]));
t.true(m.iterable(new Map()));
t.false(m.iterable(null));
t.false(m.iterable(undefined));
t.false(m.iterable(0));
t.false(m.iterable(NaN));
t.false(m.iterable(Infinity));
t.false(m.iterable({}));
});
test('is.class', t => {
class Foo {} // tslint:disable-line
const classDeclarations = [
Foo,
class Bar extends Foo {} // tslint:disable-line
];
for (const x of classDeclarations) {
t.true(m.class_(x));
}
});
test('is.typedArray', t => {
// Typescript currently does not support empty constructors for these
// See https://github.com/Microsoft/TypeScript/issues/19680
const typedArrays = [
new Int8Array(0),
new Uint8Array(0),
new Uint8ClampedArray(0),
new Uint16Array(0),
new Int32Array(0),
new Uint32Array(0),
new Float32Array(0),
new Float64Array(0)
];
for (const el of typedArrays) {
t.true(m.typedArray(el));
}
t.false(m.typedArray(new ArrayBuffer(1)));
t.false(m.typedArray([]));
t.false(m.typedArray({}));
});
test('is.arrayLike', t => {
(() => {
t.true(m.arrayLike(arguments));
})();
t.true(m.arrayLike([]));
t.true(m.arrayLike('unicorn'));
t.false(m.arrayLike({}));
t.false(m.arrayLike(() => {})); // tslint:disable-line:no-empty
t.false(m.arrayLike(new Map()));
});
test('is.inRange', t => {
const x = 3;
t.true(m.inRange(x, [0, 5]));
t.true(m.inRange(x, [5, 0]));
t.true(m.inRange(x, [-5, 5]));
t.true(m.inRange(x, [5, -5]));
t.false(m.inRange(x, [4, 8]));
t.true(m.inRange(-7, [-5, -10]));
t.true(m.inRange(-5, [-5, -10]));
t.true(m.inRange(-10, [-5, -10]));
t.true(m.inRange(x, 10));
t.true(m.inRange(0, 0));
t.true(m.inRange(-2, -3));
t.false(m.inRange(x, 2));
t.false(m.inRange(-3, -2));
t.throws(() => {
m.inRange(0, []);
});
t.throws(() => {
m.inRange(0, [5]);
});
t.throws(() => {
m.inRange(0, [1, 2, 3]);
});
});
test('is.domElement', t => {
testType(t, 'domElement');
t.false(m.domElement({nodeType: 1, nodeName: 'div'}));
});
test('is.infinite', t => {
testType(t, 'infinite', ['number']);
});
test('is.even', t => {
for (const el of [-6, 2, 4]) {
t.true(m.even(el));
}
for (const el of [-3, 1, 5]) {
t.false(m.even(el));
}
});
test('is.odd', t => {
for (const el of [-5, 7, 13]) {
t.true(m.odd(el));
}
for (const el of [-8, 8, 10]) {
t.false(m.odd(el));
}
});
test('is.empty', t => {
t.true(m.empty(null));
t.true(m.empty(undefined));
t.true(m.empty(false));
t.false(m.empty(true));
t.true(m.empty(''));
t.false(m.empty('🦄'));
t.true(m.empty([]));
t.false(m.empty(['🦄']));
t.true(m.empty({}));
t.false(m.empty({unicorn: '🦄'}));
const tempMap = new Map();
t.true(m.empty(tempMap));
tempMap.set('unicorn', '🦄');
t.false(m.empty(tempMap));
const tempSet = new Set();
t.true(m.empty(tempSet));
tempSet.add(1);
t.false(m.empty(tempSet));
});
test('is.emptyOrWhitespace', t => {
t.true(m.emptyOrWhitespace(''));
t.true(m.emptyOrWhitespace(' '));
t.false(m.emptyOrWhitespace('🦄'));
t.false(m.emptyOrWhitespace('unicorn'));
});
test('is.any', t => {
t.true(m.any(m.string, {}, true, '🦄'));
t.true(m.any(m.object, false, {}, 'unicorns'));
t.false(m.any(m.boolean, '🦄', [], 3));
t.false(m.any(m.integer, true, 'lol', {}));
t.throws(() => {
m.any(null, true);
});
t.throws(() => {
m.any(m.string);
});
});
test('is.all', t => {
t.true(m.all(m.object, {}, new Set(), new Map()));
t.true(m.all(m.boolean, true, false));
t.false(m.all(m.string, '🦄', []));
t.false(m.all(m.set, new Map(), {}));
t.throws(() => {
m.all(null, true);
});
t.throws(() => {
m.all(m.string);
});
});