File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change 1+ /**@file order.js */
2+
3+ /**
4+ * Callback for Array.Sort
5+ * сортировка карточек по параметру order
6+ * @param {object } a
7+ * @param {object } b
8+ */
9+ export function sortByOrder ( a , b ) {
10+ if ( a . order > b . order ) return - 1
11+ if ( a . order < b . order ) return 1
12+ return 0
13+ }
14+
15+ /**
16+ * Вычисление порядка новой заметки
17+ * @param {Array<object> } notesArr
18+ */
19+ export function calcOrder ( notesArr = [ ] ) {
20+ let order = 0
21+ notesArr . forEach ( ( note ) => {
22+ if ( note . order >= order ) order = note . order + 1
23+ } )
24+ return order
25+ }
26+
27+ /**
28+ * Исправление параметров order в массиве заметок
29+ * @param {Array<object> } notesArr
30+ */
31+ export function fixOrders ( notesArr = [ ] ) {
32+ let fixedArr = notesArr
33+ const sortedArr = Array . isArray ( notesArr ) ? notesArr . sort ( sortByOrder ) : [ ]
34+ sortedArr . forEach ( ( sortedNote , sortedNoteIndex ) => {
35+ fixedArr . forEach ( ( note ) => {
36+ if ( note . id === sortedNote . id ) note . order = sortedNoteIndex
37+ } )
38+ } )
39+ return fixedArr
40+ }
You can’t perform that action at this time.
0 commit comments