@dxtmisha/functional-basic 0.1.0 → 0.1.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 (122) hide show
  1. package/package.json +5 -3
  2. package/src/classes/Api.ts +407 -0
  3. package/src/classes/ApiDefault.ts +83 -0
  4. package/src/classes/ApiHeaders.ts +52 -0
  5. package/src/classes/ApiPreparation.ts +114 -0
  6. package/src/classes/ApiResponse.ts +293 -0
  7. package/src/classes/ApiStatus.ts +173 -0
  8. package/src/classes/BroadcastMessage.ts +73 -0
  9. package/src/classes/Cache.ts +60 -0
  10. package/src/classes/CacheItem.ts +95 -0
  11. package/src/classes/CacheStatic.ts +30 -0
  12. package/src/classes/Cookie.ts +135 -0
  13. package/src/classes/CookieBlock.ts +31 -0
  14. package/src/classes/DataStorage.ts +194 -0
  15. package/src/classes/Datetime.ts +891 -0
  16. package/src/classes/EventItem.ts +373 -0
  17. package/src/classes/Geo.ts +320 -0
  18. package/src/classes/GeoFlag.ts +386 -0
  19. package/src/classes/GeoIntl.ts +839 -0
  20. package/src/classes/GeoPhone.ts +272 -0
  21. package/src/classes/Global.ts +32 -0
  22. package/src/classes/Hash.ts +142 -0
  23. package/src/classes/Icons.ts +165 -0
  24. package/src/classes/Loading.ts +90 -0
  25. package/src/classes/Meta.ts +284 -0
  26. package/src/classes/MetaManager.ts +200 -0
  27. package/src/classes/MetaOg.ts +147 -0
  28. package/src/classes/MetaTwitter.ts +154 -0
  29. package/src/classes/ScrollbarWidth.ts +86 -0
  30. package/src/classes/Translate.ts +293 -0
  31. package/src/classes/__tests__/Api.test.ts +728 -0
  32. package/src/classes/__tests__/ApiDefault.test.ts +222 -0
  33. package/src/classes/__tests__/ApiHeaders.test.ts +447 -0
  34. package/src/classes/__tests__/ApiPreparation.test.ts +257 -0
  35. package/src/classes/__tests__/ApiResponse.test.ts +547 -0
  36. package/src/classes/__tests__/ApiStatus.test.ts +403 -0
  37. package/src/classes/__tests__/Meta.test.ts +629 -0
  38. package/src/classes/__tests__/MetaManager.test.ts +836 -0
  39. package/src/classes/__tests__/MetaOg.test.ts +677 -0
  40. package/src/classes/__tests__/MetaTwitter.test.ts +423 -0
  41. package/src/functions/anyToString.ts +36 -0
  42. package/src/functions/applyTemplate.ts +63 -0
  43. package/src/functions/arrFill.ts +10 -0
  44. package/src/functions/copyObject.ts +10 -0
  45. package/src/functions/createElement.ts +40 -0
  46. package/src/functions/domQuerySelector.ts +15 -0
  47. package/src/functions/domQuerySelectorAll.ts +15 -0
  48. package/src/functions/encodeAttribute.ts +15 -0
  49. package/src/functions/eventStopPropagation.ts +10 -0
  50. package/src/functions/executeFunction.ts +13 -0
  51. package/src/functions/executePromise.ts +19 -0
  52. package/src/functions/forEach.ts +39 -0
  53. package/src/functions/frame.ts +38 -0
  54. package/src/functions/getAttributes.ts +27 -0
  55. package/src/functions/getClipboardData.ts +13 -0
  56. package/src/functions/getColumn.ts +18 -0
  57. package/src/functions/getElement.ts +35 -0
  58. package/src/functions/getElementId.ts +39 -0
  59. package/src/functions/getElementItem.ts +27 -0
  60. package/src/functions/getElementOrWindow.ts +23 -0
  61. package/src/functions/getExp.ts +21 -0
  62. package/src/functions/getItemByPath.ts +24 -0
  63. package/src/functions/getKey.ts +9 -0
  64. package/src/functions/getLengthOfAllArray.ts +13 -0
  65. package/src/functions/getMaxLengthAllArray.ts +13 -0
  66. package/src/functions/getMinLengthAllArray.ts +13 -0
  67. package/src/functions/getMouseClient.ts +17 -0
  68. package/src/functions/getMouseClientX.ts +9 -0
  69. package/src/functions/getMouseClientY.ts +9 -0
  70. package/src/functions/getObjectByKeys.ts +24 -0
  71. package/src/functions/getObjectNoUndefined.ts +23 -0
  72. package/src/functions/getObjectOrNone.ts +11 -0
  73. package/src/functions/getRandomText.ts +29 -0
  74. package/src/functions/getRequestString.ts +21 -0
  75. package/src/functions/getStepPercent.ts +19 -0
  76. package/src/functions/getStepValue.ts +19 -0
  77. package/src/functions/goScroll.ts +40 -0
  78. package/src/functions/inArray.ts +10 -0
  79. package/src/functions/initScrollbarOffset.ts +14 -0
  80. package/src/functions/intersectKey.ts +34 -0
  81. package/src/functions/isArray.ts +9 -0
  82. package/src/functions/isDifferent.ts +27 -0
  83. package/src/functions/isDomRuntime.ts +12 -0
  84. package/src/functions/isFilled.ts +49 -0
  85. package/src/functions/isFloat.ts +16 -0
  86. package/src/functions/isFunction.ts +11 -0
  87. package/src/functions/isInDom.ts +15 -0
  88. package/src/functions/isIntegerBetween.ts +11 -0
  89. package/src/functions/isNull.ts +11 -0
  90. package/src/functions/isNumber.ts +16 -0
  91. package/src/functions/isObject.ts +9 -0
  92. package/src/functions/isObjectNotArray.ts +11 -0
  93. package/src/functions/isSelected.ts +32 -0
  94. package/src/functions/isSelectedByList.ts +19 -0
  95. package/src/functions/isString.ts +9 -0
  96. package/src/functions/isWindow.ts +11 -0
  97. package/src/functions/random.ts +10 -0
  98. package/src/functions/replaceRecursive.ts +60 -0
  99. package/src/functions/replaceTemplate.ts +22 -0
  100. package/src/functions/secondToTime.ts +20 -0
  101. package/src/functions/setElementItem.ts +56 -0
  102. package/src/functions/setValues.ts +59 -0
  103. package/src/functions/splice.ts +59 -0
  104. package/src/functions/strFill.ts +12 -0
  105. package/src/functions/toArray.ts +19 -0
  106. package/src/functions/toCamelCase.ts +16 -0
  107. package/src/functions/toCamelCaseFirst.ts +12 -0
  108. package/src/functions/toDate.ts +44 -0
  109. package/src/functions/toKebabCase.ts +25 -0
  110. package/src/functions/toNumber.ts +35 -0
  111. package/src/functions/toNumberByMax.ts +33 -0
  112. package/src/functions/toPercent.ts +10 -0
  113. package/src/functions/toPercentBy100.ts +12 -0
  114. package/src/functions/transformation.ts +59 -0
  115. package/src/functions/uniqueArray.ts +9 -0
  116. package/src/functions/writeClipboardData.ts +17 -0
  117. package/src/library.ts +116 -0
  118. package/src/types/apiTypes.ts +143 -0
  119. package/src/types/basicTypes.ts +155 -0
  120. package/src/types/geoTypes.ts +109 -0
  121. package/src/types/metaTypes.ts +764 -0
  122. package/src/vite-env.d.ts +1 -0
