Skip to content
This repository has been archived by the owner on May 24, 2023. It is now read-only.

Commit

Permalink
style(*): Linted everything
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerkin committed Apr 6, 2018
1 parent 7654b36 commit d0c844c
Show file tree
Hide file tree
Showing 43 changed files with 2,663 additions and 2,629 deletions.
5 changes: 4 additions & 1 deletion diaspora.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@
{
"path": "."
}
]
],
"settings": {
"typescript.tsdk": "node_modules/typescript/lib"
}
}
7 changes: 0 additions & 7 deletions inch.json

This file was deleted.

12 changes: 2 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
},
"scripts": {
"lint": "tslint -t codeFrame 'src/**/*.ts' 'test/**/*.ts'",
"lint:fix": "tslint --fix -t codeFrame 'src/**/*.ts' 'test/**/*.ts'",
"lint:fix": "tslint --fix -t codeFrame 'src/**/*.ts' 'test/**/*.ts' || true",
"doc": "typedoc --out dist/docs --target es6 --theme minimal --mode file src",
"prebuild": "rimraf dist",
"build": "tsc --module commonjs --outDir dist/lib && rollup -c rollup.config.ts && npm run doc",
Expand Down Expand Up @@ -52,16 +52,9 @@
"url": "https://github.com/GerkinDev/diaspora/issues"
},
"homepage": "https://github.com/GerkinDev/diaspora#readme",
"prettier": {
"printWidth": 80,
"useTabs": true,
"tabWidth": 1,
"singleQuote": true,
"trailingComma": "es5"
},
"lint-staged": {
"{src,test}/**/*.ts": [
"prettier --write",
"npm run lint:fix",
"git add"
]
},
Expand Down Expand Up @@ -130,7 +123,6 @@
"lint-staged": "^7.0.0",
"lodash.camelcase": "^4.3.0",
"node-localstorage": "^1.3.1",
"prettier": "^1.11.1",
"prompt": "^1.0.0",
"replace-in-file": "^3.4.0",
"rimraf": "^2.6.2",
Expand Down
143 changes: 69 additions & 74 deletions src/adapters/base/adapter-utils.ts
Original file line number Diff line number Diff line change
@@ -1,54 +1,49 @@
import * as _ from 'lodash';

import { IRawEntityAttributes } from '../../entity/entityFactory';
import {
AdapterEntity,
IAdapterEntityCtr,
IRawAdapterEntityAttributes,
} from './entity';
import { AdapterEntity, IRawAdapterEntityAttributes } from './entity';
import { QueryLanguage } from './queryLanguage';
import { Adapter } from './adapter';

export interface Constructable<T> {
new (...args: any[]): T;
new ( ...args: any[] ): T;
}

