@snowplow/react-native-tracker 2.1.2 → 4.0.2-dev.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.
Files changed (232) hide show
  1. package/LICENSE +29 -203
  2. package/README.md +59 -143
  3. package/dist/index.cjs.d.ts +499 -0
  4. package/dist/index.cjs.js +2656 -0
  5. package/dist/index.cjs.js.map +1 -0
  6. package/dist/index.cjs.min.d.ts +499 -0
  7. package/dist/index.cjs.min.js +63 -0
  8. package/dist/index.cjs.min.js.map +1 -0
  9. package/dist/index.module.d.ts +499 -0
  10. package/dist/index.module.js +250 -0
  11. package/dist/index.module.js.map +1 -0
  12. package/package.json +48 -158
  13. package/.editorconfig +0 -15
  14. package/.eslintignore +0 -6
  15. package/.gitattributes +0 -3
  16. package/.github/ISSUE_TEMPLATE/bug_report.md +0 -29
  17. package/.github/ISSUE_TEMPLATE/feature_request.md +0 -20
  18. package/.github/actions/setup/action.yml +0 -28
  19. package/.github/workflows/build.yml +0 -155
  20. package/.github/workflows/deploy.yml +0 -97
  21. package/.github/workflows/e2e-android.yml +0 -121
  22. package/.github/workflows/e2e-ios.yml +0 -95
  23. package/.github/workflows/snyk.yml +0 -21
  24. package/.gitignore +0 -70
  25. package/.npmignore +0 -60
  26. package/.nvmrc +0 -1
  27. package/.watchmanconfig +0 -1
  28. package/.yarnrc +0 -3
  29. package/CHANGELOG +0 -184
  30. package/CONTRIBUTING.md +0 -80
  31. package/android/build.gradle +0 -110
  32. package/android/gradle/wrapper/gradle-wrapper.jar +0 -0
  33. package/android/gradle/wrapper/gradle-wrapper.properties +0 -5
  34. package/android/gradle.properties +0 -5
  35. package/android/gradlew +0 -234
  36. package/android/gradlew.bat +0 -89
  37. package/android/src/main/AndroidManifest.xml +0 -3
  38. package/android/src/main/AndroidManifestNew.xml +0 -2
  39. package/android/src/main/java/com/snowplow/reactnativetracker/ReactNativeTrackerModule.kt +0 -744
  40. package/android/src/main/java/com/snowplow/reactnativetracker/ReactNativeTrackerPackage.kt +0 -17
  41. package/android/src/main/java/com/snowplow/reactnativetracker/util/ConfigUtil.kt +0 -274
  42. package/android/src/main/java/com/snowplow/reactnativetracker/util/EventUtil.kt +0 -233
  43. package/android/src/main/java/com/snowplow/reactnativetracker/util/TrackerVersion.kt +0 -5
  44. package/babel.config.js +0 -3
  45. package/example/.watchmanconfig +0 -1
  46. package/example/Gemfile +0 -6
  47. package/example/android/app/build.gradle +0 -129
  48. package/example/android/app/debug.keystore +0 -0
  49. package/example/android/app/proguard-rules.pro +0 -10
  50. package/example/android/app/src/androidTest/java/com/reactnativetrackerexample/DetoxTest.java +0 -36
  51. package/example/android/app/src/androidTest/java/com/reactnativetrackerexample/DetoxTestAppJUnitRunner.java +0 -21
  52. package/example/android/app/src/debug/AndroidManifest.xml +0 -13
  53. package/example/android/app/src/debug/java/com/reactnativetrackerexample/ReactNativeFlipper.java +0 -75
  54. package/example/android/app/src/main/AndroidManifest.xml +0 -26
  55. package/example/android/app/src/main/java/com/reactnativetrackerexample/MainActivity.java +0 -32
  56. package/example/android/app/src/main/java/com/reactnativetrackerexample/MainApplication.java +0 -62
  57. package/example/android/app/src/main/res/drawable/rn_edit_text_material.xml +0 -36
  58. package/example/android/app/src/main/res/mipmap-hdpi/ic_launcher.png +0 -0
  59. package/example/android/app/src/main/res/mipmap-hdpi/ic_launcher_round.png +0 -0
  60. package/example/android/app/src/main/res/mipmap-mdpi/ic_launcher.png +0 -0
  61. package/example/android/app/src/main/res/mipmap-mdpi/ic_launcher_round.png +0 -0
  62. package/example/android/app/src/main/res/mipmap-xhdpi/ic_launcher.png +0 -0
  63. package/example/android/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png +0 -0
  64. package/example/android/app/src/main/res/mipmap-xxhdpi/ic_launcher.png +0 -0
  65. package/example/android/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png +0 -0
  66. package/example/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png +0 -0
  67. package/example/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png +0 -0
  68. package/example/android/app/src/main/res/values/strings.xml +0 -3
  69. package/example/android/app/src/main/res/values/styles.xml +0 -9
  70. package/example/android/app/src/release/java/com/reactnativetrackerexample/ReactNativeFlipper.java +0 -20
  71. package/example/android/build.gradle +0 -31
  72. package/example/android/gradle/wrapper/gradle-wrapper.jar +0 -0
  73. package/example/android/gradle/wrapper/gradle-wrapper.properties +0 -6
  74. package/example/android/gradle.properties +0 -44
  75. package/example/android/gradlew +0 -244
  76. package/example/android/gradlew.bat +0 -92
  77. package/example/android/settings.gradle +0 -4
  78. package/example/app.json +0 -4
  79. package/example/babel.config.js +0 -17
  80. package/example/detox.config.js +0 -92
  81. package/example/e2e/config.json +0 -10
  82. package/example/e2e/emitEvents.e2e.detox.js +0 -95
  83. package/example/e2e/environment.js +0 -23
  84. package/example/e2e/helpers/microCommands.js +0 -140
  85. package/example/e2e/helpers/microHelpers.js +0 -333
  86. package/example/e2e/helpers/microHelpers.test.js +0 -295
  87. package/example/e2e/helpers/schemas.js +0 -49
  88. package/example/e2e/jest.config.js +0 -16
  89. package/example/e2e/setup.js +0 -4
  90. package/example/e2e/testEvents.micro.test.js +0 -287
  91. package/example/index.js +0 -5
  92. package/example/ios/.xcode.env +0 -11
  93. package/example/ios/Podfile +0 -62
  94. package/example/ios/Podfile.lock +0 -730
  95. package/example/ios/ReactNativeTrackerExample/AppDelegate.h +0 -6
  96. package/example/ios/ReactNativeTrackerExample/AppDelegate.mm +0 -26
  97. package/example/ios/ReactNativeTrackerExample/Images.xcassets/AppIcon.appiconset/Contents.json +0 -53
  98. package/example/ios/ReactNativeTrackerExample/Images.xcassets/Contents.json +0 -6
  99. package/example/ios/ReactNativeTrackerExample/Info.plist +0 -55
  100. package/example/ios/ReactNativeTrackerExample/LaunchScreen.storyboard +0 -47
  101. package/example/ios/ReactNativeTrackerExample/PrivacyInfo.xcprivacy +0 -37
  102. package/example/ios/ReactNativeTrackerExample/main.m +0 -10
  103. package/example/ios/ReactNativeTrackerExample-Bridging-Header.h +0 -3
  104. package/example/ios/ReactNativeTrackerExample.xcodeproj/project.pbxproj +0 -739
  105. package/example/ios/ReactNativeTrackerExample.xcodeproj/xcshareddata/xcschemes/ReactNativeTrackerExample.xcscheme +0 -88
  106. package/example/ios/ReactNativeTrackerExample.xcworkspace/contents.xcworkspacedata +0 -10
  107. package/example/ios/ReactNativeTrackerExample.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +0 -8
  108. package/example/ios/ReactNativeTrackerExampleTests/Info.plist +0 -24
  109. package/example/ios/ReactNativeTrackerExampleTests/ReactNativeTrackerExampleTests.m +0 -66
  110. package/example/metro.config.js +0 -45
  111. package/example/package.json +0 -54
  112. package/example/react-native.config.js +0 -10
  113. package/example/src/App.js +0 -566
  114. package/example/tsconfig.json +0 -5
  115. package/example/yarn.lock +0 -7703
  116. package/ios/ReactNativeTracker-Bridging-Header.h +0 -2
  117. package/ios/ReactNativeTracker.mm +0 -171
  118. package/ios/ReactNativeTracker.swift +0 -822
  119. package/ios/ReactNativeTracker.xcodeproj/project.pbxproj +0 -301
  120. package/ios/Util/ConfigUtils.swift +0 -209
  121. package/ios/Util/TrackerVersion.swift +0 -3
  122. package/ios/Util/Utilities.swift +0 -37
  123. package/lefthook.yml +0 -11
  124. package/lib/commonjs/api.js +0 -490
  125. package/lib/commonjs/api.js.map +0 -1
  126. package/lib/commonjs/configurations.js +0 -196
  127. package/lib/commonjs/configurations.js.map +0 -1
  128. package/lib/commonjs/constants.js +0 -77
  129. package/lib/commonjs/constants.js.map +0 -1
  130. package/lib/commonjs/events.js +0 -277
  131. package/lib/commonjs/events.js.map +0 -1
  132. package/lib/commonjs/index.js +0 -132
  133. package/lib/commonjs/index.js.map +0 -1
  134. package/lib/commonjs/jsCore.js +0 -427
  135. package/lib/commonjs/jsCore.js.map +0 -1
  136. package/lib/commonjs/native.js +0 -16
  137. package/lib/commonjs/native.js.map +0 -1
  138. package/lib/commonjs/subject.js +0 -235
  139. package/lib/commonjs/subject.js.map +0 -1
  140. package/lib/commonjs/tracker.js +0 -248
  141. package/lib/commonjs/tracker.js.map +0 -1
  142. package/lib/commonjs/types.js +0 -2
  143. package/lib/commonjs/types.js.map +0 -1
  144. package/lib/commonjs/utils.js +0 -72
  145. package/lib/commonjs/utils.js.map +0 -1
  146. package/lib/commonjs/webViewInterface.js +0 -66
  147. package/lib/commonjs/webViewInterface.js.map +0 -1
  148. package/lib/module/api.js +0 -452
  149. package/lib/module/api.js.map +0 -1
  150. package/lib/module/configurations.js +0 -184
  151. package/lib/module/configurations.js.map +0 -1
  152. package/lib/module/constants.js +0 -72
  153. package/lib/module/constants.js.map +0 -1
  154. package/lib/module/events.js +0 -259
  155. package/lib/module/events.js.map +0 -1
  156. package/lib/module/index.js +0 -120
  157. package/lib/module/index.js.map +0 -1
  158. package/lib/module/jsCore.js +0 -424
  159. package/lib/module/jsCore.js.map +0 -1
  160. package/lib/module/native.js +0 -12
  161. package/lib/module/native.js.map +0 -1
  162. package/lib/module/subject.js +0 -222
  163. package/lib/module/subject.js.map +0 -1
  164. package/lib/module/tracker.js +0 -234
  165. package/lib/module/tracker.js.map +0 -1
  166. package/lib/module/types.js +0 -2
  167. package/lib/module/types.js.map +0 -1
  168. package/lib/module/utils.js +0 -66
  169. package/lib/module/utils.js.map +0 -1
  170. package/lib/module/webViewInterface.js +0 -62
  171. package/lib/module/webViewInterface.js.map +0 -1
  172. package/lib/typescript/__tests__/api.test.d.ts +0 -2
  173. package/lib/typescript/__tests__/api.test.d.ts.map +0 -1
  174. package/lib/typescript/__tests__/configurations.test.d.ts +0 -2
  175. package/lib/typescript/__tests__/configurations.test.d.ts.map +0 -1
  176. package/lib/typescript/__tests__/events.test.d.ts +0 -2
  177. package/lib/typescript/__tests__/events.test.d.ts.map +0 -1
  178. package/lib/typescript/__tests__/index.test.d.ts +0 -2
  179. package/lib/typescript/__tests__/index.test.d.ts.map +0 -1
  180. package/lib/typescript/__tests__/jsCore.test.d.ts +0 -2
  181. package/lib/typescript/__tests__/jsCore.test.d.ts.map +0 -1
  182. package/lib/typescript/__tests__/utils.test.d.ts +0 -2
  183. package/lib/typescript/__tests__/utils.test.d.ts.map +0 -1
  184. package/lib/typescript/api.d.ts +0 -240
  185. package/lib/typescript/api.d.ts.map +0 -1
  186. package/lib/typescript/configurations.d.ts +0 -73
  187. package/lib/typescript/configurations.d.ts.map +0 -1
  188. package/lib/typescript/constants.d.ts +0 -64
  189. package/lib/typescript/constants.d.ts.map +0 -1
  190. package/lib/typescript/events.d.ts +0 -115
  191. package/lib/typescript/events.d.ts.map +0 -1
  192. package/lib/typescript/index.d.ts +0 -27
  193. package/lib/typescript/index.d.ts.map +0 -1
  194. package/lib/typescript/jsCore.d.ts +0 -152
  195. package/lib/typescript/jsCore.d.ts.map +0 -1
  196. package/lib/typescript/native.d.ts +0 -4
  197. package/lib/typescript/native.d.ts.map +0 -1
  198. package/lib/typescript/subject.d.ts +0 -91
  199. package/lib/typescript/subject.d.ts.map +0 -1
  200. package/lib/typescript/tracker.d.ts +0 -111
  201. package/lib/typescript/tracker.d.ts.map +0 -1
  202. package/lib/typescript/types.d.ts +0 -981
  203. package/lib/typescript/types.d.ts.map +0 -1
  204. package/lib/typescript/utils.d.ts +0 -35
  205. package/lib/typescript/utils.d.ts.map +0 -1
  206. package/lib/typescript/webViewInterface.d.ts +0 -15
  207. package/lib/typescript/webViewInterface.d.ts.map +0 -1
  208. package/scripts/bootstrap.js +0 -29
  209. package/snowplow-react-native-tracker.podspec +0 -42
  210. package/src/__mocks__/react-native.js +0 -7
  211. package/src/__tests__/api.test.ts +0 -21
  212. package/src/__tests__/configurations.test.ts +0 -608
  213. package/src/__tests__/events.test.ts +0 -736
  214. package/src/__tests__/index.test.ts +0 -14
  215. package/src/__tests__/jsCore.test.ts +0 -199
  216. package/src/__tests__/utils.test.ts +0 -10
  217. package/src/api.ts +0 -556
  218. package/src/configurations.ts +0 -325
  219. package/src/constants.ts +0 -97
  220. package/src/events.ts +0 -355
  221. package/src/index.ts +0 -184
  222. package/src/jsCore.ts +0 -603
  223. package/src/native.ts +0 -21
  224. package/src/subject.ts +0 -302
  225. package/src/tracker.ts +0 -377
  226. package/src/types.ts +0 -1162
  227. package/src/utils.ts +0 -65
  228. package/src/webViewInterface.ts +0 -92
  229. package/tsconfig.build.json +0 -5
  230. package/tsconfig.json +0 -28
  231. package/turbo.json +0 -34
  232. package/yarn.lock +0 -9398
