pawajs 1.3.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.
package/utils.js ADDED
@@ -0,0 +1,328 @@
1
+
2
+ export const splitAndAdd = str => str.split('-').join('').toUpperCase();
3
+
4
+ export const pawaWayRemover=async (comment,endComment)=>{
5
+ if (!comment?.nextSibling) {
6
+ return
7
+ }
8
+ if (comment.nextSibling === endComment) {
9
+
10
+ return
11
+ } else {
12
+ if (comment?.nextSibling?.nodeType === 8) {
13
+ // console.log(comment)
14
+ if(comment.nextSibling?._controlComponent){
15
+ comment.nextSibling._remove()
16
+ }else{
17
+ comment.nextSibling.remove()
18
+ }
19
+
20
+ } else if (comment.nextSibling.nodeType === 1) {
21
+ if (!comment.nextSibling._remove) {
22
+ comment.nextSibling.remove()
23
+ return
24
+ }
25
+ await comment.nextSibling._remove()
26
+ }
27
+ }
28
+ return await pawaWayRemover(comment,endComment)
29
+ }
30
+ export const processNode = (node, itemContext) => {
31
+
32
+ if (typeof itemContext === 'number') {
33
+ return
34
+ }
35
+
36
+ if (node.nodeType === 3) { // Text node
37
+ const text = node.textContent
38
+ const newText = text.replace(/{{(.+?)}}/g, (match, exp) => {
39
+ try {
40
+ const keys = Object.keys(itemContext)
41
+
42
+ const values = keys.map(key => itemContext[key])
43
+
44
+ return new Function(...keys, `return ${exp}`)(...values)
45
+ } catch {
46
+ return match
47
+ }
48
+ })
49
+ //console.log(newText)
50
+ node.textContent = newText
51
+ } else if (node.attributes) {
52
+ Array.from(node.attributes).forEach(attr => {
53
+ const newValue = attr.value.replace(/{{(.+?)}}/g, (match, exp) => {
54
+ try {
55
+ const keys = Object.keys(itemContext)
56
+ const values = keys.map(key => itemContext[key])
57
+ return new Function(...keys, `return ${exp}`)(...values)
58
+ } catch {
59
+ return match
60
+ }
61
+ })
62
+ attr.value = newValue
63
+ })
64
+ }
65
+ Array.from(node.childNodes).forEach((n) => {
66
+ processNode(n, itemContext)
67
+ })
68
+ }
69
+
70
+ export const extractContextValues = (context) => {
71
+ const keys = Object.keys(context);
72
+ const values = keys.map((key) => key.split('.').reduce((acc, k) => acc?.[k], context));
73
+ return { keys, values };
74
+ };
75
+
76
+ export const evaluation=(context,exp) => {
77
+ try {
78
+ const keys = Object.keys(context);
79
+ const resolvePath = (path, obj) => {
80
+ return path.split('.').reduce((acc, key) => acc?.[key], obj);
81
+ };
82
+ const values = keys.map((key) => resolvePath(key, context));
83
+ return new Function(...keys,`return ${exp}`)(...values)
84
+ } catch (e) {
85
+ throw e
86
+ }
87
+ }
88
+
89
+ export const setPawaDevError=({message,error,template})=>{
90
+ console.error(message,error.message,error.stack)
91
+ __pawaDev.setError({msg:message ,stack:error.stack,el:template})
92
+ }
93
+
94
+ export const propsValidator=(obj={},propsAttri,name,template,el)=>{
95
+ let done=true
96
+ for (const[key,value] of Object.entries(obj)) {
97
+ const propsValue=propsAttri[key]
98
+ if(typeof value === 'object'){
99
+ if(propsAttri[key] || propsAttri[key] === 0){
100
+ // console.log(propsAttri[key])
101
+ const checker=ComponentProps(propsAttri[key],value?.err,name,key)
102
+ if (value.type) {
103
+ if (Array.isArray(value.type)) {
104
+ let isValid = false
105
+ for (const type of value.type) {
106
+ try {
107
+ checker[type.name]()
108
+ isValid = true
109
+ break
110
+ } catch (error) {}
111
+ }
112
+ if (!isValid) {
113
+ const types = value.type.map(t => t.name).join(' or ')
114
+ throw new Error(value?.err ? value.err : `${key} must be type of ${types} at ${name} component`);
115
+ }
116
+ } else {
117
+ try {
118
+ checker[value.type.name]()
119
+ } catch (error) {
120
+ throw new Error(value?.err ? value.err : `${key} must be type of ${value.type.name} at ${name} component`);
121
+ }
122
+ }
123
+ }
124
+ }else{
125
+ if (value.strict) {
126
+ const msg=value.err?`${value.err}. the props is needed `: `props "${key}" is undefined at ${name}`
127
+ console.warn(`${name.toUpperCase()} component props "${key}" is needed. ${msg}`)
128
+ setPawaDevError({
129
+ message:`${name.toUpperCase()} component props "${key}" is needed. ${msg}`,
130
+ error:{
131
+ message:`This props "${key}" is needed`,
132
+ stack:`at PawaComponent.${name}`
133
+ },
134
+ template:template
135
+ })
136
+ done=false
137
+ throw new Error(`${msg} error at ${template}`);
138
+ }else{
139
+ if (value?.default !== undefined ) {
140
+ propsAttri[key]=()=>value?.default
141
+ el._props[key]=()=>value?.default
142
+ }
143
+ }
144
+
145
+ }
146
+ }
147
+ }
148
+ return done
149
+ }
150
+ export const safeEval=(context,expression,el,resolve=false)=>{
151
+ try{
152
+ const keys = Object.keys(context);
153
+ const resolvePath = (path, obj) => {
154
+ return path.split('.').reduce((acc, key) => acc?.[key], obj);
155
+ };
156
+ if(resolve){
157
+ return new Function(...keys,`
158
+ try{
159
+ return ${expression}
160
+ }catch(error){
161
+ console.error(error.message,error.stack)
162
+ __pawaDev.setError({msg:error.message,stack:error.stack})
163
+ }
164
+ `)(...getEvalValues(context))
165
+ }else{
166
+ return new Function(...keys,`
167
+ try{
168
+ return ${expression}
169
+ }catch(error){
170
+ console.error(error.message,error.stack)
171
+ __pawaDev.setError({msg:error.message,stack:error.stack})
172
+ }
173
+ `)
174
+ }
175
+
176
+ }catch(error){
177
+ setPawaDevError({
178
+ message:`Error : ${error.message} ${error.stack}`,
179
+ error:error,
180
+ template:el._template
181
+ })
182
+ }
183
+ }
184
+ /**
185
+ *
186
+ * @param {object} context
187
+ * @returns {Array}
188
+ */
189
+ export const getEvalValues=(context)=>{
190
+ const keys = Object.keys(context);
191
+ const resolvePath = (path, obj) => {
192
+ return path.split('.').reduce((acc, key) => acc?.[key], obj);
193
+ };
194
+ const values = keys.map((key) => resolvePath(key, context))
195
+ return values
196
+ }
197
+ export const sanitizeTemplate = (temp) => {
198
+ if (typeof temp !== 'string') return '';
199
+ return temp.replace(/<script\b[^>]*>([\s\S]*?)<\/script>/gi, '');
200
+ };
201
+
202
+ export const ComponentProps=(somes,message,name,key)=>{
203
+ let some=somes?.() || somes
204
+ return({
205
+ Array:()=>{
206
+
207
+ if (Array.isArray(some)) {
208
+ return true
209
+ }else{
210
+ throw new Error(message ?message + ' / Not type of an Array ': `${key} must be an array at ${name} component`);
211
+
212
+ }
213
+ },
214
+ String:()=>{
215
+ if (typeof some === 'string') {
216
+ return true
217
+ }else{
218
+ throw new Error(message? message + ' / Not type of a String' :`${key} must be a string at ${name} component`);
219
+
220
+ }
221
+ },
222
+ Number:()=>{
223
+ if (typeof some === 'number') {
224
+ return true
225
+ }else{
226
+ throw new Error(message? message+' / Not type of a Number ': `${key} must be a number at ${name} component`);
227
+
228
+ }
229
+ },
230
+ Object:()=>{
231
+ if (typeof some === 'object') {
232
+ return true
233
+ }else{
234
+ throw new Error(message? message+' / Not type of an Object ' :`${key} must be an object at ${name} component`);
235
+
236
+ }
237
+ },
238
+ Function:()=>{
239
+ if (typeof some === 'function') {
240
+ return true
241
+ }else{
242
+ throw new Error(message? message+' / Not type of a Function ': `${key} must be a function at ${name} component`);
243
+ }
244
+ },
245
+ Boolean:()=>{
246
+ if (typeof some === 'boolean') {
247
+ return true
248
+ }else{
249
+ throw new Error(message? message+' / Not type of a Boolean ' :`${key} must be a Boolean at ${name} component`);
250
+
251
+ }
252
+ },
253
+ })
254
+ }
255
+
256
+ export const getComment=(node,setComment,value,fors='notKey')=>{
257
+ if(node.previousSibling.nodeType === 8 && node.previousSibling?.data){
258
+ const c=node.previousSibling.data.split('@-$@-$@')
259
+ if (c[1] === value && c[0] !== 'forKey') {
260
+ setComment(node.previousSibling)
261
+ }else{
262
+ getComment(node.previousSibling,setComment,value)
263
+ }
264
+
265
+ }else{
266
+ getComment(node.previousSibling,setComment,value)
267
+ }
268
+ }
269
+ export const getEndComment=(comment,setEndComment,value,children)=>{
270
+ const isComment=comment.nextSibling
271
+ if (comment.nextSibling.nodeType === 8 ) {
272
+ const data=isComment.data.split('@-$@-$@')
273
+ const check=data[0]
274
+ if(data[1] === value){
275
+ if(check === 'forKey' || check === 'endForKey'){
276
+ getEndComment(isComment,setEndComment,value,children)
277
+ return
278
+ }
279
+ setEndComment(isComment)
280
+ }else{
281
+ getEndComment(isComment,setEndComment,value,children)
282
+ }
283
+ }else if (isComment.nodeType === 1) {
284
+ children.push(isComment)
285
+ getEndComment(isComment,setEndComment,value,children)
286
+ }
287
+ else{
288
+ getEndComment(isComment,setEndComment,value,children)
289
+ }
290
+ }
291
+ export const replaceOperators = (expression) => {
292
+ return expression
293
+ .replace(/#/g, '=')
294
+ .replace(/\^/g, '>')
295
+ .replace(/!\^/g, '<')
296
+ .replace(/@/g, ' ')
297
+ .replace(/::(.)(?=.)/g, (_, char) => char.toUpperCase())
298
+ .replace(/%/g, '"');
299
+ };
300
+
301
+ export const replaceTemplateOperators = (expression) => {
302
+ return expression
303
+ .replace(/\/\*/g, '`')
304
+ .replace(/\*\//g, '`'); // Also replace closing */ with backtick if needed
305
+ // .replace(/&gt;/g,'>')
306
+ };
307
+
308
+ export function stringToUniqueNumber(str) {
309
+ let hash = 0;
310
+ for (let i = 0; i < str.length; i++) {
311
+ const char = str.charCodeAt(i);
312
+ hash = (hash << 5) - hash + char;
313
+ hash = hash & hash; // Convert to 32bit integer
314
+ }
315
+ return Math.abs(hash);
316
+ }
317
+
318
+ /**
319
+ * @param {string[]} array
320
+ * @param {string} str The string to match
321
+ * @returns {boolean}
322
+ */
323
+ export function checkKeywordsExistence(array,str){
324
+ if(!Array.isArray(array) || array.length === 0) return
325
+ const pattern = array.join('|')
326
+ const regex=new RegExp(`(?<!\\w)(${pattern})(?!\\w)`,'i')
327
+ return regex.test(str)
328
+ }