@bitbybit-dev/base 0.19.6 → 0.19.8

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.
Files changed (61) hide show
  1. package/babel.config.cjs +0 -1
  2. package/{index.js → index.ts} +2 -1
  3. package/lib/api/inputs/base-inputs.ts +18 -0
  4. package/lib/api/inputs/{color-inputs.d.ts → color-inputs.ts} +48 -26
  5. package/lib/api/inputs/{lists-inputs.d.ts → lists-inputs.ts} +190 -91
  6. package/lib/api/inputs/{logic-inputs.d.ts → logic-inputs.ts} +46 -24
  7. package/lib/api/inputs/{math-inputs.d.ts → math-inputs.ts} +97 -53
  8. package/lib/api/inputs/{point-inputs.d.ts → point-inputs.ts} +168 -77
  9. package/lib/api/inputs/text-inputs.ts +108 -0
  10. package/lib/api/inputs/{transforms-inputs.d.ts → transforms-inputs.ts} +64 -35
  11. package/lib/api/inputs/{vector-inputs.d.ts → vector-inputs.ts} +104 -48
  12. package/lib/api/services/color.test.ts +86 -0
  13. package/lib/api/services/{color.js → color.ts} +34 -15
  14. package/lib/api/services/{geometry-helper.js → geometry-helper.ts} +43 -31
  15. package/lib/api/services/{index.d.ts → index.ts} +1 -1
  16. package/lib/api/services/lists.test.ts +612 -0
  17. package/lib/api/services/{lists.js → lists.ts} +83 -59
  18. package/lib/api/services/logic.test.ts +187 -0
  19. package/lib/api/services/{logic.js → logic.ts} +32 -24
  20. package/lib/api/services/math.test.ts +622 -0
  21. package/lib/api/services/{math.js → math.ts} +136 -71
  22. package/lib/api/services/{point.js → point.ts} +67 -32
  23. package/lib/api/services/text.test.ts +55 -0
  24. package/lib/api/services/{text.js → text.ts} +17 -7
  25. package/lib/api/services/{transforms.js → transforms.ts} +83 -37
  26. package/lib/api/services/vector.test.ts +360 -0
  27. package/lib/api/services/{vector.js → vector.ts} +80 -42
  28. package/lib/{index.d.ts → index.ts} +1 -0
  29. package/package.json +1 -1
  30. package/tsconfig.bitbybit.json +26 -0
  31. package/tsconfig.json +24 -0
  32. package/babel.config.d.cts +0 -5
  33. package/index.d.ts +0 -1
  34. package/lib/api/index.js +0 -1
  35. package/lib/api/inputs/base-inputs.d.ts +0 -35
  36. package/lib/api/inputs/base-inputs.js +0 -1
  37. package/lib/api/inputs/color-inputs.js +0 -164
  38. package/lib/api/inputs/index.js +0 -9
  39. package/lib/api/inputs/inputs.js +0 -9
  40. package/lib/api/inputs/lists-inputs.js +0 -576
  41. package/lib/api/inputs/logic-inputs.js +0 -111
  42. package/lib/api/inputs/math-inputs.js +0 -391
  43. package/lib/api/inputs/point-inputs.js +0 -521
  44. package/lib/api/inputs/text-inputs.d.ts +0 -83
  45. package/lib/api/inputs/text-inputs.js +0 -120
  46. package/lib/api/inputs/transforms-inputs.js +0 -200
  47. package/lib/api/inputs/vector-inputs.js +0 -304
  48. package/lib/api/services/color.d.ts +0 -114
  49. package/lib/api/services/geometry-helper.d.ts +0 -15
  50. package/lib/api/services/index.js +0 -9
  51. package/lib/api/services/lists.d.ts +0 -287
  52. package/lib/api/services/logic.d.ts +0 -99
  53. package/lib/api/services/math.d.ts +0 -349
  54. package/lib/api/services/point.d.ts +0 -222
  55. package/lib/api/services/text.d.ts +0 -69
  56. package/lib/api/services/transforms.d.ts +0 -122
  57. package/lib/api/services/vector.d.ts +0 -320
  58. package/lib/index.js +0 -1
  59. /package/lib/api/{index.d.ts → index.ts} +0 -0
  60. /package/lib/api/inputs/{index.d.ts → index.ts} +0 -0
  61. /package/lib/api/inputs/{inputs.d.ts → inputs.ts} +0 -0