function getNum(fullMatch: string, sign: string, val: string): number;
function getNum([fullMatch, sign, val]: string[]): number;
function getNum(...params: (string | string[])[]) {
const flatten = _.flattenDeep(params) as string[];
function getNum( fullMatch: string, sign: string, val: string ): number;
function getNum( [fullMatch, sign, val]: string[] ): number;
function getNum( ...params: Array<string | string[]> ) {
const flatten = _.flattenDeep( params ) as string[];
const [fullMatch, sign, val] = flatten;
if ('∞' === val) {
if ('-' === sign) {
if ( '∞' === val ) {
if ( '-' === sign ) {
return -Infinity;
} else {
return Infinity;
}
} else {
return parseInt(fullMatch, 10);
return parseInt( fullMatch, 10 );
}
}

const validations = {
type: {
int(key: string, val: string | number) {
if (_.isString(val)) {
val = parseInt(val, 10);
int( key: string, val: string | number ) {
if ( _.isString( val ) ) {
val = parseInt( val, 10 );
}
if (!_.isInteger(val) && isFinite(val)) {
throw new TypeError(`Expect "${key}" to be an integer`);
if ( !_.isInteger( val ) && isFinite( val ) ) {
throw new TypeError( `Expect "${key}" to be an integer` );
}
return val;
},
},
rng(key: string, val: string | number, range: string) {
const rangeMatch = range.match(/^([[\]])((-)?(\d+|)),((-)?(\d+|))([[\]])$/);
if (rangeMatch) {
const lower = getNum(rangeMatch.splice(2, 3));
const upper = getNum(rangeMatch.splice(2, 3));
rng( key: string, val: string | number, range: string ) {
const rangeMatch = range.match( /^([[\]])((-)?(\d+|)),((-)?(\d+|))([[\]])$/ );
if ( rangeMatch ) {
const lower = getNum( rangeMatch.splice( 2, 3 ) );
const upper = getNum( rangeMatch.splice( 2, 3 ) );
const isInRangeLower = '[' === rangeMatch[1] ? val >= lower : val > lower;
const isInRangeUpper = ']' === rangeMatch[2] ? val <= upper : val < upper;
if (!(isInRangeLower && isInRangeUpper)) {
if ( !( isInRangeLower && isInRangeUpper ) ) {
throw new RangeError(
`Expect "${key}" to be within ${range}, have "${val}"`
);
Expand All @@ -63,11 +58,11 @@ const validateOption = (
config: { type: string; rng?: string }
): any => {
const valTypes: any = validations.type;
if (valTypes[config.type]) {
val = valTypes[config.type](key, val);
if ( valTypes[config.type] ) {
val = valTypes[config.type]( key, val );
}
if (config.rng) {
val = validations.rng(key, val, config.rng);
if ( config.rng ) {
val = validations.rng( key, val, config.rng );
}
return val;
};
Expand All @@ -87,23 +82,23 @@ export const iterateLimit = async (
found?: IRawAdapterEntityAttributes | true
): Promise<IRawAdapterEntityAttributes[]> => {
// If the search returned nothing, then just finish the findMany
if (_.isNil(found)) {
return Promise.resolve(foundEntities);
if ( _.isNil( found ) ) {
return Promise.resolve( foundEntities );
// Else, if this is a value and not the initial `true`, add it to the list
} else if (typeof found === 'object') {
foundEntities.push(found);
} else if ( typeof found === 'object' ) {
foundEntities.push( found );
}
// If we found enough items, return them
if (foundCount === options.limit) {
return Promise.resolve(foundEntities);
if ( foundCount === options.limit ) {
return Promise.resolve( foundEntities );
}
options.skip = origSkip + foundCount;
// Next time we'll skip 1 more item
foundCount++;
// Do the query & loop
return loopFind(await query(options));
return loopFind( await query( options ) );
};
return loopFind(true);
return loopFind( true );
};

/**
Expand All @@ -118,50 +113,50 @@ export const remapIO = <T extends AdapterEntity>(
query: QueryLanguage.SelectQuery,
input: boolean
): QueryLanguage.SelectQueryRemapped => {
if (_.isNil(query)) {
if ( _.isNil( query ) ) {
return query;
}
const direction = true === input ? 'input' : 'output';
const filtered = _.mapValues(query, (value, key) => {
const filtered = _.mapValues( query, ( value, key ) => {
const filter = _.get(
adapter,
['filters', tableName, direction, key],
undefined
);
if (_.isFunction(filter)) {
return filter(value);
if ( _.isFunction( filter ) ) {
return filter( value );
}
return value;
});
} );
const remapType = true === input ? 'normal' : 'inverted';
const remaped = _.mapKeys(filtered, (value, key) => {
return _.get(adapter, ['remaps', tableName, remapType, key], key);
});
const remaped = _.mapKeys( filtered, ( value, key ) => {
return _.get( adapter, ['remaps', tableName, remapType, key], key );
} );
return remaped;
};

export interface IQueryCheckFunction {
(entityVal: any, targetVal: any): boolean;
( entityVal: any, targetVal: any ): boolean;
}

export interface IEnumeratedHash<T> {
[key: string]: T;
}
export const OPERATORS: IEnumeratedHash<IQueryCheckFunction | undefined> = {
$exists: (entityVal: any, targetVal: any) =>
targetVal === !_.isUndefined(entityVal),
$equal: (entityVal: any, targetVal: any) =>
!_.isUndefined(entityVal) && entityVal === targetVal,
$diff: (entityVal: any, targetVal: any) =>
!_.isUndefined(entityVal) && entityVal !== targetVal,
$less: (entityVal: any, targetVal: any) =>
!_.isUndefined(entityVal) && entityVal < targetVal,
$lessEqual: (entityVal: any, targetVal: any) =>
!_.isUndefined(entityVal) && entityVal <= targetVal,
$greater: (entityVal: any, targetVal: any) =>
!_.isUndefined(entityVal) && entityVal > targetVal,
$greaterEqual: (entityVal: any, targetVal: any) =>
!_.isUndefined(entityVal) && entityVal >= targetVal,
$exists: ( entityVal: any, targetVal: any ) =>
targetVal === !_.isUndefined( entityVal ),
$equal: ( entityVal: any, targetVal: any ) =>
!_.isUndefined( entityVal ) && entityVal === targetVal,
$diff: ( entityVal: any, targetVal: any ) =>
!_.isUndefined( entityVal ) && entityVal !== targetVal,
$less: ( entityVal: any, targetVal: any ) =>
!_.isUndefined( entityVal ) && entityVal < targetVal,
$lessEqual: ( entityVal: any, targetVal: any ) =>
!_.isUndefined( entityVal ) && entityVal <= targetVal,
$greater: ( entityVal: any, targetVal: any ) =>
!_.isUndefined( entityVal ) && entityVal > targetVal,
$greaterEqual: ( entityVal: any, targetVal: any ) =>
!_.isUndefined( entityVal ) && entityVal >= targetVal,
};
export const CANONICAL_OPERATORS: IEnumeratedHash<string> = {
'~': '$exists',
Expand All @@ -173,39 +168,39 @@ export const CANONICAL_OPERATORS: IEnumeratedHash<string> = {
'>=': '$greaterEqual',
};
export const QUERY_OPTIONS_TRANSFORMS: IEnumeratedHash<
((ops: QueryLanguage.QueryOptionsRaw) => void)
( ( ops: QueryLanguage.QueryOptionsRaw ) => void )
> = {
limit(opts: QueryLanguage.QueryOptionsRaw) {
opts.limit = validateOption('limit', opts.limit as number, {
limit( opts: QueryLanguage.QueryOptionsRaw ) {
opts.limit = validateOption( 'limit', opts.limit as number, {
type: 'int',
rng: '[0,∞]',
});
} );
},
skip(opts: QueryLanguage.QueryOptionsRaw) {
opts.skip = validateOption('skip', opts.skip as number, {
skip( opts: QueryLanguage.QueryOptionsRaw ) {
opts.skip = validateOption( 'skip', opts.skip as number, {
type: 'int',
rng: '[0,∞[',
});
} );
},
page(opts: QueryLanguage.QueryOptionsRaw) {
if (!opts.hasOwnProperty('limit')) {
page( opts: QueryLanguage.QueryOptionsRaw ) {
if ( !opts.hasOwnProperty( 'limit' ) ) {
throw new ReferenceError(
'Usage of "options.page" requires "options.limit" to be defined.'
);
}
if (!isFinite(opts.limit as number)) {
if ( !isFinite( opts.limit as number ) ) {
throw new RangeError(
'Usage of "options.page" requires "options.limit" to not be infinite'
);
}
if (opts.hasOwnProperty('skip')) {
throw new ReferenceError('Use either "options.page" or "options.skip"');
if ( opts.hasOwnProperty( 'skip' ) ) {
throw new ReferenceError( 'Use either "options.page" or "options.skip"' );
}
opts.skip =
validateOption('page', opts.page as number, {
validateOption( 'page', opts.page as number, {
type: 'int',
rng: '[0,∞[',
}) * (opts.limit as number);
} ) * ( opts.limit as number );
delete opts.page;
},
};
Loading

0 comments on commit d0c844c

Please sign in to comment.