2021-02-25 02:54:58 +00:00
|
|
|
/*
|
2022-02-03 01:43:10 +00:00
|
|
|
* Copyright (C) 2021-2022 Yomichan Authors
|
2021-02-25 02:54:58 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class has some general utility functions for working with Anki data.
|
|
|
|
*/
|
|
|
|
class AnkiUtil {
|
|
|
|
/**
|
|
|
|
* 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 '::'.
|
2022-05-20 14:28:38 +00:00
|
|
|
* @param {string} deckName A string of the deck name.
|
|
|
|
* @returns {string} A string corresponding to the name of the root deck.
|
2021-02-25 02:54:58 +00:00
|
|
|
*/
|
|
|
|
static 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.
|
2022-05-20 14:28:38 +00:00
|
|
|
* @param {string} string A string to check.
|
|
|
|
* @returns {boolean} `true` if the text contains an Anki field marker, `false` otherwise.
|
2021-02-25 02:54:58 +00:00
|
|
|
*/
|
|
|
|
static stringContainsAnyFieldMarker(string) {
|
|
|
|
const result = this._markerPattern.test(string);
|
|
|
|
this._markerPattern.lastIndex = 0;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a list of all markers that are contained in a string.
|
2022-05-20 14:28:38 +00:00
|
|
|
* @param {string} string A string to check.
|
|
|
|
* @returns {string[]} An array of marker strings.
|
2021-02-25 02:54:58 +00:00
|
|
|
*/
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a regular expression which can be used to find markers in a string.
|
2022-05-20 14:28:38 +00:00
|
|
|
* @param {boolean} global Whether or not the regular expression should have the global flag.
|
|
|
|
* @returns {RegExp} A new `RegExp` instance.
|
2021-02-25 02:54:58 +00:00
|
|
|
*/
|
|
|
|
static cloneFieldMarkerPattern(global) {
|
|
|
|
return new RegExp(this._markerPattern.source, global ? 'g' : '');
|
|
|
|
}
|
2021-02-25 03:23:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether or not a note object is valid.
|
2022-05-20 14:28:38 +00:00
|
|
|
* @param {*} note A note object to check.
|
|
|
|
* @returns {boolean} `true` if the note is valid, `false` otherwise.
|
2021-02-25 03:23:40 +00:00
|
|
|
*/
|
|
|
|
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
|
|
|
|
);
|
|
|
|
}
|
2021-02-25 02:54:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// eslint-disable-next-line no-underscore-dangle
|
|
|
|
AnkiUtil._markerPattern = /\{([\w-]+)\}/g;
|