react-native-wagmi-charts 1.0.4 → 1.2.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 (132) hide show
  1. package/.DS_Store +0 -0
  2. package/.prettierrc.js +5 -0
  3. package/README.md +171 -121
  4. package/example/.DS_Store +0 -0
  5. package/example/README.md +32 -0
  6. package/example/package.json +1 -1
  7. package/example/src/App.tsx +32 -7
  8. package/example/src/CandlestickChart.tsx +23 -11
  9. package/example/src/LineChart.tsx +51 -20
  10. package/example/src/{candlestick-data.json → data/candlestick-data.json} +0 -0
  11. package/example/src/{candlestick-data2.json → data/candlestick-data2.json} +0 -0
  12. package/example/src/{line-data.json → data/line-data.json} +0 -0
  13. package/example/src/{line-data2.json → data/line-data2.json} +0 -0
  14. package/example/yarn.lock +5 -5
  15. package/lib/commonjs/charts/candle/Crosshair.js +6 -17
  16. package/lib/commonjs/charts/candle/Crosshair.js.map +1 -1
  17. package/lib/commonjs/charts/candle/CrosshairTooltip.js +12 -8
  18. package/lib/commonjs/charts/candle/CrosshairTooltip.js.map +1 -1
  19. package/lib/commonjs/charts/candle/DatetimeText.js +7 -8
  20. package/lib/commonjs/charts/candle/DatetimeText.js.map +1 -1
  21. package/lib/commonjs/charts/candle/Line.js.map +1 -1
  22. package/lib/commonjs/charts/candle/PriceText.js +7 -8
  23. package/lib/commonjs/charts/candle/PriceText.js.map +1 -1
  24. package/lib/commonjs/charts/candle/useCandleData.js.map +1 -1
  25. package/lib/commonjs/charts/candle/useCandlestickChart.js.map +1 -1
  26. package/lib/commonjs/charts/candle/useDatetime.js.map +1 -1
  27. package/lib/commonjs/charts/candle/usePrice.js.map +1 -1
  28. package/lib/commonjs/charts/candle/utils.js.map +1 -1
  29. package/lib/commonjs/charts/line/Chart.js.map +1 -1
  30. package/lib/commonjs/charts/line/Cursor.js.map +1 -1
  31. package/lib/commonjs/charts/line/CursorCrosshair.js +4 -3
  32. package/lib/commonjs/charts/line/CursorCrosshair.js.map +1 -1
  33. package/lib/commonjs/charts/line/CursorLine.js +8 -1
  34. package/lib/commonjs/charts/line/CursorLine.js.map +1 -1
  35. package/lib/commonjs/charts/line/DatetimeText.js +7 -8
  36. package/lib/commonjs/charts/line/DatetimeText.js.map +1 -1
  37. package/lib/commonjs/charts/line/Path.js +13 -16
  38. package/lib/commonjs/charts/line/Path.js.map +1 -1
  39. package/lib/commonjs/charts/line/PriceText.js +7 -8
  40. package/lib/commonjs/charts/line/PriceText.js.map +1 -1
  41. package/lib/commonjs/charts/line/interpolatePath.js +600 -0
  42. package/lib/commonjs/charts/line/interpolatePath.js.map +1 -0
  43. package/lib/commonjs/charts/line/useDatetime.js.map +1 -1
  44. package/lib/commonjs/charts/line/usePrice.js.map +1 -1
  45. package/lib/commonjs/charts/line/utils.js +1 -70
  46. package/lib/commonjs/charts/line/utils.js.map +1 -1
  47. package/lib/commonjs/components/AnimatedText.js +63 -0
  48. package/lib/commonjs/components/AnimatedText.js.map +1 -0
  49. package/lib/commonjs/utils.js.map +1 -1
  50. package/lib/module/charts/candle/Crosshair.js +7 -16
  51. package/lib/module/charts/candle/Crosshair.js.map +1 -1
  52. package/lib/module/charts/candle/CrosshairTooltip.js +10 -8
  53. package/lib/module/charts/candle/CrosshairTooltip.js.map +1 -1
  54. package/lib/module/charts/candle/DatetimeText.js +6 -7
  55. package/lib/module/charts/candle/DatetimeText.js.map +1 -1
  56. package/lib/module/charts/candle/Line.js.map +1 -1
  57. package/lib/module/charts/candle/PriceText.js +6 -7
  58. package/lib/module/charts/candle/PriceText.js.map +1 -1
  59. package/lib/module/charts/candle/useCandleData.js.map +1 -1
  60. package/lib/module/charts/candle/useCandlestickChart.js.map +1 -1
  61. package/lib/module/charts/candle/useDatetime.js.map +1 -1
  62. package/lib/module/charts/candle/usePrice.js.map +1 -1
  63. package/lib/module/charts/candle/utils.js.map +1 -1
  64. package/lib/module/charts/line/Chart.js.map +1 -1
  65. package/lib/module/charts/line/Cursor.js.map +1 -1
  66. package/lib/module/charts/line/CursorCrosshair.js +4 -3
  67. package/lib/module/charts/line/CursorCrosshair.js.map +1 -1
  68. package/lib/module/charts/line/CursorLine.js +7 -1
  69. package/lib/module/charts/line/CursorLine.js.map +1 -1
  70. package/lib/module/charts/line/DatetimeText.js +6 -7
  71. package/lib/module/charts/line/DatetimeText.js.map +1 -1
  72. package/lib/module/charts/line/Path.js +11 -15
  73. package/lib/module/charts/line/Path.js.map +1 -1
  74. package/lib/module/charts/line/PriceText.js +6 -7
  75. package/lib/module/charts/line/PriceText.js.map +1 -1
  76. package/lib/module/charts/line/interpolatePath.js +587 -0
  77. package/lib/module/charts/line/interpolatePath.js.map +1 -0
  78. package/lib/module/charts/line/useDatetime.js.map +1 -1
  79. package/lib/module/charts/line/usePrice.js.map +1 -1
  80. package/lib/module/charts/line/utils.js +1 -66
  81. package/lib/module/charts/line/utils.js.map +1 -1
  82. package/lib/module/components/AnimatedText.js +43 -0
  83. package/lib/module/components/AnimatedText.js.map +1 -0
  84. package/lib/module/utils.js.map +1 -1
  85. package/lib/typescript/src/charts/candle/Crosshair.d.ts +4 -4
  86. package/lib/typescript/src/charts/candle/DatetimeText.d.ts +3 -2
  87. package/lib/typescript/src/charts/candle/Line.d.ts +1 -1
  88. package/lib/typescript/src/charts/candle/PriceText.d.ts +3 -3
  89. package/lib/typescript/src/charts/candle/types.d.ts +8 -3
  90. package/lib/typescript/src/charts/candle/useCandleData.d.ts +3 -3
  91. package/lib/typescript/src/charts/candle/useCandlestickChart.d.ts +2 -1
  92. package/lib/typescript/src/charts/candle/useDatetime.d.ts +5 -7
  93. package/lib/typescript/src/charts/candle/usePrice.d.ts +5 -8
  94. package/lib/typescript/src/charts/candle/utils.d.ts +1 -1
  95. package/lib/typescript/src/charts/line/Chart.d.ts +1 -1
  96. package/lib/typescript/src/charts/line/Cursor.d.ts +1 -2
  97. package/lib/typescript/src/charts/line/CursorCrosshair.d.ts +3 -2
  98. package/lib/typescript/src/charts/line/DatetimeText.d.ts +5 -6
  99. package/lib/typescript/src/charts/line/Path.d.ts +17 -1
  100. package/lib/typescript/src/charts/line/PriceText.d.ts +3 -2
  101. package/lib/typescript/src/charts/line/interpolatePath.d.ts +50 -0
  102. package/lib/typescript/src/charts/line/useDatetime.d.ts +4 -5
  103. package/lib/typescript/src/charts/line/usePrice.d.ts +3 -2
  104. package/lib/typescript/src/charts/line/utils.d.ts +0 -13
  105. package/lib/typescript/src/components/AnimatedText.d.ts +9 -0
  106. package/lib/typescript/src/utils.d.ts +2 -4
  107. package/package.json +4 -4
  108. package/src/charts/candle/Crosshair.tsx +20 -20
  109. package/src/charts/candle/CrosshairTooltip.tsx +11 -4
  110. package/src/charts/candle/DatetimeText.tsx +5 -4
  111. package/src/charts/candle/Line.tsx +1 -1
  112. package/src/charts/candle/PriceText.tsx +5 -5
  113. package/src/charts/candle/types.ts +11 -3
  114. package/src/charts/candle/useCandleData.ts +3 -2
  115. package/src/charts/candle/useCandlestickChart.ts +2 -1
  116. package/src/charts/candle/useDatetime.ts +7 -3
  117. package/src/charts/candle/usePrice.ts +10 -3
  118. package/src/charts/candle/utils.ts +1 -1
  119. package/src/charts/line/Chart.tsx +1 -1
  120. package/src/charts/line/Cursor.tsx +7 -3
  121. package/src/charts/line/CursorCrosshair.tsx +4 -3
  122. package/src/charts/line/CursorLine.tsx +9 -1
  123. package/src/charts/line/DatetimeText.tsx +8 -7
  124. package/src/charts/line/Path.tsx +25 -21
  125. package/src/charts/line/PriceText.tsx +5 -4
  126. package/src/charts/line/interpolatePath.ts +650 -0
  127. package/src/charts/line/useDatetime.ts +3 -2
  128. package/src/charts/line/usePrice.ts +2 -1
  129. package/src/charts/line/utils.ts +2 -79
  130. package/src/components/AnimatedText.tsx +53 -0
  131. package/src/utils.ts +3 -3
  132. package/yarn.lock +5 -5
