Skip to content

Commit

Permalink
Merge pull request #1 from gmaxlev/feature_tsguarder
Browse files Browse the repository at this point in the history
feat: delegated type checking to the external package tsguarder
  • Loading branch information
gmaxlev authored Apr 4, 2023
2 parents cc5fa6b + ba9d95b commit 5fd4d7e
Show file tree
Hide file tree
Showing 24 changed files with 57 additions and 148 deletions.
15 changes: 13 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
},
"dependencies": {
"temp": "^0.9.4",
"tsguarder": "^1.0.1",
"type-fest": "^3.6.1"
}
}
9 changes: 2 additions & 7 deletions src/api/native/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
import fsp from "fs/promises";
import { isString, isUndefined, isBoolean, isRecord } from "tsguarder";
import { ModelSource, isModelSource } from "./types";
import type { CallPictOptions } from "./../../common/pict";
import {
isString,
isUndefined,
isPositiveNumber,
isBoolean,
} from "./../../common/utils";
import { isPositiveNumber } from "./../../common/utils";
import {
PictCliOptions,
isRandomOption,
isModelSeparator,
} from "../../common/types";
import { isRecord } from "../../common/utils";
import { callPict, pictEntries } from "../../common/pict";

interface NativeOptions {
Expand Down
3 changes: 1 addition & 2 deletions src/api/native/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { isRecord, isString } from "./../../common/utils";
import { createTypeGuard, TypeGuard } from "../../common/utils";
import { createTypeGuard, TypeGuard, isRecord, isString } from "tsguarder";

export interface ModelFileSource {
file: string;
Expand Down
2 changes: 1 addition & 1 deletion src/api/pict/ValuesIdMap.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { PictModelId } from "../../common/types";
import { isString } from "../../common/utils";
import { isString } from "tsguarder";

type ParameterValueToIdMap = Map<unknown, PictModelId>;

Expand Down
8 changes: 2 additions & 6 deletions src/api/pict/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isArray, isRecord, isUndefined } from "tsguarder";
import {
isInputSeed,
isRandomOption,
Expand All @@ -8,12 +9,7 @@ import {
} from "../../common/types";
import type { PictTypedModel } from "./types";
import { callPict, CallPictOptions } from "../../common/pict";
import {
isArray,
isPositiveNumber,
isRecord,
isUndefined,
} from "../../common/utils";
import { isPositiveNumber } from "../../common/utils";
import { createModel } from "./model";
import { createSeed } from "./seed";
import { parseResult } from "./parse";
Expand Down
2 changes: 1 addition & 1 deletion src/api/pict/seed.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isArray } from "../../common/utils";
import { isArray } from "tsguarder";
import { SeedBuilder } from "../../common/SeedBuilder";
import type { ValuesIdMap } from "./ValuesIdMap";
import type { PictTypedModel } from "./types";
Expand Down
8 changes: 2 additions & 6 deletions src/api/pict/types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import {
createTypeGuard,
isPropertyKey,
isRecord,
TypeGuard,
} from "../../common/utils";
import { createTypeGuard, isRecord, TypeGuard } from "tsguarder";
import { isPropertyKey } from "../../common/utils";
import type { PictModel } from "../../common/types";

export type PictTypedModel = PictModel<PropertyKey, unknown>;
Expand Down
9 changes: 2 additions & 7 deletions src/api/strings/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isArray, isRecord, isUndefined, isBoolean } from "tsguarder";
import {
isInputSeed,
isRandomOption,
Expand All @@ -9,13 +10,7 @@ import {
InputSubModel,
} from "../../common/types";
import { callPict, CallPictOptions } from "../../common/pict";
import {
isArray,
isRecord,
isUndefined,
isPositiveNumber,
isBoolean,
} from "../../common/utils";
import { isPositiveNumber } from "../../common/utils";
import { createStringModel } from "./model";
import { createSeed } from "./seed";
import { parseResult } from "./parse";
Expand Down
2 changes: 1 addition & 1 deletion src/api/strings/model.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { isString, isArray } from "tsguarder";
import {
isPictStringModel,
PictStringModel,
CreateModelOptions,
} from "./types";
import { isString, isArray } from "../../common/utils";
import { ModelBuilder, ModelBuilderOptions } from "../../common/ModelBuilder";
import { isAliasOperator } from "../../common/operators/alias";
import { isNegativeOperator } from "../../common/operators/negative";
Expand Down
2 changes: 1 addition & 1 deletion src/api/strings/seed.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { isArray, isString } from "tsguarder";
import type { PictModel } from "./../../common/types";
import { SeedBuilder } from "../../common/SeedBuilder";
import type { InputSeed } from "../../common/types";
import type { PictStringModel } from "./types";
import { isArray, isString } from "../../common/utils";

export function createSeed<
S extends InputSeed<ReadonlyArray<PictModel>>,
Expand Down
14 changes: 7 additions & 7 deletions src/api/strings/types.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import type { WeightOperatorObject } from "./../../common/operators/weight";
import type { NegativeOperatorObject } from "./../../common/operators/negative";
import type { AliasOperatorObject } from "./../../common/operators/alias";
import type { InputSubModel, PictModel } from "../../common/types";
import {
createTypeGuard,
TypeGuard,
isString,
isArray,
isRecord,
createTypeGuard,
TypeGuard,
} from "../../common/utils";
} from "tsguarder";
import type { WeightOperatorObject } from "./../../common/operators/weight";
import type { NegativeOperatorObject } from "./../../common/operators/negative";
import type { AliasOperatorObject } from "./../../common/operators/alias";
import type { InputSubModel, PictModel } from "../../common/types";

