forked from nomcopter/react-mosaic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mosaicUtilities.ts
172 lines (159 loc) · 4.25 KB
/
mosaicUtilities.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import clone from 'lodash/clone';
import get from 'lodash/get';
import { MosaicBranch, MosaicDirection, MosaicKey, MosaicNode, MosaicParent, MosaicPath } from '../types';
function alternateDirection<T extends MosaicKey>(
node: MosaicNode<T>,
direction: MosaicDirection = 'row',
): MosaicNode<T> {
if (isParent(node)) {
const nextDirection = getOtherDirection(direction);
return {
direction,
first: alternateDirection(node.first, nextDirection),
second: alternateDirection(node.second, nextDirection),
};
} else {
return node;
}
}
export enum Corner {
TOP_LEFT = 1,
TOP_RIGHT,
BOTTOM_LEFT,
BOTTOM_RIGHT,
}
/**
* Returns `true` if `node` is a MosaicParent
* @param node
* @returns {boolean}
*/
export function isParent<T extends MosaicKey>(node: MosaicNode<T>): node is MosaicParent<T> {
return (node as MosaicParent<T>).direction != null;
}
/**
* Creates a balanced binary tree from `leaves` with the goal of making them as equal area as possible
* @param leaves
* @param startDirection
* @returns {MosaicNode<T>}
*/
export function createBalancedTreeFromLeaves<T extends MosaicKey>(
leaves: MosaicNode<T>[],
startDirection: MosaicDirection = 'row',
): MosaicNode<T> | null {
if (leaves.length === 0) {
return null;
}
let current: MosaicNode<T>[] = clone(leaves);
let next: MosaicNode<T>[] = [];
while (current.length > 1) {
while (current.length > 0) {
if (current.length > 1) {
next.push({
direction: 'row',
first: current.shift()!,
second: current.shift()!,
});
} else {
next.unshift(current.shift()!);
}
}
current = next;
next = [];
}
return alternateDirection(current[0], startDirection);
}
/**
* Gets the sibling of `branch`
* @param branch
* @returns {any}
*/
export function getOtherBranch(branch: MosaicBranch): MosaicBranch {
if (branch === 'first') {
return 'second';
} else if (branch === 'second') {
return 'first';
} else {
throw new Error(`Branch '${branch}' not a valid branch`);
}
}
/**
* Gets the opposite of `direction`
* @param direction
* @returns {any}
*/
export function getOtherDirection(direction: MosaicDirection): MosaicDirection {
if (direction === 'row') {
return 'column';
} else {
return 'row';
}
}
/**
* Traverses `tree` to find the path to the specified `corner`
* @param tree
* @param corner
* @returns {MosaicPath}
*/
export function getPathToCorner(tree: MosaicNode<any>, corner: Corner): MosaicPath {
let currentNode: MosaicNode<any> = tree;
const currentPath: MosaicPath = [];
while (isParent(currentNode)) {
if (currentNode.direction === 'row' && (corner === Corner.TOP_LEFT || corner === Corner.BOTTOM_LEFT)) {
currentPath.push('first');
currentNode = currentNode.first;
} else if (currentNode.direction === 'column' && (corner === Corner.TOP_LEFT || corner === Corner.TOP_RIGHT)) {
currentPath.push('first');
currentNode = currentNode.first;
} else {
currentPath.push('second');
currentNode = currentNode.second;
}
}
return currentPath;
}
/**
* Gets all leaves of `tree`
* @param tree
* @returns {T[]}
*/
export function getLeaves<T extends MosaicKey>(tree: MosaicNode<T> | null): T[] {
if (tree == null) {
return [];
} else if (isParent(tree)) {
return getLeaves(tree.first).concat(getLeaves(tree.second));
} else {
return [tree];
}
}
/**
* Gets node at `path` from `tree`
* @param tree
* @param path
* @returns {MosaicNode<T>|null}
*/
export function getNodeAtPath<T extends MosaicKey>(tree: MosaicNode<T> | null, path: MosaicPath): MosaicNode<T> | null {
if (path.length > 0) {
return get(tree, path, null);
} else {
return tree;
}
}
/**
* Gets node at `path` from `tree` and verifies that neither `tree` nor the result are null
* @param tree
* @param path
* @returns {MosaicNode<T>}
*/
export function getAndAssertNodeAtPathExists<T extends MosaicKey>(
tree: MosaicNode<T> | null,
path: MosaicPath,
): MosaicNode<T> {
if (tree == null) {
throw new Error('Root is empty, cannot fetch path');
}
const node = getNodeAtPath(tree, path);
if (node == null) {
throw new Error(`Path [${path.join(', ')}] did not resolve to a node`);
}
return node;
}