epic-formulae
Version:
Simple data store to house epic formulae
48 lines (47 loc) • 1.75 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Formula = void 0;
/* eslint-disable unicorn/prefer-spread */
const mongoose_1 = require("mongoose");
const schema = new mongoose_1.Schema({
text: { type: String, index: { unique: true } },
metre: String,
referent: { type: String, index: true },
metricalSortKey: { type: String, index: true },
}, { collection: 'formulae' });
const prettySymbol = (char) => {
switch (char) {
case 'u':
return '◡';
case '-':
return '—';
default:
throw new Error(`Invalid character ${char}`);
}
};
const generateMetricalSortKey = (metre) => metre
.slice(0, -1) // Remove the last syllable as it doesn't matter (brevis in longo)
.replace(/[()]/g, '') // Remove parentheses (from initial ghost syllable)
.split('')
.reverse() // Right-to-left sorting on metre
.join('');
schema.pre(['updateOne', 'findOneAndUpdate'], function (next) {
const data = this.getUpdate();
data.metricalSortKey = generateMetricalSortKey(data.metre);
next();
});
schema.pre('save', function (next) {
this.metricalSortKey = generateMetricalSortKey(this.metre);
next();
});
schema.virtual('prettyMetreForGrouping').get(function () {
return this.metre
.replace(/[()]/g, '') // Remove parentheses (from initial ghost syllable)
.split('')
.slice(0, -1) // Remove the last syllable (to be replaced with ×)
.map((char) => prettySymbol(char))
.concat(['×']) // Final syllable can be either, brevis in longo
.join(' '); // Space out the characters
});
const Formula = (0, mongoose_1.model)('Formula', schema);
exports.Formula = Formula;