@splitsoftware/splitio-commons 1.17.1-rc.4 → 2.0.0-rc.1

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 (177) hide show
  1. package/CHANGES.txt +4 -0
  2. package/cjs/evaluator/Engine.js +1 -1
  3. package/cjs/evaluator/index.js +1 -1
  4. package/cjs/evaluator/matchers/semver_inlist.js +1 -2
  5. package/cjs/evaluator/matchers/whitelist.js +1 -2
  6. package/cjs/listeners/browser.js +1 -2
  7. package/cjs/logger/browser/DebugLogger.js +1 -2
  8. package/cjs/logger/browser/ErrorLogger.js +1 -2
  9. package/cjs/logger/browser/InfoLogger.js +1 -2
  10. package/cjs/logger/browser/WarnLogger.js +1 -2
  11. package/cjs/logger/index.js +1 -2
  12. package/cjs/sdkClient/clientCS.js +5 -8
  13. package/cjs/sdkClient/sdkClientMethodCS.js +1 -1
  14. package/cjs/services/decorateHeaders.js +1 -2
  15. package/cjs/storages/KeyBuilderCS.js +0 -9
  16. package/cjs/storages/inLocalStorage/MySegmentsCacheInLocal.js +1 -21
  17. package/cjs/storages/inLocalStorage/SplitsCacheInLocal.js +4 -4
  18. package/cjs/storages/inLocalStorage/index.js +1 -1
  19. package/cjs/storages/inMemory/InMemoryStorageCS.js +1 -1
  20. package/cjs/storages/inMemory/SegmentsCacheInMemory.js +2 -3
  21. package/cjs/storages/inMemory/SplitsCacheInMemory.js +2 -3
  22. package/cjs/storages/inMemory/UniqueKeysCacheInMemory.js +2 -2
  23. package/cjs/storages/inMemory/UniqueKeysCacheInMemoryCS.js +2 -2
  24. package/cjs/storages/inRedis/RedisAdapter.js +2 -2
  25. package/cjs/storages/inRedis/SplitsCacheInRedis.js +1 -1
  26. package/cjs/storages/inRedis/TelemetryCacheInRedis.js +3 -4
  27. package/cjs/storages/inRedis/UniqueKeysCacheInRedis.js +1 -1
  28. package/cjs/storages/pluggable/SplitsCachePluggable.js +1 -1
  29. package/cjs/storages/pluggable/TelemetryCachePluggable.js +6 -7
  30. package/cjs/storages/pluggable/UniqueKeysCachePluggable.js +1 -1
  31. package/cjs/storages/pluggable/inMemoryWrapper.js +6 -6
  32. package/cjs/sync/polling/updaters/splitChangesUpdater.js +3 -3
  33. package/cjs/sync/streaming/parseUtils.js +0 -1
  34. package/cjs/sync/streaming/pushManager.js +2 -3
  35. package/cjs/utils/LRUCache/index.js +1 -2
  36. package/cjs/utils/constants/browser.js +1 -4
  37. package/cjs/utils/lang/index.js +6 -9
  38. package/cjs/utils/lang/objectAssign.js +12 -77
  39. package/cjs/utils/lang/sets.js +10 -107
  40. package/cjs/utils/settingsValidation/index.js +0 -9
  41. package/cjs/utils/settingsValidation/logger/builtinLogger.js +1 -2
  42. package/esm/evaluator/Engine.js +1 -1
  43. package/esm/evaluator/index.js +2 -2
  44. package/esm/evaluator/matchers/semver_inlist.js +1 -2
  45. package/esm/evaluator/matchers/whitelist.js +1 -2
  46. package/esm/listeners/browser.js +1 -2
  47. package/esm/logger/browser/DebugLogger.js +1 -2
  48. package/esm/logger/browser/ErrorLogger.js +1 -2
  49. package/esm/logger/browser/InfoLogger.js +1 -2
  50. package/esm/logger/browser/WarnLogger.js +1 -2
  51. package/esm/logger/index.js +1 -2
  52. package/esm/sdkClient/clientCS.js +5 -8
  53. package/esm/sdkClient/sdkClientMethodCS.js +1 -1
  54. package/esm/services/decorateHeaders.js +1 -2
  55. package/esm/storages/KeyBuilderCS.js +0 -9
  56. package/esm/storages/inLocalStorage/MySegmentsCacheInLocal.js +1 -21
  57. package/esm/storages/inLocalStorage/SplitsCacheInLocal.js +4 -4
  58. package/esm/storages/inLocalStorage/index.js +1 -1
  59. package/esm/storages/inMemory/InMemoryStorageCS.js +1 -1
  60. package/esm/storages/inMemory/SegmentsCacheInMemory.js +2 -3
  61. package/esm/storages/inMemory/SplitsCacheInMemory.js +2 -3
  62. package/esm/storages/inMemory/UniqueKeysCacheInMemory.js +2 -2
  63. package/esm/storages/inMemory/UniqueKeysCacheInMemoryCS.js +2 -2
  64. package/esm/storages/inRedis/RedisAdapter.js +2 -2
  65. package/esm/storages/inRedis/SplitsCacheInRedis.js +2 -2
  66. package/esm/storages/inRedis/TelemetryCacheInRedis.js +3 -4
  67. package/esm/storages/inRedis/UniqueKeysCacheInRedis.js +1 -1
  68. package/esm/storages/pluggable/SplitsCachePluggable.js +2 -2
  69. package/esm/storages/pluggable/TelemetryCachePluggable.js +6 -7
  70. package/esm/storages/pluggable/UniqueKeysCachePluggable.js +1 -1
  71. package/esm/storages/pluggable/inMemoryWrapper.js +7 -7
  72. package/esm/sync/polling/updaters/splitChangesUpdater.js +3 -3
  73. package/esm/sync/streaming/parseUtils.js +0 -1
  74. package/esm/sync/streaming/pushManager.js +2 -3
  75. package/esm/utils/LRUCache/index.js +1 -2
  76. package/esm/utils/constants/browser.js +0 -3
  77. package/esm/utils/lang/index.js +6 -9
  78. package/esm/utils/lang/objectAssign.js +12 -77
  79. package/esm/utils/lang/sets.js +9 -105
  80. package/esm/utils/settingsValidation/index.js +0 -9
  81. package/esm/utils/settingsValidation/logger/builtinLogger.js +1 -2
  82. package/package.json +2 -2
  83. package/src/evaluator/Engine.ts +1 -1
  84. package/src/evaluator/index.ts +4 -4
  85. package/src/evaluator/matchers/semver_inlist.ts +1 -2
  86. package/src/evaluator/matchers/whitelist.ts +1 -3
  87. package/src/listeners/browser.ts +1 -2
  88. package/src/logger/browser/DebugLogger.ts +1 -2
  89. package/src/logger/browser/ErrorLogger.ts +1 -2
  90. package/src/logger/browser/InfoLogger.ts +1 -2
  91. package/src/logger/browser/WarnLogger.ts +1 -2
  92. package/src/logger/index.ts +3 -4
  93. package/src/sdkClient/clientCS.ts +5 -8
  94. package/src/sdkClient/sdkClientMethodCS.ts +1 -1
  95. package/src/sdkFactory/types.ts +1 -1
  96. package/src/services/decorateHeaders.ts +1 -2
  97. package/src/storages/AbstractSplitsCacheAsync.ts +1 -2
  98. package/src/storages/AbstractSplitsCacheSync.ts +1 -2
  99. package/src/storages/KeyBuilderCS.ts +0 -13
  100. package/src/storages/inLocalStorage/MySegmentsCacheInLocal.ts +1 -21
  101. package/src/storages/inLocalStorage/SplitsCacheInLocal.ts +5 -5
  102. package/src/storages/inLocalStorage/index.ts +1 -1
  103. package/src/storages/inMemory/InMemoryStorageCS.ts +1 -1
  104. package/src/storages/inMemory/SegmentsCacheInMemory.ts +3 -4
  105. package/src/storages/inMemory/SplitsCacheInMemory.ts +4 -5
  106. package/src/storages/inMemory/UniqueKeysCacheInMemory.ts +4 -4
  107. package/src/storages/inMemory/UniqueKeysCacheInMemoryCS.ts +4 -4
  108. package/src/storages/inRedis/RedisAdapter.ts +3 -3
  109. package/src/storages/inRedis/SplitsCacheInRedis.ts +3 -3
  110. package/src/storages/inRedis/TelemetryCacheInRedis.ts +3 -4
  111. package/src/storages/inRedis/UniqueKeysCacheInRedis.ts +1 -1
  112. package/src/storages/pluggable/SegmentsCachePluggable.ts +0 -1
  113. package/src/storages/pluggable/SplitsCachePluggable.ts +3 -3
  114. package/src/storages/pluggable/TelemetryCachePluggable.ts +6 -7
  115. package/src/storages/pluggable/UniqueKeysCachePluggable.ts +1 -1
  116. package/src/storages/pluggable/inMemoryWrapper.ts +9 -9
  117. package/src/storages/types.ts +3 -4
  118. package/src/sync/polling/updaters/splitChangesUpdater.ts +4 -4
  119. package/src/sync/streaming/parseUtils.ts +0 -1
  120. package/src/sync/streaming/pushManager.ts +3 -4
  121. package/src/sync/submitters/types.ts +3 -4
  122. package/src/types.ts +1 -9
  123. package/src/utils/LRUCache/index.ts +2 -3
  124. package/src/utils/constants/browser.ts +0 -4
  125. package/src/utils/lang/index.ts +6 -7
  126. package/src/utils/lang/objectAssign.ts +13 -92
  127. package/src/utils/lang/sets.ts +10 -122
  128. package/src/utils/settingsValidation/index.ts +0 -10
  129. package/src/utils/settingsValidation/logger/builtinLogger.ts +1 -2
  130. package/src/utils/settingsValidation/types.ts +0 -2
  131. package/types/logger/index.d.ts +1 -2
  132. package/types/sdkClient/clientCS.d.ts +2 -3
  133. package/types/sdkFactory/types.d.ts +1 -1
  134. package/types/storages/AbstractSplitsCacheAsync.d.ts +1 -2
  135. package/types/storages/AbstractSplitsCacheSync.d.ts +1 -2
  136. package/types/storages/KeyBuilderCS.d.ts +0 -2
  137. package/types/storages/inLocalStorage/SplitsCacheInLocal.d.ts +1 -2
  138. package/types/storages/inMemory/SplitsCacheInMemory.d.ts +1 -2
  139. package/types/storages/inMemory/UniqueKeysCacheInMemory.d.ts +2 -3
  140. package/types/storages/inRedis/SplitsCacheInRedis.d.ts +1 -2
  141. package/types/storages/pluggable/SplitsCachePluggable.d.ts +1 -2
  142. package/types/storages/pluggable/inMemoryWrapper.d.ts +1 -2
  143. package/types/storages/types.d.ts +3 -4
  144. package/types/sync/polling/updaters/splitChangesUpdater.d.ts +1 -2
  145. package/types/sync/submitters/types.d.ts +3 -4
  146. package/types/types.d.ts +1 -9
  147. package/types/utils/LRUCache/index.d.ts +1 -2
  148. package/types/utils/constants/browser.d.ts +0 -2
  149. package/types/utils/lang/objectAssign.d.ts +3 -0
  150. package/types/utils/lang/sets.d.ts +2 -61
  151. package/types/utils/settingsValidation/index.d.ts +0 -1
  152. package/types/utils/settingsValidation/types.d.ts +0 -2
  153. package/cjs/integrations/browser.js +0 -31
  154. package/cjs/integrations/ga/GaToSplit.js +0 -257
  155. package/cjs/integrations/ga/GoogleAnalyticsToSplit.js +0 -14
  156. package/cjs/integrations/ga/SplitToGa.js +0 -123
  157. package/cjs/integrations/ga/SplitToGoogleAnalytics.js +0 -14
  158. package/cjs/integrations/ga/types.js +0 -2
  159. package/cjs/sdkClient/sdkClientMethodCSWithTT.js +0 -75
  160. package/cjs/utils/lang/maps.js +0 -96
  161. package/esm/integrations/browser.js +0 -27
  162. package/esm/integrations/ga/GaToSplit.js +0 -250
  163. package/esm/integrations/ga/GoogleAnalyticsToSplit.js +0 -10
  164. package/esm/integrations/ga/SplitToGa.js +0 -120
  165. package/esm/integrations/ga/SplitToGoogleAnalytics.js +0 -10
  166. package/esm/integrations/ga/types.js +0 -1
  167. package/esm/sdkClient/sdkClientMethodCSWithTT.js +0 -71
  168. package/esm/utils/lang/maps.js +0 -92
  169. package/src/integrations/browser.ts +0 -35
  170. package/src/integrations/ga/GaToSplit.ts +0 -299
  171. package/src/integrations/ga/GoogleAnalyticsToSplit.ts +0 -14
  172. package/src/integrations/ga/SplitToGa.ts +0 -135
  173. package/src/integrations/ga/SplitToGoogleAnalytics.ts +0 -14
  174. package/src/integrations/ga/autoRequire.js +0 -33
  175. package/src/integrations/ga/types.ts +0 -153
  176. package/src/sdkClient/sdkClientMethodCSWithTT.ts +0 -96
  177. package/src/utils/lang/maps.ts +0 -108
