js-spread-grid 0.0.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 (67) hide show
  1. package/package.json +26 -0
  2. package/src/core/render.js +400 -0
  3. package/src/core/state.js +183 -0
  4. package/src/core-utils/defaults.js +4 -0
  5. package/src/core-utils/rect.js +49 -0
  6. package/src/core-utils/rect.test.js +111 -0
  7. package/src/core-utils/roundToPixels.js +3 -0
  8. package/src/core-utils/stringifyId.js +27 -0
  9. package/src/core-utils/stringifyId.test.js +27 -0
  10. package/src/index.js +595 -0
  11. package/src/state-utils/getActive.js +17 -0
  12. package/src/state-utils/getCellSection.js +22 -0
  13. package/src/state-utils/getCellType.js +7 -0
  14. package/src/state-utils/getClipboardData.js +53 -0
  15. package/src/state-utils/getColumnIndex.js +25 -0
  16. package/src/state-utils/getCombinedCells.js +6 -0
  17. package/src/state-utils/getDataFormatting.js +46 -0
  18. package/src/state-utils/getEditableCells.js +23 -0
  19. package/src/state-utils/getEditedCellsAndFilters.js +4 -0
  20. package/src/state-utils/getEdition.js +6 -0
  21. package/src/state-utils/getFilterFormatting.js +3 -0
  22. package/src/state-utils/getFiltered.js +61 -0
  23. package/src/state-utils/getFilteringRules.js +5 -0
  24. package/src/state-utils/getFixedSize.js +8 -0
  25. package/src/state-utils/getFormatResolver.js +5 -0
  26. package/src/state-utils/getFormattingRules.js +5 -0
  27. package/src/state-utils/getHighlightedCells.js +40 -0
  28. package/src/state-utils/getHoveredCell.js +43 -0
  29. package/src/state-utils/getInputFormatting.js +3 -0
  30. package/src/state-utils/getInputPlacement.js +51 -0
  31. package/src/state-utils/getInvoked.js +5 -0
  32. package/src/state-utils/getIsTextValid.js +3 -0
  33. package/src/state-utils/getKeys.js +3 -0
  34. package/src/state-utils/getLookup.js +3 -0
  35. package/src/state-utils/getMeasureFormatting.js +3 -0
  36. package/src/state-utils/getMeasured.js +113 -0
  37. package/src/state-utils/getMousePosition.js +8 -0
  38. package/src/state-utils/getNewSortBy.js +20 -0
  39. package/src/state-utils/getPinned.js +8 -0
  40. package/src/state-utils/getPlaced.js +45 -0
  41. package/src/state-utils/getReducedCells.js +6 -0
  42. package/src/state-utils/getRenderFormatting.js +122 -0
  43. package/src/state-utils/getResizable.js +49 -0
  44. package/src/state-utils/getResolved.js +42 -0
  45. package/src/state-utils/getResolvedFilters.js +7 -0
  46. package/src/state-utils/getResolvedSortBy.js +7 -0
  47. package/src/state-utils/getRowIndex.js +25 -0
  48. package/src/state-utils/getScrollRect.js +41 -0
  49. package/src/state-utils/getSections.js +101 -0
  50. package/src/state-utils/getSelection.js +5 -0
  51. package/src/state-utils/getSorted.js +130 -0
  52. package/src/state-utils/getSortingFormatting.js +3 -0
  53. package/src/state-utils/getSortingRules.js +5 -0
  54. package/src/state-utils/getTextResolver.js +5 -0
  55. package/src/state-utils/getToggledValue.js +11 -0
  56. package/src/state-utils/getTotalSize.js +6 -0
  57. package/src/state-utils/getUnfolded.js +86 -0
  58. package/src/types/Edition.js +37 -0
  59. package/src/types/FilteringRules.js +48 -0
  60. package/src/types/FormatResolver.js +19 -0
  61. package/src/types/FormattingRules.js +120 -0
  62. package/src/types/FormattingRules.test.js +90 -0
  63. package/src/types/RulesLookup.js +118 -0
  64. package/src/types/Selection.js +25 -0
  65. package/src/types/SortingRules.js +62 -0
  66. package/src/types/TextResolver.js +60 -0
  67. package/src/types/VisibilityResolver.js +61 -0
