Fix linter issues

This commit is contained in:
Lukas Tetzlaff 2017-11-02 13:10:19 +01:00
parent 57e8b70644
commit b66faf461f
2 changed files with 191 additions and 181 deletions

View file

@ -1,11 +1,11 @@
import * as util from "util"; import * as util from 'util';
const toString = Object.prototype.toString; const toString = Object.prototype.toString;
const getObjectType = (value: any) => toString.call(value).slice(8, -1) as string; const getObjectType = (value: any) => toString.call(value).slice(8, -1) as string;
const isOfType = (type: string) => (value: any) => typeof value === type; const isOfType = (type: string) => (value: any) => typeof value === type; // tslint:disable-line
const isObjectOfType = (type: string) => (value: any) => getObjectType(value) === type; const isObjectOfType = (type: string) => (value: any) => getObjectType(value) === type;
function is(value: any) { function is(value: any) { // tslint:disable-line:only-arrow-functions
if (value === null) { if (value === null) {
return 'null'; return 'null';
} }
@ -54,38 +54,43 @@ function is(value: any) {
} }
return 'Object'; return 'Object';
}; }
namespace is { 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 undefined = isOfType('undefined');
export const string = isOfType('string'); export const string = isOfType('string');
export const number = isOfType('number'); export const number = isOfType('number');
export const boolean = (value: any) => value === true || value === false;
export const symbol = isOfType('symbol');
export const function_ = isOfType('function'); export const function_ = isOfType('function');
export const null_ = (value: any) => value === null; 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 array = Array.isArray;
export const buffer = Buffer.isBuffer; export const buffer = Buffer.isBuffer;
const isObject = (value: any) => typeof value === 'object'; export const nullOrUndefined = (value: any) => null_(value) || undefined(value);
export const object = (value: any) => !nullOrUndefined(value) && (function_(value) || isObject(value));
export const object = (value: any) => !is.nullOrUndefined(value) && (is.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'); export const nativePromise = isObjectOfType('Promise');
const hasPromiseAPI = (value: any) => const hasPromiseAPI = (value: any) =>
!is.null_(value) && !null_(value) &&
isObject(value) && isObject(value) &&
is.function_(value.then) && function_(value.then) &&
is.function_(value.catch); function_(value.catch);
export const promise = (value: any) => is.nativePromise(value) || hasPromiseAPI(value); export const promise = (value: any) => nativePromise(value) || hasPromiseAPI(value);
export const generator = (value: any) => is.iterable(value) && is.function_(value.next) && is.function_(value.throw);
// TODO: Change to use `isObjectOfType` once Node.js 6 or higher is targeted // TODO: Change to use `isObjectOfType` once Node.js 6 or higher is targeted
const isFunctionOfType = (type: string) => (value: any) => is.function_(value) && is.function_(value.constructor) && value.constructor.name === type; const isFunctionOfType = (type: string) => (value: any) => function_(value) && function_(value.constructor) && value.constructor.name === type;
export const generatorFunction = isFunctionOfType('GeneratorFunction'); export const generatorFunction = isFunctionOfType('GeneratorFunction');
export const asyncFunction = isFunctionOfType('AsyncFunction'); export const asyncFunction = isFunctionOfType('AsyncFunction');
@ -116,8 +121,6 @@ namespace is {
export const nan = (value: any) => Number.isNaN(value); export const nan = (value: any) => Number.isNaN(value);
export const nullOrUndefined = (value: any) => is.null_(value) || is.undefined(value);
const primitiveTypes = new Set([ const primitiveTypes = new Set([
'undefined', 'undefined',
'string', 'string',
@ -126,7 +129,7 @@ namespace is {
'symbol' 'symbol'
]); ]);
export const primitive = (value: any) => is.null_(value) || primitiveTypes.has(typeof value); export const primitive = (value: any) => null_(value) || primitiveTypes.has(typeof value);
export const integer = (value: any) => Number.isInteger(value); export const integer = (value: any) => Number.isInteger(value);
export const safeInteger = (value: any) => Number.isSafeInteger(value); export const safeInteger = (value: any) => Number.isSafeInteger(value);
@ -134,15 +137,12 @@ namespace is {
export const plainObject = (value: any) => { export const plainObject = (value: any) => {
// From: https://github.com/sindresorhus/is-plain-obj/blob/master/index.js // From: https://github.com/sindresorhus/is-plain-obj/blob/master/index.js
let prototype; let prototype;
return getObjectType(value) === 'Object' && return getObjectType(value) === 'Object' &&
(prototype = Object.getPrototypeOf(value), prototype === null || (prototype = Object.getPrototypeOf(value), prototype === null || // tslint:disable-line:ban-comma-operator
prototype === Object.getPrototypeOf({})); prototype === Object.getPrototypeOf({}));
}; };
export const iterable = (value: any) => !is.nullOrUndefined(value) && is.function_(value[Symbol.iterator]);
export const class_ = (value: any) => is.function_(value) && value.toString().startsWith('class ');
const typedArrayTypes = new Set([ const typedArrayTypes = new Set([
'Int8Array', 'Int8Array',
'Uint8Array', 'Uint8Array',
@ -156,15 +156,15 @@ namespace is {
]); ]);
export const typedArray = (value: any) => typedArrayTypes.has(getObjectType(value)); export const typedArray = (value: any) => typedArrayTypes.has(getObjectType(value));
const isValidLength = (value: any) => is.safeInteger(value) && value > -1; const isValidLength = (value: any) => safeInteger(value) && value > -1;
export const arrayLike = (value: any) => !is.nullOrUndefined(value) && !is.function_(value) && isValidLength(value.length); export const arrayLike = (value: any) => !nullOrUndefined(value) && !function_(value) && isValidLength(value.length);
export const inRange = (value: number, range: number | number[]) => { export const inRange = (value: number, range: number | number[]) => {
if (is.number(range)) { if (number(range)) {
return value >= Math.min(0, range as number) && value <= Math.max(range as number, 0); return value >= Math.min(0, range as number) && value <= Math.max(range as number, 0);
} }
if (is.array(range) && range.length === 2) { if (array(range) && range.length === 2) {
return value >= Math.min(...range) && value <= Math.max(...range); return value >= Math.min(...range) && value <= Math.max(...range);
} }
@ -180,51 +180,52 @@ namespace is {
'nodeValue' 'nodeValue'
]; ];
export const domElement = (value: any) => is.object(value) && value.nodeType === NODE_TYPE_ELEMENT && is.string(value.nodeName) && export const domElement = (value: any) => object(value) && value.nodeType === NODE_TYPE_ELEMENT && string(value.nodeName) &&
!is.plainObject(value) && DOM_PROPERTIES_TO_CHECK.every(property => property in value); !plainObject(value) && DOM_PROPERTIES_TO_CHECK.every(property => property in value);
export const infinite = (value: any) => value === Infinity || value === -Infinity; export const infinite = (value: any) => value === Infinity || value === -Infinity;
const isAbsoluteMod2 = (value: number) => (rem: number) => is.integer(rem) && Math.abs(rem % 2) === value; const isAbsoluteMod2 = (value: number) => (rem: number) => integer(rem) && Math.abs(rem % 2) === value;
export const even = isAbsoluteMod2(0); export const even = isAbsoluteMod2(0);
export const odd = isAbsoluteMod2(1); export const odd = isAbsoluteMod2(1);
const isWhiteSpaceString = (value: any) => is.string(value) && /\S/.test(value) === false; const isWhiteSpaceString = (value: any) => string(value) && /\S/.test(value) === false;
const isEmptyStringOrArray = (value: any) => (is.string(value) || is.array(value)) && value.length === 0; const isEmptyStringOrArray = (value: any) => (string(value) || array(value)) && value.length === 0;
const isEmptyObject = (value: any) => !is.map(value) && !is.set(value) && is.object(value) && Object.keys(value).length === 0; const isEmptyObject = (value: any) => !map(value) && !set(value) && object(value) && Object.keys(value).length === 0;
const isEmptyMapOrSet = (value: any) => (is.map(value) || is.set(value)) && value.size === 0; const isEmptyMapOrSet = (value: any) => (map(value) || set(value)) && value.size === 0;
export const empty = (value: any) => is.falsy(value) || isEmptyStringOrArray(value) || isEmptyObject(value) || isEmptyMapOrSet(value); export const empty = (value: any) => falsy(value) || isEmptyStringOrArray(value) || isEmptyObject(value) || isEmptyMapOrSet(value);
export const emptyOrWhitespace = (value: any) => is.empty(value) || isWhiteSpaceString(value); export const emptyOrWhitespace = (value: any) => empty(value) || isWhiteSpaceString(value);
type ArrayMethod = (fn: (value: any, index: number, arr: any[]) => boolean, thisArg?: any) => boolean type ArrayMethod = (fn: (value: any, index: number, arr: any[]) => boolean, thisArg?: any) => boolean;
const predicateOnArray = (method: ArrayMethod, predicate: any, values: any[]) => { const predicateOnArray = (method: ArrayMethod, predicate: any, values: any[]) => {
if (is.function_(predicate) === false) { if (function_(predicate) === false) {
throw new TypeError(`Invalid predicate: ${util.inspect(predicate)}`); throw new TypeError(`Invalid predicate: ${util.inspect(predicate)}`);
} }
if (values.length === 0) { if (values.length === 0) {
throw new TypeError(`Invalid number of values`); throw new TypeError('Invalid number of values');
} }
return method.call(values, predicate); return method.call(values, predicate);
}; };
export const any = function (predicate: any, ...values: any[]) { // tslint:disable-next-line:variable-name
export const any = (predicate: any, ...values: any[]) => {
return predicateOnArray(Array.prototype.some, predicate, values); return predicateOnArray(Array.prototype.some, predicate, values);
}; };
export const all = function (predicate: any, ...values: any[]) { export const all = (predicate: any, ...values: any[]) => {
return predicateOnArray(Array.prototype.every, predicate, values); return predicateOnArray(Array.prototype.every, predicate, values);
}; };
} }
// Some few keywords are reserved, but we'll populate them for the node-folks // Some few keywords are reserved, but we'll populate them for the node-folks
// see https://github.com/Microsoft/TypeScript/issues/2536 // See https://github.com/Microsoft/TypeScript/issues/2536
Object.defineProperties(is, { Object.defineProperties(is, {
"class": { value: is.class_ }, class: {value: is.class_},
"function": { value: is.function_ }, function: {value: is.function_},
"null": { value: is.null_ } null: {value: is.null_}
}); });
export default is export default is; // tslint:disable-line:no-default-export

View file

@ -1,17 +1,18 @@
import * as util from 'util'; import * as util from 'util';
import test, {TestContext, Context} from 'ava'; import test, {TestContext, Context} from 'ava';
import * as jsdom from 'jsdom'; import * as jsdom from 'jsdom';
import m from '../src'; import m from '../src'; // tslint:disable-line:import-name
const isNode8orHigher = Number(process.versions.node.split('.')[0]) >= 8; const isNode8orHigher = Number(process.versions.node.split('.')[0]) >= 8;
class PromiseSubclassFixture<T> extends Promise<T> {}; /* Currently out of order, see https://github.com/Microsoft/TypeScript/issues/15202
class ErrorSubclassFixture extends Error {}; class PromiseSubclassFixture<T> extends Promise<T> {}*/
class ErrorSubclassFixture extends Error {}
const document = new jsdom.JSDOM().window.document const document = new jsdom.JSDOM().window.document;
const createDomElement = (el: string) => document.createElement(el); const createDomElement = (el: string) => document.createElement(el);
type Test = { is: (value: any) => boolean, fixtures: any[] } interface Test {is(value: any): boolean; fixtures: any[]; }
const types = new Map<string, Test>([ const types = new Map<string, Test>([
['undefined', ['undefined',
{is: m.undefined, fixtures: [undefined]} {is: m.undefined, fixtures: [undefined]}
@ -37,14 +38,16 @@ const types = new Map<string, Test>([
], ['symbol', ], ['symbol',
{is: m.symbol, fixtures: [Symbol('🦄')]} {is: m.symbol, fixtures: [Symbol('🦄')]}
], ['array', ], ['array',
{ is: m.array, fixtures: [[1, 2], new Array(2)] } {is: m.array, fixtures: [[1, 2], new Array(2)]} // tslint:disable-line:prefer-array-literal
], ['function', ], ['function',
{is: m.function_, fixtures: [ {is: m.function_, fixtures: [
function foo() {}, // eslint-disable-line func-names // tslint:disable:no-empty no-unused-variable only-arrow-functions no-function-expression
function foo() {}, // tslint:disable-line:no-unused
function() {}, function() {},
() => {}, () => {},
async function() {}, async function() {},
function *(): any {} function *(): any {}
// tslint:enable:no-empty no-unused-variable only-arrow-functions no-function-expression
]} ]}
], ['buffer', ], ['buffer',
{is: m.buffer, fixtures: [Buffer.from('🦄')]} {is: m.buffer, fixtures: [Buffer.from('🦄')]}
@ -68,24 +71,22 @@ const types = new Map<string, Test>([
], ['nativePromise', ], ['nativePromise',
{is: m.nativePromise, fixtures: [ {is: m.nativePromise, fixtures: [
Promise.resolve(), Promise.resolve(),
// currently out of order, see https://github.com/Microsoft/TypeScript/issues/15202
// PromiseSubclassFixture.resolve() // PromiseSubclassFixture.resolve()
]} ]}
], ['promise', ], ['promise',
{ is: m.promise, fixtures: [{then() {}, catch() {}}] } {is: m.promise, fixtures: [{then() {}, catch() {}}]} // tslint:disable-line:no-empty
], ['generator', ], ['generator',
{ is: m.generator, fixtures: [(function * () { {is: m.generator, fixtures: [
yield 4; (function *() { yield 4; })() // tslint:disable-line
})()] } ]}
], ['generatorFunction', ], ['generatorFunction',
{ is: m.generatorFunction, fixtures: [function * () { {is: m.generatorFunction, fixtures: [
yield 4; function *() { yield 4; } // tslint:disable-line
}] } ]}
], ['asyncFunction', ], ['asyncFunction',
{is: m.asyncFunction, fixtures: [ {is: m.asyncFunction, fixtures: [
async function () {}, async function() {}, // tslint:disable-line:no-empty only-arrow-functions no-function-expression
async () => {} async () => {} // tslint:disable-line:no-empty
]} ]}
], ['map', ], ['map',
{is: m.map, fixtures: [new Map()]} {is: m.map, fixtures: [new Map()]}
@ -123,7 +124,7 @@ const types = new Map<string, Test>([
{is: m.plainObject, fixtures: [ {is: m.plainObject, fixtures: [
{x: 1}, {x: 1},
Object.create(null), Object.create(null),
new Object() // eslint-disable-line no-new-object new Object()
]} ]}
], ['integer', ], ['integer',
{is: m.integer, fixtures: [6]} {is: m.integer, fixtures: [6]}
@ -147,7 +148,7 @@ const types = new Map<string, Test>([
document.createProcessingInstruction('xml-stylesheet', 'href="mycss.css" type="text/css"'), document.createProcessingInstruction('xml-stylesheet', 'href="mycss.css" type="text/css"'),
document.createComment('This is a comment'), document.createComment('This is a comment'),
document, document,
document.implementation.createDocumentType('svg:svg', '-//W3C//DTD SVG 1.1//EN', 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'), 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() document.createDocumentFragment()
]} ]}
], ['infinite', ], ['infinite',
@ -155,15 +156,18 @@ const types = new Map<string, Test>([
] ]
]); ]);
// This ensures a certain method matches only the types /* This ensures a certain method matches only the types
// it's supposed to and none of the other methods' types it's supposed to and none of the other methods' types */
const testType = (t: TestContext & Context<any>, type: string, exclude?: Array<string>) => { const testType = (t: TestContext & Context<any>, type: string, exclude?: string[]) => {
const test = types.get(type) const testData = types.get(type);
if (test === undefined) {
t.fail(`is.${type} not defined`) if (testData === undefined) {
return t.fail(`is.${type} not defined`);
return;
} }
const { is } = test as Test
const {is} = testData;
types.forEach(({fixtures}, key) => { types.forEach(({fixtures}, key) => {
// TODO: Automatically exclude value types in other tests that we have in the current one. // TODO: Automatically exclude value types in other tests that we have in the current one.
@ -177,7 +181,7 @@ const testType = (t: TestContext & Context<any>, type: string, exclude?: Array<s
for (const fixture of fixtures) { for (const fixture of fixtures) {
assert(is(fixture), `Value: ${util.inspect(fixture)}`); assert(is(fixture), `Value: ${util.inspect(fixture)}`);
} }
}) });
}; };
test('is', t => { test('is', t => {
@ -224,9 +228,15 @@ test('is.buffer', t => {
}); });
test('is.object', t => { test('is.object', t => {
const testData = types.get('object') const testData = types.get('object');
if (testData === undefined) t.fail("is.object not defined")
for (const el of (testData as Test).fixtures) { if (testData === undefined) {
t.fail('is.object not defined');
return;
}
for (const el of testData.fixtures) {
t.true(m.object(el)); t.true(m.object(el));
} }
}); });
@ -381,7 +391,6 @@ test('is.safeInteger', t => {
t.false(m.safeInteger(-Math.pow(2, 53))); t.false(m.safeInteger(-Math.pow(2, 53)));
}); });
test('is.plainObject', t => { test('is.plainObject', t => {
testType(t, 'plainObject', ['object', 'promise']); testType(t, 'plainObject', ['object', 'promise']);
}); });
@ -399,10 +408,10 @@ test('is.iterable', t => {
}); });
test('is.class', t => { test('is.class', t => {
class Foo {} class Foo {} // tslint:disable-line
const classDeclarations = [ const classDeclarations = [
Foo, Foo,
class Bar extends Foo {} class Bar extends Foo {} // tslint:disable-line
]; ];
for (const x of classDeclarations) { for (const x of classDeclarations) {
@ -412,7 +421,7 @@ test('is.class', t => {
test('is.typedArray', t => { test('is.typedArray', t => {
// Typescript currently does not support empty constructors for these // Typescript currently does not support empty constructors for these
// see https://github.com/Microsoft/TypeScript/issues/19680 // See https://github.com/Microsoft/TypeScript/issues/19680
const typedArrays = [ const typedArrays = [
new Int8Array(0), new Int8Array(0),
new Uint8Array(0), new Uint8Array(0),
@ -441,7 +450,7 @@ test('is.arrayLike', t => {
t.true(m.arrayLike('unicorn')); t.true(m.arrayLike('unicorn'));
t.false(m.arrayLike({})); t.false(m.arrayLike({}));
t.false(m.arrayLike(() => {})); t.false(m.arrayLike(() => {})); // tslint:disable-line:no-empty
t.false(m.arrayLike(new Map())); t.false(m.arrayLike(new Map()));
}); });