diff --git a/.gitignore b/.gitignore index b1e3d2a..bca2098 100644 --- a/.gitignore +++ b/.gitignore @@ -10,4 +10,7 @@ src/**/*.d.ts .vscode coverage package-lock.json -yarn.lock \ No newline at end of file +yarn.lock +.npmrc +.nvmrc +.yvmrc diff --git a/.travis.yml b/.travis.yml index 79141b6..ac4cf3b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,8 +9,13 @@ services: - xvfb cache: - yarn: true + npm: true directories: - node_modules +script: + - npm run lint + - npm run build + - npm run test + after_success: npm run cover diff --git a/CHANGELOG.md b/CHANGELOG.md index c8bc704..1882ed9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# 10.1.0 + +- Add linting [#98](https://github.com/fknop/angular-pipes/pull/98) + # 10.0.0 - Added a module for each pipe [#97](https://github.com/fknop/angular-pipes/pull/97) diff --git a/package.json b/package.json index b4e211b..6f84e91 100644 --- a/package.json +++ b/package.json @@ -1,15 +1,17 @@ { + "$schema": "./node_modules/ng-packagr/package.schema.json", "name": "angular-pipes", - "version": "10.0.0", + "version": "10.1.0", "description": "Angular pipes library", "scripts": { "cover": "cat ./coverage/*/lcovonly | ./node_modules/.bin/coveralls", "karma": "node_modules/.bin/karma start karma.conf.js", - "pretty": "prettier --check '**/*.{ts,scss,js,json,yml,md,html}'", - "pretty:write": "yarn pretty --write", - "packagr": "ng-packagr -p package.json", + "build": "ng-packagr -p package.json", "publish-dist": "npm publish dist", - "test": "npm run karma" + "test": "yarn karma", + "lint": "run-p lint:*", + "lint:ts": "tslint --project tsconfig.json --config tslint.json --format stylish", + "lint:pretty": "prettier --check '**/*.{ts,scss,js,json,yml,md,html}'" }, "author": "Florian Knop", "repository": { @@ -27,16 +29,18 @@ "library" ], "license": "MIT", + "dependencies": {}, "devDependencies": { - "@angular/animations": "^8.1.3", - "@angular/common": "^8.1.3", - "@angular/compiler": "^8.1.3", - "@angular/compiler-cli": "^8.1.3", - "@angular/core": "^8.1.3", - "@angular/platform-browser": "^8.1.3", - "@angular/platform-browser-dynamic": "^8.1.3", - "@angular/platform-server": "^8.1.3", - "@types/jasmine": "^3.3.16", + "@angular/animations": "^8.2.10", + "@angular/common": "^8.2.10", + "@angular/compiler": "^8.2.10", + "@angular/compiler-cli": "^8.2.10", + "@angular/core": "^8.2.10", + "@angular/platform-browser": "^8.2.10", + "@angular/platform-browser-dynamic": "^8.2.10", + "@angular/platform-server": "^8.2.10", + "@types/jasmine": "^3.4.0", + "codelyzer": "^5.1.2", "core-js": "^3.1.4", "coveralls": "^3.0.5", "jasmine-core": "^3.4.0", @@ -45,22 +49,24 @@ "karma-jasmine": "^2.0.1", "karma-spec-reporter": "0.0.32", "karma-typescript": "^4.1.1", - "ng-packagr": "^5.4.3", + "ng-packagr": "^5.6.1", + "npm-run-all": "^4.1.5", "prettier": "^1.18.2", "reflect-metadata": "^0.1.13", "rxjs": "^6.5.2", - "tsickle": "^0.36.0", + "tsickle": "^0.37.0", "tslib": "^1.10.0", + "tslint": "^5.20.0", + "tslint-microsoft-contrib": "^6.2.0", + "tslint-sonarts": "^1.9.0", "typescript": "3.5.3", "zone.js": "~0.9.1" }, - "dependencies": {}, "prettier": { "printWidth": 120, "singleQuote": true, "trailingComma": "es5" }, - "$schema": "./node_modules/ng-packagr/package.schema.json", "ngPackage": { "lib": { "entryFile": "src/public_api.ts" diff --git a/src/aggregate/group-by.pipe.ts b/src/aggregate/group-by.pipe.ts index 40812da..6af4cb8 100644 --- a/src/aggregate/group-by.pipe.ts +++ b/src/aggregate/group-by.pipe.ts @@ -5,12 +5,12 @@ import { getProperty, isArray, isUndefined } from '../utils/utils'; name: 'groupBy', }) export class GroupByPipe implements PipeTransform { - transform(input: any, prop: string): Array { + transform(input: any, prop: string): any[] { if (!isArray(input)) { return input; } - const arr: { [key: string]: Array } = {}; + const arr: { [key: string]: any[] } = {}; for (const value of input) { const field: any = getProperty(value, prop); diff --git a/src/array/chunk.pipe.ts b/src/array/chunk.pipe.ts index 99e1e66..2cd367f 100644 --- a/src/array/chunk.pipe.ts +++ b/src/array/chunk.pipe.ts @@ -5,7 +5,7 @@ import { isArray } from '../utils/utils'; name: 'chunk', }) export class ChunkPipe implements PipeTransform { - transform(input: any, size: number = 1): any { + transform(input: any, size = 1): any { if (!isArray(input)) { return input; } diff --git a/src/array/first-or-default.pipe.ts b/src/array/first-or-default.pipe.ts index 11106ad..3cdd9d2 100644 --- a/src/array/first-or-default.pipe.ts +++ b/src/array/first-or-default.pipe.ts @@ -18,7 +18,7 @@ export class FirstOrDefaultPipe implements PipeTransform { result = input[i]; } - if (typeof result === 'undefined' && typeof defaultValue !== 'undefined') { + if (result == undefined && defaultValue != undefined) { result = defaultValue; } @@ -31,15 +31,17 @@ export class FirstOrDefaultPipe implements PipeTransform { } if (isFunction(predicate)) { - return FirstOrDefaultPipe.find(input, predicate, defaultValue); - } else if (isArray(predicate)) { - const [key, value] = predicate; + return FirstOrDefaultPipe.find(input, predicate as CollectionPredicate, defaultValue); + } + if (isArray(predicate)) { + const [key, value] = predicate as string[]; return FirstOrDefaultPipe.find(input, (item: any) => getProperty(item, key) === value, defaultValue); - } else if (predicate) { - return FirstOrDefaultPipe.find(input, item => item === predicate, defaultValue); - } else { - return input; } + if (predicate) { + return FirstOrDefaultPipe.find(input, item => item === predicate, defaultValue); + } + + return input; } } diff --git a/src/array/join.pipe.ts b/src/array/join.pipe.ts index 0b761b1..d040bd8 100644 --- a/src/array/join.pipe.ts +++ b/src/array/join.pipe.ts @@ -5,7 +5,7 @@ import { isArray } from '../utils/utils'; name: 'join', }) export class JoinPipe implements PipeTransform { - transform(input: any, character: string = ''): any { + transform(input: any, character = ''): any { if (!isArray(input)) { return input; } diff --git a/src/array/order-by.pipe.ts b/src/array/order-by.pipe.ts index 835be93..cf70ae4 100644 --- a/src/array/order-by.pipe.ts +++ b/src/array/order-by.pipe.ts @@ -1,3 +1,4 @@ +// tslint:disable:cognitive-complexity import { Pipe, PipeTransform, NgModule } from '@angular/core'; import { isArray } from '../utils/utils'; @@ -41,35 +42,35 @@ export class OrderByPipe implements PipeTransform { const comparator = OrderByPipe._orderBy(a, b); return desc ? -comparator : comparator; }); - } else { - // If contains + or -, substring the property - const property = first === '+' || desc ? propertyToCheck.substr(1) : propertyToCheck; - - return [...input].sort((a: any, b: any) => { - const comparator = OrderByPipe._orderBy(a[property], b[property]); - return desc ? -comparator : comparator; - }); } - } else { - // Config is an array of property + + // If contains + or -, substring the property + const property = first === '+' || desc ? propertyToCheck.substr(1) : propertyToCheck; return [...input].sort((a: any, b: any) => { - for (let i: number = 0; i < config.length; ++i) { - const first = config[i].substr(0, 1); - const desc = first === '-'; - const property = first === '+' || desc ? config[i].substr(1) : config[i]; + const comparator = OrderByPipe._orderBy(a[property], b[property]); + return desc ? -comparator : comparator; + }); + } + + // Config is an array of property + + return [...input].sort((a: any, b: any) => { + for (const conf of config) { + const first = conf.substr(0, 1); + const desc = first === '-'; + const property = first === '+' || desc ? conf.substr(1) : conf; - const comparator = OrderByPipe._orderBy(a[property], b[property]); - const comparison = desc ? -comparator : comparator; + const comparator = OrderByPipe._orderBy(a[property], b[property]); + const comparison = desc ? -comparator : comparator; - if (comparison !== 0) { - return comparison; - } + if (comparison !== 0) { + return comparison; } + } - return 0; - }); - } + return 0; + }); } } diff --git a/src/array/range.pipe.ts b/src/array/range.pipe.ts index 7a7ce5c..b3f4e20 100644 --- a/src/array/range.pipe.ts +++ b/src/array/range.pipe.ts @@ -4,11 +4,12 @@ import { Pipe, PipeTransform, NgModule } from '@angular/core'; name: 'range', }) export class RangePipe implements PipeTransform { - transform(_input: any, size: number = 0, start: number = 1, step: number = 1): any { + transform(_input: any, size = 0, start = 1, step = 1): any { const range: number[] = []; + let _start = start; for (let length = 0; length < size; ++length) { - range.push(start); - start += step; + range.push(_start); + _start += step; } return range; diff --git a/src/array/where.pipe.ts b/src/array/where.pipe.ts index 66c2c8c..169ba63 100644 --- a/src/array/where.pipe.ts +++ b/src/array/where.pipe.ts @@ -15,14 +15,16 @@ export class WherePipe implements PipeTransform { if (isFunction(fn)) { return input.filter(fn); - } else if (isArray(fn)) { + } + if (isArray(fn)) { const [key, value] = fn; return input.filter((item: any) => getProperty(item, key) === value); - } else if (fn) { + } + if (fn) { return input.filter((item: any) => item === fn); - } else { - return input; } + + return input; } } diff --git a/src/math/bytes.pipe.ts b/src/math/bytes.pipe.ts index f1dafbf..80aa414 100644 --- a/src/math/bytes.pipe.ts +++ b/src/math/bytes.pipe.ts @@ -16,7 +16,16 @@ export class BytesPipe implements PipeTransform { TB: { max: Number.MAX_SAFE_INTEGER, prev: 'GB' }, }; - transform(input: any, decimal: number = 0, from: ByteUnit = 'B', to?: ByteUnit): any { + static formatResult(result: number, unit: string): string { + return `${result} ${unit}`; + } + + static calculateResult(format: { max: number; prev?: ByteUnit }, bytes: number) { + const prev = format.prev ? BytesPipe.formats[format.prev] : undefined; + return prev ? bytes / prev.max : bytes; + } + + transform(input: any, decimal = 0, from: ByteUnit = 'B', to?: ByteUnit): any { if (!(isNumberFinite(input) && isNumberFinite(decimal) && isInteger(decimal) && isPositive(decimal))) { return input; } @@ -25,7 +34,7 @@ export class BytesPipe implements PipeTransform { let unit = from; while (unit !== 'B') { bytes *= 1024; - unit = BytesPipe.formats[unit].prev!; + unit = BytesPipe.formats[unit].prev; } if (to) { @@ -47,15 +56,6 @@ export class BytesPipe implements PipeTransform { } } } - - static formatResult(result: number, unit: string): string { - return `${result} ${unit}`; - } - - static calculateResult(format: { max: number; prev?: ByteUnit }, bytes: number) { - const prev = format.prev ? BytesPipe.formats[format.prev] : undefined; - return prev ? bytes / prev.max : bytes; - } } @NgModule({ diff --git a/src/math/ceil.pipe.ts b/src/math/ceil.pipe.ts index 41038ae..ab2aff9 100644 --- a/src/math/ceil.pipe.ts +++ b/src/math/ceil.pipe.ts @@ -5,12 +5,13 @@ import { createRound, isString } from '../utils/utils'; name: 'ceil', }) export class CeilPipe implements PipeTransform { - transform(value: any, precision: any = 0): any { - if (isString(precision)) { - precision = parseInt(precision); + transform(value: any, precision: any = 0, radix = 10): any { + let _precision = precision; + if (isString(_precision)) { + _precision = parseInt(_precision, radix); } - return createRound('ceil')(value, precision); + return createRound('ceil')(value, _precision); } } diff --git a/src/math/floor.pipe.ts b/src/math/floor.pipe.ts index db4b9db..2dbe95d 100644 --- a/src/math/floor.pipe.ts +++ b/src/math/floor.pipe.ts @@ -5,12 +5,13 @@ import { createRound, isString } from '../utils/utils'; name: 'floor', }) export class FloorPipe implements PipeTransform { - transform(value: any, precision: any = 0): any { - if (isString(precision)) { - precision = parseInt(precision); + transform(value: any, precision: any = 0, radix = 10): any { + let _precision = precision; + if (isString(_precision)) { + _precision = parseInt(_precision, radix); } - return createRound('floor')(value, precision); + return createRound('floor')(value, _precision); } } diff --git a/src/math/ordinal.pipe.ts b/src/math/ordinal.pipe.ts index 6ea73ea..8127faa 100644 --- a/src/math/ordinal.pipe.ts +++ b/src/math/ordinal.pipe.ts @@ -11,20 +11,20 @@ export class OrdinalPipe implements PipeTransform { } if (this.endsWithTenth(input)) { - return input + 'th'; - } else { - const cardinal = input.toString().charAt(input.toString().length - 1); + return `${input}th`; + } + + const cardinal = input.toString().charAt(input.toString().length - 1); - switch (cardinal) { - case '1': - return input + 'st'; - case '2': - return input + 'nd'; - case '3': - return input + 'rd'; - default: - return input + 'th'; - } + switch (cardinal) { + case '1': + return `${input}st`; + case '2': + return `${input}nd`; + case '3': + return `${input}rd`; + default: + return `${input}th`; } } diff --git a/src/math/pow.pipe.ts b/src/math/pow.pipe.ts index ac207a1..8684c85 100644 --- a/src/math/pow.pipe.ts +++ b/src/math/pow.pipe.ts @@ -5,7 +5,7 @@ import { isNumberFinite } from '../utils/utils'; name: 'pow', }) export class PowPipe implements PipeTransform { - transform(input: any, power: number = 2): any { + transform(input: any, power = 2): any { if (!isNumberFinite(input)) { return 'NaN'; } diff --git a/src/math/random.pipe.ts b/src/math/random.pipe.ts index 196bfaa..e72cf06 100644 --- a/src/math/random.pipe.ts +++ b/src/math/random.pipe.ts @@ -5,17 +5,19 @@ import { isNumberFinite } from '../utils/utils'; name: 'random', }) export class RandomPipe implements PipeTransform { - transform(input: any, min: number = 0, max: number = 1): any { - if (!isNumberFinite(min) || !isNumberFinite(max)) { + transform(input: any, min = 0, max = 1): any { + let _min = min; + let _max = max; + if (!isNumberFinite(_min) || !isNumberFinite(_max)) { return input; } - if (min > max) { - max = min; - min = 0; + if (_min > _max) { + _max = _min; + _min = 0; } - return Math.random() * (max - min) + min; + return Math.random() * (_max - _min) + _min; } } diff --git a/src/math/round.pipe.ts b/src/math/round.pipe.ts index 6853c20..d7fa097 100644 --- a/src/math/round.pipe.ts +++ b/src/math/round.pipe.ts @@ -5,12 +5,13 @@ import { createRound, isString } from '../utils/utils'; name: 'round', }) export class RoundPipe implements PipeTransform { - transform(value: any, precision: any = 0): any { + transform(value: any, precision: any = 0, radix = 10): any { + let _precision = precision; if (isString(precision)) { - precision = parseInt(precision); + _precision = parseInt(precision, radix); } - return createRound('round')(value, precision); + return createRound('round')(value, _precision); } } diff --git a/src/object/defaults.pipe.ts b/src/object/defaults.pipe.ts index 40d5b41..a3a0369 100644 --- a/src/object/defaults.pipe.ts +++ b/src/object/defaults.pipe.ts @@ -15,7 +15,10 @@ export class DefaultsPipe implements PipeTransform { if (isArray(input)) { return input.map((item: any) => { if (isObject(item)) { - return Object.assign({}, defaults, item); + return { + ...defaults, + ...item, + }; } if (isNil(item)) { @@ -27,7 +30,10 @@ export class DefaultsPipe implements PipeTransform { } if (isObject(input)) { - return Object.assign({}, defaults, input); + return { + ...defaults, + ...input, + }; } return input; diff --git a/src/public_api.ts b/src/public_api.ts index b89885e..6f290d2 100644 --- a/src/public_api.ts +++ b/src/public_api.ts @@ -1,3 +1,4 @@ +// tslint:disable:file-name-casing export * from './pipes.module'; export * from './aggregate/aggregate.module'; diff --git a/src/string/capitalize.pipe.ts b/src/string/capitalize.pipe.ts index 65863b5..5b39982 100644 --- a/src/string/capitalize.pipe.ts +++ b/src/string/capitalize.pipe.ts @@ -5,20 +5,20 @@ import { isString, upperFirst } from '../utils/utils'; name: 'capitalize', }) export class CapitalizePipe implements PipeTransform { - transform(input: any, all: boolean = false): any { + transform(input: any, all = false): any { if (!isString(input)) { return input; } if (!all) { return upperFirst(input.toLowerCase()); - } else { - return input - .toLowerCase() - .split(' ') - .map((value: string) => upperFirst(value)) - .join(' '); } + + return input + .toLowerCase() + .split(' ') + .map(upperFirst) + .join(' '); } } diff --git a/src/string/latinize.pipe.ts b/src/string/latinize.pipe.ts index a6fd778..0c1ee54 100644 --- a/src/string/latinize.pipe.ts +++ b/src/string/latinize.pipe.ts @@ -10,7 +10,7 @@ export class LatinizePipe implements PipeTransform { return input; } - let replacementList = [ + const replacementList = [ { base: ' ', chars: '\u00A0' }, { base: '0', chars: '\u07C0' }, { @@ -186,11 +186,11 @@ export class LatinizePipe implements PipeTransform { { base: 'z', chars: '\u24E9\uFF5A\u017A\u1E91\u017C\u017E\u1E93\u1E95\u01B6\u0225\u0240\u2C6C\uA763' }, ]; - let diacriticsMap: any = {}; - for (let i = 0; i < replacementList.length; i += 1) { - let chars = replacementList[i].chars; - for (let j = 0; j < chars.length; j += 1) { - diacriticsMap[chars[j]] = replacementList[i].base; + const diacriticsMap: any = {}; + for (const replaceItem of replacementList) { + const chars = replaceItem.chars; + for (const char of chars) { + diacriticsMap[char] = replaceItem.base; } } diff --git a/src/string/left-pad.pipe.ts b/src/string/left-pad.pipe.ts index 569922c..7cbc0e4 100644 --- a/src/string/left-pad.pipe.ts +++ b/src/string/left-pad.pipe.ts @@ -5,7 +5,7 @@ import { leftPad, isString } from '../utils/utils'; name: 'leftpad', }) export class LeftPadPipe implements PipeTransform { - transform(input: any, length: number = 0, character: string = ' '): any { + transform(input: any, length = 0, character = ' '): any { if (!isString(input)) { return input; } diff --git a/src/string/pad.pipe.ts b/src/string/pad.pipe.ts index bea42a7..a412b6c 100644 --- a/src/string/pad.pipe.ts +++ b/src/string/pad.pipe.ts @@ -5,7 +5,7 @@ import { pad, isString } from '../utils/utils'; name: 'pad', }) export class PadPipe implements PipeTransform { - transform(input: any, length: number = 0, character: string = ' '): any { + transform(input: any, length = 0, character = ' '): any { if (!isString(input)) { return input; } diff --git a/src/string/repeat.pipe.ts b/src/string/repeat.pipe.ts index 850d727..7e0c32a 100644 --- a/src/string/repeat.pipe.ts +++ b/src/string/repeat.pipe.ts @@ -5,17 +5,18 @@ import { isString } from '../utils/utils'; name: 'repeat', }) export class RepeatPipe implements PipeTransform { - transform(input: any, times: number = 1, characters: string = ''): any { + transform(input: any, times = 1, characters = ''): any { + let _times = times; if (!isString(input)) { return input; } - if (times <= 0) { - times = 1; + if (_times <= 0) { + _times = 1; } const repeated = [input]; - for (let i = 1; i < times; ++i) { + for (let i = 1; i < _times; ++i) { repeated.push(input); } diff --git a/src/string/right-pad.pipe.ts b/src/string/right-pad.pipe.ts index 13fd26e..10427b5 100644 --- a/src/string/right-pad.pipe.ts +++ b/src/string/right-pad.pipe.ts @@ -5,7 +5,7 @@ import { rightPad, isString } from '../utils/utils'; name: 'rightpad', }) export class RightPadPipe implements PipeTransform { - transform(input: any, length: number = 0, character: string = ' '): any { + transform(input: any, length = 0, character = ' '): any { if (!isString(input)) { return input; } diff --git a/src/string/split.pipe.ts b/src/string/split.pipe.ts index 3b95af9..4465393 100644 --- a/src/string/split.pipe.ts +++ b/src/string/split.pipe.ts @@ -5,7 +5,7 @@ import { isString } from '../utils/utils'; name: 'split', }) export class SplitPipe implements PipeTransform { - transform(input: any, separator: string = ' ', limit?: number): any { + transform(input: any, separator = ' ', limit?: number): any { if (!isString(input)) { return input; } diff --git a/src/string/strip-tags.pipe.ts b/src/string/strip-tags.pipe.ts index 9d8f2fb..ced62a4 100644 --- a/src/string/strip-tags.pipe.ts +++ b/src/string/strip-tags.pipe.ts @@ -8,7 +8,9 @@ import { isString, isUndefined } from '../utils/utils'; }) export class StripTagsPipe implements PipeTransform { transform(input: string): any { - if (!isString(input) || isUndefined(input)) return input; + if (!isString(input) || isUndefined(input)) { + return input; + } return input.replace(/<\S[^><]*>/g, ''); } diff --git a/src/string/truncate.pipe.ts b/src/string/truncate.pipe.ts index 5c5d764..daf09f0 100644 --- a/src/string/truncate.pipe.ts +++ b/src/string/truncate.pipe.ts @@ -7,27 +7,21 @@ import { isString, isUndefined } from '../utils/utils'; name: 'truncate', }) export class TruncatePipe implements PipeTransform { - transform(input: any, length?: number, suffix?: string, preserve?: boolean): any { + transform(input: any, length?: number, suffix = '', preserve = false): any { if (!isString(input)) { return input; } - length = isUndefined(length) ? input.length : length; + const _length = isUndefined(length) ? input.length : length; - if (input.length <= length) { + if (input.length <= _length) { return input; } - preserve = preserve || false; - suffix = suffix || ''; - let index = length; + let index = _length; if (preserve) { - if (input.indexOf(' ', length) === -1) { - index = input.length; - } else { - index = input.indexOf(' ', length); - } + index = input.indexOf(' ', _length) === -1 ? input.length : input.indexOf(' ', _length); } return input.substring(0, index) + suffix; diff --git a/src/string/with.pipe.ts b/src/string/with.pipe.ts index 3717f81..08fa5c5 100644 --- a/src/string/with.pipe.ts +++ b/src/string/with.pipe.ts @@ -1,34 +1,34 @@ +// tslint:disable:cognitive-complexity import { Pipe, PipeTransform, NgModule } from '@angular/core'; import { isString, isNull } from '../utils/utils'; @Pipe({ name: 'with' }) export class WithPipe implements PipeTransform { - transform(input: string, start: string | null = null, ends: string | null = null, csensitive: boolean = false): any { + transform(input: string, start: string | null = null, ends: string | null = null, csensitive = false): any { if (!isString(input) || (isNull(start) && isNull(ends)) || start == '' || ends == '') { return input; } - input = csensitive ? input : input.toLowerCase(); + const _input = csensitive ? input : input.toLowerCase(); if (!isNull(start) && !isNull(ends)) { - let a: boolean = !input.indexOf(csensitive ? start : start.toLowerCase()); - let b: boolean = input.indexOf(csensitive ? ends : ends.toLowerCase(), input.length - ends.length) !== -1; + const a: boolean = !_input.indexOf(csensitive ? start : start.toLowerCase()); + const b: boolean = _input.indexOf(csensitive ? ends : ends.toLowerCase(), _input.length - ends.length) !== -1; - if (a == true && b == true) { + if (a && b) { return true; - } else { - return false; } + return false; } if (!isNull(start)) { - return !input.indexOf(csensitive ? start : start.toLowerCase()); + return !_input.indexOf(csensitive ? start : start.toLowerCase()); } if (!isNull(ends)) { - let position: any = input.length - ends.length; + const position: any = _input.length - ends.length; - return input.indexOf(csensitive ? ends : ends.toLowerCase(), position) !== -1; + return _input.indexOf(csensitive ? ends : ends.toLowerCase(), position) !== -1; } } } diff --git a/src/utils/utils.ts b/src/utils/utils.ts index fa3ccca..ef8c460 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -1,7 +1,7 @@ export type CollectionPredicate = (item?: any, index?: number, collection?: any[]) => boolean; export function isUndefined(value: any): value is undefined { - return typeof value === 'undefined'; + return value === undefined; } export function isNull(value: any): value is null { @@ -27,7 +27,7 @@ export function isInteger(value: number): boolean { } export function isNil(value: any): value is null | undefined { - return value === null || typeof value === 'undefined'; + return value === null || value == undefined; } export function isString(value: any): value is string { @@ -54,84 +54,83 @@ export function upperFirst(value: string): string { return value.slice(0, 1).toUpperCase() + value.slice(1); } +// tslint:disable:prefer-template restrict-plus-operands export function createRound(method: string): Function { // Math to suppress error - const func: any = (Math)[method]; - return function(value: number, precision: number = 0) { + const func: any = (Math as any)[method]; + return (value: number, precision = 0) => { + let _precision = precision; if (typeof value === 'string') { throw new TypeError('Rounding method needs a number'); } - if (typeof precision !== 'number' || isNaN(precision)) { - precision = 0; + if (typeof _precision !== 'number' || isNaN(_precision)) { + _precision = 0; } - if (precision) { + if (_precision) { let pair = `${value}e`.split('e'); - const val = func(`${pair[0]}e` + (+pair[1] + precision)); + const val = func(`${pair[0]}e` + (+pair[1] + _precision)); pair = `${val}e`.split('e'); - return +(pair[0] + 'e' + (+pair[1] - precision)); + return +(pair[0] + 'e' + (+pair[1] - _precision)); } return func(value); }; } +// tslint:disable:prefer-template restrict-plus-operands -export function leftPad(str: string, len: number = 0, ch: any = ' ') { - str = String(str); - ch = toString(ch); +export function leftPad(str: string, len = 0, ch: any = ' ') { + let _str = String(str); + const _ch = toString(ch); let i = -1; - const length = len - str.length; + const length = len - _str.length; - while (++i < length && str.length + ch.length <= len) { - str = ch + str; + while (++i < length && _str.length + _ch.length <= len) { + _str = _ch + _str; } - return str; + return _str; } -export function rightPad(str: string, len: number = 0, ch: any = ' ') { - str = String(str); - ch = toString(ch); +export function rightPad(str: string, len = 0, ch: any = ' ') { + let _str = String(str); + const _ch = toString(ch); let i = -1; - const length = len - str.length; + const length = len - _str.length; - while (++i < length && str.length + ch.length <= len) { - str += ch; + while (++i < length && _str.length + _ch.length <= len) { + _str += _ch; } - return str; + return _str; } export function toString(value: number | string) { return `${value}`; } -export function pad(str: string, len: number = 0, ch: any = ' '): string { - str = String(str); - ch = toString(ch); +export function pad(str: string, len = 0, ch: any = ' '): string { + let _str = String(str); + const _ch = toString(ch); let i = -1; - const length = len - str.length; + const length = len - _str.length; let left = true; while (++i < length) { - const l = str.length + ch.length <= len ? str.length + ch.length : str.length + 1; + const l = _str.length + _ch.length <= len ? _str.length + _ch.length : _str.length + 1; - if (left) { - str = leftPad(str, l, ch); - } else { - str = rightPad(str, l, ch); - } + _str = left ? leftPad(_str, l, _ch) : rightPad(_str, l, _ch); left = !left; } - return str; + return _str; } -export function flatten(input: any[], index: number = 0): any[] { +export function flatten(input: any[], index = 0): any[] { if (index >= input.length) { return input; } @@ -149,20 +148,20 @@ export function getProperty(value: { [key: string]: any }, key: string): any { } const keys: string[] = key.split('.'); - let result: any = value[keys.shift()!]; + let result: any = value[keys.shift()]; - for (const key of keys) { + for (const keyFor of keys) { if (isNil(result) || !isObject(result)) { return undefined; } - result = result[key]; + result = result[keyFor]; } return result; } -export function sum(input: Array, initial = 0): number { +export function sum(input: number[], initial = 0): number { return input.reduce((previous: number, current: number) => previous + current, initial); } @@ -214,10 +213,9 @@ export function deepEqual(a: any, b: any) { } // Test for A's keys different from B. - var hasOwn = Object.prototype.hasOwnProperty; - for (let i = 0; i < keysA.length; i++) { - const key = keysA[i]; - if (!hasOwn.call(b, keysA[i]) || !deepEqual(a[key], b[key])) { + const hasOwn = Object.prototype.hasOwnProperty; + for (const key of keysA) { + if (!hasOwn.call(b, key) || !deepEqual(a[key], b[key])) { return false; } } @@ -242,7 +240,7 @@ export function unwrapDeep(object: any) { } export class DeepWrapper { - public __isDeepObject__: boolean = true; + __isDeepObject__ = true; constructor(public data: any) {} } diff --git a/tsconfig.json b/tsconfig.json index bf86e67..4a42f0b 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,21 +1,28 @@ { + "compileOnSave": false, "compilerOptions": { - "target": "es5", - "module": "es6", + "baseUrl": "./", + "module": "esnext", "moduleResolution": "node", + "types": ["node"], + "typeRoots": ["node_modules/@types"], // don't look in parent folders + "lib": ["es2018", "dom", "dom.iterable", "esnext"], + "declaration": false, "sourceMap": true, + "downlevelIteration": true, // needed with ES5 target + "esModuleInterop": true, // https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-7.html#support-for-import-d-from-cjs-from-commonjs-modules-with---esmoduleinterop + "importHelpers": true, // https://github.com/Microsoft/TypeScript/wiki/What%27s-new-in-TypeScript#support-for-external-helpers-library-tslib "emitDecoratorMetadata": true, "experimentalDecorators": true, - "removeComments": false, - "noImplicitAny": true, + "allowSyntheticDefaultImports": true, "noUnusedLocals": true, "noUnusedParameters": true, - "suppressImplicitAnyIndexErrors": false, - "declaration": true, - "lib": ["es6", "dom"] + "target": "es6" }, "exclude": ["node_modules", "**/*.spec.ts"], "angularCompilerOptions": { + "fullTemplateTypeCheck": true, + "strictInjectionParameters": true, "skipTemplateCodegen": true } } diff --git a/tslint.json b/tslint.json new file mode 100644 index 0000000..8271890 --- /dev/null +++ b/tslint.json @@ -0,0 +1,151 @@ +{ + "rulesDirectory": ["tslint-microsoft-contrib", "codelyzer", "tslint-sonarts"], + "extends": ["tslint-sonarts"], + "rules": { + "component-class-suffix": true, + "component-selector": [true, "element", "app", "kebab-case"], + "contextual-decorator": true, + "contextual-lifecycle": true, + "directive-class-suffix": true, + "directive-selector": [true, "attribute", "app", "camelCase"], + "no-attribute-decorator": true, + "no-conflicting-lifecycle": true, + "no-inputs-metadata-property": true, + "no-output-native": true, + "no-output-on-prefix": true, + "no-outputs-metadata-property": true, + "no-pipe-impure": true, + "pipe-naming": ["camelCase", "app"], + "prefer-on-push-component-change-detection": true, + "relative-url-prefix": true, + "template-accessibility-alt-text": true, + "template-accessibility-elements-content": true, + "template-accessibility-label-for": true, + "template-accessibility-tabindex-no-positive": true, + "template-accessibility-table-scope": true, + "template-accessibility-valid-aria": true, + "template-banana-in-box": true, + "template-click-events-have-key-events": false, + "template-mouse-events-have-key-events": true, + "template-no-any": true, + "template-no-call-expression": true, + "template-no-distracting-elements": true, + "template-no-negated-async": true, + "template-use-track-by-function": true, + "use-component-view-encapsulation": true, + "use-lifecycle-interface": true, + "use-pipe-decorator": true, + "use-pipe-transform-interface": true, + + "adjacent-overload-signatures": true, + "array-type": [true, "array"], + "arrow-return-shorthand": true, + "await-promise": true, + "ban-comma-operator": true, + "ban-ts-ignore": true, + "callable-types": true, + "class-name": true, + "comment-format": [true, "check-space"], + "curly": true, + "deprecation": { + "severity": "warn" + }, + "file-name-casing": [true, "kebab-case"], + "forin": true, + "function-constructor": true, + "import-blacklist": [true, "rxjs/Rx", "rxjs/index", "rxjs/internal/operators"], + "interface-over-type-literal": true, + "label-position": true, + "member-access": [true, "no-public"], + "member-ordering": [ + true, + { + "order": ["static-field", "static-method", "instance-field", "constructor", "instance-method"] + } + ], + "no-angle-bracket-type-assertion": true, + "no-any": false, + "no-arg": true, + "no-async-without-await": true, + "no-bitwise": true, + "no-boolean-literal-compare": true, + "no-conditional-assignment": true, + "no-console": [true, "debug", "info", "time", "timeEnd", "trace"], + "no-construct": true, + "no-debugger": true, + "no-default-export": true, + "no-duplicate-imports": [ + true, + { + "allow-namespace-imports": true + } + ], + "no-duplicate-super": true, + "no-duplicate-switch-case": true, + "no-empty": false, + "no-empty-interface": true, + "no-eval": true, + "no-for-in-array": true, + "no-inferrable-types": true, + "no-inferred-empty-object-type": true, + "no-invalid-template-strings": true, + "no-invalid-this": true, + "no-misused-new": true, + "no-non-null-assertion": true, + "no-null-undefined-union": true, + "no-object-literal-type-assertion": true, + "no-parameter-reassignment": true, + "no-require-imports": true, + "no-relative-imports": false, + "no-restricted-globals": true, + "no-return-await": true, + "no-shadowed-variable": true, + "no-single-line-block-comment": true, + "no-sparse-arrays": true, + "no-string-literal": false, + "no-string-throw": true, + "no-suspicious-comment": true, + "no-switch-case-fall-through": true, + "no-tautology-expression": true, + "no-this-assignment": true, + "no-typeof-undefined": true, + "no-unnecessary-callback-wrapper": true, + "no-unnecessary-class": [true, "allow-empty-class"], + "no-unnecessary-field-initialization": true, + "no-unnecessary-initializer": true, + "no-unnecessary-qualifier": true, + "no-unnecessary-local-variable": true, + "no-unnecessary-type-assertion": true, + "no-unsafe-finally": true, + "no-unused-expression": true, + "no-var-keyword": true, + "no-var-requires": true, + "object-literal-shorthand": true, + "object-literal-sort-keys": false, + "one-variable-per-declaration": [true, "ignore-for-loop"], + "only-arrow-functions": [true, "allow-declarations", "allow-named-functions"], + "ordered-imports": false, + "prefer-conditional-expression": [true, "check-else-if"], + "prefer-const": true, + "prefer-for-of": true, + "prefer-method-signature": true, + "prefer-object-spread": true, + "prefer-readonly": true, + "prefer-switch": true, + "prefer-template": true, + "prefer-while": true, + "promise-function-async": true, + "radix": true, + "restrict-plus-operands": true, + "return-undefined": true, + "static-this": true, + "switch-final-break": [true, "always"], + "triple-equals": false, + "unified-signatures": true, + "unnecessary-bind": true, + "unnecessary-constructor": true, + "unnecessary-else": true, + "use-default-type-parameter": true, + "use-isnan": true + } +}