diff options
author | (quasar) nebula <qznebula@protonmail.com> | 2025-02-22 21:29:41 -0400 |
---|---|---|
committer | (quasar) nebula <qznebula@protonmail.com> | 2025-02-22 21:29:41 -0400 |
commit | 485efaccad4d2283169f0c74b6c4cd2b6a9f64ba (patch) | |
tree | 64fb9f3694f742c1ae071783f1d757f908d22143 /src | |
parent | 3a88bab962ca0d7613e9fd5e3146be195bcb0e70 (diff) |
data: DocumentSortingRule: multiple selectDocumentsFollowing
Diffstat (limited to 'src')
-rw-r--r-- | src/data/things/sorting-rule.js | 72 |
1 files changed, 43 insertions, 29 deletions
diff --git a/src/data/things/sorting-rule.js b/src/data/things/sorting-rule.js index 18da452e..0e3a2b0a 100644 --- a/src/data/things/sorting-rule.js +++ b/src/data/things/sorting-rule.js @@ -6,7 +6,7 @@ import * as path from 'node:path'; import {input} from '#composite'; import {chunkByProperties, compareArrays, unique} from '#sugar'; import Thing from '#thing'; -import {isObject, isStringNonEmpty, strictArrayOf} from '#validators'; +import {isObject, isStringNonEmpty, anyOf, strictArrayOf} from '#validators'; import { compareCaseLessSensitive, @@ -24,6 +24,17 @@ import { import {flag} from '#composite/wiki-properties'; +function isSelectFollowingEntry(value) { + isObject(value); + + const {length} = Object.keys(value); + if (length !== 1) { + throw new Error(`Expected object with 1 key, got ${length}`); + } + + return true; +} + export class SortingRule extends Thing { static [Thing.friendlyName] = `Sorting Rule`; @@ -186,16 +197,17 @@ export class DocumentSortingRule extends ThingSortingRule { flags: {update: true, expose: true}, update: { - validate(value) { - isObject(value); - - const {length} = Object.keys(value); - if (length !== 1) { - throw new Error(`Expected object with 1 key, got ${length}`); - } + validate: + anyOf( + isSelectFollowingEntry, + strictArrayOf(isSelectFollowingEntry)), + }, - return true; - }, + compute: { + transform: value => + (Array.isArray(value) + ? value + : [value]), }, }, @@ -317,31 +329,33 @@ export class DocumentSortingRule extends ThingSortingRule { } if (this.selectDocumentsFollowing) { - const [field, value] = Object.entries(this.selectDocumentsFollowing)[0]; + for (const entry of this.selectDocumentsFollowing) { + const [field, value] = Object.entries(entry)[0]; - const after = - sortable.findIndex(thing => - thing[Thing.yamlSourceDocument][field] === value); + const after = + sortable.findIndex(thing => + thing[Thing.yamlSourceDocument][field] === value); - const different = - after + - sortable - .slice(after) - .findIndex(thing => - Object.hasOwn(thing[Thing.yamlSourceDocument], field) && - thing[Thing.yamlSourceDocument][field] !== value); + const different = + after + + sortable + .slice(after) + .findIndex(thing => + Object.hasOwn(thing[Thing.yamlSourceDocument], field) && + thing[Thing.yamlSourceDocument][field] !== value); - const before = - (different === -1 - ? sortable.length - : different); + const before = + (different === -1 + ? sortable.length + : different); - const subsortable = - sortable.slice(after + 1, before); + const subsortable = + sortable.slice(after + 1, before); - this.sort(subsortable); + this.sort(subsortable); - sortable.splice(after + 1, before - after - 1, ...subsortable); + sortable.splice(after + 1, before - after - 1, ...subsortable); + } } else if (this.selectDocumentsUnder) { const field = this.selectDocumentsUnder; |