@cocreate/utils 1.11.0 → 1.12.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/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ # [1.12.0](https://github.com/CoCreate-app/CoCreate-utils/compare/v1.11.0...v1.12.0) (2022-11-25)
2
+
3
+
4
+ ### Features
5
+
6
+ * functions for search, query and sorting objects and arrays ([a0ef76d](https://github.com/CoCreate-app/CoCreate-utils/commit/a0ef76d24969e3679159987b2f3c4633bc46c138))
7
+
1
8
  # [1.11.0](https://github.com/CoCreate-app/CoCreate-utils/compare/v1.10.23...v1.11.0) (2022-11-25)
2
9
 
3
10
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/utils",
3
- "version": "1.11.0",
3
+ "version": "1.12.0",
4
4
  "description": "A simple utils component in vanilla javascript. Easily configured using HTML5 attributes and/or JavaScript API.",
5
5
  "keywords": [
6
6
  "utils",
@@ -61,6 +61,6 @@
61
61
  "webpack-log": "^3.0.1"
62
62
  },
63
63
  "dependencies": {
64
- "@cocreate/docs": "^1.4.3"
64
+ "@cocreate/docs": "^1.4.4"
65
65
  }
66
66
  }
package/src/utils.js CHANGED
@@ -74,9 +74,9 @@
74
74
  }
75
75
 
76
76
  function parseTextToHtml(text) {
77
- let doc = new DOMParser().parseFromString(text, "text/html");
78
- if (doc.head.children[0]) return doc.head.children[0];
79
- else return doc.body.children[0];
77
+ let doc = new DOMParser().parseFromString(text, "text/html");
78
+ if (doc.head.children[0]) return doc.head.children[0];
79
+ else return doc.body.children[0];
80
80
  }
81
81
 
82
82
  function cssPath(node, container) {
@@ -247,6 +247,228 @@
247
247
  }
248
248
  }
249
249
 
