@fluentui/priority-overflow
Advanced tools
Comparing version 0.0.0-nightly-20230321-0440.1 to 0.0.0-nightly-20230322-0439.1
@@ -5,5 +5,5 @@ { | ||
{ | ||
"date": "Tue, 21 Mar 2023 04:49:51 GMT", | ||
"tag": "@fluentui/priority-overflow_v0.0.0-nightly-20230321-0440.1", | ||
"version": "0.0.0-nightly-20230321-0440.1", | ||
"date": "Wed, 22 Mar 2023 04:46:56 GMT", | ||
"tag": "@fluentui/priority-overflow_v0.0.0-nightly-20230322-0439.1", | ||
"version": "0.0.0-nightly-20230322-0439.1", | ||
"comments": { | ||
@@ -21,2 +21,23 @@ "prerelease": [ | ||
{ | ||
"date": "Tue, 21 Mar 2023 21:23:19 GMT", | ||
"tag": "@fluentui/priority-overflow_v9.0.2", | ||
"version": "9.0.2", | ||
"comments": { | ||
"patch": [ | ||
{ | ||
"author": "tristan.watanabe@gmail.com", | ||
"package": "@fluentui/priority-overflow", | ||
"commit": "ead1c6d4c2ac3f3596b62b8cbc07b0a03041f11f", | ||
"comment": "fix: add node field to package.json exports map." | ||
}, | ||
{ | ||
"author": "tristan.watanabe@gmail.com", | ||
"package": "@fluentui/priority-overflow", | ||
"commit": "2fac1a139149bd13b76b1306207bc988dca9c72c", | ||
"comment": "chore: migrate to swc transpilation approach." | ||
} | ||
] | ||
} | ||
}, | ||
{ | ||
"date": "Tue, 07 Feb 2023 14:13:08 GMT", | ||
@@ -23,0 +44,0 @@ "tag": "@fluentui/priority-overflow_v9.0.1", |
# Change Log - @fluentui/priority-overflow | ||
This log was last generated on Tue, 21 Mar 2023 04:49:51 GMT and should not be manually modified. | ||
This log was last generated on Wed, 22 Mar 2023 04:46:56 GMT and should not be manually modified. | ||
<!-- Start content --> | ||
## [0.0.0-nightly-20230321-0440.1](https://github.com/microsoft/fluentui/tree/@fluentui/priority-overflow_v0.0.0-nightly-20230321-0440.1) | ||
## [0.0.0-nightly-20230322-0439.1](https://github.com/microsoft/fluentui/tree/@fluentui/priority-overflow_v0.0.0-nightly-20230322-0439.1) | ||
Tue, 21 Mar 2023 04:49:51 GMT | ||
[Compare changes](https://github.com/microsoft/fluentui/compare/@fluentui/priority-overflow_v9.0.1..@fluentui/priority-overflow_v0.0.0-nightly-20230321-0440.1) | ||
Wed, 22 Mar 2023 04:46:56 GMT | ||
[Compare changes](https://github.com/microsoft/fluentui/compare/@fluentui/priority-overflow_v9.0.2..@fluentui/priority-overflow_v0.0.0-nightly-20230322-0439.1) | ||
@@ -16,2 +16,12 @@ ### Changes | ||
## [9.0.2](https://github.com/microsoft/fluentui/tree/@fluentui/priority-overflow_v9.0.2) | ||
Tue, 21 Mar 2023 21:23:19 GMT | ||
[Compare changes](https://github.com/microsoft/fluentui/compare/@fluentui/priority-overflow_v9.0.1..@fluentui/priority-overflow_v9.0.2) | ||
### Patches | ||
- fix: add node field to package.json exports map. ([PR #27154](https://github.com/microsoft/fluentui/pull/27154) by tristan.watanabe@gmail.com) | ||
- chore: migrate to swc transpilation approach. ([PR #27250](https://github.com/microsoft/fluentui/pull/27250) by tristan.watanabe@gmail.com) | ||
## [9.0.1](https://github.com/microsoft/fluentui/tree/@fluentui/priority-overflow_v9.0.1) | ||
@@ -18,0 +28,0 @@ |
@@ -1,7 +0,1 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.debounce = void 0; | ||
/** | ||
@@ -12,18 +6,25 @@ * Microtask debouncer | ||
* @returns debounced function | ||
*/ | ||
*/ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
Object.defineProperty(exports, "debounce", { | ||
enumerable: true, | ||
get: ()=>debounce | ||
}); | ||
function debounce(fn) { | ||
let pending; | ||
return () => { | ||
if (!pending) { | ||
pending = true; | ||
queueMicrotask(() => { | ||
// Need to set pending to `false` before the debounced function is run. | ||
// React can actually interrupt the function while it's running! | ||
pending = false; | ||
fn(); | ||
}); | ||
} | ||
}; | ||
} | ||
exports.debounce = debounce; | ||
let pending; | ||
return ()=>{ | ||
if (!pending) { | ||
pending = true; | ||
queueMicrotask(()=>{ | ||
// Need to set pending to `false` before the debounced function is run. | ||
// React can actually interrupt the function while it's running! | ||
pending = false; | ||
fn(); | ||
}); | ||
} | ||
}; | ||
} //# sourceMappingURL=debounce.js.map | ||
//# sourceMappingURL=debounce.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
value: true | ||
}); | ||
exports.createOverflowManager = void 0; | ||
var overflowManager_1 = /*#__PURE__*/require("./overflowManager"); | ||
Object.defineProperty(exports, "createOverflowManager", { | ||
enumerable: true, | ||
get: function () { | ||
return overflowManager_1.createOverflowManager; | ||
} | ||
enumerable: true, | ||
get: ()=>_overflowManager.createOverflowManager | ||
}); | ||
const _overflowManager = require("./overflowManager"); | ||
//# sourceMappingURL=index.js.map | ||
//# sourceMappingURL=index.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
value: true | ||
}); | ||
exports.createOverflowManager = void 0; | ||
const debounce_1 = /*#__PURE__*/require("./debounce"); | ||
const priorityQueue_1 = /*#__PURE__*/require("./priorityQueue"); | ||
/** | ||
* @internal | ||
* @returns overflow manager instance | ||
*/ | ||
Object.defineProperty(exports, "createOverflowManager", { | ||
enumerable: true, | ||
get: ()=>createOverflowManager | ||
}); | ||
const _debounce = require("./debounce"); | ||
const _priorityQueue = require("./priorityQueue"); | ||
function createOverflowManager() { | ||
let container; | ||
let overflowMenu; | ||
// Set as true when resize observer is observing | ||
let observing = false; | ||
// If true, next update will dispatch to onUpdateOverflow even if queue top states don't change | ||
let forceDispatch = false; | ||
const options = { | ||
padding: 10, | ||
overflowAxis: 'horizontal', | ||
overflowDirection: 'end', | ||
minimumVisible: 0, | ||
onUpdateItemVisibility: () => undefined, | ||
onUpdateOverflow: () => undefined | ||
}; | ||
const overflowItems = {}; | ||
const overflowGroups = {}; | ||
const resizeObserver = new ResizeObserver(entries => { | ||
if (!entries[0] || !container) { | ||
return; | ||
} | ||
update(); | ||
}); | ||
const invisibleItemQueue = priorityQueue_1.createPriorityQueue((a, b) => { | ||
const itemA = overflowItems[a]; | ||
const itemB = overflowItems[b]; | ||
// Higher priority at the top of the queue | ||
const priority = itemB.priority - itemA.priority; | ||
if (priority !== 0) { | ||
return priority; | ||
} | ||
const positionStatusBit = options.overflowDirection === 'end' ? Node.DOCUMENT_POSITION_FOLLOWING : Node.DOCUMENT_POSITION_PRECEDING; | ||
// equal priority, use DOM order | ||
// eslint-disable-next-line no-bitwise | ||
return itemA.element.compareDocumentPosition(itemB.element) & positionStatusBit ? -1 : 1; | ||
}); | ||
const visibleItemQueue = priorityQueue_1.createPriorityQueue((a, b) => { | ||
const itemA = overflowItems[a]; | ||
const itemB = overflowItems[b]; | ||
// Lower priority at the top of the queue | ||
const priority = itemA.priority - itemB.priority; | ||
if (priority !== 0) { | ||
return priority; | ||
} | ||
const positionStatusBit = options.overflowDirection === 'end' ? Node.DOCUMENT_POSITION_PRECEDING : Node.DOCUMENT_POSITION_FOLLOWING; | ||
// equal priority, use DOM order | ||
// eslint-disable-next-line no-bitwise | ||
return itemA.element.compareDocumentPosition(itemB.element) & positionStatusBit ? -1 : 1; | ||
}); | ||
const getOffsetSize = el => { | ||
return options.overflowAxis === 'horizontal' ? el.offsetWidth : el.offsetHeight; | ||
}; | ||
const makeItemVisible = () => { | ||
const nextVisible = invisibleItemQueue.dequeue(); | ||
visibleItemQueue.enqueue(nextVisible); | ||
const item = overflowItems[nextVisible]; | ||
options.onUpdateItemVisibility({ | ||
item, | ||
visible: true | ||
let container; | ||
let overflowMenu; | ||
// Set as true when resize observer is observing | ||
let observing = false; | ||
// If true, next update will dispatch to onUpdateOverflow even if queue top states don't change | ||
let forceDispatch = false; | ||
const options = { | ||
padding: 10, | ||
overflowAxis: 'horizontal', | ||
overflowDirection: 'end', | ||
minimumVisible: 0, | ||
onUpdateItemVisibility: ()=>undefined, | ||
onUpdateOverflow: ()=>undefined | ||
}; | ||
const overflowItems = {}; | ||
const overflowGroups = {}; | ||
const resizeObserver = new ResizeObserver((entries)=>{ | ||
if (!entries[0] || !container) { | ||
return; | ||
} | ||
update(); | ||
}); | ||
if (item.groupId) { | ||
overflowGroups[item.groupId].invisibleItemIds.delete(item.id); | ||
overflowGroups[item.groupId].visibleItemIds.add(item.id); | ||
} | ||
return getOffsetSize(item.element); | ||
}; | ||
const makeItemInvisible = () => { | ||
const nextInvisible = visibleItemQueue.dequeue(); | ||
invisibleItemQueue.enqueue(nextInvisible); | ||
const item = overflowItems[nextInvisible]; | ||
const width = getOffsetSize(item.element); | ||
options.onUpdateItemVisibility({ | ||
item, | ||
visible: false | ||
const invisibleItemQueue = (0, _priorityQueue.createPriorityQueue)((a, b)=>{ | ||
const itemA = overflowItems[a]; | ||
const itemB = overflowItems[b]; | ||
// Higher priority at the top of the queue | ||
const priority = itemB.priority - itemA.priority; | ||
if (priority !== 0) { | ||
return priority; | ||
} | ||
const positionStatusBit = options.overflowDirection === 'end' ? Node.DOCUMENT_POSITION_FOLLOWING : Node.DOCUMENT_POSITION_PRECEDING; | ||
// equal priority, use DOM order | ||
// eslint-disable-next-line no-bitwise | ||
return itemA.element.compareDocumentPosition(itemB.element) & positionStatusBit ? -1 : 1; | ||
}); | ||
if (item.groupId) { | ||
overflowGroups[item.groupId].visibleItemIds.delete(item.id); | ||
overflowGroups[item.groupId].invisibleItemIds.add(item.id); | ||
} | ||
return width; | ||
}; | ||
const dispatchOverflowUpdate = () => { | ||
const visibleItemIds = visibleItemQueue.all(); | ||
const invisibleItemIds = invisibleItemQueue.all(); | ||
const visibleItems = visibleItemIds.map(itemId => overflowItems[itemId]); | ||
const invisibleItems = invisibleItemIds.map(itemId => overflowItems[itemId]); | ||
const groupVisibility = {}; | ||
Object.entries(overflowGroups).forEach(([groupId, groupState]) => { | ||
if (groupState.invisibleItemIds.size && groupState.visibleItemIds.size) { | ||
groupVisibility[groupId] = 'overflow'; | ||
} else if (groupState.visibleItemIds.size === 0) { | ||
groupVisibility[groupId] = 'hidden'; | ||
} else { | ||
groupVisibility[groupId] = 'visible'; | ||
} | ||
const visibleItemQueue = (0, _priorityQueue.createPriorityQueue)((a, b)=>{ | ||
const itemA = overflowItems[a]; | ||
const itemB = overflowItems[b]; | ||
// Lower priority at the top of the queue | ||
const priority = itemA.priority - itemB.priority; | ||
if (priority !== 0) { | ||
return priority; | ||
} | ||
const positionStatusBit = options.overflowDirection === 'end' ? Node.DOCUMENT_POSITION_PRECEDING : Node.DOCUMENT_POSITION_FOLLOWING; | ||
// equal priority, use DOM order | ||
// eslint-disable-next-line no-bitwise | ||
return itemA.element.compareDocumentPosition(itemB.element) & positionStatusBit ? -1 : 1; | ||
}); | ||
options.onUpdateOverflow({ | ||
visibleItems, | ||
invisibleItems, | ||
groupVisibility | ||
}); | ||
}; | ||
const processOverflowItems = () => { | ||
if (!container) { | ||
return false; | ||
} | ||
const availableSize = getOffsetSize(container) - options.padding; | ||
const overflowMenuOffset = overflowMenu ? getOffsetSize(overflowMenu) : 0; | ||
// Snapshot of the visible/invisible state to compare for updates | ||
const visibleTop = visibleItemQueue.peek(); | ||
const invisibleTop = invisibleItemQueue.peek(); | ||
const visibleItemIds = visibleItemQueue.all(); | ||
let currentWidth = visibleItemIds.reduce((sum, visibleItemId) => { | ||
const child = overflowItems[visibleItemId].element; | ||
return sum + getOffsetSize(child); | ||
}, 0); | ||
// Add items until available width is filled - can result in overflow | ||
while (currentWidth < availableSize && invisibleItemQueue.size() > 0) { | ||
currentWidth += makeItemVisible(); | ||
} | ||
// Remove items until there's no more overflow | ||
while (currentWidth > availableSize && visibleItemQueue.size() > 0) { | ||
if (visibleItemQueue.size() <= options.minimumVisible) { | ||
break; | ||
} | ||
currentWidth -= makeItemInvisible(); | ||
} | ||
// make sure the overflow menu can fit | ||
if (visibleItemQueue.size() > options.minimumVisible && invisibleItemQueue.size() > 0 && currentWidth + overflowMenuOffset > availableSize) { | ||
makeItemInvisible(); | ||
} | ||
// only update when the state of visible/invisible items has changed | ||
if (visibleItemQueue.peek() !== visibleTop || invisibleItemQueue.peek() !== invisibleTop) { | ||
return true; | ||
} | ||
return false; | ||
}; | ||
const forceUpdate = () => { | ||
if (processOverflowItems() || forceDispatch) { | ||
forceDispatch = false; | ||
dispatchOverflowUpdate(); | ||
} | ||
}; | ||
const update = debounce_1.debounce(forceUpdate); | ||
const observe = (observedContainer, userOptions) => { | ||
Object.assign(options, userOptions); | ||
observing = true; | ||
Object.values(overflowItems).forEach(item => visibleItemQueue.enqueue(item.id)); | ||
container = observedContainer; | ||
resizeObserver.observe(container); | ||
}; | ||
const disconnect = () => { | ||
observing = false; | ||
resizeObserver.disconnect(); | ||
}; | ||
const addItem = item => { | ||
if (overflowItems[item.id]) { | ||
return; | ||
} | ||
overflowItems[item.id] = item; | ||
// some options can affect priority which are only set on `observe` | ||
if (observing) { | ||
// Updates to elements might not change the queue tops | ||
// i.e. new element is enqueued but the top of the queue stays the same | ||
// force a dispatch on the next batched update | ||
forceDispatch = true; | ||
visibleItemQueue.enqueue(item.id); | ||
} | ||
if (item.groupId) { | ||
if (!overflowGroups[item.groupId]) { | ||
overflowGroups[item.groupId] = { | ||
visibleItemIds: new Set(), | ||
invisibleItemIds: new Set() | ||
}; | ||
} | ||
overflowGroups[item.groupId].visibleItemIds.add(item.id); | ||
} | ||
update(); | ||
}; | ||
const addOverflowMenu = el => { | ||
overflowMenu = el; | ||
}; | ||
const removeOverflowMenu = () => { | ||
overflowMenu = undefined; | ||
}; | ||
const removeItem = itemId => { | ||
if (!overflowItems[itemId]) { | ||
return; | ||
} | ||
const item = overflowItems[itemId]; | ||
visibleItemQueue.remove(itemId); | ||
invisibleItemQueue.remove(itemId); | ||
if (item.groupId) { | ||
overflowGroups[item.groupId].visibleItemIds.delete(item.id); | ||
overflowGroups[item.groupId].invisibleItemIds.delete(item.id); | ||
} | ||
delete overflowItems[itemId]; | ||
update(); | ||
}; | ||
return { | ||
addItem, | ||
disconnect, | ||
forceUpdate, | ||
observe, | ||
removeItem, | ||
update, | ||
addOverflowMenu, | ||
removeOverflowMenu | ||
}; | ||
} | ||
exports.createOverflowManager = createOverflowManager; | ||
const getOffsetSize = (el)=>{ | ||
return options.overflowAxis === 'horizontal' ? el.offsetWidth : el.offsetHeight; | ||
}; | ||
const makeItemVisible = ()=>{ | ||
const nextVisible = invisibleItemQueue.dequeue(); | ||
visibleItemQueue.enqueue(nextVisible); | ||
const item = overflowItems[nextVisible]; | ||
options.onUpdateItemVisibility({ | ||
item, | ||
visible: true | ||
}); | ||
if (item.groupId) { | ||
overflowGroups[item.groupId].invisibleItemIds.delete(item.id); | ||
overflowGroups[item.groupId].visibleItemIds.add(item.id); | ||
} | ||
return getOffsetSize(item.element); | ||
}; | ||
const makeItemInvisible = ()=>{ | ||
const nextInvisible = visibleItemQueue.dequeue(); | ||
invisibleItemQueue.enqueue(nextInvisible); | ||
const item = overflowItems[nextInvisible]; | ||
const width = getOffsetSize(item.element); | ||
options.onUpdateItemVisibility({ | ||
item, | ||
visible: false | ||
}); | ||
if (item.groupId) { | ||
overflowGroups[item.groupId].visibleItemIds.delete(item.id); | ||
overflowGroups[item.groupId].invisibleItemIds.add(item.id); | ||
} | ||
return width; | ||
}; | ||
const dispatchOverflowUpdate = ()=>{ | ||
const visibleItemIds = visibleItemQueue.all(); | ||
const invisibleItemIds = invisibleItemQueue.all(); | ||
const visibleItems = visibleItemIds.map((itemId)=>overflowItems[itemId]); | ||
const invisibleItems = invisibleItemIds.map((itemId)=>overflowItems[itemId]); | ||
const groupVisibility = {}; | ||
Object.entries(overflowGroups).forEach(([groupId, groupState])=>{ | ||
if (groupState.invisibleItemIds.size && groupState.visibleItemIds.size) { | ||
groupVisibility[groupId] = 'overflow'; | ||
} else if (groupState.visibleItemIds.size === 0) { | ||
groupVisibility[groupId] = 'hidden'; | ||
} else { | ||
groupVisibility[groupId] = 'visible'; | ||
} | ||
}); | ||
options.onUpdateOverflow({ | ||
visibleItems, | ||
invisibleItems, | ||
groupVisibility | ||
}); | ||
}; | ||
const processOverflowItems = ()=>{ | ||
if (!container) { | ||
return false; | ||
} | ||
const availableSize = getOffsetSize(container) - options.padding; | ||
const overflowMenuOffset = overflowMenu ? getOffsetSize(overflowMenu) : 0; | ||
// Snapshot of the visible/invisible state to compare for updates | ||
const visibleTop = visibleItemQueue.peek(); | ||
const invisibleTop = invisibleItemQueue.peek(); | ||
const visibleItemIds = visibleItemQueue.all(); | ||
let currentWidth = visibleItemIds.reduce((sum, visibleItemId)=>{ | ||
const child = overflowItems[visibleItemId].element; | ||
return sum + getOffsetSize(child); | ||
}, 0); | ||
// Add items until available width is filled - can result in overflow | ||
while(currentWidth < availableSize && invisibleItemQueue.size() > 0){ | ||
currentWidth += makeItemVisible(); | ||
} | ||
// Remove items until there's no more overflow | ||
while(currentWidth > availableSize && visibleItemQueue.size() > 0){ | ||
if (visibleItemQueue.size() <= options.minimumVisible) { | ||
break; | ||
} | ||
currentWidth -= makeItemInvisible(); | ||
} | ||
// make sure the overflow menu can fit | ||
if (visibleItemQueue.size() > options.minimumVisible && invisibleItemQueue.size() > 0 && currentWidth + overflowMenuOffset > availableSize) { | ||
makeItemInvisible(); | ||
} | ||
// only update when the state of visible/invisible items has changed | ||
if (visibleItemQueue.peek() !== visibleTop || invisibleItemQueue.peek() !== invisibleTop) { | ||
return true; | ||
} | ||
return false; | ||
}; | ||
const forceUpdate = ()=>{ | ||
if (processOverflowItems() || forceDispatch) { | ||
forceDispatch = false; | ||
dispatchOverflowUpdate(); | ||
} | ||
}; | ||
const update = (0, _debounce.debounce)(forceUpdate); | ||
const observe = (observedContainer, userOptions)=>{ | ||
Object.assign(options, userOptions); | ||
observing = true; | ||
Object.values(overflowItems).forEach((item)=>visibleItemQueue.enqueue(item.id)); | ||
container = observedContainer; | ||
resizeObserver.observe(container); | ||
}; | ||
const disconnect = ()=>{ | ||
observing = false; | ||
resizeObserver.disconnect(); | ||
}; | ||
const addItem = (item)=>{ | ||
if (overflowItems[item.id]) { | ||
return; | ||
} | ||
overflowItems[item.id] = item; | ||
// some options can affect priority which are only set on `observe` | ||
if (observing) { | ||
// Updates to elements might not change the queue tops | ||
// i.e. new element is enqueued but the top of the queue stays the same | ||
// force a dispatch on the next batched update | ||
forceDispatch = true; | ||
visibleItemQueue.enqueue(item.id); | ||
} | ||
if (item.groupId) { | ||
if (!overflowGroups[item.groupId]) { | ||
overflowGroups[item.groupId] = { | ||
visibleItemIds: new Set(), | ||
invisibleItemIds: new Set() | ||
}; | ||
} | ||
overflowGroups[item.groupId].visibleItemIds.add(item.id); | ||
} | ||
update(); | ||
}; | ||
const addOverflowMenu = (el)=>{ | ||
overflowMenu = el; | ||
}; | ||
const removeOverflowMenu = ()=>{ | ||
overflowMenu = undefined; | ||
}; | ||
const removeItem = (itemId)=>{ | ||
if (!overflowItems[itemId]) { | ||
return; | ||
} | ||
const item = overflowItems[itemId]; | ||
visibleItemQueue.remove(itemId); | ||
invisibleItemQueue.remove(itemId); | ||
if (item.groupId) { | ||
overflowGroups[item.groupId].visibleItemIds.delete(item.id); | ||
overflowGroups[item.groupId].invisibleItemIds.delete(item.id); | ||
} | ||
delete overflowItems[itemId]; | ||
update(); | ||
}; | ||
return { | ||
addItem, | ||
disconnect, | ||
forceUpdate, | ||
observe, | ||
removeItem, | ||
update, | ||
addOverflowMenu, | ||
removeOverflowMenu | ||
}; | ||
} //# sourceMappingURL=overflowManager.js.map | ||
//# sourceMappingURL=overflowManager.js.map |
@@ -1,98 +0,99 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.createPriorityQueue = void 0; | ||
/** | ||
* @param compare - comparison function for items | ||
* @returns Priority queue implemented with a min heap | ||
*/ | ||
*/ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
Object.defineProperty(exports, "createPriorityQueue", { | ||
enumerable: true, | ||
get: ()=>createPriorityQueue | ||
}); | ||
function createPriorityQueue(compare) { | ||
const arr = []; | ||
let size = 0; | ||
const left = i => { | ||
return 2 * i + 1; | ||
}; | ||
const right = i => { | ||
return 2 * i + 2; | ||
}; | ||
const parent = i => { | ||
return Math.floor((i - 1) / 2); | ||
}; | ||
const swap = (a, b) => { | ||
const tmp = arr[a]; | ||
arr[a] = arr[b]; | ||
arr[b] = tmp; | ||
}; | ||
const heapify = i => { | ||
let smallest = i; | ||
const l = left(i); | ||
const r = right(i); | ||
if (l < size && compare(arr[l], arr[smallest]) < 0) { | ||
smallest = l; | ||
} | ||
if (r < size && compare(arr[r], arr[smallest]) < 0) { | ||
smallest = r; | ||
} | ||
if (smallest !== i) { | ||
swap(smallest, i); | ||
heapify(smallest); | ||
} | ||
}; | ||
const dequeue = () => { | ||
if (size === 0) { | ||
throw new Error('Priority queue empty'); | ||
} | ||
const res = arr[0]; | ||
arr[0] = arr[--size]; | ||
heapify(0); | ||
return res; | ||
}; | ||
const peek = () => { | ||
if (size === 0) { | ||
return null; | ||
} | ||
return arr[0]; | ||
}; | ||
const enqueue = item => { | ||
arr[size++] = item; | ||
let i = size - 1; | ||
let p = parent(i); | ||
while (i > 0 && compare(arr[p], arr[i]) > 0) { | ||
swap(p, i); | ||
i = p; | ||
p = parent(i); | ||
} | ||
}; | ||
const contains = item => { | ||
const index = arr.indexOf(item); | ||
return index >= 0 && index < size; | ||
}; | ||
const remove = item => { | ||
const i = arr.indexOf(item); | ||
if (i === -1 || i >= size) { | ||
return; | ||
} | ||
arr[i] = arr[--size]; | ||
heapify(i); | ||
}; | ||
const clear = () => { | ||
size = 0; | ||
}; | ||
const all = () => { | ||
return arr.slice(0, size); | ||
}; | ||
return { | ||
all, | ||
clear, | ||
contains, | ||
dequeue, | ||
enqueue, | ||
peek, | ||
remove, | ||
size: () => size | ||
}; | ||
} | ||
exports.createPriorityQueue = createPriorityQueue; | ||
const arr = []; | ||
let size = 0; | ||
const left = (i)=>{ | ||
return 2 * i + 1; | ||
}; | ||
const right = (i)=>{ | ||
return 2 * i + 2; | ||
}; | ||
const parent = (i)=>{ | ||
return Math.floor((i - 1) / 2); | ||
}; | ||
const swap = (a, b)=>{ | ||
const tmp = arr[a]; | ||
arr[a] = arr[b]; | ||
arr[b] = tmp; | ||
}; | ||
const heapify = (i)=>{ | ||
let smallest = i; | ||
const l = left(i); | ||
const r = right(i); | ||
if (l < size && compare(arr[l], arr[smallest]) < 0) { | ||
smallest = l; | ||
} | ||
if (r < size && compare(arr[r], arr[smallest]) < 0) { | ||
smallest = r; | ||
} | ||
if (smallest !== i) { | ||
swap(smallest, i); | ||
heapify(smallest); | ||
} | ||
}; | ||
const dequeue = ()=>{ | ||
if (size === 0) { | ||
throw new Error('Priority queue empty'); | ||
} | ||
const res = arr[0]; | ||
arr[0] = arr[--size]; | ||
heapify(0); | ||
return res; | ||
}; | ||
const peek = ()=>{ | ||
if (size === 0) { | ||
return null; | ||
} | ||
return arr[0]; | ||
}; | ||
const enqueue = (item)=>{ | ||
arr[size++] = item; | ||
let i = size - 1; | ||
let p = parent(i); | ||
while(i > 0 && compare(arr[p], arr[i]) > 0){ | ||
swap(p, i); | ||
i = p; | ||
p = parent(i); | ||
} | ||
}; | ||
const contains = (item)=>{ | ||
const index = arr.indexOf(item); | ||
return index >= 0 && index < size; | ||
}; | ||
const remove = (item)=>{ | ||
const i = arr.indexOf(item); | ||
if (i === -1 || i >= size) { | ||
return; | ||
} | ||
arr[i] = arr[--size]; | ||
heapify(i); | ||
}; | ||
const clear = ()=>{ | ||
size = 0; | ||
}; | ||
const all = ()=>{ | ||
return arr.slice(0, size); | ||
}; | ||
return { | ||
all, | ||
clear, | ||
contains, | ||
dequeue, | ||
enqueue, | ||
peek, | ||
remove, | ||
size: ()=>size | ||
}; | ||
} //# sourceMappingURL=priorityQueue.js.map | ||
//# sourceMappingURL=priorityQueue.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
value: true | ||
}); | ||
//# sourceMappingURL=types.js.map | ||
//# sourceMappingURL=types.js.map |
@@ -6,4 +6,3 @@ /** | ||
* @returns debounced function | ||
*/ | ||
export function debounce(fn) { | ||
*/export function debounce(fn) { | ||
let pending; | ||
@@ -10,0 +9,0 @@ return () => { |
/** | ||
* @param compare - comparison function for items | ||
* @returns Priority queue implemented with a min heap | ||
*/ | ||
export function createPriorityQueue(compare) { | ||
*/export function createPriorityQueue(compare) { | ||
const arr = []; | ||
@@ -7,0 +6,0 @@ let size = 0; |
{ | ||
"name": "@fluentui/priority-overflow", | ||
"version": "0.0.0-nightly-20230321-0440.1", | ||
"version": "0.0.0-nightly-20230322-0439.1", | ||
"description": "Vanilla JS utilities to implement overflow menus", | ||
@@ -23,3 +23,3 @@ "main": "lib-commonjs/index.js", | ||
"type-check": "tsc -b tsconfig.json", | ||
"generate-api": "tsc -p ./tsconfig.lib.json --emitDeclarationOnly && just-scripts api-extractor" | ||
"generate-api": "just-scripts generate-api" | ||
}, | ||
@@ -32,3 +32,3 @@ "devDependencies": { | ||
"dependencies": { | ||
"tslib": "^2.1.0" | ||
"@swc/helpers": "^0.4.14" | ||
}, | ||
@@ -35,0 +35,0 @@ "beachball": {}, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
27
1058
87336
+ Added@swc/helpers@^0.4.14
+ Added@swc/helpers@0.4.37(transitive)
- Removedtslib@^2.1.0