@@ -1,153 +0,0 @@
1
- import { SplitIO } from '../../types';
2
-
3
- /**
4
- * A pair of user key and it's trafficType, required for tracking valid Split events.
5
- * @typedef {Object} Identity
6
- * @property {string} key The user key.
7
- * @property {string} trafficType The key traffic type.
8
- */
9
- export type Identity = {
10
- key: string;
11
- trafficType: string;
12
- };
13
-
14
- /**
15
- * Options for GoogleAnalyticsToSplit integration plugin
16
- */
17
- export interface GoogleAnalyticsToSplitOptions {
18
- /**
19
- * Optional flag to filter GA hits from being tracked as Split events.
20
- * @property {boolean} hits
21
- * @default true
22
- */
23
- hits?: boolean,
24
- /**
25
- * Optional predicate used to define a custom filter for tracking GA hits as Split events.
26
- * For example, the following filter allows to track only 'event' hits:
27
- * `(model) => model.get('hitType') === 'event'`
28
- * By default, all hits are tracked as Split events.
29
- */
30
- filter?: (model: UniversalAnalytics.Model) => boolean,
31
- /**
32
- * Optional function useful when you need to modify the Split event before tracking it.
33
- * This function is invoked with two arguments:
34
- * 1. the GA model object representing the hit.
35
- * 2. the default format of the mapped Split event instance.
36
- * The return value must be a Split event, that can be the second argument or a new object.
37
- *
38
- * For example, the following mapper adds a custom property to events:
39
- * `(model, defaultMapping) => {
40
- * defaultMapping.properties.someProperty = SOME_VALUE;
41
- * return defaultMapping;
42
- * }`
43
- */
44
- mapper?: (model: UniversalAnalytics.Model, defaultMapping: SplitIO.EventData) => SplitIO.EventData,
45
- /**
46
- * Optional prefix for EventTypeId, to prevent any kind of data collision between events.
47
- * @property {string} prefix
48
- * @default 'ga'
49
- */
50
- prefix?: string,
51
- /**
52
- * List of Split identities (key & traffic type pairs) used to track events.
53
- * If not provided, events are sent using the key and traffic type provided at SDK config
54
- */
55
- identities?: Identity[],
56
- /**
57
- * Optional flag to log an error if the `auto-require` script is not detected.
58
- * The auto-require script automatically requires the `splitTracker` plugin for created trackers,
59
- * and should be placed right after your Google Analytics, Google Tag Manager or gtag.js script tag.
60
- *
61
- * @see {@link https://help.split.io/hc/en-us/articles/360040838752#set-up-with-gtm-and-gtag.js}
62
- *
63
- * @property {boolean} autoRequire
64
- * @default false
65
- */
66
- autoRequire?: boolean,
67
- }
68
-
69
- /**
70
- * Enable 'Google Analytics to Split' integration, to track Google Analytics hits as Split events.
71
- * Used by the browser variant of the isomorphic JS SDK.
72
- *
73
- * @see {@link https://help.split.io/hc/en-us/articles/360040838752#google-analytics-to-split}
74
- */
75
- export interface IGoogleAnalyticsToSplitConfig extends GoogleAnalyticsToSplitOptions {
76
- type: 'GOOGLE_ANALYTICS_TO_SPLIT'
77
- }
78
-
79
- /**
80
- * Options for SplitToGoogleAnalytics integration plugin
81
- */
82
- export interface SplitToGoogleAnalyticsOptions {
83
- /**
84
- * Optional flag to filter Split impressions from being tracked as GA hits.
85
- * @property {boolean} impressions
86
- * @default true
87
- */
88
- impressions?: boolean,
89
- /**
90
- * Optional flag to filter Split events from being tracked as GA hits.
91
- * @property {boolean} events
92
- * @default true
93
- */
94
- events?: boolean,
95
- /**
96
- * Optional predicate used to define a custom filter for tracking Split data (events and impressions) as GA hits.
97
- * For example, the following filter allows to track only impressions, equivalent to setting events to false:
98
- * `(data) => data.type === 'IMPRESSION'`
99
- */
100
- filter?: (data: SplitIO.IntegrationData) => boolean,
101
- /**
102
- * Optional function useful when you need to modify the GA hit before sending it.
103
- * This function is invoked with two arguments:
104
- * 1. the input data (Split event or impression).
105
- * 2. the default format of the mapped FieldsObject instance (GA hit).
106
- * The return value must be a FieldsObject, that can be the second argument or a new object.
107
- *
108
- * For example, the following mapper adds a custom dimension to hits:
109
- * `(data, defaultMapping) => {
110
- * defaultMapping.dimension1 = SOME_VALUE;
111
- * return defaultMapping;
112
- * }`
113
- *
114
- * Default FieldsObject instance for data.type === 'IMPRESSION':
115
- * `{
116
- * hitType: 'event',
117
- * eventCategory: 'split-impression',
118
- * eventAction: 'Evaluate ' + data.payload.impression.feature,
119
- * eventLabel: 'Treatment: ' + data.payload.impression.treatment + '. Targeting rule: ' + data.payload.impression.label + '.',
120
- * nonInteraction: true,
121
- * }`
122
- * Default FieldsObject instance for data.type === 'EVENT':
123
- * `{
124
- * hitType: 'event',
125
- * eventCategory: 'split-event',
126
- * eventAction: data.payload.eventTypeId,
127
- * eventValue: data.payload.value,
128
- * nonInteraction: true,
129
- * }`
130
- */
131
- mapper?: (data: SplitIO.IntegrationData, defaultMapping: UniversalAnalytics.FieldsObject) => UniversalAnalytics.FieldsObject,
132
- /**
133
- * List of tracker names to send the hit. An empty string represents the default tracker.
134
- * If not provided, hits are only sent to default tracker.
135
- */
136
- trackerNames?: string[],
137
- }
138
-
139
- /**
140
- * Enable 'Split to Google Analytics' integration, to track Split impressions and events as Google Analytics hits.
141
- * Used by the browser variant of the isomorphic JS SDK.
142
- *
143
- * @see {@link https://help.split.io/hc/en-us/articles/360040838752#split-to-google-analytics}
144
- */
145
- export interface ISplitToGoogleAnalyticsConfig extends SplitToGoogleAnalyticsOptions {
146
- type: 'SPLIT_TO_GOOGLE_ANALYTICS'
147
- }
148
-
149
- /**
150
- * Available integration options for the browser
151
- * Used by the browser variant of the isomorphic JS SDK.
152
- */
153
- export type BrowserIntegration = ISplitToGoogleAnalyticsConfig | IGoogleAnalyticsToSplitConfig;
@@ -1,96 +0,0 @@
1
- import { clientCSDecorator } from './clientCS';
2
- import { SplitIO } from '../types';
3
- import { validateKey } from '../utils/inputValidation/key';
4
- import { validateTrafficType } from '../utils/inputValidation/trafficType';
5
- import { getMatching, keyParser } from '../utils/key';
6
- import { sdkClientFactory } from './sdkClient';
7
- import { ISyncManagerCS } from '../sync/types';
8
- import { objectAssign } from '../utils/lang/objectAssign';
9
- import { RETRIEVE_CLIENT_DEFAULT, NEW_SHARED_CLIENT, RETRIEVE_CLIENT_EXISTING, LOG_PREFIX_CLIENT_INSTANTIATION } from '../logger/constants';
10
- import { SDK_SEGMENTS_ARRIVED } from '../readiness/constants';
11
- import { ISdkFactoryContext } from '../sdkFactory/types';
12
- import { buildInstanceId } from './identity';
13
-
14
- /**
15
- * Factory of client method for the client-side (browser) variant of the Isomorphic JS SDK,
16
- * where clients can have a bound TT for the track method, which is provided via the settings
17
- * (default client) or the client method (shared clients).
18
- */
19
- export function sdkClientMethodCSFactory(params: ISdkFactoryContext): (key?: SplitIO.SplitKey, trafficType?: string) => SplitIO.ICsClient {
20
- const { clients, storage, syncManager, sdkReadinessManager, settings: { core: { key, trafficType }, log } } = params;
21
-
22
- const mainClientInstance = clientCSDecorator(
23
- log,
24
- sdkClientFactory(params) as SplitIO.IClient,
25
- key,
26
- trafficType
27
- );
28
-
29
- const parsedDefaultKey = keyParser(key);
30
- const defaultInstanceId = buildInstanceId(parsedDefaultKey, trafficType);
31
-
32
- // Cache instances created per factory.
33
- clients[defaultInstanceId] = mainClientInstance;
34
-
35
- return function client(key?: SplitIO.SplitKey, trafficType?: string) {
36
- if (key === undefined) {
37
- log.debug(RETRIEVE_CLIENT_DEFAULT);
38
- return mainClientInstance;
39
- }
40
-
41
- // Validate the key value
42
- const validKey = validateKey(log, key, LOG_PREFIX_CLIENT_INSTANTIATION);
43
- if (validKey === false) {
44
- throw new Error('Shared Client needs a valid key.');
45
- }
46
-
47
- let validTrafficType;
48
- if (trafficType !== undefined) {
49
- validTrafficType = validateTrafficType(log, trafficType, LOG_PREFIX_CLIENT_INSTANTIATION);
50
- if (validTrafficType === false) {
51
- throw new Error('Shared Client needs a valid traffic type or no traffic type at all.');
52
- }
53
- }
54
- const instanceId = buildInstanceId(validKey, validTrafficType);
55
-
56
- if (!clients[instanceId]) {
57
- const matchingKey = getMatching(validKey);
58
-
59
- const sharedSdkReadiness = sdkReadinessManager.shared();
60
- const sharedStorage = storage.shared && storage.shared(matchingKey, (err) => {
61
- if (err) {
62
- sharedSdkReadiness.readinessManager.timeout();
63
- return;
64
- }
65
- // Emit SDK_READY in consumer mode for shared clients
66
- sharedSdkReadiness.readinessManager.segments.emit(SDK_SEGMENTS_ARRIVED);
67
- });
68
-
69
- // 3 possibilities:
70
- // - Standalone mode: both syncManager and sharedSyncManager are defined
71
- // - Consumer mode: both syncManager and sharedSyncManager are undefined
72
- // - Consumer partial mode: syncManager is defined (only for submitters) but sharedSyncManager is undefined
73
- // @ts-ignore
74
- const sharedSyncManager = syncManager && sharedStorage && (syncManager as ISyncManagerCS).shared(matchingKey, sharedSdkReadiness.readinessManager, sharedStorage);
75
-
76
- // As shared clients reuse all the storage information, we don't need to check here if we
77
- // will use offline or online mode. We should stick with the original decision.
78
- clients[instanceId] = clientCSDecorator(
79
- log,
80
- sdkClientFactory(objectAssign({}, params, {
81
- sdkReadinessManager: sharedSdkReadiness,
82
- storage: sharedStorage || storage,
83
- syncManager: sharedSyncManager,
84
- }), true) as SplitIO.IClient,
85
- validKey,
86
- validTrafficType
87
- );
88
-
89
- log.info(NEW_SHARED_CLIENT);
90
- } else {
91
- log.debug(RETRIEVE_CLIENT_EXISTING);
92
- }
93
-
94
- return clients[instanceId] as SplitIO.ICsClient;
95
- };
96
- }
@@ -1,108 +0,0 @@
1
- /**
2
- * Map implementation based on es6-map polyfill (https://github.com/medikoo/es6-map/blob/master/polyfill.js),
3
- * with the minimal features used by the SDK.
4
-
5
- Copyright (C) 2013 Mariusz Nowak (www.medikoo.com)
6
-
7
- Permission is hereby granted, free of charge, to any person obtaining a copy
8
- of this software and associated documentation files (the "Software"), to deal
9
- in the Software without restriction, including without limitation the rights
10
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
- copies of the Software, and to permit persons to whom the Software is
12
- furnished to do so, subject to the following conditions:
13
-
14
- The above copyright notice and this permission notice shall be included in
15
- all copies or substantial portions of the Software.
16
-
17
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
- THE SOFTWARE.
24
- **/
25
-
26
- export interface IMap<K, V> {
27
- clear(): void;
28
- delete(key: K): boolean;
29
- forEach(callbackfn: (value: V, key: K, map: Map<K, V>) => void, thisArg?: any): void;
30
- get(key: K): V | undefined;
31
- has(key: K): boolean;
32
- set(key: K, value: V): this;
33
- readonly size: number;
34
- }
35
-
36
- export class MapPoly<K, V> implements IMap<K, V>{
37
- private __mapKeysData__: K[] = [];
38
- private __mapValuesData__: V[] = [];
39
-
40
- // unlike ES6 `Map`, it only accepts an array as first argument iterable
41
- constructor(entries?: readonly (readonly [K, V])[] | null) {
42
- if (Array.isArray(entries)) entries.forEach(entry => { this.set(entry[0], entry[1]); });
43
- }
44
-
45
- clear() {
46
- if (!this.__mapKeysData__.length) return;
47
- this.__mapKeysData__.length = 0;
48
- this.__mapValuesData__.length = 0;
49
- }
50
-
51
- delete(key: K) {
52
- const index = this.__mapKeysData__.indexOf(key);
53
- if (index === -1) return false;
54
- this.__mapKeysData__.splice(index, 1);
55
- this.__mapValuesData__.splice(index, 1);
56
- return true;
57
- }
58
-
59
- forEach(callbackfn: (value: V, key: K, map: Map<K, V>) => void, thisArg?: any) {
60
- for (let i = 0; i < this.__mapKeysData__.length; i++) {
61
- callbackfn.call(thisArg, this.__mapValuesData__[i], this.__mapKeysData__[i], this as any);
62
- }
63
- }
64
-
65
- get(key: K) {
66
- const index = this.__mapKeysData__.indexOf(key);
67
- if (index === -1) return;
68
- return this.__mapValuesData__[index];
69
- }
70
-
71
- has(key: K): boolean {
72
- return this.__mapKeysData__.indexOf(key) !== -1;
73
- }
74
-
75
- set(key: K, value: V) {
76
- let index = this.__mapKeysData__.indexOf(key);
77
- if (index === -1) index = this.__mapKeysData__.push(key) - 1;
78
- this.__mapValuesData__[index] = value;
79
- return this;
80
- }
81
-
82
- get size() {
83
- return this.__mapKeysData__.length;
84
- }
85
-
86
- }
87
-
88
- interface IMapConstructor {
89
- new(): IMap<any, any>;
90
- new <K, V>(entries?: readonly (readonly [K, V])[] | null): IMap<K, V>;
91
- readonly prototype: IMap<any, any>;
92
- }
93
-
94
- /**
95
- * return the Map constructor to use. If native Map is not available or it doesn't support the required features (e.g., IE11),
96
- * a ponyfill with minimal features is returned instead.
97
- *
98
- * Exported for testing purposes only.
99
- */
100
- export function __getMapConstructor(): IMapConstructor {
101
- // eslint-disable-next-line compat/compat
102
- if (typeof Array.from === 'function' && typeof Map === 'function' && Map.prototype && Map.prototype.values) {
103
- return Map;
104
- }
105
- return MapPoly;
106
- }
107
-
108
- export const _Map = __getMapConstructor();