« get me outta code hell

hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/data/things/sorting-rule.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/data/things/sorting-rule.js')
-rw-r--r--src/data/things/sorting-rule.js36
1 files changed, 23 insertions, 13 deletions
diff --git a/src/data/things/sorting-rule.js b/src/data/things/sorting-rule.js
index b169a541..101a4966 100644
--- a/src/data/things/sorting-rule.js
+++ b/src/data/things/sorting-rule.js
@@ -3,7 +3,7 @@ export const SORTING_RULE_DATA_FILE = 'sorting-rules.yaml';
 import {readFile, writeFile} from 'node:fs/promises';
 import * as path from 'node:path';
 
-import {input} from '#composite';
+import {V} from '#composite';
 import {chunkByProperties, compareArrays, unique} from '#sugar';
 import Thing from '#thing';
 import {isObject, isStringNonEmpty, anyOf, strictArrayOf} from '#validators';
@@ -22,6 +22,7 @@ import {
   reorderDocumentsInYAMLSourceText,
 } from '#yaml';
 
+import {exposeConstant} from '#composite/control-flow';
 import {flag} from '#composite/wiki-properties';
 
 function isSelectFollowingEntry(value) {
@@ -37,16 +38,21 @@ function isSelectFollowingEntry(value) {
 
 export class SortingRule extends Thing {
   static [Thing.friendlyName] = `Sorting Rule`;
+  static [Thing.wikiData] = 'sortingRules';
 
   static [Thing.getPropertyDescriptors] = () => ({
     // Update & expose
 
-    active: flag(true),
+    active: flag(V(true)),
 
     message: {
       flags: {update: true, expose: true},
       update: {validate: isStringNonEmpty},
     },
+
+    // Expose only
+
+    isSortingRule: exposeConstant(V(true)),
   });
 
   static [Thing.yamlDocumentSpec] = {
@@ -68,8 +74,6 @@ export class SortingRule extends Thing {
       (document['Sort Documents']
         ? DocumentSortingRule
         : null),
-
-    save: (results) => ({sortingRules: results}),
   });
 
   check(opts) {
@@ -119,17 +123,21 @@ export class ThingSortingRule extends SortingRule {
         validate: strictArrayOf(isStringNonEmpty),
       },
     },
+
+    // Expose only
+
+    isThingSortingRule: exposeConstant(V(true)),
   });
 
-  static [Thing.yamlDocumentSpec] = Thing.extendDocumentSpec(SortingRule, {
+  static [Thing.yamlDocumentSpec] = {
     fields: {
       'By Properties': {property: 'properties'},
     },
-  });
+  };
 
   sort(sortable) {
     if (this.properties) {
-      for (const property of this.properties.slice().reverse()) {
+      for (const property of this.properties.toReversed()) {
         const get = thing => thing[property];
         const lc = property.toLowerCase();
 
@@ -218,9 +226,13 @@ export class DocumentSortingRule extends ThingSortingRule {
       flags: {update: true, expose: true},
       update: {validate: isStringNonEmpty},
     },
+
+    // Expose only
+
+    isDocumentSortingRule: exposeConstant(V(true)),
   });
 
-  static [Thing.yamlDocumentSpec] = Thing.extendDocumentSpec(ThingSortingRule, {
+  static [Thing.yamlDocumentSpec] = {
     fields: {
       'Sort Documents': {property: 'filename'},
       'Select Documents Following': {property: 'selectDocumentsFollowing'},
@@ -233,7 +245,7 @@ export class DocumentSortingRule extends ThingSortingRule {
         'Select Documents Under',
       ]},
     ],
-  });
+  };
 
   static async apply(rule, {wikiData, dataPath, dry}) {
     const oldLayout = getThingLayoutForFilename(rule.filename, wikiData);
@@ -261,10 +273,8 @@ export class DocumentSortingRule extends ThingSortingRule {
   }
 
   static async* applyAll(rules, {wikiData, dataPath, dry}) {
-    rules =
-      rules
-        .slice()
-        .sort((a, b) => a.filename.localeCompare(b.filename, 'en'));
+    rules = rules
+      .toSorted((a, b) => a.filename.localeCompare(b.filename, 'en'));
 
     for (const {chunk, filename} of chunkByProperties(rules, ['filename'])) {
       const initialLayout = getThingLayoutForFilename(filename, wikiData);