@@ -0,0 +1,650 @@
1
+ // @ts-nocheck
2
+
3
+ /**
4
+ * Reanimated compatible fork of https://github.com/pbeshai/d3-interpolate-path
5
+ */
6
+
7
+ //////////////////////////////////////////////////////////////////////////////////////////////////////
8
+
9
+ /**
10
+ * de Casteljau's algorithm for drawing and splitting bezier curves.
11
+ * Inspired by https://pomax.github.io/bezierinfo/
12
+ *
13
+ * @param {Number[][]} points Array of [x,y] points: [start, control1, control2, ..., end]
14
+ * The original segment to split.
15
+ * @param {Number} t Where to split the curve (value between [0, 1])
16
+ * @return {Object} An object { left, right } where left is the segment from 0..t and
17
+ * right is the segment from t..1.
18
+ */
19
+ function decasteljau(points, t) {
20
+ 'worklet';
21
+
22
+ const left = [];
23
+ const right = [];
24
+
25
+ function decasteljauRecurse(points, t) {
26
+ 'worklet';
27
+
28
+ if (points.length === 1) {
29
+ left.push(points[0]);
30
+ right.push(points[0]);
31
+ } else {
32
+ const newPoints = Array(points.length - 1);
33
+
34
+ for (let i = 0; i < newPoints.length; i++) {
35
+ if (i === 0) {
36
+ left.push(points[0]);
37
+ }
38
+ if (i === newPoints.length - 1) {
39
+ right.push(points[i + 1]);
40
+ }
41
+
42
+ newPoints[i] = [
43
+ (1 - t) * points[i][0] + t * points[i + 1][0],
44
+ (1 - t) * points[i][1] + t * points[i + 1][1],
45
+ ];
46
+ }
47
+
48
+ decasteljauRecurse(newPoints, t);
49
+ }
50
+ }
51
+
52
+ if (points.length) {
53
+ decasteljauRecurse(points, t);
54
+ }
55
+
56
+ return { left, right: right.reverse() };
57
+ }
58
+
59
+ /**
60
+ * Convert segments represented as points back into a command object
61
+ *
62
+ * @param {Number[][]} points Array of [x,y] points: [start, control1, control2, ..., end]
63
+ * Represents a segment
64
+ * @return {Object} A command object representing the segment.
65
+ */
66
+ function pointsToCommand(points) {
67
+ 'worklet';
68
+
69
+ const command = {};
70
+
71
+ if (points.length === 4) {
72
+ command.x2 = points[2][0];
73
+ command.y2 = points[2][1];
74
+ }
75
+ if (points.length >= 3) {
76
+ command.x1 = points[1][0];
77
+ command.y1 = points[1][1];
78
+ }
79
+
80
+ command.x = points[points.length - 1][0];
81
+ command.y = points[points.length - 1][1];
82
+
83
+ if (points.length === 4) {
84
+ // start, control1, control2, end
85
+ command.type = 'C';
86
+ } else if (points.length === 3) {
87
+ // start, control, end
88
+ command.type = 'Q';
89
+ } else {
90
+ // start, end
91
+ command.type = 'L';
92
+ }
93
+
94
+ return command;
95
+ }
96
+
97
+ /**
98
+ * Runs de Casteljau's algorithm enough times to produce the desired number of segments.
99
+ *
100
+ * @param {Number[][]} points Array of [x,y] points for de Casteljau (the initial segment to split)
101
+ * @param {Number} segmentCount Number of segments to split the original into
102
+ * @return {Number[][][]} Array of segments
103
+ */
104
+ function splitCurveAsPoints(points, segmentCount) {
105
+ 'worklet';
106
+
107
+ segmentCount = segmentCount || 2;
108
+
109
+ const segments = [];
110
+ let remainingCurve = points;
111
+ const tIncrement = 1 / segmentCount;
112
+
113
+ // x-----x-----x-----x
114
+ // t= 0.33 0.66 1
115
+ // x-----o-----------x
116
+ // r= 0.33
117
+ // x-----o-----x
118
+ // r= 0.5 (0.33 / (1 - 0.33)) === tIncrement / (1 - (tIncrement * (i - 1))
119
+
120
+ // x-----x-----x-----x----x
121
+ // t= 0.25 0.5 0.75 1
122
+ // x-----o----------------x
123
+ // r= 0.25
124
+ // x-----o----------x
125
+ // r= 0.33 (0.25 / (1 - 0.25))
126
+ // x-----o----x
127
+ // r= 0.5 (0.25 / (1 - 0.5))
128
+
129
+ for (let i = 0; i < segmentCount - 1; i++) {
130
+ const tRelative = tIncrement / (1 - tIncrement * i);
131
+ const split = decasteljau(remainingCurve, tRelative);
132
+ segments.push(split.left);
133
+ remainingCurve = split.right;
134
+ }
135
+
136
+ // last segment is just to the end from the last point
137
+ segments.push(remainingCurve);
138
+
139
+ return segments;
140
+ }
141
+
142
+ /**
143
+ * Convert command objects to arrays of points, run de Casteljau's algorithm on it
144
+ * to split into to the desired number of segments.
145
+ *
146
+ * @param {Object} commandStart The start command object
147
+ * @param {Object} commandEnd The end command object
148
+ * @param {Number} segmentCount The number of segments to create
149
+ * @return {Object[]} An array of commands representing the segments in sequence
150
+ */
151
+ export function splitCurve(commandStart, commandEnd, segmentCount) {
152
+ 'worklet';
153
+
154
+ const points = [[commandStart.x, commandStart.y]];
155
+ if (commandEnd.x1 != null) {
156
+ points.push([commandEnd.x1, commandEnd.y1]);
157
+ }
158
+ if (commandEnd.x2 != null) {
159
+ points.push([commandEnd.x2, commandEnd.y2]);
160
+ }
161
+ points.push([commandEnd.x, commandEnd.y]);
162
+
163
+ return splitCurveAsPoints(points, segmentCount).map(pointsToCommand);
164
+ }
165
+
166
+ /**
167
+ * List of params for each command type in a path `d` attribute
168
+ */
169
+ const typeMap = {
170
+ M: ['x', 'y'],
171
+ L: ['x', 'y'],
172
+ H: ['x'],
173
+ V: ['y'],
174
+ C: ['x1', 'y1', 'x2', 'y2', 'x', 'y'],
175
+ S: ['x2', 'y2', 'x', 'y'],
176
+ Q: ['x1', 'y1', 'x', 'y'],
177
+ T: ['x', 'y'],
178
+ A: ['rx', 'ry', 'xAxisRotation', 'largeArcFlag', 'sweepFlag', 'x', 'y'],
179
+ Z: [],
180
+ };
181
+
182
+ // Add lower case entries too matching uppercase (e.g. 'm' == 'M')
183
+ Object.keys(typeMap).forEach((key) => {
184
+ typeMap[key.toLowerCase()] = typeMap[key];
185
+ });
186
+
187
+ function arrayOfLength(length, value) {
188
+ 'worklet';
189
+
190
+ const array = Array(length);
191
+ for (let i = 0; i < length; i++) {
192
+ array[i] = value;
193
+ }
194
+
195
+ return array;
196
+ }
197
+
198
+ /**
199
+ * Converts a command object to a string to be used in a `d` attribute
200
+ * @param {Object} command A command object
201
+ * @return {String} The string for the `d` attribute
202
+ */
203
+ function commandToString(command) {
204
+ 'worklet';
205
+
206
+ return `${command.type}${typeMap[command.type]
207
+ .map((p) => command[p])
208
+ .join(',')}`;
209
+ }
210
+
211
+ /**
212
+ * Converts command A to have the same type as command B.
213
+ *
214
+ * e.g., L0,5 -> C0,5,0,5,0,5
215
+ *
216
+ * Uses these rules:
217
+ * x1 <- x
218
+ * x2 <- x
219
+ * y1 <- y
220
+ * y2 <- y
221
+ * rx <- 0
222
+ * ry <- 0
223
+ * xAxisRotation <- read from B
224
+ * largeArcFlag <- read from B
225
+ * sweepflag <- read from B
226
+ *
227
+ * @param {Object} aCommand Command object from path `d` attribute
228
+ * @param {Object} bCommand Command object from path `d` attribute to match against
229
+ * @return {Object} aCommand converted to type of bCommand
230
+ */
231
+ function convertToSameType(aCommand, bCommand) {
232
+ 'worklet';
233
+
234
+ const conversionMap = {
235
+ x1: 'x',
236
+ y1: 'y',
237
+ x2: 'x',
238
+ y2: 'y',
239
+ };
240
+
241
+ const readFromBKeys = ['xAxisRotation', 'largeArcFlag', 'sweepFlag'];
242
+
243
+ // convert (but ignore M types)
244
+ if (aCommand.type !== bCommand.type && bCommand.type.toUpperCase() !== 'M') {
245
+ const aConverted = {};
246
+ Object.keys(bCommand).forEach((bKey) => {
247
+ const bValue = bCommand[bKey];
248
+ // first read from the A command
249
+ let aValue = aCommand[bKey];
250
+
251
+ // if it is one of these values, read from B no matter what
252
+ if (aValue === undefined) {
253
+ if (readFromBKeys.includes(bKey)) {
254
+ aValue = bValue;
255
+ } else {
256
+ // if it wasn't in the A command, see if an equivalent was
257
+ if (aValue === undefined && conversionMap[bKey]) {
258
+ aValue = aCommand[conversionMap[bKey]];
259
+ }
260
+
261
+ // if it doesn't have a converted value, use 0
262
+ if (aValue === undefined) {
263
+ aValue = 0;
264
+ }
265
+ }
266
+ }
267
+
268
+ aConverted[bKey] = aValue;
269
+ });
270
+
271
+ // update the type to match B
272
+ aConverted.type = bCommand.type;
273
+ aCommand = aConverted;
274
+ }
275
+
276
+ return aCommand;
277
+ }
278
+
279
+ /**
280
+ * Interpolate between command objects commandStart and commandEnd segmentCount times.
281
+ * If the types are L, Q, or C then the curves are split as per de Casteljau's algorithm.
282
+ * Otherwise we just copy commandStart segmentCount - 1 times, finally ending with commandEnd.
283
+ *
284
+ * @param {Object} commandStart Command object at the beginning of the segment
285
+ * @param {Object} commandEnd Command object at the end of the segment
286
+ * @param {Number} segmentCount The number of segments to split this into. If only 1
287
+ * Then [commandEnd] is returned.
288
+ * @return {Object[]} Array of ~segmentCount command objects between commandStart and
289
+ * commandEnd. (Can be segmentCount+1 objects if commandStart is type M).
290
+ */
291
+ function splitSegment(commandStart, commandEnd, segmentCount) {
292
+ 'worklet';
293
+
294
+ let segments = [];
295
+
296
+ // line, quadratic bezier, or cubic bezier
297
+ if (
298
+ commandEnd.type === 'L' ||
299
+ commandEnd.type === 'Q' ||
300
+ commandEnd.type === 'C'
301
+ ) {
302
+ segments = segments.concat(
303
+ splitCurve(commandStart, commandEnd, segmentCount)
304
+ );
305
+
306
+ // general case - just copy the same point
307
+ } else {
308
+ const copyCommand = Object.assign({}, commandStart);
309
+
310
+ // convert M to L
311
+ if (copyCommand.type === 'M') {
312
+ copyCommand.type = 'L';
313
+ }
314
+
315
+ segments = segments.concat(
316
+ arrayOfLength(segmentCount - 1).map(() => copyCommand)
317
+ );
318
+ segments.push(commandEnd);
319
+ }
320
+
321
+ return segments;
322
+ }
323
+ /**
324
+ * Extends an array of commandsToExtend to the length of the referenceCommands by
325
+ * splitting segments until the number of commands match. Ensures all the actual
326
+ * points of commandsToExtend are in the extended array.
327
+ *
328
+ * @param {Object[]} commandsToExtend The command object array to extend
329
+ * @param {Object[]} referenceCommands The command object array to match in length
330
+ * @param {Function} excludeSegment a function that takes a start command object and
331
+ * end command object and returns true if the segment should be excluded from splitting.
332
+ * @return {Object[]} The extended commandsToExtend array
333
+ */
334
+ function extend(commandsToExtend, referenceCommands, excludeSegment) {
335
+ 'worklet';
336
+
337
+ // compute insertion points:
338
+ // number of segments in the path to extend
339
+ const numSegmentsToExtend = commandsToExtend.length - 1;
340
+
341
+ // number of segments in the reference path.
342
+ const numReferenceSegments = referenceCommands.length - 1;
343
+
344
+ // this value is always between [0, 1].
345
+ const segmentRatio = numSegmentsToExtend / numReferenceSegments;
346
+
347
+ // create a map, mapping segments in referenceCommands to how many points
348
+ // should be added in that segment (should always be >= 1 since we need each
349
+ // point itself).
350
+ // 0 = segment 0-1, 1 = segment 1-2, n-1 = last vertex
351
+ const countPointsPerSegment = arrayOfLength(numReferenceSegments).reduce(
352
+ (accum, d, i) => {
353
+ let insertIndex = Math.floor(segmentRatio * i);
354
+
355
+ // handle excluding segments
356
+ if (
357
+ excludeSegment &&
358
+ insertIndex < commandsToExtend.length - 1 &&
359
+ excludeSegment(
360
+ commandsToExtend[insertIndex],
361
+ commandsToExtend[insertIndex + 1]
362
+ )
363
+ ) {
364
+ // set the insertIndex to the segment that this point should be added to:
365
+
366
+ // round the insertIndex essentially so we split half and half on
367
+ // neighbouring segments. hence the segmentRatio * i < 0.5
368
+ const addToPriorSegment = (segmentRatio * i) % 1 < 0.5;
369
+
370
+ // only skip segment if we already have 1 point in it (can't entirely remove a segment)
371
+ if (accum[insertIndex]) {
372
+ // TODO - Note this is a naive algorithm that should work for most d3-area use cases
373
+ // but if two adjacent segments are supposed to be skipped, this will not perform as
374
+ // expected. Could be updated to search for nearest segment to place the point in, but
375
+ // will only do that if necessary.
376
+
377
+ // add to the prior segment
378
+ if (addToPriorSegment) {
379
+ if (insertIndex > 0) {
380
+ insertIndex -= 1;
381
+
382
+ // not possible to add to previous so adding to next
383
+ } else if (insertIndex < commandsToExtend.length - 1) {
384
+ insertIndex += 1;
385
+ }
386
+ // add to next segment
387
+ } else if (insertIndex < commandsToExtend.length - 1) {
388
+ insertIndex += 1;
389
+
390
+ // not possible to add to next so adding to previous
391
+ } else if (insertIndex > 0) {
392
+ insertIndex -= 1;
393
+ }
394
+ }
395
+ }
396
+
397
+ accum[insertIndex] = (accum[insertIndex] || 0) + 1;
398
+
399
+ return accum;
400
+ },
401
+ []
402
+ );
403
+
404
+ // extend each segment to have the correct number of points for a smooth interpolation
405
+ const extended = countPointsPerSegment.reduce((extended, segmentCount, i) => {
406
+ // if last command, just add `segmentCount` number of times
407
+ if (i === commandsToExtend.length - 1) {
408
+ const lastCommandCopies = arrayOfLength(
409
+ segmentCount,
410
+ Object.assign({}, commandsToExtend[commandsToExtend.length - 1])
411
+ );
412
+
413
+ // convert M to L
414
+ if (lastCommandCopies[0].type === 'M') {
415
+ lastCommandCopies.forEach((d) => {
416
+ d.type = 'L';
417
+ });
418
+ }
419
+ return extended.concat(lastCommandCopies);
420
+ }
421
+
422
+ // otherwise, split the segment segmentCount times.
423
+ return extended.concat(
424
+ splitSegment(commandsToExtend[i], commandsToExtend[i + 1], segmentCount)
425
+ );
426
+ }, []);
427
+
428
+ // add in the very first point since splitSegment only adds in the ones after it
429
+ extended.unshift(commandsToExtend[0]);
430
+
431
+ return extended;
432
+ }
433
+
434
+ /**
435
+ * Takes a path `d` string and converts it into an array of command
436
+ * objects. Drops the `Z` character.
437
+ *
438
+ * @param {String|null} d A path `d` string
439
+ */
440
+ export function pathCommandsFromString(d) {
441
+ 'worklet';
442
+
443
+ // split into valid tokens
444
+ const tokens = (d || '').match(/[MLCSTQAHVZmlcstqahv]|-?[\d.e+-]+/g) || [];
445
+ const commands = [];
446
+ let commandArgs;
447
+ let command;
448
+
449
+ // iterate over each token, checking if we are at a new command
450
+ // by presence in the typeMap
451
+ for (let i = 0; i < tokens.length; ++i) {
452
+ commandArgs = typeMap[tokens[i]];
453
+
454
+ // new command found:
455
+ if (commandArgs) {
456
+ command = {
457
+ type: tokens[i],
458
+ };
459
+
460
+ // add each of the expected args for this command:
461
+ for (let a = 0; a < commandArgs.length; ++a) {
462
+ command[commandArgs[a]] = +tokens[i + a + 1];
463
+ }
464
+
465
+ // need to increment our token index appropriately since
466
+ // we consumed token args
467
+ i += commandArgs.length;
468
+
469
+ commands.push(command);
470
+ }
471
+ }
472
+ return commands;
473
+ }
474
+
475
+ /**
476
+ * Interpolate from A to B by extending A and B during interpolation to have
477
+ * the same number of points. This allows for a smooth transition when they
478
+ * have a different number of points.
479
+ *
480
+ * Ignores the `Z` command in paths unless both A and B end with it.
481
+ *
482
+ * This function works directly with arrays of command objects instead of with
483
+ * path `d` strings (see interpolatePath for working with `d` strings).
484
+ *
485
+ * @param {Object[]} aCommandsInput Array of path commands
486
+ * @param {Object[]} bCommandsInput Array of path commands
487
+ * @param {Function} excludeSegment a function that takes a start command object and
488
+ * end command object and returns true if the segment should be excluded from splitting.
489
+ * @returns {Function} Interpolation function that maps t ([0, 1]) to an array of path commands.
490
+ */
491
+ export function interpolatePathCommands(
492
+ aCommandsInput,
493
+ bCommandsInput,
494
+ excludeSegment
495
+ ) {
496
+ 'worklet';
497
+
498
+ // make a copy so we don't mess with the input arrays
499
+ let aCommands = aCommandsInput == null ? [] : aCommandsInput.slice();
500
+ let bCommands = bCommandsInput == null ? [] : bCommandsInput.slice();
501
+
502
+ // both input sets are empty, so we don't interpolate
503
+ if (!aCommands.length && !bCommands.length) {
504
+ return function nullInterpolator() {
505
+ 'worklet';
506
+
507
+ return [];
508
+ };
509
+ }
510
+
511
+ // do we add Z during interpolation? yes if both have it. (we'd expect both to have it or not)
512
+ const addZ =
513
+ (aCommands.length === 0 || aCommands[aCommands.length - 1].type === 'Z') &&
514
+ (bCommands.length === 0 || bCommands[bCommands.length - 1].type === 'Z');
515
+
516
+ // we temporarily remove Z
517
+ if (aCommands.length > 0 && aCommands[aCommands.length - 1].type === 'Z') {
518
+ aCommands.pop();
519
+ }
520
+ if (bCommands.length > 0 && bCommands[bCommands.length - 1].type === 'Z') {
521
+ bCommands.pop();
522
+ }
523
+
524
+ // if A is empty, treat it as if it used to contain just the first point
525
+ // of B. This makes it so the line extends out of from that first point.
526
+ if (!aCommands.length) {
527
+ aCommands.push(bCommands[0]);
528
+
529
+ // otherwise if B is empty, treat it as if it contains the first point
530
+ // of A. This makes it so the line retracts into the first point.
531
+ } else if (!bCommands.length) {
532
+ bCommands.push(aCommands[0]);
533
+ }
534
+
535
+ // extend to match equal size
536
+ const numPointsToExtend = Math.abs(bCommands.length - aCommands.length);
537
+
538
+ if (numPointsToExtend !== 0) {
539
+ // B has more points than A, so add points to A before interpolating
540
+ if (bCommands.length > aCommands.length) {
541
+ aCommands = extend(aCommands, bCommands, excludeSegment);
542
+
543
+ // else if A has more points than B, add more points to B
544
+ } else if (bCommands.length < aCommands.length) {
545
+ bCommands = extend(bCommands, aCommands, excludeSegment);
546
+ }
547
+ }
548
+
549
+ // commands have same length now.
550
+ // convert commands in A to the same type as those in B
551
+ aCommands = aCommands.map((aCommand, i) =>
552
+ convertToSameType(aCommand, bCommands[i])
553
+ );
554
+
555
+ // create mutable interpolated command objects
556
+ const interpolatedCommands = aCommands.map((aCommand) => ({ ...aCommand }));
557
+
558
+ if (addZ) {
559
+ interpolatedCommands.push({ type: 'Z' });
560
+ aCommands.push({ type: 'Z' }); // required for when returning at t == 0
561
+ }
562
+
563
+ return function pathCommandInterpolator(t) {
564
+ 'worklet';
565
+
566
+ // at 1 return the final value without the extensions used during interpolation
567
+ if (t === 1) {
568
+ return bCommandsInput == null ? [] : bCommandsInput;
569
+ }
570
+
571
+ // work with aCommands directly since interpolatedCommands are mutated
572
+ if (t === 0) {
573
+ return aCommands;
574
+ }
575
+
576
+ // interpolate the commands using the mutable interpolated command objs
577
+ for (let i = 0; i < interpolatedCommands.length; ++i) {
578
+ // if (interpolatedCommands[i].type === 'Z') continue;
579
+
580
+ const aCommand = aCommands[i];
581
+ const bCommand = bCommands[i];
582
+ const interpolatedCommand = interpolatedCommands[i];
583
+ for (let j = 0; j < typeMap[interpolatedCommand.type].length; j++) {
584
+ const arg = typeMap[interpolatedCommand.type][j];
585
+ interpolatedCommand[arg] = (1 - t) * aCommand[arg] + t * bCommand[arg];
586
+
587
+ // do not use floats for flags (#27), round to integer
588
+ if (arg === 'largeArcFlag' || arg === 'sweepFlag') {
589
+ interpolatedCommand[arg] = Math.round(interpolatedCommand[arg]);
590
+ }
591
+ }
592
+ }
593
+
594
+ return interpolatedCommands;
595
+ };
596
+ }
597
+
598
+ /**
599
+ * Interpolate from A to B by extending A and B during interpolation to have
600
+ * the same number of points. This allows for a smooth transition when they
601
+ * have a different number of points.
602
+ *
603
+ * Ignores the `Z` character in paths unless both A and B end with it.
604
+ *
605
+ * @param {String} a The `d` attribute for a path
606
+ * @param {String} b The `d` attribute for a path
607
+ * @param {Function} excludeSegment a function that takes a start command object and
608
+ * end command object and returns true if the segment should be excluded from splitting.
609
+ * @returns {Function} Interpolation function that maps t ([0, 1]) to a path `d` string.
610
+ */
611
+ export default function interpolatePath(a, b, excludeSegment) {
612
+ 'worklet';
613
+
614
+ let aCommands = pathCommandsFromString(a);
615
+ let bCommands = pathCommandsFromString(b);
616
+
617
+ if (!aCommands.length && !bCommands.length) {
618
+ return function nullInterpolator() {
619
+ 'worklet';
620
+
621
+ return '';
622
+ };
623
+ }
624
+
625
+ const commandInterpolator = interpolatePathCommands(
626
+ aCommands,
627
+ bCommands,
628
+ excludeSegment
629
+ );
630
+
631
+ return function pathStringInterpolator(t) {
632
+ 'worklet';
633
+
634
+ // at 1 return the final value without the extensions used during interpolation
635
+ if (t === 1) {
636
+ return b == null ? '' : b;
637
+ }
638
+
639
+ const interpolatedCommands = commandInterpolator(t);
640
+
641
+ // convert to a string (fastest concat: https://jsperf.com/join-concat/150)
642
+ let interpolatedString = '';
643
+ for (let i = 0; i < interpolatedCommands.length; i++) {
644
+ const interpolatedCommand = interpolatedCommands[i];
645
+ interpolatedString += commandToString(interpolatedCommand);
646
+ }
647
+
648
+ return interpolatedString;
649
+ };
650
+ }
@@ -1,6 +1,7 @@
1
1
  import { useDerivedValue } from 'react-native-reanimated';
