feat: add snjs package

This commit is contained in:
Karol Sójko
2022-07-06 14:04:18 +02:00
parent 321a055bae
commit 0e40469e2f
296 changed files with 46109 additions and 187 deletions

View File

@@ -0,0 +1,43 @@
import { ContentType } from '@standardnotes/common'
import { FullyFormedPayloadInterface } from '@standardnotes/models'
/**
* Sorts payloads according by most recently modified first, according to the priority,
* whereby the earlier a content_type appears in the priorityList,
* the earlier it will appear in the resulting sorted array.
*/
export function SortPayloadsByRecentAndContentPriority(
payloads: FullyFormedPayloadInterface[],
priorityList: ContentType[],
): FullyFormedPayloadInterface[] {
return payloads.sort((a, b) => {
const dateResult = new Date(b.serverUpdatedAt).getTime() - new Date(a.serverUpdatedAt).getTime()
let aPriority = 0
let bPriority = 0
if (priorityList) {
aPriority = priorityList.indexOf(a.content_type)
bPriority = priorityList.indexOf(b.content_type)
if (aPriority === -1) {
/** Not found in list, not prioritized. Set it to max value */
aPriority = priorityList.length
}
if (bPriority === -1) {
/** Not found in list, not prioritized. Set it to max value */
bPriority = priorityList.length
}
}
if (aPriority === bPriority) {
return dateResult
}
if (aPriority < bPriority) {
return -1
} else {
return 1
}
})
}