refactor: Prevent negated conditions

This commit is contained in:
Joachim Van Herwegen
2023-10-30 17:04:20 +01:00
parent c9e4c7041c
commit 98f5d8fb73
9 changed files with 28 additions and 30 deletions

View File

@@ -88,14 +88,14 @@ function emitStoredErrors(this: Guarded, event: string, func: (error: Error) =>
*/
export function guardStream<T extends NodeJS.EventEmitter>(stream: T): Guarded<T> {
const guarded = stream as Guarded<T>;
if (!isGuarded(stream)) {
guarded[guardedErrors] = [];
guarded.on('error', guardingErrorListener);
guarded.on('newListener', emitStoredErrors);
} else {
if (isGuarded(stream)) {
// This makes sure the guarding error listener is the last one in the list again
guarded.removeListener('error', guardingErrorListener);
guarded.on('error', guardingErrorListener);
} else {
guarded[guardedErrors] = [];
guarded.on('error', guardingErrorListener);
guarded.on('newListener', emitStoredErrors);
}
return guarded;
}

View File

@@ -139,7 +139,7 @@ function transformPathComponents(path: string, transform: (part: string) => stri
.map((part, index): string =>
index % 2 === 0 ? transform(part) : part.toUpperCase())
.join('');
return !queryString ? transformed : `${transformed}${queryString}`;
return queryString ? `${transformed}${queryString}` : transformed;
}
/**

View File

@@ -72,8 +72,8 @@ export class FilterPattern {
* @param object - Optionally filter based on a specific object.
*/
public constructor(subject?: string, predicate?: string, object?: string) {
this.subject = typeof subject !== 'undefined' ? toNamedTerm(subject) : null;
this.predicate = typeof predicate !== 'undefined' ? toNamedTerm(predicate) : null;
this.object = typeof object !== 'undefined' ? toNamedTerm(object) : null;
this.subject = typeof subject === 'string' ? toNamedTerm(subject) : null;
this.predicate = typeof predicate === 'string' ? toNamedTerm(predicate) : null;
this.object = typeof object === 'string' ? toNamedTerm(object) : null;
}
}

View File

@@ -31,11 +31,11 @@ export class SliceStream extends Transform {
let start = options.start;
const end = options.end ?? Number.POSITIVE_INFINITY;
if (options.start < 0) {
if (typeof options.size !== 'number') {
throw new RangeNotSatisfiedHttpError('Slicing data at the end of a stream requires a known size.');
} else {
if (typeof options.size === 'number') {
// `start` is a negative number here so need to add
start = options.size + start;
} else {
throw new RangeNotSatisfiedHttpError('Slicing data at the end of a stream requires a known size.');
}
}