react-focus-flow
Version:
React components to define isolated focus flows
172 lines (153 loc) • 5.17 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
// this is actually a sorted doubly circular linked list
var LinkedList = function LinkedList() {
return _defineProperty({
head: null,
insert: function insert(newNode) {
if (this.isEmpty()) {
this.head = newNode;
this.head.next = newNode;
this.head.prev = newNode;
} else {
// find position in the list to insert by finding node to insert before,
// if newNode has largest index in the list, append to the end of the list
// by inserting before the head
var after = this.find(function (node) {
return newNode.index < node.index;
});
this.insertBefore(newNode, after || this.head);
if (after === this.head) {
this.head = newNode;
}
}
},
remove: function remove(node) {
/* eslint-disable no-param-reassign */
// if length is one, null out the head and return
if (node === node.next) {
this.head = null;
return;
} // stitch refs
node.next.prev = node.prev;
node.prev.next = node.next; // if removing head, update position to next
if (node === this.head) {
this.head = node.next;
}
},
isEmpty: function isEmpty() {
return this.head === null;
},
find: function find(predicate, start) {
var direction = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : "forward";
/* eslint-disable no-restricted-syntax */
var nodes = this[Symbol.iterator](start, direction);
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = nodes[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var _node = _step.value;
if (predicate(_node)) {
return _node;
}
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return != null) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
return undefined;
},
forEach: function forEach(fn) {
var start = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.head;
var direction = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : "forward";
var nodes = this[Symbol.iterator](start, direction);
var _iteratorNormalCompletion2 = true;
var _didIteratorError2 = false;
var _iteratorError2 = undefined;
try {
for (var _iterator2 = nodes[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
var _node2 = _step2.value;
fn(_node2);
}
} catch (err) {
_didIteratorError2 = true;
_iteratorError2 = err;
} finally {
try {
if (!_iteratorNormalCompletion2 && _iterator2.return != null) {
_iterator2.return();
}
} finally {
if (_didIteratorError2) {
throw _iteratorError2;
}
}
}
},
length: function length() {
var count = 0;
this.forEach(function () {
count += 1;
});
return count;
},
insertBefore: function insertBefore(node, after) {
node.prev = after.prev;
node.next = after;
node.prev.next = node;
after.prev = node;
}
}, Symbol.iterator,
/*#__PURE__*/
regeneratorRuntime.mark(function linkedListGenerator() {
var start,
direction,
cur,
_args = arguments;
return regeneratorRuntime.wrap(function linkedListGenerator$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
start = _args.length > 0 && _args[0] !== undefined ? _args[0] : this.head;
direction = _args.length > 1 && _args[1] !== undefined ? _args[1] : "forward";
cur = start;
if (!this.isEmpty()) {
_context.next = 5;
break;
}
return _context.abrupt("return");
case 5:
_context.next = 7;
return cur;
case 7:
cur = direction === "forward" ? cur.next : cur.prev;
case 8:
if (cur !== start && cur !== null) {
_context.next = 5;
break;
}
case 9:
case "end":
return _context.stop();
}
}
}, linkedListGenerator, this);
}));
};
var _default = LinkedList;
exports.default = _default;