kalpesh77 commited on
Commit
db5dcad
·
verified ·
1 Parent(s): 9db58c8

Update js/adapter.js

Browse files
Files changed (1) hide show
  1. js/adapter.js +39 -189
js/adapter.js CHANGED
@@ -1,228 +1,78 @@
1
- /**
2
- * ========================================
3
- * Adapter Interface - पुरुष लेयर: स्तर ४
4
- * नासदीय संदर्भ: (प्रकृति द्वार)
5
- * ========================================
6
- * कार्य: डोमेन मॉड्यूल्ससाठी मानक JSON स्कीमा
7
- * डेटा व्हेलिडेशन आणि रूपांतरण
8
- * ========================================
9
- */
10
-
11
  class Adapter {
12
  constructor() {
13
- // मानक डेटा स्कीमा
14
  this.schema = {
15
  required: ['id', 'x', 'y', 'type'],
16
- optional: ['label', 'layer', 'meta', 'color', 'radius'],
17
- types: {
18
- id: 'string',
19
- x: 'number',
20
- y: 'number',
21
- type: 'string',
22
- label: 'string',
23
- layer: 'number',
24
- meta: 'object',
25
- color: 'string',
26
- radius: 'number'
27
- },
28
- ranges: {
29
- x: { min: 0, max: 100 },
30
- y: { min: 0, max: 100 },
31
- layer: { min: 0, max: 10 }
32
- }
33
  };
34
-
35
- // व्हेलिडेशन फ्लॅग
36
  this.strictMode = true;
37
  }
38
-
39
- /**
40
- * डोमेन डेटा व्हेलिडेट करा
41
- * @param {Object|Array} data - डोमेनकडून मिळालेला डेटा
42
- * @returns {Object} { valid: boolean, errors: Array, sanitized: Array }
43
- */
44
  validate(data) {
45
  const errors = [];
46
  const sanitized = [];
47
-
48
- // Array किंवा Object
49
  const items = Array.isArray(data) ? data : [data];
50
- items.forEach((item, index) => {
 
51
  const itemErrors = this._validateItem(item, index);
52
  errors.push(...itemErrors);
53
-
54
- // जर त्रुटी नसतील तर sanitized मध्ये ॲड करा
55
- if (itemErrors.length === 0) {
56
- sanitized.push(this._sanitize(item));
57
- }
58
  });
59
-
60
- return {
61
- valid: errors.length === 0,
62
- errors,
63
- sanitized,
64
- total: items.length,
65
- validCount: sanitized.length
66
- };
67
  }
68
-
69
- /**
70
- * एकक डेटा व्हेलिडेट करा
71
- * @param {Object} item
72
- * @param {number} index
73
- * @returns {Array} errors
74
- * @private
75
- */
76
  _validateItem(item, index) {
77
  const errors = [];
78
-
79
- // प्रकार तपासा
80
  if (typeof item !== 'object' || item === null) {
81
  errors.push(`[${index}] डेटा ऑब्जेक्ट असणे आवश्यक आहे`);
82
  return errors;
83
  }
84
-
85
- // आवश्यक फील्ड्स तपासा
86
  this.schema.required.forEach(field => {
87
- if (!(field in item)) {
88
- errors.push(`[${index}] आवश्यक फील्ड '${field}' गहाळ`);
89
- }
90
  });
91
-
92
- // डेटा प्रकार तपासा
93
  Object.keys(item).forEach(key => {
94
- if (this.schema.types[key]) {
95
- const expectedType = this.schema.types[key];
96
- const actualType = typeof item[key];
97
-
98
- if (actualType !== expectedType) {
99
- errors.push( `[${index}] '${key}' चा प्रकार '${expectedType}' अपेक्षित, '${actualType}' मिळाला`
100
- );
101
- }
102
- }
103
- });
104
-
105
- // रेंज तपासा
106
- Object.keys(this.schema.ranges).forEach(field => {
107
- if (field in item) {
108
- const range = this.schema.ranges[field];
109
- if (item[field] < range.min || item[field] > range.max) {
110
- errors.push(
111
- `[${index}] '${field}' ${range.min}-${range.max} दरम्यान असणे आवश्यक आहे`
112
- );
113
- }
114
  }
115
  });
116
-
117
  return errors;
118
  }
119
-
120
- /**
121
- * डेटा स्वच्छ करा (सॅनिटाइझ)
122
- * @param {Object} item
123
- * @returns {Object}
124
- * @private
125
- */
126
  _sanitize(item) {
127
- const sanitized = {};
128
-
129
- // सर्व फील्ड्स कॉपी करा
130
- Object.keys(item).forEach(key => {
131
- if (this.schema.required.includes(key) || this.schema.optional.includes(key)) {
132
- sanitized[key] = item[key];
133
- }
134
- });
135
-
136
- // डिफॉल्ट मूल्ये सेट करा
137
- if (!sanitized.label) sanitized.label = null;
138
- if (!sanitized.layer) sanitized.layer = 0;
139
- if (!sanitized.meta) sanitized.meta = {};
140
- if (!sanitized.color) sanitized.color = '#4a9eff';
141
- if (!sanitized.radius) sanitized.radius = 5;
142
-
143
- return sanitized;
144
  }
145
-
146
- /**
147
- * डेटासेटचे रूपांतरण करा (Parquet → Standard Format)
148
- * @param {Array} dataset - डेटासेटमधील raw डेटा * @param {Object} mapping - फील्ड मॅपिंग
149
- * @returns {Array}
150
- */
151
  transform(dataset, mapping = {}) {
152
- const defaultMapping = {
153
- x: 'x',
154
- y: 'y',
155
- id: 'id',
156
- type: 'type',
157
- label: 'label'
158
- };
159
-
160
- const fieldMap = { ...defaultMapping, ...mapping };
161
-
162
  return dataset.map(item => ({
163
- id: String(item[fieldMap.id] || `auto_${Date.now()}_${Math.random()}`),
164
  x: Number(item[fieldMap.x]),
165
  y: Number(item[fieldMap.y]),
166
- type: String(item[fieldMap.type] || 'node'),
167
  label: item[fieldMap.label] ? String(item[fieldMap.label]) : null,
168
- meta: {
169
- original: item,
170
- domain: item.domain || 'vedic',
171
- timestamp: Date.now()
172
- }
173
  }));
174
  }
175
-
176
- /**
177
- * डोमेन-स्पेसिफिक ॲडाप्टर नोंदणी करा
178
- * @param {string} domain - डोमेन नाव (उदा. 'jyotish', 'quantum', 'vastu')
179
- * @param {Object} adapter - कस्टम ॲडाप्टर
180
- */
181
- registerDomainAdapter(domain, adapter) {
182
- if (!this.domainAdapters) {
183
- this.domainAdapters = {};
184
- }
185
-
186
- this.domainAdapters[domain] = adapter;
187
- console.log(`Domain adapter नोंदवला: ${domain}`);
188
- }
189
-
190
- /**
191
- * डोमेन ॲडाप्टर वापरा
192
- * @param {string} domain
193
- * @param {any} data
194
- * @returns {Array}
195
- */
196
- adaptFromDomain(domain, data) {
197
- if (this.domainAdapters && this.domainAdapters[domain]) { return this.domainAdapters[domain].adapt(data);
198
- }
199
-
200
- // डिफॉल्ट ॲडाप्टर
201
- return this.transform(Array.isArray(data) ? data : [data]);
202
- }
203
-
204
- /**
205
- * Strict mode चालू/बंद करा
206
- * @param {boolean} enabled
207
- */
208
- setStrictMode(enabled) {
209
- this.strictMode = enabled;
210
- console.log(`Adapter strict mode: ${enabled ? 'चालू' : 'बंद'}`);
211
- }
212
-
213
- /**
214
- * स्कीमा मिळवा
215
- * @returns {Object}
216
- */
217
- getSchema() {
218
- return { ...this.schema };
219
- }
220
- }
221
 