250
+ function searchData(data, filter) {
251
+ if (filter && filter.search) {
252
+ if (filter['search']['type'] == 'and') {
253
+ data = andSearch(data, filter['search']['value']);
254
+ } else {
255
+ data = orSearch(data, filter['search']['value']);
256
+ }
257
+
258
+ const total = data.length;
259
+ const startIndex = filter.startIndex;
260
+ const count = filter.count;
261
+ let result_data = [];
262
+
263
+ if (startIndex)
264
+ data = data.slice(startIndex, total);
265
+ if (count)
266
+ data = data.slice(0, count)
267
+
268
+ result_data = data;
269
+ filter['startIndex'] = startIndex
270
+ if (count)
271
+ filter['count'] = count
272
+ filter['total'] = total
273
+ return result_data
274
+ } else {
275
+ return data
276
+ }
277
+ }
278
+
279
+ //. or operator
280
+ function orSearch(results, search) {
281
+ var tmp
282
+ if (search && search.length > 0) {
283
+
284
+ tmp = results.filter(function(item) {
285
+
286
+ for (var key in item) {
287
+ var value = item[key];
288
+ var __status = false;
289
+
290
+ var str_value = value;
291
+
292
+ if (Array.isArray(str_value) || typeof str_value == 'number') {
293
+ str_value = str_value.toString();
294
+ }
295
+
296
+ if (typeof str_value == 'string') {
297
+ str_value = str_value.toUpperCase();
298
+ }
299
+
300
+ for (let i = 0; i < search.length; i++) {
301
+ if (typeof search[i] == 'string' && typeof str_value == 'string') {
302
+ if (str_value.indexOf(search[i].toUpperCase()) > -1) {
303
+ __status = true;
304
+ break;
305
+ }
306
+ } else {
307
+ if (value == search[i]) {
308
+ __status = true;
309
+ break;
310
+ }
311
+ }
312
+ }
313
+
314
+ if (__status) {
315
+ return true;
316
+ }
317
+ }
318
+
319
+ return false;
320
+ })
321
+ } else {
322
+ tmp = results;
323
+ }
324
+
325
+ return tmp;
326
+ }
327
+
328
+
329
+ //. and operator
330
+ function andSearch(results, search) {
331
+ var tmp
332
+ if (search && search.length > 0) {
333
+
334
+ tmp = results.filter(function(item) {
335
+
336
+ for (let i = 0; i < search.length; i++) {
337
+ var __status = false;
338
+
339
+ for (var key in item) {
340
+ var value = item[key];
341
+
342
+ if (typeof search[i] == 'string') {
343
+
344
+ if (Array.isArray(value) || typeof value == 'number' ) {
345
+ value = value.toString();
346
+ }
347
+
348
+ if (typeof value == 'string') {
349
+ value = value.toUpperCase();
350
+ if (value.indexOf(search[i].toUpperCase()) > -1) {
351
+ __status = true;
352
+ break;
353
+ }
354
+ }
355
+
356
+ } else {
357
+ if (value == search[i]) {
358
+ __status = true;
359
+ break;
360
+ }
361
+ }
362
+ }
363
+
364
+ if (!__status) {
365
+ return false;
366
+ }
367
+ }
368
+
369
+ return true;
370
+ })
371
+ } else {
372
+ tmp = results;
373
+ }
374
+
375
+ return tmp;
376
+ }
377
+
378
+ function sortData(data, sorts) {
379
+ if (!Array.isArray(sorts))
380
+ sorts = [sorts]
381
+ for (let sort of sorts) {
382
+ let name = sort.name
383
+ if (name) {
384
+ data.sort((a, b) => {
385
+ if (!a[name])
386
+ a[name] = ''
387
+ if (!b[name])
388
+ b[name] = ''
389
+ if (sort.type == '-1') {
390
+ if (sort.valueType == 'number')
391
+ return b[name] - a[name]
392
+ else
393
+ return b[name].localeCompare(a[name])
394
+ } else {
395
+ if (sort.valueType == 'number')
396
+ return a[name] - b[name]
397
+ else
398
+ return a[name].localeCompare(b[name])
399
+ }
400
+ });
401
+ }
402
+ }
403
+ return data;
404
+ }
405
+
406
+ function queryData(item, query) {
407
+ //. $contain, $range, $eq, $ne, $lt, $lte, $gt, $gte, $in, $nin, $geoWithin
408
+ let flag = true;
409
+ if (!item || !query) {
410
+ return false;
411
+ }
412
+ if (Array.isArray(item)) return false;
413
+ for (let i = 0; i < query.length; i++) {
414
+ let fieldValue = item[query[i].name];
415
+ if (fieldValue == undefined)
416
+ fieldValue = ''
417
+ let values = query[i].value
418
+ if (!Array.isArray(values))
419
+ values = [values]
420
+
421
+ for (let value of values) {
422
+ switch (query[i].operator) {
423
+ case '$contain':
424
+ if (!fieldValue.includes(value))
425
+ flag = false;
426
+ break;
427
+ case '$range':
428
+ if (value !== null && value !== null) {
429
+ if (value[0] > fieldValue || value[1] <= fieldValue)
430
+ flag = false;
431
+ } else if (item.value[0] == null && value[1] >= fieldValue) {
432
+ flag = false;
433
+ } else if (item.value[1] == null && value[0] <= fieldValue) {
434
+ flag = false;
435
+ }
436
+ break;
437
+ case '$eq':
438
+ if (fieldValue != value) flag = false;
439
+ break;
440
+ case '$ne':
441
+ if (fieldValue == value) flag = false;
442
+ break;
443
+ case '$lt':
444
+ if (fieldValue >= value) flag = false;
445
+ break;
446
+ case '$lte':
447
+ if (fieldValue > value) flag = false;
448
+ break;
449
+ case '$gt':
450
+ if (fieldValue <= value) flag = false;
451
+ break;
452
+ case '$gte':
453
+ if (fieldValue < value) flag = false;
454
+ break;
455
+ case '$in':
456
+ if (!Array.isArray(fieldValue) || !fieldValue.some(x => value.includes(x))) flag = false;
457
+ break;
458
+ case '$nin':
459
+ if (Array.isArray(fieldValue) && fieldValue.some(x => value.includes(x))) flag = false;
460
+ break;
461
+ default:
462
+ // if (!Array.isArray(fieldValue) || !fieldValue.some(x => value.includes(x))) flag = false;
463
+ if (fieldValue && !fieldValue.includes(value)) flag = false;
464
+ break;
465
+ }
466
+ }
467
+ }
468
+ return flag;
469
+ }
470
+
471
+
250
472
  // function computeStyles(el, properties) {
251
473
  // let computed = window.getComputedStyle(el);
252
474
  // let result = {};
@@ -277,7 +499,12 @@
277
499
  cssPath,
278
500
  domParser,
279
501
  queryDocumentSelector,
280
- queryDocumentSelectorAll
502
+ queryDocumentSelectorAll,
503
+ searchData,
504
+ andSearch,
505
+ orSearch,
506
+ sortData,
507
+ queryData
281
508
  }
282
509
 
283
510
  }));