aboutsummaryrefslogtreecommitdiff
path: root/ext/js/data/anki-util.js
diff options
context:
space:
mode:
Diffstat (limited to 'ext/js/data/anki-util.js')
-rw-r--r--ext/js/data/anki-util.js117
1 files changed, 56 insertions, 61 deletions
diff --git a/ext/js/data/anki-util.js b/ext/js/data/anki-util.js
index 57684887..123e5d2f 100644
--- a/ext/js/data/anki-util.js
+++ b/ext/js/data/anki-util.js
@@ -18,72 +18,67 @@
import {isObject} from '../core/utilities.js';
+/** @type {RegExp} @readonly */
+const markerPattern = /\{([\w-]+)\}/g;
+
/**
- * This class has some general utility functions for working with Anki data.
+ * Gets the root deck name of a full deck name. If the deck is a root deck,
+ * the same name is returned. Nested decks are separated using '::'.
+ * @param {string} deckName A string of the deck name.
+ * @returns {string} A string corresponding to the name of the root deck.
*/
-export class AnkiUtil {
- /** @type {RegExp} @readonly */
- static _markerPattern = /\{([\w-]+)\}/g;
-
- /**
- * Gets the root deck name of a full deck name. If the deck is a root deck,
- * the same name is returned. Nested decks are separated using '::'.
- * @param {string} deckName A string of the deck name.
- * @returns {string} A string corresponding to the name of the root deck.
- */
- static getRootDeckName(deckName) {
- const index = deckName.indexOf('::');
- return index >= 0 ? deckName.substring(0, index) : deckName;
- }
+export function getRootDeckName(deckName) {
+ const index = deckName.indexOf('::');
+ return index >= 0 ? deckName.substring(0, index) : deckName;
+}
- /**
- * Checks whether or not any marker is contained in a string.
- * @param {string} string A string to check.
- * @returns {boolean} `true` if the text contains an Anki field marker, `false` otherwise.
- */
- static stringContainsAnyFieldMarker(string) {
- const result = this._markerPattern.test(string);
- this._markerPattern.lastIndex = 0;
- return result;
- }
+/**
+ * Checks whether or not any marker is contained in a string.
+ * @param {string} string A string to check.
+ * @returns {boolean} `true` if the text contains an Anki field marker, `false` otherwise.
+ */
+export function stringContainsAnyFieldMarker(string) {
+ const result = markerPattern.test(string);
+ markerPattern.lastIndex = 0;
+ return result;
+}
- /**
- * Gets a list of all markers that are contained in a string.
- * @param {string} string A string to check.
- * @returns {string[]} An array of marker strings.
- */
- static getFieldMarkers(string) {
- const pattern = this._markerPattern;
- const markers = [];
- while (true) {
- const match = pattern.exec(string);
- if (match === null) { break; }
- markers.push(match[1]);
- }
- return markers;
+/**
+ * Gets a list of all markers that are contained in a string.
+ * @param {string} string A string to check.
+ * @returns {string[]} An array of marker strings.
+ */
+export function getFieldMarkers(string) {
+ const pattern = markerPattern;
+ const markers = [];
+ while (true) {
+ const match = pattern.exec(string);
+ if (match === null) { break; }
+ markers.push(match[1]);
}
+ return markers;
+}
- /**
- * Returns a regular expression which can be used to find markers in a string.
- * @param {boolean} global Whether or not the regular expression should have the global flag.
- * @returns {RegExp} A new `RegExp` instance.
- */
- static cloneFieldMarkerPattern(global) {
- return new RegExp(this._markerPattern.source, global ? 'g' : '');
- }
+/**
+ * Returns a regular expression which can be used to find markers in a string.
+ * @param {boolean} global Whether or not the regular expression should have the global flag.
+ * @returns {RegExp} A new `RegExp` instance.
+ */
+export function cloneFieldMarkerPattern(global) {
+ return new RegExp(markerPattern.source, global ? 'g' : '');
+}
- /**
- * Checks whether or not a note object is valid.
- * @param {import('anki').Note} note A note object to check.
- * @returns {boolean} `true` if the note is valid, `false` otherwise.
- */
- static isNoteDataValid(note) {
- if (!isObject(note)) { return false; }
- const {fields, deckName, modelName} = note;
- return (
- typeof deckName === 'string' &&
- typeof modelName === 'string' &&
- Object.entries(fields).length > 0
- );
- }
+/**
+ * Checks whether or not a note object is valid.
+ * @param {import('anki').Note} note A note object to check.
+ * @returns {boolean} `true` if the note is valid, `false` otherwise.
+ */
+export function isNoteDataValid(note) {
+ if (!isObject(note)) { return false; }
+ const {fields, deckName, modelName} = note;
+ return (
+ typeof deckName === 'string' &&
+ typeof modelName === 'string' &&
+ Object.entries(fields).length > 0
+ );
}