222
- // ग्लोबल इन्स्टन्स
223
- window.VedicAdapter = new Adapter();
 
224
 
225
- // एक्सपोर्ट
226
- if (typeof module !== 'undefined' && module.exports) {
227
- module.exports = Adapter;
228
- }
 
 
 
 
 
 
 
 
 
 
 
1
  class Adapter {
2
  constructor() {
 
3
  this.schema = {
4
  required: ['id', 'x', 'y', 'type'],
5
+ types: { id: 'string', x: 'number', y: 'number', type: 'string', label: 'string', layer: 'number' },
6
+ ranges: { x: { min: 0, max: 100 }, y: { min: 0, max: 100 } }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  };
8
+ this.scaleFactor = 0.1; // Dataset 0-1000 → Core 0-100
 
9
  this.strictMode = true;
10
  }
11
+
 
 
 
 
 
12
  validate(data) {
13
  const errors = [];
14
  const sanitized = [];
 
 
15
  const items = Array.isArray(data) ? data : [data];
16
+
17
+ items.forEach((item, index) => {
18
  const itemErrors = this._validateItem(item, index);
19
  errors.push(...itemErrors);
20
+ if (itemErrors.length === 0) sanitized.push(this._sanitize(item));
 
 
 
 
21
  });
22
+
23
+ return { valid: errors.length === 0, errors, sanitized, total: items.length, validCount: sanitized.length };
 
 
 
 
 
 
24
  }
25
+
 
 
 
 
 
 
 
26
  _validateItem(item, index) {
27
  const errors = [];
 
 
28
  if (typeof item !== 'object' || item === null) {
29
  errors.push(`[${index}] डेटा ऑब्जेक्ट असणे आवश्यक आहे`);
30
  return errors;
31
  }
 
 
32
  this.schema.required.forEach(field => {
33
+ if (!(field in item)) errors.push(`[${index}] आवश्यक फील्ड '${field}' गहाळ`);
 
 
34
  });
 
 
35
  Object.keys(item).forEach(key => {
36
+ if (this.schema.types[key] && typeof item[key] !== this.schema.types[key]) {
37
+ errors.push(`[${index}] '${key}' चा प्रकार '${this.schema.types[key]}' अपेक्षित`);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  }
39
  });
 
40
  return errors;
41
  }
42
+
 
 
 
 
 
 
43
  _sanitize(item) {
44
+ // Coordinate Normalization: 0-1000 → 0-100
45
+ const normalizedX = Number(item.x) * this.scaleFactor;
46
+ const normalizedY = Number(item.y) * this.scaleFactor;
47
+
48
+ return {
49
+ id: String(item.id || `auto_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`),
50
+ x: Math.max(0, Math.min(100, normalizedX)),
51
+ y: Math.max(0, Math.min(100, normalizedY)),
52
+ type: String(item.type || 'node'),
53
+ label: item.label ? String(item.label) : null,
54
+ layer: Number(item.layer || 0),
55
+ meta: { original: item, domain: 'vedic', timestamp: Date.now() },
56
+ color: item.color || '#4a9eff',
57
+ radius: item.radius || 5
58
+ };
 
 
59
  }
60
+
 
 
 
 
 
61
  transform(dataset, mapping = {}) {
62
+ const fieldMap = { x: 'x', y: 'y', id: 'id', type: 'type', label: 'label', ...mapping };
 
 
 
 
 
 
 
 
 
63
  return dataset.map(item => ({
64
+ id: String(item[fieldMap.id]),
65
  x: Number(item[fieldMap.x]),
66
  y: Number(item[fieldMap.y]),
67
+ type: String(item[fieldMap.type]),
68
  label: item[fieldMap.label] ? String(item[fieldMap.label]) : null,
69
+ layer: Number(item[fieldMap.layer] || 0),
70
+ meta: { original: item, domain: 'vedic', timestamp: Date.now() }
 
 
 
71
  }));
72
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
 
74
+ setStrictMode(enabled) { this.strictMode = enabled; }
75
+ getSchema() { return { ...this.schema }; }
76
+ }
77
 
78
+ window.VedicAdapter = new Adapter();