export interface CreateModelOptions<M extends ReadonlyArray<PictStringModel>> {
models: M;
Expand Down
2 changes: 1 addition & 1 deletion src/common/ModelBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isNumber, isRecord, isString, isUndefined } from "./utils";
import { isNumber, isRecord, isString, isUndefined } from "tsguarder";
import { EOL } from "os";

type ModelBuilderConstraint = string;
Expand Down
2 changes: 1 addition & 1 deletion src/common/SeedBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isString, isUndefined } from "./utils";
import { isString, isUndefined } from "tsguarder";
import { EOL } from "os";

/**
Expand Down
2 changes: 1 addition & 1 deletion src/common/operators/alias.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isRecord } from "../utils";
import { isRecord } from "tsguarder";

export const ALIAS_OPERATOR: unique symbol = Symbol("ALIAS_OPERATOR");

Expand Down
2 changes: 1 addition & 1 deletion src/common/operators/negative.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isRecord } from "../utils";
import { isRecord } from "tsguarder";

export const NEGATIVE_OPERATOR: unique symbol = Symbol("NEGATIVE_OPERATOR");

Expand Down
3 changes: 2 additions & 1 deletion src/common/operators/weight.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { isRecord, isPositiveNumber } from "../utils";
import { isRecord } from "tsguarder";
import { isPositiveNumber } from "../utils";

export const WEIGHT_OPERATOR: unique symbol = Symbol("WEIGHT_OPERATOR");

Expand Down
9 changes: 2 additions & 7 deletions src/common/pict.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import { IS_WIN, IS_TEST } from "./../env";
import type { PictCliOptions, PickPictCliOptions } from "./types";
import { execSync } from "child_process";
import {
isBoolean,
isNumber,
isString,
isUndefined,
writeTempFile,
} from "./utils";
import { writeTempFile } from "./utils";
import { isBoolean, isNumber, isString, isUndefined } from "tsguarder";
import path from "path";
import { EOL } from "os";
import url from "url";
Expand Down
8 changes: 4 additions & 4 deletions src/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import type { WeightOperatorObject } from "./operators/weight";
import type { UnionToIntersection } from "type-fest";
import {
createTypeGuard,
isNumber,
isPositiveNumber,
TypeGuard,
isRecord,
isString,
isUndefined,
TypeGuard,
} from "./utils";
isNumber,
} from "tsguarder";
import { isPositiveNumber } from "./utils";

export interface PictCliOptions {
order: number;
Expand Down
70 changes: 1 addition & 69 deletions src/common/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fsp from "fs/promises";
import * as temp from "temp";
import { createTypeGuard, TypeGuard, isNumber } from "tsguarder";

/**
* Creates a temporary file and writes the content to it.
Expand All @@ -13,40 +14,6 @@ export async function writeTempFile(content: string) {
return file;
}

export type TypeGuard<T> = {
(value: unknown): value is T;
assert: (value: unknown, before?: string) => asserts value is T;
};

export function createTypeGuard<T>(
description: string,
guard: (value: unknown) => value is T
) {
function check(value: unknown) {
return guard(value);
}

check.assert = function (
value: unknown,
before?: string
): asserts value is T {
if (!guard(value)) {
const message = before ? `${before}: ${description}` : description;
throw new TypeError(message);
}
};

return check as TypeGuard<T>;
}

export const isRecord: TypeGuard<Record<PropertyKey, unknown>> =
createTypeGuard("must be an object", function (value): value is Record<
PropertyKey,
unknown
> {
return typeof value === "object" && value !== null && !Array.isArray(value);
});

export const isPropertyKey: TypeGuard<PropertyKey> = createTypeGuard(
"must be a string, number or symbol",
(value): value is PropertyKey => {
Expand All @@ -58,51 +25,16 @@ export const isPropertyKey: TypeGuard<PropertyKey> = createTypeGuard(
}
);

export const isNumber: TypeGuard<number> = createTypeGuard(
"must be a number",
(value): value is number => {
return typeof value === "number";
}
);

export const isPositiveNumber: TypeGuard<number> = createTypeGuard(
"must be a positive number",
(value): value is number => {
return isNumber(value) && value > 0;
}
);

export const isBoolean: TypeGuard<boolean> = createTypeGuard(
"must be a boolean",
(value): value is boolean => {
return typeof value === "boolean";
}
);

export const isString: TypeGuard<string> = createTypeGuard(
"must be a string",
(value): value is string => {
return typeof value === "string";
}
);

export const isUndefined: TypeGuard<undefined> = createTypeGuard(
"must be undefined",
(value): value is undefined => {
return typeof value === "undefined";
}
);

export const isBuffer: TypeGuard<Buffer> = createTypeGuard(
"must be a Buffer",
(value): value is Buffer => {
return value instanceof Buffer;
}
);

export const isArray: TypeGuard<Array<unknown>> = createTypeGuard(
"must be an array",
(value): value is Array<unknown> => {
return Array.isArray(value);
}
);
6 changes: 3 additions & 3 deletions src/test/native.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe("native()", () => {
const result = expect(act);

await result.rejects.toThrowError(
"the first argument: must be an object"
"the first argument: must be a record"
);
}
});
Expand All @@ -34,7 +34,7 @@ describe("native()", () => {

const result = expect(act);

await result.rejects.toThrowError('"options": must be an object');
await result.rejects.toThrowError('"options": must be a record');
}
});
test('Should throw an error if "options.order" is not a positive number', async () => {
Expand Down Expand Up @@ -92,7 +92,7 @@ describe("native()", () => {
const result = expect(act);

await result.rejects.toThrowError(
'"options.caseSensitive": must be a boolean'
'"options.caseSensitive": must be boolean'
);
}
});
Expand Down
4 changes: 2 additions & 2 deletions src/test/pict.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe("pict()", () => {
const act = async () => await pict(notRecord);
const result = expect(act);
await result.rejects.toThrowError(
"the first argument: must be an object"
"the first argument: must be a record"
);
}
});
Expand All @@ -42,7 +42,7 @@ describe("pict()", () => {
const result = expect(act);

await result.rejects.toThrowError(
"the second argument: must be an object"
"the second argument: must be a record"
);
}
});
Expand Down
Loading

0 comments on commit 5fd4d7e

Please sign in to comment.