2
2
 
3
3
  import { formatDatetime } from '../../utils';
4
+ import type { TFormatterFn } from '../candle/types';
4
5
  import { useLineChart } from './useLineChart';
5
6
 
6
7
  export function useLineChartDatetime({
@@ -8,9 +9,9 @@ export function useLineChartDatetime({
8
9
  locale,
9
10
  options,
10
11
  }: {
11
- format?: any;
12
+ format?: TFormatterFn<number>;
12
13
  locale?: string;
13
- options?: { [key: string]: string };
14
+ options?: Intl.DateTimeFormatOptions;
14
15
  } = {}) {
15
16
  const { currentIndex, data } = useLineChart();
16
17
 
@@ -1,12 +1,13 @@
1
1
  import { useDerivedValue } from 'react-native-reanimated';
2
2
 
3
3
  import { formatPrice } from '../../utils';
4
+ import type { TFormatterFn } from '../candle/types';
4
5
  import { useLineChart } from './useLineChart';
5
6
 
6
7
  export function useLineChartPrice({
7
8
  format,
8
9
  precision = 2,
9
- }: { format?: any; precision?: number } = {}) {
10
+ }: { format?: TFormatterFn<string>; precision?: number } = {}) {
10
11
  const { currentIndex, data } = useLineChart();
11
12
 
12
13
  const float = useDerivedValue(() => {