use nested search function for secretariat.global.search

This commit is contained in:
buzz-lightsnack-2007 2024-05-25 00:01:19 +08:00
parent 0c52ce4d08
commit a377d28da8

View file

@ -73,79 +73,13 @@ class global {
@param {object} OPTIONS the options
@return {object} the results
*/
static async search(SOURCE, TERM, ADDITIONAL_PLACES, STRICT = 0, OPTIONS = {}) {
static async search(SOURCE, TERM, ADDITIONAL_PLACES, OPTIONS) {
// Set the default options.
OPTIONS = Object.assign({}, {"strictness": 0, "criteria": ADDITIONAL_PLACES}, OPTIONS);
// Initialize the data.
let DATA = await global.read(SOURCE, (OPTIONS[`cloud`] != null) ? OPTIONS[`cloud`] : 0);
let RESULTS;
if (DATA) {
RESULTS = {};
if (TERM && (!(typeof ADDITIONAL_PLACES).includes(`str`) || !ADDITIONAL_PLACES)) {
// Sequentially search through the data, first by key.
(Object.keys(DATA)).forEach((DATA_NAME) => {
if (STRICT ? DATA_NAME == TERM : (DATA_NAME.includes(TERM) || TERM.includes(DATA_NAME))) {
RESULTS[DATA_NAME] = DATA[DATA_NAME];
}
});
// Then, get the additional places.
if ((ADDITIONAL_PLACES != null ? Array.isArray(ADDITIONAL_PLACES) : false) ? ADDITIONAL_PLACES.length > 0 : false) {
ADDITIONAL_PLACES.forEach((ADDITIONAL_PLACE) => {
// Recursively search
RESULTS = Object.assign({}, RESULTS, global.search(SOURCE, TERM, ADDITIONAL_PLACE, STRICT));
})
}
} else if (((typeof ADDITIONAL_PLACES).includes(`str`) && (ADDITIONAL_PLACES)) ? ADDITIONAL_PLACES.trim() : false) {
// Perform a sequential search on the data.
if ((typeof DATA).includes(`obj`) && !Array.isArray(DATA) && SOURCE != null) {
let VALUE = {};
for (let DICTIONARY_INDEX = 0; DICTIONARY_INDEX < (Object.keys(DATA)).length; DICTIONARY_INDEX++) {
VALUE[`parent`] = DATA[(Object.keys(DATA))[DICTIONARY_INDEX]];
/* Test for a valid RegEx.
@param {string} item the item to test
*/
function isRegEx(item) {
let RESULT = {};
RESULT[`state`] = false;
try {
RESULT[`expression`] = new RegExp(item);
RESULT[`state`] = true;
} catch(err) {};
return (RESULT[`state`]);
};
if (((typeof VALUE[`parent`]).includes(`obj`) && !Array.isArray(VALUE[`parent`]) && VALUE[`parent`] != null) ? (Object.keys(VALUE[`parent`])).length > 0 : false) {
VALUE[`current`] = VALUE[`parent`][ADDITIONAL_PLACES];
}
if (VALUE[`current`] ? ((STRICT >= 1) ? VALUE[`current`] == TERM : (((STRICT < 0.5) ? (VALUE[`current`].includes(TERM)) : false) || TERM.includes(VALUE[`current`]) || (isRegEx(VALUE[`current`]) ? (new RegExp(VALUE[`current`])).test(TERM) : false))) : false) {
// Add the data.
RESULTS[(Object.keys(DATA))[DICTIONARY_INDEX]] = (Object.entries(DATA))[DICTIONARY_INDEX][1];
};
};
} else {
for (let ELEMENT_INDEX = 0; ELEMENT_INDEX < DATA.length; ELEMENT_INDEX++) {
if (
((STRICT || (typeof DATA[ELEMENT_INDEX]).includes(`num`)) && DATA[ELEMENT_INDEX] == TERM) ||
((!STRICT && !((typeof DATA[ELEMENT_INDEX]).includes(`num`)))
? (TERM.includes(DATA[ELEMENT_INDEX]) || DATA[ELEMENT_INDEX].includes(TERM) ||
(typeof(DATA[ELEMENT_INDEX])).includes(`str`)
? new RegExp(DATA[ELEMENT_INDEX]).test(TERM)
: false
) : false
)
) {
RESULTS[SOURCE] = DATA;
break;
}
}
}
}
}
let RESULTS = nested.dictionary.search(DATA, TERM, OPTIONS);;
return RESULTS;
};
@ -167,19 +101,19 @@ class global {
DATA_CHECK[`state`] = await compare([...NAME], DATA);
(!DATA_CHECK[`state`])
? logging.error((new texts(`error_msg_save_failed`)).localized, NAME.join(``), JSON.stringify(DATA))
? GUI_INFO[`log`] = logging.error((new texts(`error_msg_save_failed`)).localized, NAME.join(``), JSON.stringify(DATA))
: ((((typeof OPTIONS).includes(`obj`) && OPTIONS != null) ? (!(!!OPTIONS[`silent`])) : true)
? new logging (new texts(`saving_done`).localized)
? GUI_INFO[`log`] = new logging (new texts(`saving_done`).localized)
: false);
return (DATA_CHECK[`state`]);
}
let DATA_ALL;
let DATA_ALL, GUI_INFO = {};
// Inform the user that saving is in progress.
if (((typeof OPTIONS).includes(`obj`) && OPTIONS != null) ? (!(!!OPTIONS[`silent`])) : true) {
let LOG = new logging ((new texts(`saving_current`)).localized, (new texts(`saving_current_message`)).localized, false)
GUI_INFO[`log`] = new logging ((new texts(`saving_current`)).localized, (new texts(`saving_current_message`)).localized, false)
};
// Get all data and set a blank value if it doesn't exist yet.
@ -203,7 +137,7 @@ class global {
// Write!
chrome.storage[(CLOUD > 0) ? `sync` : `local`].set(DATA_INJECTED);
(typeof LOG).includes(`undef`) ? false : LOG.clear();
GUI_INFO[`log`] ? GUI_INFO[`log`].clear() : false;
return ((OPTIONS[`verify`] != null ? (OPTIONS[`verify`]) : true) ? verify(DATA_NAME, data) : true);
}
@ -297,66 +231,7 @@ class global {
return CONFIRMATION;
}
}
class session {
/*
Recall session storage data.
@param {string} path the path to the data
@return {object} the data
*/
static async read(path) {
// Change PATH to array if it isn't.
let PATH = (!(Array.isArray(path)) && path && path != undefined)
? String(path).trim().split(",")
: ((path != null) ? path : []);
// Prepare data.
let DATA = {};
DATA[`all`] = await chrome.storage.session.get(null);
(DATA[`all`]) ? DATA[`selected`] = nested.dictionary.get(DATA[`all`], [...PATH]) : false;
return (DATA[`selected`]);
}
/*
Write the data to a specified path.
@param {string} PATH the path to the data
@param {object} DATA the data to be written
*/
static async write(PATH, DATA) {
async function verify (NAME, DATA) {
let DATA_CHECK = {};
// Verify the presence of the data.
DATA_CHECK[`state`] = await compare(null, [await session.read([...NAME]), DATA]);
// Only notify when writing failed.
(!DATA_CHECK[`state`])
? logging.error((new texts(`error_msg_save_failed`)).localized, NAME.join(``), JSON.stringify(DATA))
: true;
return (DATA_CHECK[`state`]);
}
DATA = {"write": DATA};
DATA[`all`] = await session.read(null);
((DATA[`all`] != null && (typeof DATA[`all`]).includes(`obj`)) ? Object.keys(DATA[`all`]).length <= 0 : true)
? DATA[`all`] = {}
: false;
let TARGET = (!(typeof PATH).includes(`obj`)) ? String(PATH).trim().split(",") : PATH;
// Merge!
DATA[`inject`] = nested.dictionary.set(DATA[`all`], [...TARGET], DATA[`write`]);
// Write!
chrome.storage.session.set(DATA[`inject`]);
return(await verify(TARGET, DATA[`write`]));
}
}
};
/*
Compare a data against the stored data. Useful when comparing dictionaries.
@ -553,4 +428,4 @@ export function observe(callback) {
}));
}
export {global, session, template, managed, background};
export {global, template, managed, background};