@@ -0,0 +1,257 @@
1
+ import { describe, it, expect, beforeEach, vi } from 'vitest'
2
+ import { ApiPreparation } from '../ApiPreparation'
3
+ import type { ApiPreparationEnd } from '../../types/apiTypes'
4
+
5
+ describe('ApiPreparation', () => {
6
+ let apiPreparation: ApiPreparation
7
+
8
+ beforeEach(() => {
9
+ apiPreparation = new ApiPreparation()
10
+ })
11
+
12
+ describe('make', () => {
13
+ it('should not call callback when active is false', async () => {
14
+ const callback = vi.fn().mockResolvedValue(undefined)
15
+ apiPreparation.set(callback)
16
+
17
+ await apiPreparation.make(false)
18
+
19
+ expect(callback).not.toHaveBeenCalled()
20
+ })
21
+
22
+ it('should not call callback when callback is not set', async () => {
23
+ const result = await apiPreparation.make(true)
24
+
25
+ expect(result).toBeUndefined()
26
+ })
27
+
28
+ it('should call callback when active is true and callback is set', async () => {
29
+ const callback = vi.fn().mockResolvedValue(undefined)
30
+ apiPreparation.set(callback)
31
+
32
+ await apiPreparation.make(true)
33
+
34
+ expect(callback).toHaveBeenCalledTimes(1)
35
+ })
36
+
37
+ it('should wait for callback to complete', async () => {
38
+ let callbackCompleted = false
39
+ const callback = vi.fn().mockImplementation(async () => {
40
+ await new Promise(resolve => setTimeout(resolve, 100))
41
+ callbackCompleted = true
42
+ })
43
+ apiPreparation.set(callback)
44
+
45
+ await apiPreparation.make(true)
46
+
47
+ expect(callbackCompleted).toBe(true)
48
+ })
49
+
50
+ it('should handle multiple concurrent calls without overlapping', async () => {
51
+ let executionCount = 0
52
+ const callback = vi.fn().mockImplementation(async () => {
53
+ executionCount++
54
+ await new Promise(resolve => setTimeout(resolve, 50))
55
+ executionCount--
56
+ })
57
+ apiPreparation.set(callback)
58
+
59
+ // Start multiple concurrent calls
60
+ const promises = [
61
+ apiPreparation.make(true),
62
+ apiPreparation.make(true),
63
+ apiPreparation.make(true)
64
+ ]
65
+
66
+ await Promise.all(promises)
67
+
68
+ // Callback should be called 3 times but never overlap
69
+ expect(callback).toHaveBeenCalledTimes(3)
70
+ expect(executionCount).toBe(0)
71
+ })
72
+ })
73
+
74
+ describe('makeEnd', () => {
75
+ it('should return empty object when active is false', async () => {
76
+ const callback = vi.fn().mockResolvedValue({ reset: true })
77
+ const mockResponse = new Response('{}', { status: 200 })
78
+ apiPreparation.setEnd(callback)
79
+
80
+ const result = await apiPreparation.makeEnd(false, mockResponse)
81
+
82
+ expect(result).toEqual({})
83
+ expect(callback).not.toHaveBeenCalled()
84
+ })
85
+
86
+ it('should return empty object when callback is not set', async () => {
87
+ const mockResponse = new Response('{}', { status: 200 })
88
+
89
+ const result = await apiPreparation.makeEnd(true, mockResponse)
90
+
91
+ expect(result).toEqual({})
92
+ })
93
+
94
+ it('should call callback and return result when active is true', async () => {
95
+ const expectedResult: ApiPreparationEnd = {
96
+ reset: true,
97
+ data: { message: 'Success' }
98
+ }
99
+ const callback = vi.fn().mockResolvedValue(expectedResult)
100
+ const mockResponse = new Response('{}', { status: 200 })
101
+ apiPreparation.setEnd(callback)
102
+
103
+ const result = await apiPreparation.makeEnd(true, mockResponse)
104
+
105
+ expect(callback).toHaveBeenCalledWith(mockResponse)
106
+ expect(result).toEqual(expectedResult)
107
+ })
108
+
109
+ it('should pass Response object to callback', async () => {
110
+ const callback = vi.fn().mockResolvedValue({})
111
+ const mockResponse = new Response('{"test": "data"}', {
112
+ status: 201,
113
+ statusText: 'Created'
114
+ })
115
+ apiPreparation.setEnd(callback)
116
+
117
+ await apiPreparation.makeEnd(true, mockResponse)
118
+
119
+ expect(callback).toHaveBeenCalledWith(mockResponse)
120
+ })
121
+
122
+ it('should handle callback returning reset flag', async () => {
123
+ const callback = vi.fn().mockResolvedValue({ reset: true })
124
+ const mockResponse = new Response('{}', { status: 200 })
125
+ apiPreparation.setEnd(callback)
126
+
127
+ const result = await apiPreparation.makeEnd(true, mockResponse)
128
+
129
+ expect(result.reset).toBe(true)
130
+ })
131
+
132
+ it('should handle callback returning data', async () => {
133
+ const testData = { userId: 123, token: 'abc' }
134
+ const callback = vi.fn().mockResolvedValue({ data: testData })
135
+ const mockResponse = new Response('{}', { status: 200 })
136
+ apiPreparation.setEnd(callback)
137
+
138
+ const result = await apiPreparation.makeEnd(true, mockResponse)
139
+
140
+ expect(result.data).toEqual(testData)
141
+ })
142
+ })
143
+
144
+ describe('set', () => {
145
+ it('should set callback function', async () => {
146
+ const callback = vi.fn().mockResolvedValue(undefined)
147
+
148
+ const returnedInstance = apiPreparation.set(callback)
149
+
150
+ expect(returnedInstance).toBe(apiPreparation)
151
+ await apiPreparation.make(true)
152
+ expect(callback).toHaveBeenCalled()
153
+ })
154
+
155
+ it('should return this for chaining', () => {
156
+ const callback = vi.fn().mockResolvedValue(undefined)
157
+
158
+ const result = apiPreparation.set(callback)
159
+
160
+ expect(result).toBe(apiPreparation)
161
+ })
162
+
163
+ it('should allow replacing callback', async () => {
164
+ const callback1 = vi.fn().mockResolvedValue(undefined)
165
+ const callback2 = vi.fn().mockResolvedValue(undefined)
166
+
167
+ apiPreparation.set(callback1)
168
+ apiPreparation.set(callback2)
169
+
170
+ await apiPreparation.make(true)
171
+
172
+ expect(callback1).not.toHaveBeenCalled()
173
+ expect(callback2).toHaveBeenCalled()
174
+ })
175
+ })
176
+
177
+ describe('setEnd', () => {
178
+ it('should set end callback function', async () => {
179
+ const callback = vi.fn().mockResolvedValue({})
180
+ const mockResponse = new Response('{}', { status: 200 })
181
+
182
+ const returnedInstance = apiPreparation.setEnd(callback)
183
+
184
+ expect(returnedInstance).toBe(apiPreparation)
185
+ await apiPreparation.makeEnd(true, mockResponse)
186
+ expect(callback).toHaveBeenCalled()
187
+ })
188
+
189
+ it('should return this for chaining', () => {
190
+ const callback = vi.fn().mockResolvedValue({})
191
+
192
+ const result = apiPreparation.setEnd(callback)
193
+
194
+ expect(result).toBe(apiPreparation)
195
+ })
196
+
197
+ it('should allow chaining set and setEnd', async () => {
198
+ const callback = vi.fn().mockResolvedValue(undefined)
199
+ const callbackEnd = vi.fn().mockResolvedValue({ reset: true })
200
+ const mockResponse = new Response('{}', { status: 200 })
201
+
202
+ apiPreparation
203
+ .set(callback)
204
+ .setEnd(callbackEnd)
205
+
206
+ await apiPreparation.make(true)
207
+ await apiPreparation.makeEnd(true, mockResponse)
208
+
209
+ expect(callback).toHaveBeenCalled()
210
+ expect(callbackEnd).toHaveBeenCalled()
211
+ })
212
+
213
+ it('should allow replacing end callback', async () => {
214
+ const callback1 = vi.fn().mockResolvedValue({})
215
+ const callback2 = vi.fn().mockResolvedValue({ data: 'test' })
216
+ const mockResponse = new Response('{}', { status: 200 })
217
+
218
+ apiPreparation.setEnd(callback1)
219
+ apiPreparation.setEnd(callback2)
220
+
221
+ await apiPreparation.makeEnd(true, mockResponse)
222
+
223
+ expect(callback1).not.toHaveBeenCalled()
224
+ expect(callback2).toHaveBeenCalled()
225
+ })
226
+ })
227
+
228
+ describe('integration', () => {
229
+ it('should handle full lifecycle with both callbacks', async () => {
230
+ let preparationDone = false
231
+ const callback = vi.fn().mockImplementation(async () => {
232
+ preparationDone = true
233
+ })
234
+ const callbackEnd = vi.fn().mockResolvedValue({ reset: true, data: { preparationDone } })
235
+ const mockResponse = new Response('{}', { status: 200 })
236
+
237
+ apiPreparation.set(callback).setEnd(callbackEnd)
238
+
239
+ await apiPreparation.make(true)
240
+ const result = await apiPreparation.makeEnd(true, mockResponse)
241
+
242
+ expect(preparationDone).toBe(true)
243
+ expect(callback).toHaveBeenCalled()
244
+ expect(callbackEnd).toHaveBeenCalled()
245
+ expect(result.reset).toBe(true)
246
+ })
247
+
248
+ it('should handle errors in end callback', async () => {
249
+ const error = new Error('End processing failed')
250
+ const callbackEnd = vi.fn().mockRejectedValue(error)
251
+ const mockResponse = new Response('{}', { status: 200 })
252
+ apiPreparation.setEnd(callbackEnd)
253
+
254
+ await expect(apiPreparation.makeEnd(true, mockResponse)).rejects.toThrow('End processing failed')
255
+ })
256
+ })
257
+ })