@aria-ui/collection
Advanced tools
Comparing version 0.0.2 to 0.0.3
declare class Collection { | ||
readonly loop: boolean; | ||
private _map; | ||
private _head; | ||
private _tail; | ||
private _items; | ||
private _indexes; | ||
constructor(items: Iterable<HTMLElement>, loop?: boolean); | ||
size(): number; | ||
private _find; | ||
/** | ||
* Returns the first enabled value. | ||
*/ | ||
first(): string | null; | ||
/** | ||
* Returns the last enabled value. | ||
*/ | ||
last(): string | null; | ||
size(): number; | ||
/** | ||
* Returns the next enabled value. | ||
*/ | ||
next(value: string | null): string | null; | ||
/** | ||
* Returns the previous enabled value. | ||
*/ | ||
prev(value: string | null): string | null; | ||
/** | ||
* Finds an element from its value. | ||
*/ | ||
getElement(value: string): HTMLElement | null; | ||
/** | ||
* Returns all values. | ||
*/ | ||
getValues(): string[]; | ||
@@ -14,0 +32,0 @@ } |
// src/index.ts | ||
var Node = class { | ||
constructor(element, prev = null, next = null) { | ||
this.element = element; | ||
this.prev = prev; | ||
this.next = next; | ||
} | ||
get value() { | ||
return this.element.value || this.element.textContent || this.element.innerText || this.element.innerHTML; | ||
} | ||
get disabled() { | ||
return this.element.disabled || this.element.getAttribute("aria-disabled") === "true" || this.element.getAttribute("aria-hidden") === "true"; | ||
} | ||
}; | ||
function getValue(element) { | ||
return element.value || element.textContent || element.innerText || element.innerHTML; | ||
} | ||
function isDisabled(element) { | ||
return element.disabled || element.getAttribute("aria-disabled") === "true" || element.getAttribute("aria-hidden") === "true"; | ||
} | ||
var Collection = class { | ||
constructor(items, loop = true) { | ||
this.loop = loop; | ||
this._map = /* @__PURE__ */ new Map(); | ||
this._head = null; | ||
this._tail = null; | ||
let prev; | ||
this._items = []; | ||
this._indexes = /* @__PURE__ */ new Map(); | ||
for (const item of items) { | ||
const node = new Node(item); | ||
const value = node.value; | ||
if (!value || this._map.has(value)) { | ||
const value = getValue(item); | ||
if (!value || this._indexes.has(value)) { | ||
continue; | ||
} | ||
this._map.set(value, node); | ||
if (!this._head) { | ||
this._head = node; | ||
this._indexes.set(value, this._items.length); | ||
this._items.push(item); | ||
} | ||
} | ||
size() { | ||
return this._items.length; | ||
} | ||
_find(startIndex, dir) { | ||
let index = startIndex; | ||
const n = this._items.length; | ||
for (let i = 0; i < n; i++) { | ||
if (index < 0 || index >= n) { | ||
if (this.loop) { | ||
index = (index + n) % n; | ||
} else { | ||
break; | ||
} | ||
} | ||
this._tail = node; | ||
if (prev) { | ||
node.prev = prev; | ||
prev.next = node; | ||
const item = this._items[index]; | ||
if (item && !isDisabled(item)) { | ||
return getValue(item); | ||
} | ||
prev = node; | ||
index += dir; | ||
} | ||
if (this.loop && this._head && this._tail) { | ||
this._tail.next = this._head; | ||
this._head.prev = this._tail; | ||
} | ||
return null; | ||
} | ||
/** | ||
* Returns the first enabled value. | ||
*/ | ||
first() { | ||
var _a; | ||
return ((_a = this._head) == null ? void 0 : _a.value) || null; | ||
return this._find(0, 1); | ||
} | ||
/** | ||
* Returns the last enabled value. | ||
*/ | ||
last() { | ||
var _a; | ||
return ((_a = this._tail) == null ? void 0 : _a.value) || null; | ||
return this._find(this.size() - 1, -1); | ||
} | ||
size() { | ||
return this._map.size; | ||
} | ||
/** | ||
* Returns the next enabled value. | ||
*/ | ||
next(value) { | ||
var _a, _b; | ||
const curr = value == null ? null : this._map.get(value); | ||
let next = (_a = curr == null ? void 0 : curr.next) != null ? _a : this._head; | ||
const size = this.size(); | ||
for (let i = 0; i < size; i++) { | ||
if (next && !next.disabled) | ||
return next.value; | ||
next = (_b = next == null ? void 0 : next.next) != null ? _b : null; | ||
if (value == null) { | ||
return this.first(); | ||
} | ||
return null; | ||
const index = this._indexes.get(value); | ||
if (index == null) { | ||
return this.first(); | ||
} | ||
return this._find(index + 1, 1); | ||
} | ||
/** | ||
* Returns the previous enabled value. | ||
*/ | ||
prev(value) { | ||
var _a, _b; | ||
const curr = value == null ? null : this._map.get(value); | ||
let prev = (_a = curr == null ? void 0 : curr.prev) != null ? _a : this._tail; | ||
const size = this.size(); | ||
for (let i = 0; i < size; i++) { | ||
if (prev && !prev.disabled) | ||
return prev.value; | ||
prev = (_b = prev == null ? void 0 : prev.prev) != null ? _b : null; | ||
if (value == null) { | ||
return this.last(); | ||
} | ||
return null; | ||
const index = this._indexes.get(value); | ||
if (index == null) { | ||
return this.last(); | ||
} | ||
return this._find(index - 1, -1); | ||
} | ||
/** | ||
* Finds an element from its value. | ||
*/ | ||
getElement(value) { | ||
var _a, _b; | ||
return (_b = (_a = this._map.get(value)) == null ? void 0 : _a.element) != null ? _b : null; | ||
var _a; | ||
const index = this._indexes.get(value); | ||
if (index == null) | ||
return null; | ||
return (_a = this._items[index]) != null ? _a : null; | ||
} | ||
/** | ||
* Returns all values. | ||
*/ | ||
getValues() { | ||
const values = []; | ||
const head = this._head; | ||
let node = head; | ||
while (node) { | ||
values.push(node.value); | ||
node = node.next; | ||
if (node === head) { | ||
node = null; | ||
} | ||
} | ||
return values; | ||
return this._items.map(getValue); | ||
} | ||
@@ -96,0 +98,0 @@ }; |
{ | ||
"name": "@aria-ui/collection", | ||
"type": "module", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"private": false, | ||
@@ -6,0 +6,0 @@ "sideEffects": false, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
152
6033