@@ -0,0 +1,111 @@
1
+ import { contains, clip, expand, area, subtract } from './rect.js';
2
+
3
+ describe('contains', () => {
4
+ it('should return true if rect is fully contained within bounds', () => {
5
+ const bounds = { top: 0, left: 0, width: 10, height: 10 };
6
+ const rect = { top: 2, left: 2, width: 4, height: 4 };
7
+ expect(contains(bounds, rect)).toBe(true);
8
+ });
9
+
10
+ it('should return false if rect is partially outside of bounds', () => {
11
+ const bounds = { top: 0, left: 0, width: 10, height: 10 };
12
+ const rect = { top: 8, left: 8, width: 4, height: 4 };
13
+ expect(contains(bounds, rect)).toBe(false);
14
+ });
15
+
16
+ it('should return false if rect is completely outside of bounds', () => {
17
+ const bounds = { top: 0, left: 0, width: 10, height: 10 };
18
+ const rect = { top: 12, left: 12, width: 4, height: 4 };
19
+ expect(contains(bounds, rect)).toBe(false);
20
+ });
21
+
22
+ it('should return true if rect is equal to bounds', () => {
23
+ const bounds = { top: 0, left: 0, width: 10, height: 10 };
24
+ const rect = { top: 0, left: 0, width: 10, height: 10 };
25
+ expect(contains(bounds, rect)).toBe(true);
26
+ });
27
+ });
28
+
29
+ describe('clip', () => {
30
+ it('should return the clipped rectangle when rect is partially outside of bounds', () => {
31
+ const bounds = { top: 0, left: 0, width: 10, height: 10 };
32
+ const rect = { top: 8, left: 8, width: 4, height: 4 };
33
+ const expected = { top: 8, left: 8, width: 2, height: 2 };
34
+ expect(clip(bounds, rect)).toEqual(expected);
35
+ });
36
+
37
+ it('should return the clipped rectangle when rect is completely outside of bounds', () => {
38
+ const bounds = { top: 0, left: 0, width: 10, height: 10 };
39
+ const rect = { top: 12, left: 12, width: 4, height: 4 };
40
+ const expected = { top: 0, left: 0, width: 0, height: 0 };
41
+ expect(clip(bounds, rect)).toEqual(expected);
42
+ });
43
+
44
+ it('should return the clipped rectangle when rect is partially inside bounds', () => {
45
+ const bounds = { top: 0, left: 0, width: 10, height: 10 };
46
+ const rect = { top: 2, left: 2, width: 8, height: 8 };
47
+ const expected = { top: 2, left: 2, width: 8, height: 8 };
48
+ expect(clip(bounds, rect)).toEqual(expected);
49
+ });
50
+
51
+ it('should return the clipped rectangle when rect is equal to bounds', () => {
52
+ const bounds = { top: 0, left: 0, width: 10, height: 10 };
53
+ const rect = { top: 0, left: 0, width: 10, height: 10 };
54
+ const expected = { top: 0, left: 0, width: 10, height: 10 };
55
+ expect(clip(bounds, rect)).toEqual(expected);
56
+ });
57
+ });
58
+
59
+ describe('expand', () => {
60
+ it('should expand the rectangle by the specified margin', () => {
61
+ const rect = { top: 10, left: 20, width: 30, height: 40 };
62
+ const margin = 5;
63
+ const expandedRect = expand(rect, margin);
64
+
65
+ expect(expandedRect.top).toBe(5);
66
+ expect(expandedRect.left).toBe(15);
67
+ expect(expandedRect.width).toBe(40);
68
+ expect(expandedRect.height).toBe(50);
69
+ });
70
+
71
+ it('should expand the rectangle even if values become negative', () => {
72
+ const rect = { top: 10, left: 20, width: 30, height: 40 };
73
+ const margin = 15;
74
+ const expandedRect = expand(rect, margin);
75
+
76
+ expect(expandedRect.top).toBe(-5);
77
+ expect(expandedRect.left).toBe(5);
78
+ expect(expandedRect.width).toBe(60);
79
+ expect(expandedRect.height).toBe(70);
80
+ });
81
+ });
82
+
83
+ describe('area', () => {
84
+ it('should return the area of the rectangle', () => {
85
+ const rect = { top: 10, left: 20, width: 30, height: 40 };
86
+ expect(area(rect)).toBe(1200);
87
+ });
88
+ });
89
+
90
+ describe('subtract', () => {
91
+ it('should subtract the margin from the rectangle', () => {
92
+ const rect = { top: 10, left: 20, width: 30, height: 40 };
93
+ const margin = { top: 5, right: 10, bottom: 5, left: 10 };
94
+ const expected = { top: 10, left: 20, width: 10, height: 30 };
95
+ expect(subtract(rect, margin)).toEqual(expected);
96
+ });
97
+
98
+ it('should handle negative values when subtracting the margin', () => {
99
+ const rect = { top: 10, left: 20, width: 30, height: 40 };
100
+ const margin = { top: 15, right: 25, bottom: 15, left: 25 };
101
+ const expected = { top: 10, left: 20, width: 0, height: 10 };
102
+ expect(subtract(rect, margin)).toEqual(expected);
103
+ });
104
+
105
+ it('should not modify the original rectangle', () => {
106
+ const rect = { top: 10, left: 20, width: 30, height: 40 };
107
+ const margin = { top: 5, right: 10, bottom: 5, left: 10 };
108
+ subtract(rect, margin);
109
+ expect(rect).toEqual({ top: 10, left: 20, width: 30, height: 40 });
110
+ });
111
+ });
@@ -0,0 +1,3 @@
1
+ export default function roundToPixels(value, devicePixelRatio) {
2
+ return Math.round(value * devicePixelRatio) / devicePixelRatio;
3
+ }
@@ -0,0 +1,27 @@
1
+ function stringifyObject(object) {
2
+ const keys = Object.keys(object).sort();
3
+ const stringified = keys.map(key => {
4
+ return `${key}:${stringifyId(object[key])}`
5
+ });
6
+
7
+ return `{${stringified.join(',')}}`;
8
+ }
9
+
10
+ function stringifyArray(array) {
11
+ const stringified = array.map(stringifyId);
12
+
13
+ return `[${stringified.join(',')}]`;
14
+ }
15
+
16
+ export default function stringifyId(key) {
17
+ if (key === null)
18
+ return 'null'
19
+
20
+ if (Array.isArray(key))
21
+ return stringifyArray(key)
22
+
23
+ if (typeof key === 'object')
24
+ return stringifyObject(key)
25
+
26
+ return JSON.stringify(key)
27
+ }
@@ -0,0 +1,27 @@
1
+ import stringifyId from './stringifyId.js';
2
+
3
+ describe('stringifyId', () => {
4
+ it('should stringify a string', () => {
5
+ expect(stringifyId('hello')).toBe('"hello"');
6
+ });
7
+
8
+ it('should stringify a number', () => {
9
+ expect(stringifyId(123)).toBe('123');
10
+ });
11
+
12
+ it('should stringify a boolean', () => {
13
+ expect(stringifyId(true)).toBe('true');
14
+ });
15
+
16
+ it('should stringify an array', () => {
17
+ expect(stringifyId([1, 'two', { three: 3 }])).toBe('[1,"two",{three:3}]');
18
+ });
19
+
20
+ it('should stringify an object with sorted properties', () => {
21
+ expect(stringifyId({ a: 1, b: 'two', c: { three: 3 } })).toBe('{a:1,b:"two",c:{three:3}}');
22
+ });
23
+
24
+ it('should stringify an object with unsorted properties', () => {
25
+ expect(stringifyId({ b: 'two', a: 1, c: { three: 3 } })).toBe('{a:1,b:"two",c:{three:3}}');
26
+ });
27
+ });