Skip to content

Commit

Permalink
types: remove type prefixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Mar 28, 2022
1 parent 8dd9c64 commit 2f1c5b7
Show file tree
Hide file tree
Showing 36 changed files with 200 additions and 214 deletions.
16 changes: 8 additions & 8 deletions packages/n4s/src/exports/compose.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import invariant from 'invariant';
import mapFirst from 'mapFirst';

import type { TComposeResult, TLazyRuleRunners } from 'genEnforceLazy';
import type { ComposeResult, LazyRuleRunners } from 'genEnforceLazy';
import { ctx } from 'n4s';
import { defaultToPassing, TRuleDetailedResult } from 'ruleReturn';
import { defaultToPassing, RuleDetailedResult } from 'ruleReturn';
import runLazyRule from 'runLazyRule';

/* eslint-disable max-lines-per-function */

export default function compose(
...composites: TLazyRuleRunners[]
): TComposeResult {
...composites: LazyRuleRunners[]
): ComposeResult {
return Object.assign(
(value: any) => {
const res = run(value);
Expand All @@ -23,17 +23,17 @@ export default function compose(
}
);

function run(value: any): TRuleDetailedResult {
function run(value: any): RuleDetailedResult {
return ctx.run({ value }, () => {
return defaultToPassing(
mapFirst(
composites,
(
composite: TLazyRuleRunners,
breakout: (res: TRuleDetailedResult) => void
composite: LazyRuleRunners,
breakout: (res: RuleDetailedResult) => void
) => {
/* HACK: Just a small white lie. ~~HELP WANTED~~.
The ideal is that instead of `TLazyRuleRunners` We would simply use `TLazy` to begin with.
The ideal is that instead of `LazyRuleRunners` We would simply use `Lazy` to begin with.
The problem is that lazy rules can't really be passed to this function due to some generic hell
so we're limiting it to a small set of functions.
*/
Expand Down
4 changes: 2 additions & 2 deletions packages/n4s/src/lib/eachEnforceRule.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import isFunction from 'isFunction';

import { baseRules, getRule, KBaseRules, TRuleBase } from 'runtimeRules';
import { baseRules, getRule, KBaseRules, RuleBase } from 'runtimeRules';

export default function eachEnforceRule(
action: (ruleName: KBaseRules, rule: TRuleBase) => void
action: (ruleName: KBaseRules, rule: RuleBase) => void
) {
for (const ruleName in baseRules) {
const ruleFn = getRule(ruleName);
Expand Down
24 changes: 12 additions & 12 deletions packages/n4s/src/lib/ruleReturn.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import defaultTo from 'defaultTo';
import type { TStringable } from 'utilityTypes';
import type { Stringable } from 'utilityTypes';

export default function ruleReturn(
pass: boolean,
message?: string
): TRuleDetailedResult {
const output: TRuleDetailedResult = { pass };
): RuleDetailedResult {
const output: RuleDetailedResult = { pass };

if (message) {
output.message = message;
Expand All @@ -14,31 +14,31 @@ export default function ruleReturn(
return output;
}

export function failing(): TRuleDetailedResult {
export function failing(): RuleDetailedResult {
return ruleReturn(false);
}

export function passing(): TRuleDetailedResult {
export function passing(): RuleDetailedResult {
return ruleReturn(true);
}

export function defaultToFailing(
callback: (...args: any[]) => TRuleDetailedResult
): TRuleDetailedResult {
callback: (...args: any[]) => RuleDetailedResult
): RuleDetailedResult {
return defaultTo(callback, failing());
}

export function defaultToPassing(
callback: (...args: any[]) => TRuleDetailedResult
): TRuleDetailedResult {
callback: (...args: any[]) => RuleDetailedResult
): RuleDetailedResult {
return defaultTo(callback, passing());
}

export type TRuleReturn =
export type RuleReturn =
| boolean
| {
pass: boolean;
message?: TStringable;
message?: Stringable;
};

export type TRuleDetailedResult = { pass: boolean; message?: string };
export type RuleDetailedResult = { pass: boolean; message?: string };
8 changes: 4 additions & 4 deletions packages/n4s/src/lib/runLazyRule.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { TLazyRuleRunners } from 'genEnforceLazy';
import type { TRuleDetailedResult } from 'ruleReturn';
import type { LazyRuleRunners } from 'genEnforceLazy';
import type { RuleDetailedResult } from 'ruleReturn';
import * as ruleReturn from 'ruleReturn';

export default function runLazyRule(
lazyRule: TLazyRuleRunners,
lazyRule: LazyRuleRunners,
currentValue: any
): TRuleDetailedResult {
): RuleDetailedResult {
try {
return lazyRule.run(currentValue);
} catch {
Expand Down
14 changes: 7 additions & 7 deletions packages/n4s/src/lib/transformResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ import invariant from 'invariant';
import { isBoolean } from 'isBooleanValue';
import optionalFunctionValue from 'optionalFunctionValue';

import ruleReturn, { TRuleReturn, TRuleDetailedResult } from 'ruleReturn';
import type { TRuleValue, TArgs } from 'runtimeRules';
import ruleReturn, { RuleReturn, RuleDetailedResult } from 'ruleReturn';
import type { RuleValue, Args } from 'runtimeRules';

/**
* Transform the result of a rule into a standard format
*/
export function transformResult(
result: TRuleReturn,
result: RuleReturn,
ruleName: string,
value: TRuleValue,
...args: TArgs
): TRuleDetailedResult {
value: RuleValue,
...args: Args
): RuleDetailedResult {
validateResult(result);

// if result is boolean
Expand All @@ -27,7 +27,7 @@ export function transformResult(
}
}

function validateResult(result: TRuleReturn): void {
function validateResult(result: RuleReturn): void {
// if result is boolean, or if result.pass is boolean
invariant(
isBoolean(result) || (result && isBoolean(result.pass)),
Expand Down
6 changes: 3 additions & 3 deletions packages/n4s/src/plugins/compounds/allOf.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import mapFirst from 'mapFirst';

import type { TLazy } from 'genEnforceLazy';
import type { Lazy } from 'genEnforceLazy';
import * as ruleReturn from 'ruleReturn';
import type { TRuleDetailedResult } from 'ruleReturn';
import type { RuleDetailedResult } from 'ruleReturn';
import runLazyRule from 'runLazyRule';

export function allOf(value: unknown, ...rules: TLazy[]): TRuleDetailedResult {
export function allOf(value: unknown, ...rules: Lazy[]): RuleDetailedResult {
return ruleReturn.defaultToPassing(
mapFirst(rules, (rule, breakout) => {
const res = runLazyRule(rule, value);
Expand Down
6 changes: 3 additions & 3 deletions packages/n4s/src/plugins/compounds/anyOf.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import mapFirst from 'mapFirst';

import type { TLazy } from 'genEnforceLazy';
import type { Lazy } from 'genEnforceLazy';
import * as ruleReturn from 'ruleReturn';
import type { TRuleDetailedResult } from 'ruleReturn';
import type { RuleDetailedResult } from 'ruleReturn';
import runLazyRule from 'runLazyRule';

export function anyOf(value: unknown, ...rules: TLazy[]): TRuleDetailedResult {
export function anyOf(value: unknown, ...rules: Lazy[]): RuleDetailedResult {
return ruleReturn.defaultToFailing(
mapFirst(rules, (rule, breakout) => {
const res = runLazyRule(rule, value);
Expand Down
6 changes: 3 additions & 3 deletions packages/n4s/src/plugins/compounds/noneOf.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import mapFirst from 'mapFirst';

import type { TLazy } from 'genEnforceLazy';
import type { TRuleDetailedResult } from 'ruleReturn';
import type { Lazy } from 'genEnforceLazy';
import type { RuleDetailedResult } from 'ruleReturn';
import * as ruleReturn from 'ruleReturn';
import runLazyRule from 'runLazyRule';

export function noneOf(value: unknown, ...rules: TLazy[]): TRuleDetailedResult {
export function noneOf(value: unknown, ...rules: Lazy[]): RuleDetailedResult {
return ruleReturn.defaultToPassing(
mapFirst(rules, (rule, breakout) => {
const res = runLazyRule(rule, value);
Expand Down
8 changes: 4 additions & 4 deletions packages/n4s/src/plugins/compounds/oneOf.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import type { TLazy } from 'genEnforceLazy';
import type { Lazy } from 'genEnforceLazy';
import { lengthEquals } from 'lengthEquals';
import { longerThan } from 'longerThan';
import ruleReturn, { TRuleDetailedResult } from 'ruleReturn';
import ruleReturn, { RuleDetailedResult } from 'ruleReturn';
import runLazyRule from 'runLazyRule';

const REQUIRED_COUNT = 1;

export function oneOf(value: unknown, ...rules: TLazy[]): TRuleDetailedResult {
const passing: TRuleDetailedResult[] = [];
export function oneOf(value: unknown, ...rules: Lazy[]): RuleDetailedResult {
const passing: RuleDetailedResult[] = [];
rules.some(rule => {
if (longerThan(passing, REQUIRED_COUNT)) {
return false;
Expand Down
8 changes: 4 additions & 4 deletions packages/n4s/src/plugins/schema/isArrayOf.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import mapFirst from 'mapFirst';

import type { TLazyRuleRunners } from 'genEnforceLazy';
import type { LazyRuleRunners } from 'genEnforceLazy';
import { ctx } from 'n4s';
import type { TRuleDetailedResult } from 'ruleReturn';
import type { RuleDetailedResult } from 'ruleReturn';
import * as ruleReturn from 'ruleReturn';
import runLazyRule from 'runLazyRule';

export function isArrayOf(
inputArray: any[],
currentRule: TLazyRuleRunners
): TRuleDetailedResult {
currentRule: LazyRuleRunners
): RuleDetailedResult {
return ruleReturn.defaultToPassing(
mapFirst(inputArray, (currentValue, breakout, index) => {
const res = ctx.run(
Expand Down
8 changes: 4 additions & 4 deletions packages/n4s/src/plugins/schema/loose.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { ctx } from 'n4s';
import type { TRuleDetailedResult } from 'ruleReturn';
import type { RuleDetailedResult } from 'ruleReturn';
import * as ruleReturn from 'ruleReturn';
import runLazyRule from 'runLazyRule';
import type { IShapeObject } from 'schemaTypes';
import type { ShapeObject } from 'schemaTypes';

export function loose(
inputObject: Record<string, any>,
shapeObject: IShapeObject
): TRuleDetailedResult {
shapeObject: ShapeObject
): RuleDetailedResult {
for (const key in shapeObject) {
const currentValue = inputObject[key];
const currentRule = shapeObject[key];
Expand Down
6 changes: 3 additions & 3 deletions packages/n4s/src/plugins/schema/optional.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { isNullish } from 'isNullish';

import type { TLazy } from 'genEnforceLazy';
import type { TRuleDetailedResult } from 'ruleReturn';
import type { Lazy } from 'genEnforceLazy';
import type { RuleDetailedResult } from 'ruleReturn';
import * as ruleReturn from 'ruleReturn';
import runLazyRule from 'runLazyRule';

export function optional(value: any, ruleChain: TLazy): TRuleDetailedResult {
export function optional(value: any, ruleChain: Lazy): RuleDetailedResult {
if (isNullish(value)) {
return ruleReturn.passing();
}
Expand Down
2 changes: 1 addition & 1 deletion packages/n4s/src/plugins/schema/partial.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { enforce } from 'n4s';

// Help needed improving the typings of this file.
// Ideally, we'd be able to extend IShapeObject, but that's not possible.
// Ideally, we'd be able to extend ShapeObject, but that's not possible.
export function partial<T extends Record<any, any>>(shapeObject: T): T {
const output = {} as T;
for (const key in shapeObject) {
Expand Down
6 changes: 3 additions & 3 deletions packages/n4s/src/plugins/schema/schemaTypes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TLazyRuleRunners } from 'genEnforceLazy';
import { LazyRuleRunners } from 'genEnforceLazy';

export interface IShapeObject
export interface ShapeObject
extends Record<string, any>,
Record<string, TLazyRuleRunners> {}
Record<string, LazyRuleRunners> {}
8 changes: 4 additions & 4 deletions packages/n4s/src/plugins/schema/shape.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import hasOwnProperty from 'hasOwnProperty';

import { loose } from 'loose';
import type { TRuleDetailedResult } from 'ruleReturn';
import type { RuleDetailedResult } from 'ruleReturn';
import * as ruleReturn from 'ruleReturn';
import type { IShapeObject } from 'schemaTypes';
import type { ShapeObject } from 'schemaTypes';

export function shape(
inputObject: Record<string, any>,
shapeObject: IShapeObject
): TRuleDetailedResult {
shapeObject: ShapeObject
): RuleDetailedResult {
const baseRes = loose(inputObject, shapeObject);
if (!baseRes.pass) {
return baseRes;
Expand Down
4 changes: 2 additions & 2 deletions packages/n4s/src/rules/isEven.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { isNumeric } from 'isNumeric';
import type { TRuleValue } from 'runtimeRules';
import type { RuleValue } from 'runtimeRules';

/**
* Validates that a given value is an even number
*/
export const isEven = (value: TRuleValue): boolean => {
export const isEven = (value: RuleValue): boolean => {
if (isNumeric(value)) {
return value % 2 === 0;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/n4s/src/rules/isOdd.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { isNumeric } from 'isNumeric';
import type { TRuleValue } from 'runtimeRules';
import type { RuleValue } from 'runtimeRules';
/**
* Validates that a given value is an odd number
*/
export const isOdd = (value: TRuleValue): boolean => {
export const isOdd = (value: RuleValue): boolean => {
if (isNumeric(value)) {
return value % 2 !== 0;
}
Expand Down
6 changes: 3 additions & 3 deletions packages/n4s/src/rules/ruleCondition.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { TRuleReturn } from 'ruleReturn';
import type { RuleReturn } from 'ruleReturn';

export function condition(
value: any,
callback: (value: any) => TRuleReturn
): TRuleReturn {
callback: (value: any) => RuleReturn
): RuleReturn {
try {
return callback(value);
} catch {
Expand Down
Loading

0 comments on commit 2f1c5b7

Please sign in to comment.