@@ -0,0 +1,187 @@
1
+ import { Logic } from "./logic";
2
+ import * as Inputs from "../inputs";
3
+
4
+ describe("Logic unit tests", () => {
5
+ let logic: Logic;
6
+ beforeAll(() => {
7
+ logic = new Logic();
8
+ });
9
+
10
+ it("should create boolean true", () => {
11
+ const res = logic.boolean({ boolean: true });
12
+ expect(res).toBe(true);
13
+ });
14
+
15
+ it("should create boolean false", () => {
16
+ const res = logic.boolean({ boolean: false });
17
+ expect(res).toBe(false);
18
+ });
19
+
20
+ it("should create reandom list of boolean values", () => {
21
+ const res = logic.randomBooleans({ length: 10, trueThreshold: 0.5 });
22
+ expect(res.length).toBe(10);
23
+ });
24
+
25
+ it("should create reandom list of boolean values with true threshold on 1", () => {
26
+ const res = logic.randomBooleans({ length: 10, trueThreshold: 1 });
27
+ expect(res).toEqual([true, true, true, true, true, true, true, true, true, true]);
28
+ });
29
+
30
+ it("should create reandom list of boolean values with true threshold on 0", () => {
31
+ const res = logic.randomBooleans({ length: 10, trueThreshold: 0 });
32
+ expect(res).toEqual([false, false, false, false, false, false, false, false, false, false]);
33
+ });
34
+
35
+ it("should create threshold boolean list", () => {
36
+ const res = logic.thresholdBooleanList({ numbers: [0.1, 0.2, 0.3], threshold: 0.2, inverse: false });
37
+ expect(res).toEqual([true, false, false]);
38
+ });
39
+
40
+ it("should create inverted threshold boolean list", () => {
41
+ const res = logic.thresholdBooleanList({ numbers: [0.1, 0.2, 0.3], threshold: 0.2, inverse: true });
42
+ expect(res).toEqual([false, true, true]);
43
+ });
44
+
45
+ it("should create two threshold random gradient", () => {
46
+ const res = logic.twoThresholdRandomGradient({ numbers: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23], thresholdTotalTrue: 5, thresholdTotalFalse: 21, nrLevels: 10 });
47
+ expect(res.length).toEqual(23);
48
+ expect(res.filter(r => r === true).length).toBeGreaterThan(5);
49
+ expect(res.filter(r => r === false).length).toBeGreaterThan(3);
50
+ });
51
+
52
+ it("should create boolean list based on gap thresholds", () => {
53
+ const res = logic.thresholdGapsBooleanList({ numbers: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], gapThresholds: [[2, 4], [6, 8]], inverse: false });
54
+ expect(res).toEqual([
55
+ false, true, true,
56
+ true, false, true,
57
+ true, true, false,
58
+ false
59
+ ]);
60
+ });
61
+
62
+ it("should create inverted boolean list based on gap thresholds", () => {
63
+ const res = logic.thresholdGapsBooleanList({ numbers: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], gapThresholds: [[2, 4], [6, 8]], inverse: true });
64
+ expect(res).toEqual([
65
+ true, false, false,
66
+ false, true, false,
67
+ false, false, true,
68
+ true
69
+ ]);
70
+ });
71
+
72
+ it("should invert true value with not", () => {
73
+ const res = logic.not({ boolean: true });
74
+ expect(res).toEqual(false);
75
+ });
76
+
77
+ it("should invert false value with not", () => {
78
+ const res = logic.not({ boolean: false });
79
+ expect(res).toEqual(true);
80
+ });
81
+
82
+ it("should invert a boolean list", () => {
83
+ const res = logic.notList({ booleans: [false, false, true, false, true] });
84
+ expect(res).toEqual([true, true, false, true, false]);
85
+ });
86
+
87
+ it("should compare two unequal values", () => {
88
+ const res = logic.compare({ first: false, second: true, operator: Inputs.Logic.BooleanOperatorsEnum.equal });
89
+ expect(res).toBe(false);
90
+ });
91
+
92
+ it("should compare two unequal values as equal without strict mode", () => {
93
+ const res = logic.compare({ first: "1", second: 1 as any, operator: Inputs.Logic.BooleanOperatorsEnum.equal });
94
+ expect(res).toBe(true);
95
+ });
96
+
97
+ it("should compare two unequal values as not equal without strict mode", () => {
98
+ const res = logic.compare({ first: "1", second: 1 as any, operator: Inputs.Logic.BooleanOperatorsEnum.notEqual });
99
+ expect(res).toBe(false);
100
+ });
101
+
102
+ it("should compare two unequal values as equal with strict mode", () => {
103
+ const res = logic.compare({ first: "1", second: 1 as any, operator: Inputs.Logic.BooleanOperatorsEnum.tripleEqual });
104
+ expect(res).toBe(false);
105
+ });
106
+
107
+ it("should compare two unequal values as not equal with strict mode", () => {
108
+ const res = logic.compare({ first: "1", second: 1 as any, operator: Inputs.Logic.BooleanOperatorsEnum.tripleNotEqual });
109
+ expect(res).toBe(true);
110
+ });
111
+
112
+ it("should compare two unequal values", () => {
113
+ const res = logic.compare({ first: false, second: true, operator: Inputs.Logic.BooleanOperatorsEnum.notEqual });
114
+ expect(res).toBe(true);
115
+ });
116
+
117
+ it("should compare two equal values", () => {
118
+ const res = logic.compare({ first: false, second: false, operator: Inputs.Logic.BooleanOperatorsEnum.equal });
119
+ expect(res).toBe(true);
120
+ });
121
+
122
+ it("should compare two equal values", () => {
123
+ const res = logic.compare({ first: false, second: false, operator: Inputs.Logic.BooleanOperatorsEnum.notEqual });
124
+ expect(res).toBe(false);
125
+ });
126
+
127
+ it("should check if 4 is less than 5", () => {
128
+ const res = logic.compare({ first: 4, second: 5, operator: Inputs.Logic.BooleanOperatorsEnum.less });
129
+ expect(res).toBe(true);
130
+ });
131
+
132
+ it("should check if 4 is greater than 5", () => {
133
+ const res = logic.compare({ first: 4, second: 5, operator: Inputs.Logic.BooleanOperatorsEnum.greater });
134
+ expect(res).toBe(false);
135
+ });
136
+
137
+ it("should check if 4 is less or equal to 5", () => {
138
+ const res = logic.compare({ first: 5, second: 5, operator: Inputs.Logic.BooleanOperatorsEnum.lessOrEqual });
139
+ expect(res).toBe(true);
140
+ });
141
+
142
+ it("should check if 5 is less or equal to 5", () => {
143
+ const res = logic.compare({ first: 5, second: 5, operator: Inputs.Logic.BooleanOperatorsEnum.lessOrEqual });
144
+ expect(res).toBe(true);
145
+ });
146
+
147
+ it("should check if 5 is greater or equal to 5", () => {
148
+ const res = logic.compare({ first: 5, second: 5, operator: Inputs.Logic.BooleanOperatorsEnum.greaterOrEqual });
149
+ expect(res).toBe(true);
150
+ });
151
+
152
+ it("should check if 6 is greater or equal to 5", () => {
153
+ const res = logic.compare({ first: 5, second: 5, operator: Inputs.Logic.BooleanOperatorsEnum.greaterOrEqual });
154
+ expect(res).toBe(true);
155
+ });
156
+
157
+ it("should return false if unknown operator is provided", () => {
158
+ const res = logic.compare({ first: 5, second: 5, operator: "whatever" as any });
159
+ expect(res).toBe(false);
160
+ });
161
+
162
+ it("should return value through the gate if boolean is true", () => {
163
+ const res = logic.valueGate({ value: { d: "a" }, boolean: true });
164
+ expect(res).toEqual({ d: "a" });
165
+ });
166
+
167
+ it("should not return value through the gate if boolean is false", () => {
168
+ const res = logic.valueGate({ value: { d: "a" }, boolean: false });
169
+ expect(res).toEqual(undefined);
170
+ });
171
+
172
+ it("should return first defined value", () => {
173
+ const res = logic.firstDefinedValueGate({ value1: { d: "a" }, value2: { c: "x" } });
174
+ expect(res).toEqual({ d: "a" });
175
+ });
176
+
177
+ it("should return first defined value", () => {
178
+ const res = logic.firstDefinedValueGate({ value1: undefined, value2: { c: "x" } });
179
+ expect(res).toEqual({ c: "x" });
180
+ });
181
+
182
+ it("should not return defined value if no values are defined", () => {
183
+ const res = logic.firstDefinedValueGate({ value1: undefined, value2: undefined });
184
+ expect(res).toEqual(undefined);
185
+ });
186
+ });
187
+
@@ -1,7 +1,10 @@
1
+ import * as Inputs from "../inputs";
2
+
1
3
  /**
2
4
  * Contains various logic methods.
3
5
  */
