@usertour/helpers 0.0.33 → 0.0.35

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.
@@ -6,6 +6,15 @@ var import_types2 = require("@usertour/types");
6
6
  // src/conditions/attribute.ts
7
7
  var import_types = require("@usertour/types");
8
8
  var import_date_fns = require("date-fns");
9
+
10
+ // src/type-utils.ts
11
+ var nativeIsArray = Array.isArray;
12
+ var ObjProto = Object.prototype;
13
+ var objToString = ObjProto.toString;
14
+ var objHasOwn = ObjProto.hasOwnProperty;
15
+ var isArray = nativeIsArray || ((obj) => objToString.call(obj) === "[object Array]");
16
+
17
+ // src/conditions/attribute.ts
9
18
  function evaluateFilterConditions(conditions, options) {
10
19
  if (!conditions || !conditions.length) {
11
20
  return true;
@@ -110,26 +119,27 @@ function evaluateStringCondition(logic, actualValue, expectedValue) {
110
119
  }
111
120
  }
112
121
  function evaluateNumberCondition(logic, actualValue, expectedValue, expectedValue2) {
113
- const numValue = Number(actualValue);
114
- const numValue2 = Number(expectedValue2);
115
- if (Number.isNaN(numValue)) {
122
+ const numActualValue = Number(actualValue);
123
+ const numExpectedValue = Number(expectedValue);
124
+ const numExpectedValue2 = Number(expectedValue2);
125
+ if (Number.isNaN(numActualValue)) {
116
126
  return false;
117
127
  }
118
128
  switch (logic) {
119
129
  case "is":
120
- return numValue === expectedValue;
130
+ return numActualValue === numExpectedValue;
121
131
  case "not":
122
- return numValue !== expectedValue;
132
+ return numActualValue !== numExpectedValue;
123
133
  case "isLessThan":
124
- return numValue < expectedValue;
134
+ return numActualValue < numExpectedValue;
125
135
  case "isLessThanOrEqualTo":
126
- return numValue <= expectedValue;
136
+ return numActualValue <= numExpectedValue;
127
137
  case "isGreaterThan":
128
- return numValue > expectedValue;
138
+ return numActualValue > numExpectedValue;
129
139
  case "isGreaterThanOrEqualTo":
130
- return numValue >= expectedValue;
140
+ return numActualValue >= numExpectedValue;
131
141
  case "between":
132
- return numValue >= expectedValue && numValue <= numValue2;
142
+ return numActualValue >= numExpectedValue && numActualValue <= numExpectedValue2;
133
143
  case "empty":
134
144
  return actualValue === null || actualValue === void 0 || actualValue === "";
135
145
  case "any":
@@ -153,7 +163,7 @@ function evaluateBooleanCondition(logic, actualValue) {
153
163
  }
154
164
  }
155
165
  function evaluateListCondition(logic, actualValue, expectedValues) {
156
- const arrayValue = Array.isArray(actualValue) ? actualValue : [];
166
+ const arrayValue = isArray(actualValue) ? actualValue : [];
157
167
  if (logic === "empty" || logic === "any") {
158
168
  switch (logic) {
159
169
  case "empty":
@@ -289,6 +299,12 @@ var testAttributes = [
289
299
  dataType: import_types2.BizAttributeTypes.Number,
290
300
  bizType: import_types2.AttributeBizTypes.User
291
301
  },
302
+ {
303
+ id: "sdsdd-attr",
304
+ codeName: "sdsdd",
305
+ dataType: import_types2.BizAttributeTypes.Number,
306
+ bizType: import_types2.AttributeBizTypes.User
307
+ },
292
308
  // Company attributes
293
309
  {
294
310
  id: "company-name-attr",
@@ -323,7 +339,8 @@ var testUserAttributes = {
323
339
  roles: ["admin", "user"],
324
340
  tags: [123, 456],
325
341
  signUpDate: "2024-01-15T10:30:00Z",
326
- score: 85.5
342
+ score: 85.5,
343
+ sdsdd: 101
327
344
  };
328
345
  var testCompanyAttributes = {
329
346
  companyName: "Example Corp",
@@ -596,6 +613,229 @@ describe("Attribute Filter - Complete Test Suite", () => {
596
613
  expect(evaluateFilterConditions(condition, defaultOptions)).toBe(true);
597
614
  });
598
615
  });
616
+ describe("Number conditions with string values", () => {
617
+ test('should evaluate "is" condition when actualValue is number and expectedValue is string', () => {
618
+ const condition = [
619
+ {
620
+ id: "number-string-condition-1",
621
+ type: "condition",
622
+ operators: "and",
623
+ data: {
624
+ logic: "is",
625
+ value: "101",
626
+ attrId: "sdsdd-attr"
627
+ }
628
+ }
629
+ ];
630
+ expect(evaluateFilterConditions(condition, defaultOptions)).toBe(true);
631
+ });
632
+ test('should evaluate "is" condition when actualValue is string and expectedValue is number', () => {
633
+ const userAttributesWithStringNumber = {
634
+ ...testUserAttributes,
635
+ sdsdd: "101"
636
+ };
637
+ const condition = [
638
+ {
639
+ id: "number-string-condition-2",
640
+ type: "condition",
641
+ operators: "and",
642
+ data: {
643
+ logic: "is",
644
+ value: 101,
645
+ attrId: "sdsdd-attr"
646
+ }
647
+ }
648
+ ];
649
+ expect(
650
+ evaluateFilterConditions(condition, {
651
+ ...defaultOptions,
652
+ userAttributes: userAttributesWithStringNumber
653
+ })
654
+ ).toBe(true);
655
+ });
656
+ test('should evaluate "is" condition when both actualValue and expectedValue are strings', () => {
657
+ const userAttributesWithStringNumber = {
658
+ ...testUserAttributes,
659
+ sdsdd: "101"
660
+ };
661
+ const condition = [
662
+ {
663
+ id: "number-string-condition-3",
664
+ type: "condition",
665
+ operators: "and",
666
+ data: {
667
+ logic: "is",
668
+ value: "101",
669
+ attrId: "sdsdd-attr"
670
+ }
671
+ }
672
+ ];
673
+ expect(
674
+ evaluateFilterConditions(condition, {
675
+ ...defaultOptions,
676
+ userAttributes: userAttributesWithStringNumber
677
+ })
678
+ ).toBe(true);
679
+ });
680
+ test('should evaluate "not" condition when actualValue is number and expectedValue is string', () => {
681
+ const condition = [
682
+ {
683
+ id: "number-string-condition-4",
684
+ type: "condition",
685
+ operators: "and",
686
+ data: {
687
+ logic: "not",
688
+ value: "102",
689
+ attrId: "sdsdd-attr"
690
+ }
691
+ }
692
+ ];
693
+ expect(evaluateFilterConditions(condition, defaultOptions)).toBe(true);
694
+ });
695
+ test('should evaluate "isLessThan" condition when expectedValue is string', () => {
696
+ const condition = [
697
+ {
698
+ id: "number-string-condition-5",
699
+ type: "condition",
700
+ operators: "and",
701
+ data: {
702
+ logic: "isLessThan",
703
+ value: "102",
704
+ attrId: "sdsdd-attr"
705
+ }
706
+ }
707
+ ];
708
+ expect(evaluateFilterConditions(condition, defaultOptions)).toBe(true);
709
+ });
710
+ test('should evaluate "isGreaterThan" condition when expectedValue is string', () => {
711
+ const condition = [
712
+ {
713
+ id: "number-string-condition-6",
714
+ type: "condition",
715
+ operators: "and",
716
+ data: {
717
+ logic: "isGreaterThan",
718
+ value: "100",
719
+ attrId: "sdsdd-attr"
720
+ }
721
+ }
722
+ ];
723
+ expect(evaluateFilterConditions(condition, defaultOptions)).toBe(true);
724
+ });
725
+ test('should evaluate "between" condition when value and value2 are strings', () => {
726
+ const condition = [
727
+ {
728
+ id: "number-string-condition-7",
729
+ type: "condition",
730
+ operators: "and",
731
+ data: {
732
+ logic: "between",
733
+ value: "100",
734
+ value2: "103",
735
+ attrId: "sdsdd-attr"
736
+ }
737
+ }
738
+ ];
739
+ expect(evaluateFilterConditions(condition, defaultOptions)).toBe(true);
740
+ });
741
+ test('should evaluate "between" condition when actualValue is string and values are strings', () => {
742
+ const userAttributesWithStringNumber = {
743
+ ...testUserAttributes,
744
+ sdsdd: "101"
745
+ };
746
+ const condition = [
747
+ {
748
+ id: "number-string-condition-8",
749
+ type: "condition",
750
+ operators: "and",
751
+ data: {
752
+ logic: "between",
753
+ value: "100",
754
+ value2: "103",
755
+ attrId: "sdsdd-attr"
756
+ }
757
+ }
758
+ ];
759
+ expect(
760
+ evaluateFilterConditions(condition, {
761
+ ...defaultOptions,
762
+ userAttributes: userAttributesWithStringNumber
763
+ })
764
+ ).toBe(true);
765
+ });
766
+ test("should return false when actualValue is NaN", () => {
767
+ const userAttributesWithNaN = {
768
+ ...testUserAttributes,
769
+ sdsdd: "not-a-number"
770
+ };
771
+ const condition = [
772
+ {
773
+ id: "number-string-condition-9",
774
+ type: "condition",
775
+ operators: "and",
776
+ data: {
777
+ logic: "is",
778
+ value: 101,
779
+ attrId: "sdsdd-attr"
780
+ }
781
+ }
782
+ ];
783
+ expect(
784
+ evaluateFilterConditions(condition, {
785
+ ...defaultOptions,
786
+ userAttributes: userAttributesWithNaN
787
+ })
788
+ ).toBe(false);
789
+ });
790
+ test("should handle decimal string values correctly", () => {
791
+ const userAttributesWithDecimal = {
792
+ ...testUserAttributes,
793
+ score: "85.5"
794
+ };
795
+ const condition = [
796
+ {
797
+ id: "number-string-condition-10",
798
+ type: "condition",
799
+ operators: "and",
800
+ data: {
801
+ logic: "is",
802
+ value: "85.5",
803
+ attrId: "score-attr"
804
+ }
805
+ }
806
+ ];
807
+ expect(
808
+ evaluateFilterConditions(condition, {
809
+ ...defaultOptions,
810
+ userAttributes: userAttributesWithDecimal
811
+ })
812
+ ).toBe(true);
813
+ });
814
+ test("should handle negative string values correctly", () => {
815
+ const userAttributesWithNegative = {
816
+ ...testUserAttributes,
817
+ sdsdd: "-50"
818
+ };
819
+ const condition = [
820
+ {
821
+ id: "number-string-condition-11",
822
+ type: "condition",
823
+ operators: "and",
824
+ data: {
825
+ logic: "isLessThan",
826
+ value: "0",
827
+ attrId: "sdsdd-attr"
828
+ }
829
+ }
830
+ ];
831
+ expect(
832
+ evaluateFilterConditions(condition, {
833
+ ...defaultOptions,
834
+ userAttributes: userAttributesWithNegative
835
+ })
836
+ ).toBe(true);
837
+ });
838
+ });
599
839
  describe("Boolean conditions (4 cases)", () => {
600
840
  test('should evaluate "true" condition correctly', () => {
601
841
  const condition = [
@@ -1,7 +1,8 @@
1
1
  import {
2
2
  evaluateAttributeCondition,
3
3
  evaluateFilterConditions
4
- } from "../chunk-E2APTIR7.js";
4
+ } from "../chunk-KYDXF7SU.js";
5
+ import "../chunk-GFH3VWOC.js";
5
6
  import "../chunk-XEO3YXBM.js";
6
7
 
7
8
  // src/__tests__/attribute.test.ts
@@ -52,6 +53,12 @@ var testAttributes = [
52
53
  dataType: BizAttributeTypes.Number,
53
54
  bizType: AttributeBizTypes.User
54
55
  },
56
+ {
57
+ id: "sdsdd-attr",
58
+ codeName: "sdsdd",
59
+ dataType: BizAttributeTypes.Number,
60
+ bizType: AttributeBizTypes.User
61
+ },
55
62
  // Company attributes
56
63
  {
57
64
  id: "company-name-attr",
@@ -86,7 +93,8 @@ var testUserAttributes = {
86
93
  roles: ["admin", "user"],
87
94
  tags: [123, 456],
88
95
  signUpDate: "2024-01-15T10:30:00Z",
89
- score: 85.5
96
+ score: 85.5,
97
+ sdsdd: 101
90
98
  };
91
99
  var testCompanyAttributes = {
92
100
  companyName: "Example Corp",
@@ -359,6 +367,229 @@ describe("Attribute Filter - Complete Test Suite", () => {
359
367
  expect(evaluateFilterConditions(condition, defaultOptions)).toBe(true);
360
368
  });
361
369
  });
370
+ describe("Number conditions with string values", () => {
371
+ test('should evaluate "is" condition when actualValue is number and expectedValue is string', () => {
372
+ const condition = [
373
+ {
374
+ id: "number-string-condition-1",
375
+ type: "condition",
376
+ operators: "and",
377
+ data: {
378
+ logic: "is",
379
+ value: "101",
380
+ attrId: "sdsdd-attr"
381
+ }
382
+ }
383
+ ];
384
+ expect(evaluateFilterConditions(condition, defaultOptions)).toBe(true);
385
+ });
386
+ test('should evaluate "is" condition when actualValue is string and expectedValue is number', () => {
387
+ const userAttributesWithStringNumber = {
388
+ ...testUserAttributes,
389
+ sdsdd: "101"
390
+ };
391
+ const condition = [
392
+ {
393
+ id: "number-string-condition-2",
394
+ type: "condition",
395
+ operators: "and",
396
+ data: {
397
+ logic: "is",
398
+ value: 101,
399
+ attrId: "sdsdd-attr"
400
+ }
401
+ }
402
+ ];
403
+ expect(
404
+ evaluateFilterConditions(condition, {
405
+ ...defaultOptions,
406
+ userAttributes: userAttributesWithStringNumber
407
+ })
408
+ ).toBe(true);
409
+ });
410
+ test('should evaluate "is" condition when both actualValue and expectedValue are strings', () => {
411
+ const userAttributesWithStringNumber = {
412
+ ...testUserAttributes,
413
+ sdsdd: "101"
414
+ };
415
+ const condition = [
416
+ {
417
+ id: "number-string-condition-3",
418
+ type: "condition",
419
+ operators: "and",
420
+ data: {
421
+ logic: "is",
422
+ value: "101",
423
+ attrId: "sdsdd-attr"
424
+ }
425
+ }
426
+ ];
427
+ expect(
428
+ evaluateFilterConditions(condition, {
429
+ ...defaultOptions,
430
+ userAttributes: userAttributesWithStringNumber
431
+ })
432
+ ).toBe(true);
433
+ });
434
+ test('should evaluate "not" condition when actualValue is number and expectedValue is string', () => {
435
+ const condition = [
436
+ {
437
+ id: "number-string-condition-4",
438
+ type: "condition",
439
+ operators: "and",
440
+ data: {
441
+ logic: "not",
442
+ value: "102",
443
+ attrId: "sdsdd-attr"
444
+ }
445
+ }
446
+ ];
447
+ expect(evaluateFilterConditions(condition, defaultOptions)).toBe(true);
448
+ });
449
+ test('should evaluate "isLessThan" condition when expectedValue is string', () => {
450
+ const condition = [
451
+ {
452
+ id: "number-string-condition-5",
453
+ type: "condition",
454
+ operators: "and",
455
+ data: {
456
+ logic: "isLessThan",
457
+ value: "102",
458
+ attrId: "sdsdd-attr"
459
+ }
460
+ }
461
+ ];
462
+ expect(evaluateFilterConditions(condition, defaultOptions)).toBe(true);
463
+ });
464
+ test('should evaluate "isGreaterThan" condition when expectedValue is string', () => {
465
+ const condition = [
466
+ {
467
+ id: "number-string-condition-6",
468
+ type: "condition",
469
+ operators: "and",
470
+ data: {
471
+ logic: "isGreaterThan",
472
+ value: "100",
473
+ attrId: "sdsdd-attr"
474
+ }
475
+ }
476
+ ];
477
+ expect(evaluateFilterConditions(condition, defaultOptions)).toBe(true);
478
+ });
479
+ test('should evaluate "between" condition when value and value2 are strings', () => {
480
+ const condition = [
481
+ {
482
+ id: "number-string-condition-7",
483
+ type: "condition",
484
+ operators: "and",
485
+ data: {
486
+ logic: "between",
487
+ value: "100",
488
+ value2: "103",
489
+ attrId: "sdsdd-attr"
490
+ }
491
+ }
492
+ ];
493
+ expect(evaluateFilterConditions(condition, defaultOptions)).toBe(true);
494
+ });
495
+ test('should evaluate "between" condition when actualValue is string and values are strings', () => {
496
+ const userAttributesWithStringNumber = {
497
+ ...testUserAttributes,
498
+ sdsdd: "101"
499
+ };
500
+ const condition = [
501
+ {
502
+ id: "number-string-condition-8",
503
+ type: "condition",
504
+ operators: "and",
505
+ data: {
506
+ logic: "between",
507
+ value: "100",
508
+ value2: "103",
509
+ attrId: "sdsdd-attr"
510
+ }
511
+ }
512
+ ];
513
+ expect(
514
+ evaluateFilterConditions(condition, {
515
+ ...defaultOptions,
516
+ userAttributes: userAttributesWithStringNumber
517
+ })
518
+ ).toBe(true);
519
+ });
520
+ test("should return false when actualValue is NaN", () => {
521
+ const userAttributesWithNaN = {
522
+ ...testUserAttributes,
523
+ sdsdd: "not-a-number"
524
+ };
525
+ const condition = [
526
+ {
527
+ id: "number-string-condition-9",
528
+ type: "condition",
529
+ operators: "and",
530
+ data: {
531
+ logic: "is",
532
+ value: 101,
533
+ attrId: "sdsdd-attr"
534
+ }
535
+ }
536
+ ];
537
+ expect(
538
+ evaluateFilterConditions(condition, {
539
+ ...defaultOptions,
540
+ userAttributes: userAttributesWithNaN
541
+ })
542
+ ).toBe(false);
543
+ });
544
+ test("should handle decimal string values correctly", () => {
545
+ const userAttributesWithDecimal = {
546
+ ...testUserAttributes,
547
+ score: "85.5"
548
+ };
549
+ const condition = [
550
+ {
551
+ id: "number-string-condition-10",
552
+ type: "condition",
553
+ operators: "and",
554
+ data: {
555
+ logic: "is",
556
+ value: "85.5",
557
+ attrId: "score-attr"
558
+ }
559
+ }
560
+ ];
561
+ expect(
562
+ evaluateFilterConditions(condition, {
563
+ ...defaultOptions,
564
+ userAttributes: userAttributesWithDecimal
565
+ })
566
+ ).toBe(true);
567
+ });
568
+ test("should handle negative string values correctly", () => {
569
+ const userAttributesWithNegative = {
570
+ ...testUserAttributes,
571
+ sdsdd: "-50"
572
+ };
573
+ const condition = [
574
+ {
575
+ id: "number-string-condition-11",
576
+ type: "condition",
577
+ operators: "and",
578
+ data: {
579
+ logic: "isLessThan",
580
+ value: "0",
581
+ attrId: "sdsdd-attr"
582
+ }
583
+ }
584
+ ];
585
+ expect(
586
+ evaluateFilterConditions(condition, {
587
+ ...defaultOptions,
588
+ userAttributes: userAttributesWithNegative
589
+ })
590
+ ).toBe(true);
591
+ });
592
+ });
362
593
  describe("Boolean conditions (4 cases)", () => {
363
594
  test('should evaluate "true" condition correctly', () => {
364
595
  const condition = [
@@ -143,6 +143,15 @@ var evaluateTimeCondition = (rules) => {
143
143
  // src/conditions/attribute.ts
144
144
  var import_types = require("@usertour/types");
145
145
  var import_date_fns2 = require("date-fns");
146
+
147
+ // src/type-utils.ts
148
+ var nativeIsArray = Array.isArray;
149
+ var ObjProto = Object.prototype;
150
+ var objToString = ObjProto.toString;
151
+ var objHasOwn = ObjProto.hasOwnProperty;
152
+ var isArray = nativeIsArray || ((obj) => objToString.call(obj) === "[object Array]");
153
+
154
+ // src/conditions/attribute.ts
146
155
  function evaluateAttributeCondition(condition, options) {
147
156
  const { data } = condition;
148
157
  if (!data) {
@@ -210,26 +219,27 @@ function evaluateStringCondition(logic, actualValue, expectedValue) {
210
219
  }
211
220
  }
212
221
  function evaluateNumberCondition(logic, actualValue, expectedValue, expectedValue2) {
213
- const numValue = Number(actualValue);
214
- const numValue2 = Number(expectedValue2);
215
- if (Number.isNaN(numValue)) {
222
+ const numActualValue = Number(actualValue);
223
+ const numExpectedValue = Number(expectedValue);
224
+ const numExpectedValue2 = Number(expectedValue2);
225
+ if (Number.isNaN(numActualValue)) {
216
226
  return false;
217
227
  }
218
228
  switch (logic) {
219
229
  case "is":
220
- return numValue === expectedValue;
230
+ return numActualValue === numExpectedValue;
221
231
  case "not":
222
- return numValue !== expectedValue;
232
+ return numActualValue !== numExpectedValue;
223
233
  case "isLessThan":
224
- return numValue < expectedValue;
234
+ return numActualValue < numExpectedValue;
225
235
  case "isLessThanOrEqualTo":
226
- return numValue <= expectedValue;
236
+ return numActualValue <= numExpectedValue;
227
237
  case "isGreaterThan":
228
- return numValue > expectedValue;
238
+ return numActualValue > numExpectedValue;
229
239
  case "isGreaterThanOrEqualTo":
230
- return numValue >= expectedValue;
240
+ return numActualValue >= numExpectedValue;
231
241
  case "between":
232
- return numValue >= expectedValue && numValue <= numValue2;
242
+ return numActualValue >= numExpectedValue && numActualValue <= numExpectedValue2;
233
243
  case "empty":
234
244
  return actualValue === null || actualValue === void 0 || actualValue === "";
235
245
  case "any":
@@ -253,7 +263,7 @@ function evaluateBooleanCondition(logic, actualValue) {
253
263
  }
254
264
  }
255
265
  function evaluateListCondition(logic, actualValue, expectedValues) {
256
- const arrayValue = Array.isArray(actualValue) ? actualValue : [];
266
+ const arrayValue = isArray(actualValue) ? actualValue : [];
257
267
  if (logic === "empty" || logic === "any") {
258
268
  switch (logic) {
259
269
  case "empty":
@@ -2,11 +2,12 @@ import {
2
2
  evaluateRulesConditions,
3
3
  filterConditionsByType,
4
4
  isConditionsActived
5
- } from "../chunk-YZQBWYCU.js";
5
+ } from "../chunk-NVSDWUJP.js";
6
6
  import "../chunk-YYIGUZNZ.js";
7
7
  import "../chunk-PAESAL23.js";
8
- import "../chunk-E2APTIR7.js";
9
8
  import "../chunk-CEK3SCQO.js";
9
+ import "../chunk-KYDXF7SU.js";
10
+ import "../chunk-GFH3VWOC.js";
10
11
  import "../chunk-3KG2HTZ3.js";
11
12
  import "../chunk-XEO3YXBM.js";
12
13