@lafrae/specification 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,84 @@
1
+ # Specs
2
+
3
+ Definição de pipeline de especificações
4
+
5
+ ## Instalando
6
+
7
+ Via npm
8
+
9
+ ```bash
10
+ npm i @sintese/specification
11
+ ```
12
+
13
+ ## Usando
14
+
15
+ * Defina um conjunto de especificações
16
+
17
+ ```javascript
18
+ // IsEvenNumber.js
19
+ class IsEvenNumber {
20
+ isSatisfiedBy(candidate) {
21
+ const isEven = candidate % 2 == 0;
22
+ return {
23
+ ok: isEven,
24
+ details: !isEven
25
+ ? [`candidate is not an even number`]
26
+ : [`candidate is an even number`],
27
+ };
28
+ }
29
+ }
30
+
31
+ module.exports = { IsEvenNumber };
32
+
33
+
34
+ // IsBiggerThan.js
35
+ class IsBiggerThan {
36
+ constructor(ref) {
37
+ this.ref = ref;
38
+ }
39
+ isSatisfiedBy(candidate) {
40
+ const isBigger = candidate > this.ref;
41
+ return {
42
+ ok: isBigger,
43
+ details: !isBigger
44
+ ? [`candidate is not bigger than ${this.ref}`]
45
+ : [`candidate is not bigger than ${this.ref}`],
46
+ };
47
+ }
48
+ }
49
+
50
+ module.exports = { IsBiggerThan };
51
+ ```
52
+
53
+ * Aplique as especificações em uma pipeline
54
+
55
+ ```javascript
56
+ // index.js
57
+ const { And } = require("@sintese/specs");
58
+
59
+ const { IsEvenNumber } = require("./IsEvenNumber.js");
60
+ const { IsBiggerThan } = require("./IsBiggerThan.js");
61
+
62
+ const specAnd = new And(
63
+ new IsEvenNumber(),
64
+ new IsBiggerThan(10),
65
+ );
66
+ const result = specAnd.isSatisfiedBy(2);
67
+
68
+ console.log(result);
69
+ ```
70
+
71
+ * Ao executar o conjunto podemos validar o resultado
72
+
73
+ ```bash
74
+ $ node index.js
75
+
76
+ {
77
+ ok: false,
78
+ details: [ 'candidate is an even number', 'candidate is not bigger than 10' ]
79
+ }
80
+ ```
81
+
82
+ ## Contribuindo
83
+
84
+ Dúvidas, contribuições e sugestões são muito bem vidas.
@@ -0,0 +1 @@
1
+ export * from "./specs/index.js";
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./specs/index.js"), exports);
@@ -0,0 +1 @@
1
+ {"type": "commonjs"}
@@ -0,0 +1,6 @@
1
+ import { IResult, ISpec } from "./ISpec";
2
+ export declare class And<T> implements ISpec<T> {
3
+ private specs;
4
+ constructor(...specs: ISpec<T>[]);
5
+ isSatisfiedBy(v: T): IResult;
6
+ }
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.And = void 0;
4
+ class And {
5
+ constructor(...specs) {
6
+ this.specs = specs;
7
+ }
8
+ isSatisfiedBy(v) {
9
+ const details = [];
10
+ let ok = true;
11
+ for (const spec of this.specs) {
12
+ const result = spec.isSatisfiedBy(v);
13
+ details.push(...(result.details || []));
14
+ if (result.ok) {
15
+ continue;
16
+ }
17
+ ok = false;
18
+ }
19
+ return { ok, details };
20
+ }
21
+ }
22
+ exports.And = And;
@@ -0,0 +1,7 @@
1
+ export type IResult = {
2
+ ok: boolean;
3
+ details?: string[];
4
+ };
5
+ export interface ISpec<T> {
6
+ isSatisfiedBy(candidate: T): IResult;
7
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,6 @@
1
+ import { IResult, ISpec } from "./ISpec";
2
+ export declare class Not<T> implements ISpec<T> {
3
+ private readonly spec;
4
+ constructor(spec: ISpec<T>);
5
+ isSatisfiedBy(candidate: T): IResult;
6
+ }
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Not = void 0;
4
+ class Not {
5
+ constructor(spec) {
6
+ this.spec = spec;
7
+ }
8
+ isSatisfiedBy(candidate) {
9
+ const result = this.spec.isSatisfiedBy(candidate);
10
+ return { ok: result.ok === false, details: result.details };
11
+ }
12
+ }
13
+ exports.Not = Not;
@@ -0,0 +1,6 @@
1
+ import { IResult, ISpec } from "./ISpec";
2
+ export declare class Or<T> implements ISpec<T> {
3
+ private specs;
4
+ constructor(...specs: ISpec<T>[]);
5
+ isSatisfiedBy(v: T): IResult;
6
+ }
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Or = void 0;
4
+ class Or {
5
+ constructor(...specs) {
6
+ this.specs = specs;
7
+ }
8
+ isSatisfiedBy(v) {
9
+ const details = [];
10
+ let ok = true;
11
+ for (const spec of this.specs) {
12
+ const result = spec.isSatisfiedBy(v);
13
+ details.push(...(result.details || []));
14
+ if (result.ok)
15
+ return { ok, details };
16
+ ok = false;
17
+ }
18
+ return { ok, details };
19
+ }
20
+ }
21
+ exports.Or = Or;
@@ -0,0 +1,4 @@
1
+ export * from "./ISpec";
2
+ export * from "./And";
3
+ export * from "./Not";
4
+ export * from "./Or";
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./ISpec"), exports);
18
+ __exportStar(require("./And"), exports);
19
+ __exportStar(require("./Not"), exports);
20
+ __exportStar(require("./Or"), exports);
@@ -0,0 +1 @@
1
+ export * from "./specs/index.js";
@@ -0,0 +1 @@
1
+ export * from "./specs/index.js";
@@ -0,0 +1 @@
1
+ {"type": "module"}
@@ -0,0 +1,6 @@
1
+ import { IResult, ISpec } from "./ISpec";
2
+ export declare class And<T> implements ISpec<T> {
3
+ private specs;
4
+ constructor(...specs: ISpec<T>[]);
5
+ isSatisfiedBy(v: T): IResult;
6
+ }
@@ -0,0 +1,18 @@
1
+ export class And {
2
+ constructor(...specs) {
3
+ this.specs = specs;
4
+ }
5
+ isSatisfiedBy(v) {
6
+ const details = [];
7
+ let ok = true;
8
+ for (const spec of this.specs) {
9
+ const result = spec.isSatisfiedBy(v);
10
+ details.push(...(result.details || []));
11
+ if (result.ok) {
12
+ continue;
13
+ }
14
+ ok = false;
15
+ }
16
+ return { ok, details };
17
+ }
18
+ }
@@ -0,0 +1,7 @@
1
+ export type IResult = {
2
+ ok: boolean;
3
+ details?: string[];
4
+ };
5
+ export interface ISpec<T> {
6
+ isSatisfiedBy(candidate: T): IResult;
7
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,6 @@
1
+ import { IResult, ISpec } from "./ISpec";
2
+ export declare class Not<T> implements ISpec<T> {
3
+ private readonly spec;
4
+ constructor(spec: ISpec<T>);
5
+ isSatisfiedBy(candidate: T): IResult;
6
+ }
@@ -0,0 +1,9 @@
1
+ export class Not {
2
+ constructor(spec) {
3
+ this.spec = spec;
4
+ }
5
+ isSatisfiedBy(candidate) {
6
+ const result = this.spec.isSatisfiedBy(candidate);
7
+ return { ok: result.ok === false, details: result.details };
8
+ }
9
+ }
@@ -0,0 +1,6 @@
1
+ import { IResult, ISpec } from "./ISpec";
2
+ export declare class Or<T> implements ISpec<T> {
3
+ private specs;
4
+ constructor(...specs: ISpec<T>[]);
5
+ isSatisfiedBy(v: T): IResult;
6
+ }
@@ -0,0 +1,17 @@
1
+ export class Or {
2
+ constructor(...specs) {
3
+ this.specs = specs;
4
+ }
5
+ isSatisfiedBy(v) {
6
+ const details = [];
7
+ let ok = true;
8
+ for (const spec of this.specs) {
9
+ const result = spec.isSatisfiedBy(v);
10
+ details.push(...(result.details || []));
11
+ if (result.ok)
12
+ return { ok, details };
13
+ ok = false;
14
+ }
15
+ return { ok, details };
16
+ }
17
+ }
@@ -0,0 +1,4 @@
1
+ export * from "./ISpec";
2
+ export * from "./And";
3
+ export * from "./Not";
4
+ export * from "./Or";
@@ -0,0 +1,4 @@
1
+ export * from "./ISpec";
2
+ export * from "./And";
3
+ export * from "./Not";
4
+ export * from "./Or";
package/package.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "@lafrae/specification",
3
+ "version": "0.0.2",
4
+ "engines": {
5
+ "node": ">=18"
6
+ },
7
+ "files": [
8
+ "dist",
9
+ "README.md"
10
+ ],
11
+ "scripts": {
12
+ "clean": "rm -rf ./dist",
13
+ "build": "npm run clean && npm run build:esm && npm run build:cjs",
14
+ "build:esm": "tsc -b ./tsconfig.esm.json && echo '{\"type\": \"module\"}' > dist/esm/package.json",
15
+ "build:cjs": "tsc -b ./tsconfig.cjs.json && echo '{\"type\": \"commonjs\"}' > dist/cjs/package.json"
16
+ },
17
+ "main": "./dist/cjs/index.js",
18
+ "module": "./dist/esm/index.js",
19
+ "types": "dist/index.d.ts",
20
+ "exports": {
21
+ ".": {
22
+ "types": "./dist/esm/index.d.ts",
23
+ "import": "./dist/esm/index.js",
24
+ "require": "./dist/cjs/index.js"
25
+ }
26
+ }
27
+ }