ondc-code-generator 0.0.6 → 0.0.7

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/dist/example.js CHANGED
@@ -2,9 +2,9 @@
2
2
  // import { readFileSync } from "fs";
3
3
  // import path from "path";
4
4
  // import { fileURLToPath } from "url";
5
- // import { SupportedLanguages } from "./types/compiler-types.js";
6
5
  // const __filename = fileURLToPath(import.meta.url);
7
6
  // const __dirname = path.dirname(__filename);
7
+ // import { SupportedLanguages } from "./types/compiler-types.js";
8
8
  // const main = async () => {
9
9
  // const compiler = new ConfigCompiler(SupportedLanguages.Typescript);
10
10
  // const buildPath = path.resolve(__dirname, "../samples/build.yaml");
@@ -0,0 +1,36 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>API SERVICE VALIDATIONS</title>
7
+ <!-- Link to the CSS file -->
8
+ <link rel="stylesheet" href="./style.css">
9
+ </head>
10
+ <body>
11
+ <div id="content">
12
+ <h2>List of validations of api</h2>
13
+ {{{content}}}
14
+ </div>
15
+ </body>
16
+ <script>
17
+ document.addEventListener('DOMContentLoaded', function() {
18
+ // Select all list items that have a nested <ul> (collapsible items)
19
+ const collapsibleItems = document.querySelectorAll('li > ul');
20
+
21
+ collapsibleItems.forEach(function(nestedList) {
22
+ const parentLi = nestedList.parentElement;
23
+ parentLi.classList.add('collapsible', 'collapsed');
24
+
25
+ // Add click event listener to the <p> inside the list item
26
+ const clickableArea = parentLi.querySelector('p');
27
+ clickableArea.addEventListener('click', function(e) {
28
+ // Prevent click events on links inside the <p>
29
+ if (e.target.tagName !== 'A') {
30
+ parentLi.classList.toggle('collapsed');
31
+ }
32
+ });
33
+ });
34
+ });
35
+ </script>
36
+ </html>
@@ -0,0 +1,204 @@
1
+ /* RESET DEFAULT STYLES */
2
+ body, ul, li, p, blockquote {
3
+ margin: 0;
4
+ padding: 0;
5
+ }
6
+ ul, li {
7
+ list-style: none;
8
+ }
9
+
10
+ /* BODY STYLES */
11
+ body {
12
+ font-family: "Helvetica Neue", Arial, sans-serif;
13
+ background-color: #ffffff; /* Light background */
14
+ color: #333333; /* Dark text */
15
+ line-height: 1.8;
16
+ padding: 20px;
17
+ }
18
+
19
+ /* LIST STYLES */
20
+ ul {
21
+ padding-left: 20px;
22
+ }
23
+
24
+ li {
25
+ margin-bottom: 16px;
26
+ position: relative;
27
+ }
28
+
29
+ li > p {
30
+ margin-bottom: 8px;
31
+ background-color: #f7f7f7; /* Light background */
32
+ color: #555555; /* Dark text */
33
+ padding: 10px 15px;
34
+ border: 1px solid #ddd;
35
+ border-radius: 5px;
36
+ box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
37
+ transition: background-color 0.3s, border 0.3s, box-shadow 0.3s;
38
+ }
39
+
40
+ li > p:hover {
41
+ background-color: #e8e8e8;
42
+ border-color: #1296d9; /* Updated to blue accent color */
43
+ box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
44
+ }
45
+
46
+ /* NESTED LIST ITEMS */
47
+ ul ul li > p {
48
+ background-color: #f0f0f0; /* Slightly darker than parent */
49
+ }
50
+
51
+ ul ul li > p:hover {
52
+ background-color: #e0e0e0;
53
+ }
54
+
55
+ /* HEADINGS AND EMPHASIS TEXT */
56
+ h2,
57
+ li > p > strong {
58
+ color: #1296d9; /* Changed header color to match blue theme */
59
+ font-weight: 600;
60
+ }
61
+
62
+ li > p > strong {
63
+ font-size: 1.1em;
64
+ }
65
+
66
+ /* BLOCKQUOTE STYLES */
67
+ blockquote {
68
+ margin: 15px 0;
69
+ padding: 15px 20px;
70
+ background-color: #f7f7f7;
71
+ border-left: 5px solid #1296d9; /* Updated border to blue */
72
+ color: #555555;
73
+ font-style: italic;
74
+ box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
75
+ transition: background-color 0.3s, border-left-color 0.3s, box-shadow 0.3s;
76
+ }
77
+
78
+ blockquote:hover {
79
+ background-color: #e8e8e8;
80
+ border-left-color: #1296d9; /* Updated hover border to blue */
81
+ box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
82
+ }
83
+
84
+ /* BLOCKQUOTE INTERNAL ELEMENTS */
85
+ blockquote > p {
86
+ margin-bottom: 12px;
87
+ padding: 10px 15px;
88
+ background-color: #f7f7f7;
89
+ border-radius: 5px;
90
+ font-size: 1em;
91
+ box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
92
+ transition: background-color 0.3s, border 0.3s, box-shadow 0.3s;
93
+ }
94
+
95
+ blockquote > p:hover {
96
+ background-color: #e8e8e8;
97
+ border: 1px solid #1296d9; /* Updated border to blue on hover */
98
+ box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
99
+ }
100
+
101
+ blockquote > ul {
102
+ padding-left: 20px;
103
+ margin-top: 10px;
104
+ }
105
+
106
+ blockquote > ul > li {
107
+ margin-bottom: 16px;
108
+ position: relative;
109
+ background-color: #f7f7f7;
110
+ padding: 10px 15px;
111
+ border: 1px solid #ddd;
112
+ border-radius: 5px;
113
+ box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
114
+ transition: background-color 0.3s, border 0.3s, box-shadow 0.3s;
115
+ }
116
+
117
+ blockquote > ul > li:hover {
118
+ background-color: #e8e8e8;
119
+ border-color: #1296d9; /* Updated border to blue on hover */
120
+ box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
121
+ }
122
+
123
+ blockquote > ul > li > strong {
124
+ color: #1296d9; /* Changed to blue for emphasis */
125
+ font-weight: 600;
126
+ font-size: 1.1em;
127
+ }
128
+
129
+ blockquote > ul > li > ul {
130
+ padding-left: 20px;
131
+ margin-top: 8px;
132
+ }
133
+
134
+ blockquote > ul > li > ul > li {
135
+ background-color: #f0f0f0;
136
+ padding: 8px 12px;
137
+ border: 1px solid #ccc;
138
+ border-radius: 4px;
139
+ box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
140
+ transition: background-color 0.3s, border 0.3s, box-shadow 0.3s;
141
+ }
142
+
143
+ blockquote > ul > li > ul > li:hover {
144
+ background-color: #e0e0e0;
145
+ border-color: #1296d9; /* Updated border to blue on hover */
146
+ box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
147
+ }
148
+
149
+ /* COLLAPSIBLE INDICATORS */
150
+ li.collapsible > p {
151
+ cursor: pointer;
152
+ }
153
+
154
+ li.collapsible > p::before {
155
+ content: "▼";
156
+ display: inline-block;
157
+ margin-right: 8px;
158
+ transition: transform 0.3s, color 0.3s;
159
+ color: #1296d9; /* Updated color to blue */
160
+ }
161
+
162
+ li.collapsible.collapsed > p::before {
163
+ transform: rotate(-90deg);
164
+ }
165
+
166
+ li.collapsible > p:hover::before {
167
+ color: #1296d9; /* Lighter blue on hover */
168
+ }
169
+
170
+ /* Hide nested lists by default */
171
+ li.collapsible.collapsed > ul {
172
+ display: none;
173
+ }
174
+
175
+ /* RESPONSIVE DESIGN */
176
+ @media only screen and (max-width: 768px) {
177
+ body {
178
+ padding: 15px;
179
+ }
180
+ ul {
181
+ padding-left: 15px;
182
+ }
183
+ li > p,
184
+ blockquote > p,
185
+ blockquote > ul > li {
186
+ font-size: 0.95em;
187
+ padding: 8px 12px;
188
+ }
189
+ }
190
+
191
+ /* CUSTOM SCROLLBAR */
192
+ ::-webkit-scrollbar {
193
+ width: 8px;
194
+ background-color: #ffffff;
195
+ }
196
+
197
+ ::-webkit-scrollbar-thumb {
198
+ background-color: #ccc;
199
+ border-radius: 10px;
200
+ }
201
+
202
+ ::-webkit-scrollbar-thumb:hover {
203
+ background-color: #bbb;
204
+ }
@@ -0,0 +1,7 @@
1
+ import payloadUtils from "../utils/json-path-utils";
2
+ import validations from "../utils/validation-utils";
3
+ import { testFunctionArray, validationInput, validationOutput } from "../types/test-config";
4
+
5
+
6
+
7
+ export default {{{functionCode}}}
@@ -0,0 +1,17 @@
1
+ import jsonpath from "jsonpath";
2
+ function getJsonPath(payload: any, path: string) {
3
+ let output = jsonpath.query(payload, path);
4
+ if (isListOfStringsOrNull(output)) {
5
+ output = output.map((o) => (o === null ? "null" : o));
6
+ }
7
+ return output.length === 0 ? [] : output;
8
+ }
9
+ function isListOfStringsOrNull(variable: any): boolean {
10
+ return (
11
+ Array.isArray(variable) &&
12
+ variable.every((item) => item === null || typeof item === "string")
13
+ );
14
+ }
15
+ export default {
16
+ getJsonPath,
17
+ };
@@ -0,0 +1,18 @@
1
+
2
+
3
+ {{#actions}}
4
+ import { {{{action}}} } from './{{{action}}}'
5
+ {{/actions}}
6
+
7
+
8
+ export default function getSchema(action : string){
9
+ switch(action){
10
+ {{#actions}}
11
+ case '{{{action}}}':
12
+ return {{{action}}};
13
+ {{/actions}}
14
+ default:
15
+ throw new Error('Action not found');
16
+ }
17
+ }
18
+
@@ -0,0 +1,28 @@
1
+
2
+ export interface ValidationConfig {
3
+ runAllValidations: boolean;
4
+ }
5
+
6
+ export type validationOutput = {
7
+ valid: boolean;
8
+ code: number;
9
+ description?: string;
10
+ }[];
11
+
12
+ /*
13
+ {% comment %} export type ExternalData = {
14
+ {{#externalData}}
15
+ {{name}}?: string[];
16
+ {{/externalData}}
17
+ }; {% endcomment %}
18
+ */
19
+
20
+ export type ExternalData = {}
21
+
22
+ export type validationInput = {
23
+ payload: any;
24
+ externalData: ExternalData;
25
+ config: ValidationConfig
26
+ };
27
+
28
+ export type testFunctionArray = Array<(input: validationInput) => validationOutput>;
@@ -0,0 +1,20 @@
1
+ function {{name}}(input: validationInput): validationOutput {
2
+ const scope = payloadUtils.getJsonPath(input.payload,"{{{scopePath}}}");
3
+ let subResults: validationOutput = [];
4
+ let valid = true;
5
+ for(const testObj of scope){
6
+ testObj._EXTERNAL = input.externalData;
7
+ {{#variables}}
8
+ const {{name}} = {{{value}}}
9
+ {{/variables}}
10
+
11
+ {{#hasContinue}}
12
+ const skipCheck = {{{skipCheckStatement}}};
13
+ if(skipCheck) continue;
14
+ {{/hasContinue}}
15
+
16
+ {{{validationCode}}}
17
+ delete testObj._EXTERNAL;
18
+ }
19
+ return [{valid: valid,code: {{successCode}} },...subResults];
20
+ }
@@ -0,0 +1,39 @@
1
+
2
+ {{#isNested}}
3
+
4
+ {{{nestedFunctions}}}
5
+
6
+ const testFunctions: testFunctionArray = [
7
+ {{#names}}
8
+ {{name}},
9
+ {{/names}}
10
+ ];
11
+
12
+ let invalidResults: validationOutput = [];
13
+ for (const fn of testFunctions) {
14
+ const subResult = fn(input);
15
+ // .filter(r => !r.valid);
16
+ invalidResults = [...invalidResults, ...subResult];
17
+ if(!input.config.runAllValidations && invalidResults.length > 0) {
18
+ return invalidResults;
19
+ }
20
+ }
21
+ if(invalidResults.length > 0) {
22
+ // return invalidResults;
23
+ subResults = invalidResults;
24
+ valid = subResults.every(r => r.valid);
25
+ }
26
+
27
+ {{/isNested}}
28
+
29
+ {{^isNested}}
30
+ const validate = {{{returnStatement}}}
31
+
32
+ if(!validate){
33
+ return [{
34
+ valid: false,
35
+ code: {{errorCode}},
36
+ description: `{{{errorDescription}}}`
37
+ }]
38
+ }
39
+ {{/isNested}}
@@ -0,0 +1,117 @@
1
+ function areUnique(operand: string[]) {
2
+ const valuesSet = new Set(operand);
3
+ return valuesSet.size === operand.length;
4
+ }
5
+
6
+ function arePresent(operand: string[]) {
7
+
8
+ return noneIn(operand, ["", "null", "undefined"]) && operand.length > 0;
9
+ }
10
+
11
+ function allIn(left: string[], right: string[]) {
12
+ return left.every((v) => right.includes(v));
13
+ }
14
+
15
+ function anyIn(left: string[], right: string[]) {
16
+ return left.some((v) => right.includes(v));
17
+ }
18
+
19
+ function noneIn(left: string[], right: string[]) {
20
+ return left.every((v) => !right.includes(v));
21
+ }
22
+
23
+ function equalTo(left: string[], right: string[]) {
24
+ if (left.length !== right.length) return false;
25
+ return left.every((v, i) => v === right[i]);
26
+ }
27
+
28
+ function greaterThan(left: string[], right: string[]): boolean {
29
+ // Check if all elements in an array are ISO 8601 dates
30
+ const areAllISO = (arr: string[]) => arr.every((v) => isISO8601(v));
31
+ // Check if all elements in an array are numbers
32
+ const areAllNumbers = (arr: string[]) =>
33
+ arr.every((v) => !isNaN(Number(v)));
34
+
35
+ if (areAllISO(left) && areAllISO(right)) {
36
+ // Compare ISO 8601 dates
37
+ const leftDates = left.map((date) => new Date(date).getTime());
38
+ const rightDates = right.map((date) => new Date(date).getTime());
39
+ return leftDates.every(
40
+ (ld, index) => ld > rightDates[index] || index >= rightDates.length,
41
+ );
42
+ } else if (areAllNumbers(left) && areAllNumbers(right)) {
43
+ // Compare numbers
44
+ const leftNumbers = left.map(Number);
45
+ const rightNumbers = right.map(Number);
46
+ return leftNumbers.every(
47
+ (ln, index) =>
48
+ ln > rightNumbers[index] || index >= rightNumbers.length,
49
+ );
50
+ }
51
+
52
+ // If neither condition is met, return false
53
+ return false;
54
+ }
55
+
56
+ function lessThan(left: string[], right: string[]): boolean {
57
+ // Check if all elements in an array are ISO 8601 dates
58
+ const areAllISO = (arr: string[]) => arr.every((v) => isISO8601(v));
59
+ // Check if all elements in an array are numbers
60
+ const areAllNumbers = (arr: string[]) =>
61
+ arr.every((v) => !isNaN(Number(v)));
62
+
63
+ if (areAllISO(left) && areAllISO(right)) {
64
+ // Compare ISO 8601 dates
65
+ const leftDates = left.map((date) => new Date(date).getTime());
66
+ const rightDates = right.map((date) => new Date(date).getTime());
67
+ return leftDates.every(
68
+ (ld, index) => ld < rightDates[index] || index >= rightDates.length,
69
+ );
70
+ } else if (areAllNumbers(left) && areAllNumbers(right)) {
71
+ // Compare numbers
72
+ const leftNumbers = left.map(Number);
73
+ const rightNumbers = right.map(Number);
74
+ return leftNumbers.every(
75
+ (ln, index) =>
76
+ ln < rightNumbers[index] || index >= rightNumbers.length,
77
+ );
78
+ }
79
+
80
+ // If neither condition is met, return false
81
+ return false;
82
+ }
83
+
84
+ function followRegex(left: string[], regexArray: string[]) {
85
+ for (const regex of regexArray) {
86
+ const re = new RegExp(regex);
87
+ if (left.some((v) => !re.test(v))) return false;
88
+ }
89
+ return true;
90
+ }
91
+
92
+ function isISO8601(str: string): boolean {
93
+ // Regular expression to match ISO 8601 format
94
+ const iso8601Regex =
95
+ /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})$/;
96
+
97
+ // Check if the string matches the ISO 8601 format
98
+ if (!iso8601Regex.test(str)) {
99
+ return false;
100
+ }
101
+
102
+ // Check if the string is a valid date
103
+ const date = new Date(str);
104
+ return !isNaN(date.getTime());
105
+ }
106
+
107
+ export default {
108
+ areUnique,
109
+ arePresent,
110
+ allIn,
111
+ anyIn,
112
+ noneIn,
113
+ equalTo,
114
+ followRegex,
115
+ greaterThan,
116
+ lessThan,
117
+ };
@@ -35,8 +35,8 @@ export class TypescriptGenerator extends CodeGenerator {
35
35
  }
36
36
  };
37
37
  this.generateCode = async () => {
38
- const jsonPathUtilsCode = readFileSync(path.resolve(__dirname, "./templates/json-path-utils.ts"), "utf-8");
39
- const validtionUtils = readFileSync(path.resolve(__dirname, "./templates/validation-utils.ts"), "utf-8");
38
+ const jsonPathUtilsCode = readFileSync(path.resolve(__dirname, "./templates/json-path-utils.mustache"), "utf-8");
39
+ const validtionUtils = readFileSync(path.resolve(__dirname, "./templates/validation-utils.mustache"), "utf-8");
40
40
  const typesTemplate = readFileSync(path.resolve(__dirname, "./templates/test-config.mustache"), "utf-8");
41
41
  const typesCode = Mustache.render(typesTemplate, {
42
42
  externalData: this.getExternalKeys(),
package/dist/index.d.ts CHANGED
@@ -1,2 +1 @@
1
- import { ConfigCompiler } from "./generator/config-compiler.js";
2
- export { ConfigCompiler };
1
+ export {};
package/dist/index.js CHANGED
@@ -1,2 +1,42 @@
1
+ // import { ConfigCompiler } from "./generator/config-compiler.js";
2
+ // export { ConfigCompiler };
3
+ import { readFileSync } from "fs";
4
+ import path from "path";
5
+ import { fileURLToPath } from "url";
6
+ import { SupportedLanguages } from "./types/compiler-types.js";
1
7
  import { ConfigCompiler } from "./generator/config-compiler.js";
2
- export { ConfigCompiler };
8
+ // Get __dirname equivalent
9
+ const __filename = fileURLToPath(import.meta.url);
10
+ const __dirname = path.dirname(__filename);
11
+ const main = async () => {
12
+ const compiler = new ConfigCompiler(SupportedLanguages.Typescript);
13
+ const buildPath = path.resolve(__dirname, "../samples/build.yaml");
14
+ const valConfigPath = path.resolve(__dirname, "../samples/validation-config.json");
15
+ const buildYaml = readFileSync(buildPath, "utf-8");
16
+ const valConfig = JSON.parse(readFileSync(valConfigPath, "utf-8"));
17
+ await compiler.initialize(buildYaml);
18
+ await compiler.generateCode(valConfig);
19
+ // await compiler.generateL0Schema();
20
+ // await compiler.performValidations(valConfig);
21
+ // const inputs = [
22
+ // "a are unique && b are unique",
23
+ // "a are unique",
24
+ // "(a are unique && b are unique) || c are unique",
25
+ // "a are unique && b are unique || c are unique",
26
+ // "a are unique && (b are unique || c are unique)",
27
+ // "a are unique && b are unique && c are unique && d are unique",
28
+ // "a are unique && !(b are unique || !(c are unique))",
29
+ // "a are unique && (b are unique || !(!(c are unique))) && !(d are unique)",
30
+ // ];
31
+ // let output = "";
32
+ // for (const input of inputs) {
33
+ // const cst = parseReturnInput(input);
34
+ // const ast = buildAst(cst);
35
+ // const out = CompileToMarkdown(ast, "A", 0, false);
36
+ // output += `\n## ${input}\n\n${out}\n`;
37
+ // }
38
+ // writeFileSync(path.resolve(__dirname, "../samples/output.md"), output);
39
+ };
40
+ (async () => {
41
+ await main();
42
+ })();
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "ondc-code-generator",
3
- "version": "0.0.6",
3
+ "version": "0.0.7",
4
4
  "description": "generate code from build.yaml ",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",
7
7
  "scripts": {
8
- "build": "tsc",
8
+ "build": "tsc && copyfiles -u 1 \"src/**/*.{yaml,html,css,mustache}\" dist/",
9
9
  "start": "node ./dist/index.js",
10
10
  "dev": "nodemon",
11
11
  "clean": "rm -rf dist",
@@ -20,6 +20,7 @@
20
20
  "@types/jsonpath": "^0.2.4",
21
21
  "@types/mustache": "^4.2.5",
22
22
  "@types/node": "^22.10.1",
23
+ "copyfiles": "^2.4.1",
23
24
  "esm-loader-typescript": "^1.0.6",
24
25
  "nodemon": "^3.1.7",
25
26
  "ts-node": "^10.9.2",
@@ -1,5 +0,0 @@
1
- declare function getJsonPath(payload: any, path: string): any[];
2
- declare const _default: {
3
- getJsonPath: typeof getJsonPath;
4
- };
5
- export default _default;
@@ -1,15 +0,0 @@
1
- import jsonpath from "jsonpath";
2
- function getJsonPath(payload, path) {
3
- let output = jsonpath.query(payload, path);
4
- if (isListOfStringsOrNull(output)) {
5
- output = output.map((o) => (o === null ? "null" : o));
6
- }
7
- return output.length === 0 ? [] : output;
8
- }
9
- function isListOfStringsOrNull(variable) {
10
- return (Array.isArray(variable) &&
11
- variable.every((item) => item === null || typeof item === "string"));
12
- }
13
- export default {
14
- getJsonPath,
15
- };
@@ -1,21 +0,0 @@
1
- declare function areUnique(operand: string[]): boolean;
2
- declare function arePresent(operand: string[]): boolean;
3
- declare function allIn(left: string[], right: string[]): boolean;
4
- declare function anyIn(left: string[], right: string[]): boolean;
5
- declare function noneIn(left: string[], right: string[]): boolean;
6
- declare function equalTo(left: string[], right: string[]): boolean;
7
- declare function greaterThan(left: string[], right: string[]): boolean;
8
- declare function lessThan(left: string[], right: string[]): boolean;
9
- declare function followRegex(left: string[], regexArray: string[]): boolean;
10
- declare const _default: {
11
- areUnique: typeof areUnique;
12
- arePresent: typeof arePresent;
13
- allIn: typeof allIn;
14
- anyIn: typeof anyIn;
15
- noneIn: typeof noneIn;
16
- equalTo: typeof equalTo;
17
- followRegex: typeof followRegex;
18
- greaterThan: typeof greaterThan;
19
- lessThan: typeof lessThan;
20
- };
21
- export default _default;
@@ -1,91 +0,0 @@
1
- function areUnique(operand) {
2
- const valuesSet = new Set(operand);
3
- return valuesSet.size === operand.length;
4
- }
5
- function arePresent(operand) {
6
- return noneIn(operand, ["", "null", "undefined"]) && operand.length > 0;
7
- }
8
- function allIn(left, right) {
9
- return left.every((v) => right.includes(v));
10
- }
11
- function anyIn(left, right) {
12
- return left.some((v) => right.includes(v));
13
- }
14
- function noneIn(left, right) {
15
- return left.every((v) => !right.includes(v));
16
- }
17
- function equalTo(left, right) {
18
- if (left.length !== right.length)
19
- return false;
20
- return left.every((v, i) => v === right[i]);
21
- }
22
- function greaterThan(left, right) {
23
- // Check if all elements in an array are ISO 8601 dates
24
- const areAllISO = (arr) => arr.every((v) => isISO8601(v));
25
- // Check if all elements in an array are numbers
26
- const areAllNumbers = (arr) => arr.every((v) => !isNaN(Number(v)));
27
- if (areAllISO(left) && areAllISO(right)) {
28
- // Compare ISO 8601 dates
29
- const leftDates = left.map((date) => new Date(date).getTime());
30
- const rightDates = right.map((date) => new Date(date).getTime());
31
- return leftDates.every((ld, index) => ld > rightDates[index] || index >= rightDates.length);
32
- }
33
- else if (areAllNumbers(left) && areAllNumbers(right)) {
34
- // Compare numbers
35
- const leftNumbers = left.map(Number);
36
- const rightNumbers = right.map(Number);
37
- return leftNumbers.every((ln, index) => ln > rightNumbers[index] || index >= rightNumbers.length);
38
- }
39
- // If neither condition is met, return false
40
- return false;
41
- }
42
- function lessThan(left, right) {
43
- // Check if all elements in an array are ISO 8601 dates
44
- const areAllISO = (arr) => arr.every((v) => isISO8601(v));
45
- // Check if all elements in an array are numbers
46
- const areAllNumbers = (arr) => arr.every((v) => !isNaN(Number(v)));
47
- if (areAllISO(left) && areAllISO(right)) {
48
- // Compare ISO 8601 dates
49
- const leftDates = left.map((date) => new Date(date).getTime());
50
- const rightDates = right.map((date) => new Date(date).getTime());
51
- return leftDates.every((ld, index) => ld < rightDates[index] || index >= rightDates.length);
52
- }
53
- else if (areAllNumbers(left) && areAllNumbers(right)) {
54
- // Compare numbers
55
- const leftNumbers = left.map(Number);
56
- const rightNumbers = right.map(Number);
57
- return leftNumbers.every((ln, index) => ln < rightNumbers[index] || index >= rightNumbers.length);
58
- }
59
- // If neither condition is met, return false
60
- return false;
61
- }
62
- function followRegex(left, regexArray) {
63
- for (const regex of regexArray) {
64
- const re = new RegExp(regex);
65
- if (left.some((v) => !re.test(v)))
66
- return false;
67
- }
68
- return true;
69
- }
70
- function isISO8601(str) {
71
- // Regular expression to match ISO 8601 format
72
- const iso8601Regex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})$/;
73
- // Check if the string matches the ISO 8601 format
74
- if (!iso8601Regex.test(str)) {
75
- return false;
76
- }
77
- // Check if the string is a valid date
78
- const date = new Date(str);
79
- return !isNaN(date.getTime());
80
- }
81
- export default {
82
- areUnique,
83
- arePresent,
84
- allIn,
85
- anyIn,
86
- noneIn,
87
- equalTo,
88
- followRegex,
89
- greaterThan,
90
- lessThan,
91
- };