@@ -1,140 +0,0 @@
1
- const Micro = require('./microHelpers.js');
2
-
3
- const microUrl = '0.0.0.0';
4
- const microPort = '9090';
5
- const microGood = '/micro/good';
6
- const microBad = '/micro/bad';
7
- const microReset = '/micro/reset';
8
-
9
- async function getMicroGood() {
10
- const good = await Micro.requestJson(microUrl, microPort, microGood);
11
- return good;
12
- }
13
-
14
- async function getMicroBad() {
15
- const bad = await Micro.requestJson(microUrl, microPort, microBad);
16
- return bad;
17
- }
18
-
19
- async function resetMicro() {
20
- await Micro.request(microUrl, microPort, microReset);
21
- }
22
-
23
- /**
24
- * setTimeout promise
25
- *
26
- * @param {number} millis The milliseconds to wait
27
- */
28
- function sleep(millis) {
29
- return new Promise(resolve => {
30
- setTimeout(() => resolve('done sleeping!'), millis);
31
- });
32
- }
33
-
34
- /**
35
- * Asserts no bad events
36
- *
37
- */
38
- function assertNoBadEvents() {
39
- return getMicroBad().then(res => {
40
- expect(res.length).toBe(0);
41
- });
42
- }
43
-
44
- /**
45
- * Asserts on the number of events having a given schema
46
- *
47
- * ```
48
- * eventsWithSchema("iglu:com.acme/test_event/jsonschema/1-0-0", 2);
49
- * ```
50
- *
51
- * @param {string} schema The event's schema to match
52
- * @param {number} [n=1] The expected number of matching events
53
- */
54
- function eventsWithSchema(schema, n = 1) {
55
- n = parseInt(n, 10);
56
- return getMicroGood().then(arr => {
57
- const res = Micro.matchBySchema(arr, schema);
58
- expect(res.length).toBe(n);
59
- });
60
- }
61
-
62
- /**
63
- * Asserts on the number of events having a given type
64
- *
65
- * ```
66
- * eventsWithEventType("struct", 7);
67
- * ```
68
- *
69
- * @param {string} eventType The event's type to match
70
- * @param {number} [n=1] The expected number of matching events
71
- */
72
- function eventsWithEventType(eventType, n = 1) {
73
- n = parseInt(n, 10);
74
- return getMicroGood().then(arr => {
75
- const res = Micro.matchByEventType(arr, eventType);
76
- expect(res.length).toBe(n);
77
- });
78
- }
79
-
80
- /**
81
- * Asserts on the number of events having a given set of properties (schema, values, contexts, parameters)
82
- *
83
- * ```
84
- * eventsWithProperties({
85
- * schema: "iglu:com.acme/test_event/jsonschema/1-0-0",
86
- * values: {
87
- * testProperty: true
88
- * },
89
- * header: "the: header"
90
- * contexts: [{
91
- * schema: "iglu:com.acme/test_context/jsonschema/1-0-0",
92
- * data: {
93
- * "testCoProp": 0,
94
- * }
95
- * }],
96
- * parameters: {
97
- * user_id: "tester",
98
- * name_tracker: "myTrackerName"
99
- * }
100
- * }, 3);
101
- * ```
102
- *
103
- * @param {Object} eventOptions The options to match against
104
- * @param {number} [n=1] The expected number of matching events
105
- */
106
- function eventsWithProperties(eventOptions, n = 1) {
107
- n = parseInt(n, 10);
108
- return getMicroGood().then(arr => {
109
- const res = Micro.matchEvents(arr, eventOptions);
110
- expect(res.length).toBe(n);
111
- });
112
- }
113
-
114
- /**
115
- * Asserts on the number of events having a given HTTP request header
116
- *
117
- * ```
118
- * eventsWithHeader("Connection: keep-alive", 2);
119
- * ```
120
- *
121
- * @param {string} header The request header to match
122
- * @param {number} [n=1] The expected number of matching events
123
- */
124
- function eventsWithHeader(header, n = 1) {
125
- n = parseInt(n, 10);
126
- return getMicroGood().then(arr => {
127
- const res = Micro.matchByHeader(arr, header);
128
- expect(res.length).toBe(n);
129
- });
130
- }
131
-
132
- module.exports = {
133
- assertNoBadEvents,
134
- eventsWithSchema,
135
- eventsWithEventType,
136
- eventsWithProperties,
137
- eventsWithHeader,
138
- resetMicro,
139
- sleep,
140
- };
@@ -1,333 +0,0 @@
1
- const http = require('http');
2
-
3
- /**
4
- * Makes an http get request
5
- *
6
- * @param {string} url
7
- * @param {string} port
8
- * @param {string} path
9
- */
10
- function request(url, port, path) {
11
- return new Promise((resolve, reject) => {
12
- const options = {
13
- hostname: url,
14
- port: port,
15
- path: path,
16
- };
17
- try {
18
- http.get(options, res => {
19
- let data = '';
20
- res.on('data', d => (data += d));
21
- res.on('end', () => resolve(data));
22
- });
23
- } catch (e) {
24
- reject(e);
25
- }
26
- });
27
- }
28
-
29
- /**
30
- * Returns a JSON parsed response from an http request
31
- *
32
- * @param {string} url
33
- * @param {string} port
34
- * @param {string} path
35
- */
36
- function requestJson(url, port, path) {
37
- return request(url, port, path).then(g => JSON.parse(g));
38
- }
39
-
40
- /**
41
- * Filters an array of Snowplow events based on eventType
42
- *
43
- * ```
44
- * matchByEventType(goodEventsArray, "page_view");
45
- *
46
- * ```
47
- * @param {Array} eventsArray An array of Snowplow events
48
- * @param {string} eventType The eventType to match
49
- * @returns {Array} An array with the matching events
50
- */
51
- function matchByEventType(eventsArray, eventType) {
52
- return eventsArray.filter(hasEventType(eventType));
53
- }
54
-
55
- /**
56
- * Filters an array of Snowplow events based on schema
57
- *
58
- * ```
59
- * matchBySchema(goodEventsArray, "iglu:com.acme/test_event/jsonschema/1-0-0");
60
- * ```
61
- *
62
- * @param {Array} eventsArray An array of Snowplow events
63
- * @param {string} schema The schema to match
64
- * @returns {Array} An array with the matching events
65
- */
66
- function matchBySchema(eventsArray, schema) {
67
- return eventsArray.filter(hasSchema(schema));
68
- }
69
-
70
- /**
71
- * Filters an array of unstructured Snowplow events based on data values
72
- *
73
- * ```
74
- * matchByVals(goodEventsArray, {targetUrl: "https://docs.snowplowanalytics.com/"});
75
- * ```
76
- *
77
- * @param {Array} eventsArray An array of unstructured Snowplow events
78
- * @param {Object} valsObj The object having as keys the data fields to match
79
- * @returns {Array} An array with the matching events
80
- */
81
- function matchByVals(eventsArray, valsObj) {
82
- return eventsArray.filter(hasValues(valsObj));
83
- }
84
-
85
- /**
86
- * Filters an array of Snowplow events based on parameter values
87
- *
88
- * ```
89
- * matchByParams(goodEventsArray,
90
- * {
91
- * event: "struct",
92
- * se_category: "Mixes",
93
- * se_action: "Play",
94
- * });
95
- * ```
96
- *
97
- * @param {Array} eventsArray An array of Snowplow events
98
- * @param {Object} paramsObj An object having as keys the event parameters to match
99
- * @returns {Array} An array with the matching events
100
- */
101
- function matchByParams(eventsArray, paramsObj) {
102
- return eventsArray.filter(hasParams(paramsObj));
103
- }
104
-
105
- /**
106
- * Filters an array of Snowplow events based on an array of attached contexts
107
- *
108
- * ```
109
- * matchByContexts(goodEventsArray,
110
- * [{
111
- * schema: "iglu:com.acme/a_test_context/jsonschema/1-0-0",
112
- * data: {"testprop": 0}
113
- * },{
114
- * schema: "iglu:com.acme/b_test_context/jsonschema/1-0-0"
115
- * }]);
116
- * ```
117
- *
118
- * @param {Array} eventsArray An array of Snowplow events
119
- * @param {Array} expectedContextsArray An array of contexts
120
- * @returns {Array} An array with the matching events
121
- */
122
- function matchByContexts(eventsArray, expectedContextsArray) {
123
- return eventsArray.filter(hasContexts(expectedContextsArray));
124
- }
125
-
126
- /**
127
- * Filters an array of Snowplow events based on the request headers
128
- *
129
- * ```
130
- * matchByHeader(goodEventsArray, "Connection: keep-alive");
131
- *
132
- * ```
133
- * @param {Array} eventsArray An array of Snowplow events
134
- * @param {string} header The HTTP request header to match
135
- * @returns {Array} An array with the matching events
136
- */
137
- function matchByHeader(eventsArray, header) {
138
- return eventsArray.filter(hasHeader(header));
139
- }
140
-
141
- /**
142
- * Filters an array of Snowplow events based on event's Properties
143
- *
144
- * ```
145
- * matchEvents(goodEventsArray,
146
- * {
147
- * schema: "iglu:com.acme/test_event/jsonschema/1-0-0",
148
- * values: {
149
- * testProperty: true
150
- * },
151
- * contexts: [{
152
- * schema: "iglu:com.acme/test_context/jsonschema/1-0-0",
153
- * data: {
154
- * testCoProp: 0,
155
- * }
156
- * }],
157
- * parameters: {
158
- * user_id: "tester",
159
- * name_tracker: "myTrackerName"
160
- * },
161
- * header: 'the: header',
162
- * });
163
- * ```
164
- *
165
- * @param {Array} microEvents An array of Snowplow events
166
- * @param {Properties} eventProps The event properties to match against
167
- * @returns {Array} An array with the matching events
168
- */
169
- function matchEvents(microEvents, eventProps) {
170
- if (Object.prototype.toString.call(microEvents) !== '[object Array]') {
171
- microEvents = [microEvents];
172
- }
173
-
174
- let res = microEvents;
175
-
176
- if (eventProps.eventType) {
177
- res = matchByEventType(res, eventProps.eventType);
178
- }
179
-
180
- if (eventProps.schema) {
181
- res = matchBySchema(res, eventProps.schema);
182
- }
183
-
184
- if (eventProps.values) {
185
- res = matchByVals(res, eventProps.values);
186
- }
187
-
188
- if (eventProps.contexts) {
189
- res = matchByContexts(res, eventProps.contexts);
190
- }
191
-
192
- if (eventProps.parameters) {
193
- res = matchByParams(res, eventProps.parameters);
194
- }
195
-
196
- if (eventProps.header) {
197
- res = matchByHeader(res, eventProps.header);
198
- }
199
-
200
- return res;
201
- }
202
-
203
- function hasEventType(evType) {
204
- return function (ev) {
205
- return ev.eventType === evType;
206
- };
207
- }
208
-
209
- function hasSchema(schema) {
210
- return function (ev) {
211
- if (ev.eventType === 'unstruct') {
212
- let unstruct_ev = ev.event.unstruct_event;
213
-
214
- return unstruct_ev.data.schema === schema;
215
- } else {
216
- return false;
217
- }
218
- };
219
- }
220
-
221
- function hasValues(values) {
222
- return function (ev) {
223
- if (ev.eventType === 'unstruct') {
224
- let unstruct_ev = ev.event.unstruct_event;
225
- let data = unstruct_ev.data.data;
226
-
227
- return (
228
- Object.keys(values).every(keyIncludedIn(data)) &&
229
- Object.keys(values).every(comparesIn(values, data))
230
- );
231
- } else {
232
- return false;
233
- }
234
- };
235
- }
236
-
237
- function hasParams(expectParams) {
238
- return function (ev) {
239
- let actualParams = ev.event;
240
-
241
- return (
242
- Object.keys(expectParams).every(keyIncludedIn(actualParams)) &&
243
- Object.keys(expectParams).every(comparesIn(expectParams, actualParams))
244
- );
245
- };
246
- }
247
-
248
- function hasContexts(expCoArr) {
249
- return function (ev) {
250
- let evCo = ev.event.contexts;
251
-
252
- if (evCo.hasOwnProperty('data')) {
253
- let actCoArr = evCo.data;
254
-
255
- return compare(expCoArr, actCoArr);
256
- } else {
257
- return false;
258
- }
259
- };
260
- }
261
-
262
- function hasHeader(header) {
263
- return function (ev) {
264
- return ev.rawEvent.context.headers.includes(header);
265
- };
266
- }
267
-
268
- function keyIncludedIn(obj) {
269
- return function (key) {
270
- return Object.keys(obj).includes(key);
271
- };
272
- }
273
-
274
- function comparesIn(expected, actual) {
275
- return function (key) {
276
- let expValue = expected[key];
277
- let actValue = actual[key];
278
-
279
- return compare(expValue, actValue);
280
- };
281
- }
282
-
283
- /**
284
- * A function to deep compare (equality for primitive data types and containership for Objects and Arrays)
285
- *
286
- * ```
287
- * compare(1, 1); // true
288
- * compare(1, "1"); // false
289
- * compare([1, 2], [3, 1, 2]); // true
290
- * compare({a:1, b:2}, {b:2, c:3, a:1}); // true
291
- * compare([1, [2]], [[3, 2], 1, 4]); // true
292
- * compare({a: [1, 2], b: {c:3}}, {b: {d:4, c:3}, a:[3, 2, 1]}); // true
293
- * ```
294
- *
295
- * @param {*} expVal The expected value
296
- * @param {*} actVal The actual value
297
- * @returns {Boolean} Whether the expVal is equal to or deeply contained in actVal
298
- */
299
- function compare(expVal, actVal) {
300
- let expType = Object.prototype.toString.call(expVal);
301
- let actType = Object.prototype.toString.call(actVal);
302
-
303
- if (expVal === null) {
304
- return actVal === null;
305
- } else if (expType !== actType) {
306
- return false;
307
- } else if (expType === '[object Array]') {
308
- return expVal.every(function (e) {
309
- return actVal.some(function (a) {
310
- return compare(e, a);
311
- });
312
- });
313
- } else if (expType === '[object Object]') {
314
- return Object.keys(expVal).every(function (k) {
315
- return Object.keys(actVal).includes(k) && compare(expVal[k], actVal[k]);
316
- });
317
- } else {
318
- return expVal === actVal;
319
- }
320
- }
321
-
322
- module.exports = {
323
- matchByEventType,
324
- matchBySchema,
325
- matchByVals,
326
- matchByParams,
327
- matchByContexts,
328
- matchByHeader,
329
- matchEvents,
330
- compare,
331
- request,
332
- requestJson,
333
- };