import {append, compose, dropRight, flow, takeRight, take, drop, cond, size, isnt, last, throws} from 'tsfun'; import {Document} from 'idai-components-2'; /** * @author Daniel de Oliveira */ export function isProjectDocument(document: Document): boolean { return document.resource.id === 'project'; } export function sortRevisionsByLastModified(documents: Array): Array { return documents.sort((l: Document, r: Document) => { const lDate = Document.getLastModified(l).date as Date; const rDate = Document.getLastModified(r).date as Date; if (lDate < rDate) return -1; if (lDate > rDate) return 1; return 0; }); } export function replaceLastPair(as: Array, replacement: A): Array { return replaceRight(as, 2, replacement); } function replaceRight(as: Array, itemsToReplace: number, replacement: A): Array { return flow(as, dropRight(itemsToReplace), append(replacement)); } const lengthIsnt2 = compose(size, isnt(2)); export const last2 = compose( takeRight(2), cond(lengthIsnt2, throws('Illegal argument, length must be at least 2'))); /** * Dissociates the given indices from an array. * * Example * > dissocIndices([0, 2])(['a', 'b', 'c', 'd') * ['b', 'd'] * * @param indices must be sorted in ascending order */ export function dissocIndices(indices: Array) { return (as: Array): Array => { const index = last(indices); if (index === undefined) return as; return dissocIndices (dropRight(1, indices)) ( take(index)(as) .concat( drop(index + 1, as) ) ) as Array; } }