Skip to content

Commit f110900

Browse files
committed
feat(guard-clause): init
1 parent 681709c commit f110900

29 files changed

+649
-0
lines changed

.pnp.cjs

Lines changed: 34 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/guard-clause/package.json

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "@atls/guard-clause",
3+
"version": "0.0.0",
4+
"license": "BSD-3-Clause",
5+
"type": "module",
6+
"exports": {
7+
"./package.json": "./package.json",
8+
".": "./src/index.ts"
9+
},
10+
"main": "src/index.ts",
11+
"files": [
12+
"dist"
13+
],
14+
"scripts": {
15+
"build": "yarn library build",
16+
"prepack": "yarn run build",
17+
"postpack": "rm -rf dist"
18+
},
19+
"dependencies": {
20+
"validator": "^13.9.0"
21+
},
22+
"devDependencies": {
23+
"@types/validator": "^13.7.17"
24+
},
25+
"publishConfig": {
26+
"exports": {
27+
"./package.json": "./package.json",
28+
".": {
29+
"import": "./dist/index.js",
30+
"types": "./dist/index.d.ts",
31+
"default": "./dist/index.js"
32+
}
33+
},
34+
"main": "dist/index.js",
35+
"typings": "dist/index.d.ts"
36+
}
37+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type DecoratorFactoryFn = (target: any, propertyKey: string, parameterIndex: number) => void
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import type { AbstractGuardExtensionFactoryOptions } from '../../factory/index.js'
2+
import type { DecoratorFactoryFn } from './decorator.interfaces.js'
3+
4+
import { GuardFactory } from '../../factory/index.js'
5+
import { NotEmptyGuardExtensionFactory } from '../../extensions/index.js'
6+
7+
export const EmptyDecoratorFactory = (
8+
parameter: string,
9+
options?: AbstractGuardExtensionFactoryOptions['options']
10+
) =>
11+
(): DecoratorFactoryFn =>
12+
function Empty(target: any, propertyKey: string, parameterIndex: number): void {
13+
GuardFactory.register(NotEmptyGuardExtensionFactory, target, propertyKey, parameterIndex, {
14+
parameter,
15+
options,
16+
})
17+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import type { AbstractGuardExtensionFactoryOptions } from '../../factory/index.js'
2+
3+
import { EmptyDecoratorFactory } from './empty.decorator.factory.js'
4+
import { NotUUIDDecoratorFactory } from './not-uuid.decorator.factory.js'
5+
import { NotNumberBetweenDecoratorFactory } from './not-number-between.decorator.factory.js'
6+
import { NotInstanceDecoratorFactory } from './not-instance.decorator.factory.js'
7+
import { NotIntegerDecoratorFactory } from './not-integer.decorator.factory.js'
8+
import { NotEnumDecoratorFactory } from './not-enum.decorator.factory.js'
9+
import { NotOneOfDecoratorFactory } from './not-one-of.decorator.factory.js'
10+
11+
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
12+
const factory = (name: string, options?: AbstractGuardExtensionFactoryOptions['options']) => ({
13+
Empty: EmptyDecoratorFactory(name, options),
14+
NotUUID: NotUUIDDecoratorFactory(name, options),
15+
NotInstance: NotInstanceDecoratorFactory(name, options),
16+
NotNumberBetween: NotNumberBetweenDecoratorFactory(name, options),
17+
NotInteger: NotIntegerDecoratorFactory(name, options),
18+
NotEnum: NotEnumDecoratorFactory(name, options),
19+
NotOneOf: NotOneOfDecoratorFactory(name, options),
20+
})
21+
22+
export const Against = (
23+
name: string
24+
): ReturnType<typeof factory> & {
25+
Optional: ReturnType<typeof factory>
26+
Each: ReturnType<typeof factory>
27+
} => ({
28+
...factory(name),
29+
Optional: factory(name, { optional: true }),
30+
Each: factory(name, { each: true }),
31+
})
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import type { AbstractGuardExtensionFactoryOptions } from '../../factory/index.js'
2+
import type { DecoratorFactoryFn } from './decorator.interfaces.js'
3+
4+
import { GuardFactory } from '../../factory/index.js'
5+
import { NotEnumGuardExtensionFactory } from '../../extensions/index.js'
6+
7+
export const NotEnumDecoratorFactory = (
8+
parameter: string,
9+
options?: AbstractGuardExtensionFactoryOptions['options']
10+
) =>
11+
(targetEnum: object): DecoratorFactoryFn =>
12+
function NotEnum(target: any, propertyKey: string, parameterIndex: number): void {
13+
GuardFactory.register(NotEnumGuardExtensionFactory, target, propertyKey, parameterIndex, {
14+
parameter,
15+
options,
16+
metadata: {
17+
targetEnum,
18+
},
19+
})
20+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import type { AbstractGuardExtensionFactoryOptions } from '../../factory/index.js'
2+
import type { DecoratorFactoryFn } from './decorator.interfaces.js'
3+
4+
import { GuardFactory } from '../../factory/index.js'
5+
import { NotInstanceGuardExtensionFactory } from '../../extensions/index.js'
6+
7+
export const NotInstanceDecoratorFactory = (
8+
parameter: string,
9+
options?: AbstractGuardExtensionFactoryOptions['options']
10+
) =>
11+
// eslint-disable-next-line @typescript-eslint/ban-types
12+
(targetTypeConstructor: Function): DecoratorFactoryFn =>
13+
function NotInstance(target: any, propertyKey: string, parameterIndex: number): void {
14+
GuardFactory.register(NotInstanceGuardExtensionFactory, target, propertyKey, parameterIndex, {
15+
parameter,
16+
options,
17+
metadata: {
18+
targetTypeConstructor,
19+
},
20+
})
21+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import type { AbstractGuardExtensionFactoryOptions } from '../../factory/index.js'
2+
import type { DecoratorFactoryFn } from './decorator.interfaces.js'
3+
4+
import { GuardFactory } from '../../factory/index.js'
5+
import { NotIntegerGuardExtensionFactory } from '../../extensions/index.js'
6+
7+
export const NotIntegerDecoratorFactory = (
8+
parameter: string,
9+
options?: AbstractGuardExtensionFactoryOptions['options']
10+
) =>
11+
(): DecoratorFactoryFn =>
12+
function NotInteger(target: any, propertyKey: string, parameterIndex: number): void {
13+
GuardFactory.register(NotIntegerGuardExtensionFactory, target, propertyKey, parameterIndex, {
14+
parameter,
15+
options,
16+
})
17+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import type { AbstractGuardExtensionFactoryOptions } from '../../factory/index.js'
2+
import type { DecoratorFactoryFn } from './decorator.interfaces.js'
3+
4+
import { GuardFactory } from '../../factory/index.js'
5+
import { NotNumberBetweenGuardExtensionFactory } from '../../extensions/index.js'
6+
7+
export const NotNumberBetweenDecoratorFactory = (
8+
parameter: string,
9+
options?: AbstractGuardExtensionFactoryOptions['options']
10+
) =>
11+
(from: number, to: number): DecoratorFactoryFn =>
12+
function NotNumberBetween(target: any, propertyKey: string, parameterIndex: number): void {
13+
GuardFactory.register(
14+
NotNumberBetweenGuardExtensionFactory,
15+
target,
16+
propertyKey,
17+
parameterIndex,
18+
{
19+
parameter,
20+
options,
21+
metadata: {
22+
from,
23+
to,
24+
},
25+
}
26+
)
27+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import type { AbstractGuardExtensionFactoryOptions } from '../../factory/index.js'
2+
import type { DecoratorFactoryFn } from './decorator.interfaces.js'
3+
4+
import { GuardFactory } from '../../factory/index.js'
5+
import { NotOneOfGuardExtensionFactory } from '../../extensions/index.js'
6+
7+
export const NotOneOfDecoratorFactory = (
8+
parameter: string,
9+
options?: AbstractGuardExtensionFactoryOptions['options']
10+
) =>
11+
(oneOf: object): DecoratorFactoryFn =>
12+
function NotOneOf(target: any, propertyKey: string, parameterIndex: number): void {
13+
GuardFactory.register(NotOneOfGuardExtensionFactory, target, propertyKey, parameterIndex, {
14+
parameter,
15+
options,
16+
metadata: {
17+
oneOf,
18+
},
19+
})
20+
}

0 commit comments

Comments
 (0)