fg-lib-gpt 1.0.0

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.
Files changed (2) hide show
  1. package/main.js +56 -0
  2. package/package.json +11 -0
package/main.js ADDED
@@ -0,0 +1,56 @@
1
+ const generateSchema = (schemaObj, name = 'schema') => {
2
+
3
+ const schema = obj => {
4
+ if (obj === null)
5
+ return { type: 'null' }
6
+
7
+ else if (Array.isArray(obj)) {
8
+ const items = obj.length > 0 ? schema(obj[0]) : {}
9
+ return { type: 'array', items: items }
10
+ }
11
+
12
+ else if (typeof obj === 'object') {
13
+ const properties = {}
14
+ const required = []
15
+ for (const key in obj) {
16
+ required.push(key)
17
+ properties[key] = schema(obj[key])
18
+ }
19
+ return {
20
+ type: 'object',
21
+ properties: properties,
22
+ required: required,
23
+ additionalProperties: false
24
+ }
25
+ }
26
+
27
+ else if (typeof obj === 'string') {
28
+ const enumMatch = obj.match(/^\[([a-zA-Z0-9,\s]+)\]$/) // [a, b, c]
29
+ if (enumMatch) {
30
+ const enumValues = enumMatch[1].split(',').map(x => x.trim())
31
+ return { type: 'string', enum: enumValues }
32
+ }
33
+ return { type: 'string' }
34
+ }
35
+
36
+ else if (typeof obj === 'number')
37
+ return Number.isInteger(obj) ? { type: 'integer' } : { type: 'number' }
38
+
39
+ else if (typeof obj === 'boolean')
40
+ return { type: 'boolean' }
41
+
42
+ else
43
+ return {}
44
+ }
45
+
46
+ return {
47
+ name,
48
+ strict: true,
49
+ schema: schema(schemaObj)
50
+ }
51
+ }
52
+
53
+
54
+ module.exports = {
55
+ generateSchema: generateSchema
56
+ }
package/package.json ADDED
@@ -0,0 +1,11 @@
1
+ {
2
+ "name": "fg-lib-gpt",
3
+ "version": "1.0.0",
4
+ "main": "main.js",
5
+ "scripts": {
6
+ "test": "echo \"Error: no test specified\" && exit 1"
7
+ },
8
+ "author": "",
9
+ "license": "ISC",
10
+ "description": ""
11
+ }