Skip to content

Commit

Permalink
Do not use 'util' imports
Browse files Browse the repository at this point in the history
  • Loading branch information
Yogu committed Feb 16, 2022
1 parent 0a80c9a commit c3bbff9
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 27 deletions.
5 changes: 2 additions & 3 deletions src/schema-generation/filter-input-types/filter-fields.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { getNamedType, GraphQLInputType, GraphQLList, GraphQLNonNull } from 'graphql';
import { ZonedDateTime } from 'js-joda';
import { isArray } from 'util';
import { EnumType, Field, ScalarType, Type, TypeKind } from '../../model';
import {
BinaryOperationQueryNode,
Expand Down Expand Up @@ -264,7 +263,7 @@ export class AndFilterField implements FilterField {
}

getFilterNode(sourceNode: QueryNode, filterValue: AnyValue): QueryNode {
if (!isArray(filterValue) || !filterValue.length) {
if (!Array.isArray(filterValue) || !filterValue.length) {
return new ConstBoolQueryNode(true);
}
const values = (filterValue || []) as ReadonlyArray<PlainObject>;
Expand All @@ -285,7 +284,7 @@ export class OrFilterField implements FilterField {
}

getFilterNode(sourceNode: QueryNode, filterValue: AnyValue): QueryNode {
if (!isArray(filterValue)) {
if (!Array.isArray(filterValue)) {
return new ConstBoolQueryNode(true); // regard as omitted
}
const values = filterValue as ReadonlyArray<PlainObject>;
Expand Down
11 changes: 5 additions & 6 deletions src/schema/scalars/fixed-point-decimals.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { GraphQLError, GraphQLScalarType, Kind, print, ValueNode } from 'graphql';
import { inspect } from 'util';

function createFixedPointDecimalType(decimals: number) {
const typeName = `Decimal${decimals}`;
Expand All @@ -12,18 +11,18 @@ function createFixedPointDecimalType(decimals: number) {
function coerceFixedPointDecimal(value: unknown) {
// same logic like Float
if (typeof value !== 'number' || !isFinite(value)) {
throw new GraphQLError(`${typeName} cannot represent non numeric value: `.concat(inspect(value)));
throw new GraphQLError(`${typeName} cannot represent non numeric value: `.concat(JSON.stringify(value)));
}

const num = Number(value.toFixed(decimals));
if (num > maxValue) {
throw new GraphQLError(
`${typeName} cannot represent value larger than ${maxValueStr}: `.concat(inspect(value))
`${typeName} cannot represent value larger than ${maxValueStr}: `.concat(JSON.stringify(value))
);
}
if (num < minValue) {
throw new GraphQLError(
`${typeName} cannot represent value smaller than ${minValueStr}: `.concat(inspect(value))
`${typeName} cannot represent value smaller than ${minValueStr}: `.concat(JSON.stringify(value))
);
}

Expand All @@ -42,12 +41,12 @@ function createFixedPointDecimalType(decimals: number) {
const num = Number(value.toFixed(decimals));
if (num > maxValue) {
throw new GraphQLError(
`${typeName} cannot represent value larger than ${maxValueStr}: `.concat(inspect(value))
`${typeName} cannot represent value larger than ${maxValueStr}: `.concat(JSON.stringify(value))
);
}
if (num < minValue) {
throw new GraphQLError(
`${typeName} cannot represent value smaller than ${minValueStr}: `.concat(inspect(value))
`${typeName} cannot represent value smaller than ${minValueStr}: `.concat(JSON.stringify(value))
);
}

Expand Down
7 changes: 3 additions & 4 deletions src/schema/scalars/int53.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import { GraphQLError, GraphQLScalarType, Kind, print, ValueNode } from 'graphql';
import { inspect } from 'util';

function coerceInt53(value: unknown) {
if (typeof value !== 'number' || !Number.isInteger(value)) {
throw new GraphQLError(`Int53 cannot represent non-integer value: ${inspect(value)}`);
throw new GraphQLError(`Int53 cannot represent non-integer value: ${JSON.stringify(value)}`);
}

if (value > Number.MAX_SAFE_INTEGER) {
throw new GraphQLError(
`Int53 cannot represent value larger than ${Number.MAX_SAFE_INTEGER}: ${inspect(value)}`
`Int53 cannot represent value larger than ${Number.MAX_SAFE_INTEGER}: ${JSON.stringify(value)}`
);
}

if (value < Number.MIN_SAFE_INTEGER) {
throw new GraphQLError(
`Int53 cannot represent value smaller than ${Number.MIN_SAFE_INTEGER}: ${inspect(value)}`
`Int53 cannot represent value smaller than ${Number.MIN_SAFE_INTEGER}: ${JSON.stringify(value)}`
);
}

Expand Down
4 changes: 1 addition & 3 deletions src/utils/error-with-cause.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { inspect } from 'util';

/**
* ErrorWithCause extends the default node Error to support causes.
* Each error can have one cause. The stack of the causing error is appended
Expand All @@ -26,5 +24,5 @@ function extractMessage(message: string, cause?: Error | unknown): string {
if (cause instanceof Error) {
return `${message}: ${cause.message}`;
}
return `${message}: NonError: ${inspect(cause)}`;
return `${message}: NonError: ${cause}`;
}
20 changes: 9 additions & 11 deletions src/utils/visitor.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { isArray } from 'util';

export type VisitResult<T> = {
recurse?: boolean
newValue: T
}
recurse?: boolean;
newValue: T;
};

export type Visitor<T> = {
enter?(object: T, key: string|undefined): VisitResult<T>,
leave?(object: T, key: string|undefined): T
enter?(object: T, key: string | undefined): VisitResult<T>;
leave?(object: T, key: string | undefined): T;
};

export function visitObject<T>(node: T, visitor: Visitor<T>): T {
Expand Down Expand Up @@ -44,24 +42,24 @@ function visitObjectProperties<T>(object: T, visitor: Visitor<T>): T {
return newObj;
}

function visitObjectOrArray<T>(nodeOrArray: T|T[], visitor: Visitor<any>, key: string|undefined): T|T[] {
function visitObjectOrArray<T>(nodeOrArray: T | T[], visitor: Visitor<any>, key: string | undefined): T | T[] {
if (typeof nodeOrArray != 'object' || nodeOrArray === null) {
return nodeOrArray;
}
if (!isArray(nodeOrArray)) {
if (!Array.isArray(nodeOrArray)) {
return visitObject0(nodeOrArray as T, visitor, key);
}
return nodeOrArray.map(item => visitObject0(item, visitor, key));
}

function enter<T>(obj: T, visitor: Visitor<T>, key: string|undefined): VisitResult<T> {
function enter<T>(obj: T, visitor: Visitor<T>, key: string | undefined): VisitResult<T> {
if (visitor.enter) {
return visitor.enter(obj, key);
}
return { newValue: obj };
}

function leave<T>(obj: T, visitor: Visitor<T>, key: string|undefined): T {
function leave<T>(obj: T, visitor: Visitor<T>, key: string | undefined): T {
if (visitor.leave) {
return visitor.leave(obj, key);
}
Expand Down

0 comments on commit c3bbff9

Please sign in to comment.