4
6
  export class Logic {
7
+
5
8
  /**
6
9
  * Creates a boolean value - true or false
7
10
  * @param inputs a true or false boolean
@@ -10,9 +13,10 @@ export class Logic {
10
13
  * @shortname boolean
11
14
  * @drawable false
12
15
  */
13
- boolean(inputs) {
16
+ boolean(inputs: Inputs.Logic.BooleanDto): boolean {
14
17
  return inputs.boolean;
15
18
  }
19
+
16
20
  /**
17
21
  * Creates a random boolean list of predefined length
18
22
  * @param inputs a length and a threshold for randomization of true values
@@ -21,16 +25,17 @@ export class Logic {
21
25
  * @shortname random booleans
22
26
  * @drawable false
23
27
  */
24
- randomBooleans(inputs) {
28
+ randomBooleans(inputs: Inputs.Logic.RandomBooleansDto): boolean[] {
25
29
  const booleans = [];
26
30
  for (let i = 0; i < inputs.length; i++) {
27
31
  booleans.push(Math.random() < inputs.trueThreshold);
28
32
  }
29
33
  return booleans;
30
34
  }
35
+
31
36
  /**
32
- * Creates a random boolean list of true and false values based on a list of numbers.
33
- * All values between true threshold will be true, all values above false threshold will be false,
37
+ * Creates a random boolean list of true and false values based on a list of numbers.
38
+ * All values between true threshold will be true, all values above false threshold will be false,
34
39
  * and the rest will be distributed between true and false based on the number of levels in a gradient pattern.
35
40
  * That means that the closer the number gets to the false threshold the bigger the chance will be to get random false value.
36
41
  * @param inputs a length and a threshold for randomization of true values
@@ -39,16 +44,14 @@ export class Logic {
39
44
  * @shortname 2 threshold random gradient
40
45
  * @drawable false
41
46
  */
42
- twoThresholdRandomGradient(inputs) {
47
+ twoThresholdRandomGradient(inputs: Inputs.Logic.TwoThresholdRandomGradientDto): boolean[] {
43
48
  const booleans = [];
44
49
  inputs.numbers.forEach(n => {
45
50
  if (n < inputs.thresholdTotalTrue) {
46
51
  booleans.push(true);
47
- }
48
- else if (n > inputs.thresholdTotalFalse) {
52
+ } else if (n > inputs.thresholdTotalFalse) {
49
53
  booleans.push(false);
50
- }
51
- else {
54
+ } else {
52
55
  const leveledNr = n - inputs.thresholdTotalTrue;
53
56
  const step = (inputs.thresholdTotalFalse - inputs.thresholdTotalTrue) / inputs.nrLevels;
54
57
  const whichCat = Math.ceil(leveledNr / step);
@@ -56,14 +59,14 @@ export class Logic {
56
59
  const random = Math.random();
57
60
  if (random > bound) {
58
61
  booleans.push(true);
59
- }
60
- else {
62
+ } else {
61
63
  booleans.push(false);
62
64
  }
63
65
  }
64
66
  });
65
67
  return booleans;
66
68
  }
69
+
67
70
  /**
68
71
  * Creates a boolean list based on a list of numbers and a threshold.
69
72
  * @param inputs a length and a threshold for randomization of true values
@@ -72,13 +75,12 @@ export class Logic {
72
75
  * @shortname threshold boolean list
73
76
  * @drawable false
74
77
  */
75
- thresholdBooleanList(inputs) {
78
+ thresholdBooleanList(inputs: Inputs.Logic.ThresholdBooleanListDto): boolean[] {
76
79
  const booleans = [];
77
80
  inputs.numbers.forEach(n => {
78
81
  if (n < inputs.threshold) {
79
82
  booleans.push(true);
80
- }
81
- else {
83
+ } else {
82
84
  booleans.push(false);
83
85
  }
84
86
  });
@@ -87,6 +89,7 @@ export class Logic {
87
89
  }
88
90
  return booleans;
89
91
  }
92
+
90
93
  /**
91
94
  * Creates a boolean list based on a list of numbers and a gap thresholds. Gap thresholds are pairs of numbers that define a range of numbers that will be true.
92
95
  * @param inputs a length and a threshold for randomization of true values
@@ -95,8 +98,9 @@ export class Logic {
95
98
  * @shortname threshold gaps boolean list
96
99
  * @drawable false
97
100
  */
98
- thresholdGapsBooleanList(inputs) {
101
+ thresholdGapsBooleanList(inputs: Inputs.Logic.ThresholdGapsBooleanListDto): boolean[] {
99
102
  const booleans = [];
103
+
100
104
  inputs.numbers.forEach(n => {
101
105
  let foundInThresholds = false;
102
106
  inputs.gapThresholds.forEach(t => {
@@ -116,6 +120,7 @@ export class Logic {
116
120
  }
117
121
  return booleans;
118
122
  }
123
+
119
124
  /**
120
125
  * Apply not operator on the boolean
121
126
  * @param inputs a true or false boolean
@@ -124,9 +129,10 @@ export class Logic {
124
129
  * @shortname not
125
130
  * @drawable false
126
131
  */
127
- not(inputs) {
132
+ not(inputs: Inputs.Logic.BooleanDto): boolean {
128
133
  return !inputs.boolean;
129
134
  }
135
+
130
136
  /**
131
137
  * Apply not operator on a list of booleans
132
138
  * @param inputs a list of true or false booleans
@@ -135,9 +141,10 @@ export class Logic {
135
141
  * @shortname not list
136
142
  * @drawable false
137
143
  */
138
- notList(inputs) {
144
+ notList(inputs: Inputs.Logic.BooleanListDto): boolean[] {
139
145
  return inputs.booleans.map(b => !b);
140
146
  }
147
+
141
148
  /**
142
149
  * Does comparison between first and second values
143
150
  * @param inputs two values to be compared
@@ -146,7 +153,7 @@ export class Logic {
146
153
  * @shortname compare
147
154
  * @drawable false
148
155
  */
149
- compare(inputs) {
156
+ compare<T>(inputs: Inputs.Logic.ComparisonDto<T>): boolean {
150
157
  switch (inputs.operator) {
151
158
  case "==":
152
159
  return inputs.first == inputs.second;
@@ -168,6 +175,7 @@ export class Logic {
168
175
  return false;
169
176
  }
170
177
  }
178
+
171
179
  /**
172
180
  * Transmits a value if boolean provided is true and undefined if boolean provided is false
173
181
  * @param inputs a value and a boolean value
@@ -176,9 +184,10 @@ export class Logic {
176
184
  * @shortname value gate
177
185
  * @drawable false
178
186
  */
179
- valueGate(inputs) {
187
+ valueGate<T>(inputs: Inputs.Logic.ValueGateDto<T>): T | undefined {
180
188
  return inputs.boolean ? inputs.value : undefined;
181
189
  }
190
+
182
191
  /**
183
192
  * Returns first defined value out of two
184
193
  * @param inputs two values
@@ -187,17 +196,16 @@ export class Logic {
187
196
  * @shortname first defined value gate
188
197
  * @drawable false
189
198
  */
190
- firstDefinedValueGate(inputs) {
199
+ firstDefinedValueGate<T, U>(inputs: Inputs.Logic.TwoValueGateDto<T, U>): T | U | undefined {
191
200
  let res;
192
201
  if (inputs.value1 !== undefined) {
193
202
  res = inputs.value1;
194
- }
195
- else if (inputs.value2 !== undefined) {
203
+ } else if (inputs.value2 !== undefined) {
196
204
  res = inputs.value2;
197
- }
198
- else {
205
+ } else {
199
206
  res = undefined;
200
207
  }
201
208
  return res;
202
209
  }
210
+
203
211
  }