-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
inserttopriorityarray.ts
47 lines (39 loc) · 1.16 KB
/
inserttopriorityarray.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
/**
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
import priorities, { type PriorityString } from './priorities.js';
/**
* @module utils/inserttopriorityarray
*/
/**
* The priority object descriptor.
*
* ```ts
* const objectWithPriority = {
* priority: 'high'
* }
* ```
*/
export interface ObjectWithPriority {
/**
* Priority of the object.
*/
priority: PriorityString;
}
/**
* Inserts any object with priority at correct index by priority so registered objects are always sorted from highest to lowest priority.
*
* @param objects Array of objects with priority to insert object to.
* @param objectToInsert Object with `priority` property.
*/
export default function insertToPriorityArray<T extends ObjectWithPriority>( objects: Array<T>, objectToInsert: T ): void {
const priority = priorities.get( objectToInsert.priority );
for ( let i = 0; i < objects.length; i++ ) {
if ( priorities.get( objects[ i ].priority ) < priority ) {
objects.splice( i, 0, objectToInsert );
return;
}
}
objects.push( objectToInsert );
}