@cocreate/utils 1.12.1 → 1.12.2

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.2](https://github.com/CoCreate-app/CoCreate-utils/compare/v1.12.1...v1.12.2) (2022-11-27)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * improved searchData function ([25febc6](https://github.com/CoCreate-app/CoCreate-utils/commit/25febc683f44bbb75e7bd4f1e69ef239837a01de))
7
+
1
8
  ## [1.12.1](https://github.com/CoCreate-app/CoCreate-utils/compare/v1.12.0...v1.12.1) (2022-11-26)
2
9
 
3
10
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/utils",
3
- "version": "1.12.1",
3
+ "version": "1.12.2",
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.5"
64
+ "@cocreate/docs": "^1.4.6"
65
65
  }
66
66
  }
package/src/utils.js CHANGED
@@ -247,168 +247,12 @@
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) {
250
+ function queryData(item, query) {
407
251
  //. $contain, $range, $eq, $ne, $lt, $lte, $gt, $gte, $in, $nin, $geoWithin
408
252
  let flag = true;
409
- if (!item || !query) {
253
+ if (!item)
410
254
  return false;
411
- }
255
+
412
256
  if (Array.isArray(item)) return false;
413
257
  for (let i = 0; i < query.length; i++) {
414
258
  let fieldValue = item[query[i].name];
@@ -468,6 +312,75 @@
468
312
  return flag;
469
313
  }
470
314
 
315
+ function searchData(data, searches) {
316
+ const search = searches['value']
317
+ const operator = searches['type']
318
+
319
+ for (var key in data) {
320
+ let value = data[key];
321
+ let status = false;
322
+
323
+ if (Array.isArray(value) || typeof value == 'number') {
324
+ value = value.toString();
325
+ }
326
+
327
+ if (typeof value == 'object') {
328
+ // run keys of object or JSON.stringify
329
+ }
330
+
331
+ if (typeof value == 'string') {
332
+ value = value.toUpperCase();
333
+ }
334
+
335
+ for (let i = 0; i < search.length; i++) {
336
+ if (typeof search[i] == 'string' && typeof value == 'string') {
337
+ if (value.indexOf(search[i].toUpperCase()) > -1) {
338
+ status = true;
339
+ }
340
+ } else {
341
+ if (value == search[i]) {
342
+ status = true;
343
+ }
344
+ }
345
+ if (operator == 'or' && status) {
346
+ return true;
347
+ }
348
+ if (operator == 'and' && !status) {
349
+ return false;
350
+ }
351
+ }
352
+
353
+ }
354
+
355
+ }
356
+
357
+ function sortData(data, sorts) {
358
+ if (!Array.isArray(sorts))
359
+ sorts = [sorts]
360
+ for (let sort of sorts) {
361
+ let name = sort.name
362
+ if (name) {
363
+ data.sort((a, b) => {
364
+ if (!a[name])
365
+ a[name] = ''
366
+ if (!b[name])
367
+ b[name] = ''
368
+ if (sort.type == '-1') {
369
+ if (sort.valueType == 'number')
370
+ return b[name] - a[name]
371
+ else
372
+ return b[name].localeCompare(a[name])
373
+ } else {
374
+ if (sort.valueType == 'number')
375
+ return a[name] - b[name]
376
+ else
377
+ return a[name].localeCompare(b[name])
378
+ }
379
+ });
380
+ }
381
+ }
382
+ return data;
383
+ }
471
384
 
472
385
  // function computeStyles(el, properties) {
473
386
  // let computed = window.getComputedStyle(el);
@@ -500,11 +413,9 @@
500
413
  domParser,
501
414
  queryDocumentSelector,
502
415
  queryDocumentSelectorAll,
416
+ queryData,
503
417
  searchData,
504
- andSearch,
505
- orSearch,
506
- sortData,
507
- queryData
418
+ sortData
508
419
  }
509
420
 
510
421
  }));