[nouns][terms][inclusive] fix sorting

This commit is contained in:
Avris 2021-07-03 13:19:43 +02:00
parent 84a4114ecf
commit 4b268fa581
5 changed files with 30 additions and 18 deletions

View File

@ -179,7 +179,7 @@
<script>
import { InclusiveEntry } from "~/src/classes";
import { buildDict, clearUrl } from "../src/helpers";
import { buildDict, clearUrl, clearLinkedText } from "../src/helpers";
import hash from "../plugins/hash";
export default {
@ -268,7 +268,7 @@
if (!a.approved && b.approved) {
return -1;
}
return a.insteadOf.toLowerCase().localeCompare(b.insteadOf.toLowerCase());
return clearLinkedText(a.insteadOf.toLowerCase()).localeCompare(clearLinkedText(b.insteadOf.toLowerCase()));
});
for (let w of sorted) {
yield [w.id, new InclusiveEntry(w)];

View File

@ -128,7 +128,7 @@
<script>
import { TermsEntry } from "~/src/classes";
import { buildDict, clearUrl } from "../src/helpers";
import { buildDict, clearUrl, clearLinkedText } from "../src/helpers";
import hash from "../plugins/hash";
export default {
@ -217,7 +217,9 @@
if (!a.approved && b.approved) {
return -1;
}
return a.term.toLowerCase().localeCompare(b.term.toLowerCase());
return clearLinkedText(a.term.toLowerCase()).localeCompare(
clearLinkedText(b.term.toLowerCase())
);
});
for (let w of sorted) {
yield [w.id, new TermsEntry(w)];

View File

@ -1,7 +1,7 @@
import { Router } from 'express';
import SQL from 'sql-template-strings';
import {ulid} from "ulid";
import {isTroll, handleErrorAsync} from "../../src/helpers";
import {isTroll, handleErrorAsync, sortClearedLinkedText} from "../../src/helpers";
import { caches } from "../../src/cache";
const approve = async (db, id) => {
@ -25,20 +25,19 @@ const router = Router();
router.get('/inclusive', handleErrorAsync(async (req, res) => {
return res.json(await caches.inclusive.fetch(async () => {
return await req.db.all(SQL`
return sortClearedLinkedText(await req.db.all(SQL`
SELECT i.*, u.username AS author FROM inclusive i
LEFT JOIN users u ON i.author_id = u.id
WHERE i.locale = ${global.config.locale}
AND i.approved >= ${req.isGranted('inclusive') ? 0 : 1}
AND i.deleted = 0
ORDER BY i.approved, i.insteadOf
`);
`), 'insteadOf');
}));
}));
router.get('/inclusive/search/:term', handleErrorAsync(async (req, res) => {
const term = '%' + req.params.term + '%';
return res.json(await req.db.all(SQL`
return res.json(sortClearedLinkedText(await req.db.all(SQL`
SELECT i.*, u.username AS author FROM inclusive i
LEFT JOIN users u ON i.author_id = u.id
WHERE i.locale = ${global.config.locale}
@ -46,7 +45,7 @@ router.get('/inclusive/search/:term', handleErrorAsync(async (req, res) => {
AND i.deleted = 0
AND (i.insteadOf like ${term} OR i.say like ${term})
ORDER BY i.approved, i.insteadOf
`));
`), 'insteadOf'));
}));
router.post('/inclusive/submit', handleErrorAsync(async (req, res) => {

View File

@ -1,7 +1,7 @@
import { Router } from 'express';
import SQL from 'sql-template-strings';
import {ulid} from "ulid";
import {isTroll, handleErrorAsync} from "../../src/helpers";
import {isTroll, handleErrorAsync, sortClearedLinkedText} from "../../src/helpers";
import { caches } from "../../src/cache";
const approve = async (db, id) => {
@ -24,29 +24,27 @@ const approve = async (db, id) => {
const router = Router();
router.get('/terms', handleErrorAsync(async (req, res) => {
return res.json(await caches.terms.fetch(() => {
return req.db.all(SQL`
return res.json(await caches.terms.fetch(async () => {
return sortClearedLinkedText(await req.db.all(SQL`
SELECT i.*, u.username AS author FROM terms i
LEFT JOIN users u ON i.author_id = u.id
WHERE i.locale = ${global.config.locale}
AND i.approved >= ${req.isGranted('terms') ? 0 : 1}
AND i.deleted = 0
ORDER BY i.term
`);
`), 'term');
}));
}));
router.get('/terms/search/:term', handleErrorAsync(async (req, res) => {
const term = '%' + req.params.term + '%';
return res.json(await req.db.all(SQL`
return res.json(sortClearedLinkedText(await req.db.all(SQL`
SELECT i.*, u.username AS author FROM terms i
LEFT JOIN users u ON i.author_id = u.id
WHERE i.locale = ${global.config.locale}
AND i.approved >= ${req.isGranted('terms') ? 0 : 1}
AND i.deleted = 0
AND (i.term like ${term} OR i.original like ${term})
ORDER BY i.term
`));
`)), 'term');
}));
router.post('/terms/submit', handleErrorAsync(async (req, res) => {

View File

@ -194,3 +194,16 @@ export const isGranted = (user, locale, area) => {
export const handleErrorAsync = func => (req, res, next) => {
func(req, res, next).catch((error) => next(error));
};
export const clearLinkedText = (text) => {
return text
.replace(/{[^}=]+=([^}=]+)}/g, '$1')
.replace(/{([^}=]+)}/g, '$1')
.replace(/[„”"']/g, '')
;
}
export const sortClearedLinkedText = (items, key) => {
items.sort((a, b) => clearLinkedText(a[key].toLowerCase()).localeCompare(clearLinkedText(b[key].toLowerCase())))
return items;
}