Skip to content

Instantly share code, notes, and snippets.

View andrewborisov's full-sized avatar

Andrei Borisov andrewborisov

View GitHub Profile
@andrewborisov
andrewborisov / reverseMonthLoop
Last active August 26, 2018 20:14
infinite reverse month loop, which returns array of objects started from provided date minus one month
const months = ['Янв', 'Фев', 'Мар', 'Апр', 'Май', 'Июнь', 'Июль', 'Авг', 'Сент', 'Окт', 'Ноя', 'Дек'];
const reverseMonthLoop = (date, iterations) => {
let titles = [];
const [d, m, y] = date.split('.');
const reversedMonths = memoizedCalculateMonth(m, iterations);
reversedMonths.map(month => {
titles.push({
month: months[month - 1],
@andrewborisov
andrewborisov / moduleImportsOrderRule.ts
Last active May 11, 2019 09:41
creating-ts-lint-rule project
import * as Lint from 'tslint';
import * as ts from 'typescript';
// Exported class always should be named "Rule" and extends Lint.Rules.AbstractRule
export class Rule extends Lint.Rules.AbstractRule {
public static FAILURE_STRING = 'Wrong import order';
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithWalker(new Walker(sourceFile, 'module-imports-order', this.getOptions()))
}
@andrewborisov
andrewborisov / package.json
Last active May 14, 2019 09:55
creating-ts-lint-rule project
{
"name": "creating-custom-ts-rule",
"version": "1.0.0",
"description": "Creating custom ts rule from scratch",
"main": "index.js",
"scripts": {
"buildRule": "tsc custom-ts-rules/moduleImportsOrderRule.ts --lib es6"
},
"author": "Andrei Borisov",
"license": "ISC",
@andrewborisov
andrewborisov / tslint.json
Last active May 14, 2019 09:54
creating-ts-lint-rule project
{
"extends": "tslint:recommended",
"rulesDirectory": ["custom-ts-rules"],
"linterOption": {
"exclude": [
"src/*.js",
"src/*.js.map"
]
},
"rules": {
@andrewborisov
andrewborisov / tsutils-utils-list.ts
Created May 11, 2019 11:17
creating-ts-lint-rule project
import * as ts from 'typescript';
export declare function isAccessorDeclaration(node: ts.Node): node is ts.AccessorDeclaration;
export declare function isArrayBindingPattern(node: ts.Node): node is ts.ArrayBindingPattern;
export declare function isArrayLiteralExpression(node: ts.Node): node is ts.ArrayLiteralExpression;
export declare function isArrayTypeNode(node: ts.Node): node is ts.ArrayTypeNode;
export declare function isArrowFunction(node: ts.Node): node is ts.ArrowFunction;
export declare function isAsExpression(node: ts.Node): node is ts.AsExpression;
export declare function isAssertionExpression(node: ts.Node): node is ts.AssertionExpression;
export declare function isAwaitExpression(node: ts.Node): node is ts.AwaitExpression;
export declare function isBinaryExpression(node: ts.Node): node is ts.BinaryExpression;
@andrewborisov
andrewborisov / lint-helper.ts
Last active May 14, 2019 09:51
creating-ts-lint-rule project
import { Linter, Configuration } from 'tslint';
import * as TSLintConfig from '../tslint.json';
// This utility helps to test ts-lint custom rules
export const lintHelper = ({ sourceFile, ruleName }: { sourceFile: string, ruleName: string }) => {
const lint = new Linter( { fix: false });
const getRuleOptions = TSLintConfig.rules[ruleName];
lint.lint('', sourceFile, Configuration.parseConfigFile({
rules: {
@andrewborisov
andrewborisov / moduleImportsOrderRule.test.ts
Created May 14, 2019 09:59
creating-ts-lint-rule project
import { lintHelper, getErrorLine } from './lint-helper';
const ruleName = 'module-imports-order';
describe('module imports order rule', () => {
it('should not fail with wrong import order', () => {
const sourceFile = `
import * as React from 'react';git push -u origin master
import { SomeGlobalModule } from '@/utils/some-util';
import { SomeGlobalModule } from '@/modules/some-global-module1';
const isObjectRecursivelyEmpty = (object) => {
if (keys(object).lengh === 0) {
return true;
}
const objKeys = keys(object);
const result = objKeys.map((i) => {
const currentValue = object[i];
if (currentValue === '') {
return true;
}
const findRecursivelyValuesQuantity = (object) => {
if (keys(object).lenght === 0) {
return 0;
}
const objKeys = keys(object);
const result = objKeys.map((i) => {
const currentValue = object[i];
if (isFunction(currentValue) || (isArray(currentValue) && !isEmpty(currentValue)) || isNumber(currentValue)
|| (!isObject(currentValue) && !isEmpty(currentValue))) {
return 1;
const CompareDatesEnumType = 'equal' | 'first' | 'invalid' | 'second';
const CompareDatesEnum = {
equal: 'equal',
first: 'first',
invalid: 'invalid',
second: 'second',
};
const compareDates = (firstDate: string = '01.01.1970', secondDate: string = '01.01.1970'): CompareDatesEnumType => {
const firstDateObject = new Date(dmyToMdyFormatter(firstDate));