Skip to content
Snippets Groups Projects
findSlave.js 3.02 KiB
Newer Older
  • Learn to ignore specific revisions
  • App.FindSlave = {};
    
    /**
     * Fragment searching: See if every needle can found somewhere in the field of haystacks
     * @param {string[]} haystacks
     * @param {RegExp[]} needles
     * @returns {boolean}
     */
    App.FindSlave._fragmentSearch = function(haystacks, needles) {
    
    klorpa's avatar
    klorpa committed
    	const hs = haystacks.join(" ");
    	return needles.every((needle) => { return needle.test(hs); });
    
    svornost's avatar
    svornost committed
     * Get slave ids which match a predicate
    
    svornost's avatar
    svornost committed
     * @param {function(App.Entity.SlaveState): boolean} predicate
    
    App.FindSlave._slaveIDs = function(predicate) {
    	return V.slaves.reduce((acc, slave) => {
    
    klorpa's avatar
    klorpa committed
    		if (predicate(createReadonlyProxy(slave))) {
    
    			acc.push(slave.ID);
    
    klorpa's avatar
    klorpa committed
    		}
    		return acc;
    	}, []);
    
    /**
     * Display a list of results, or text indicating that there were none
     * @param {number[]} ids
     * @param {DocumentFragment} frag
     */
    App.FindSlave._appendResultList = function(ids, frag) {
    	if (ids.length === 0) {
    		App.UI.DOM.appendNewElement("p", frag, "No matching slaves.");
    	} else {
    		frag.appendChild(App.UI.SlaveList.render.listDOM(ids, [], App.UI.SlaveList.SlaveInteract.stdInteract));
    	}
    };
    
    
    /**
     * Generate a slave list as the result of fragment searching all the name-type fields
     * @param {string} query
     * @returns {DocumentFragment}
     */
    App.FindSlave.searchByName = function(query) {
    
    	const frag = document.createDocumentFragment();
    	if (query) {
    		const resultTitle = App.UI.DOM.appendNewElement("p", frag, "Query results for name: ");
    		App.UI.DOM.appendNewElement("code", resultTitle, query);
    		const needles = query.split(" ").map((needle) => { return new RegExp(needle, "i"); });
    		const ids = this._slaveIDs((slave) => { return this._fragmentSearch([slave.slaveName, slave.slaveSurname, slave.birthName, slave.birthSurname], needles); });
    		this._appendResultList(ids, frag);
    	}
    	return frag;
    
    };
    
    /**
     * Generate a slave list as the result of fragment searching profession and origin
     * @param {string} query
     * @returns {DocumentFragment}
     */
    App.FindSlave.searchByBackground = function(query) {
    
    	const frag = document.createDocumentFragment();
    	if (query) {
    		const resultTitle = App.UI.DOM.appendNewElement("p", frag, "Query results for background: ");
    		App.UI.DOM.appendNewElement("code", resultTitle, query);
    		const needles = query.split(" ").map((needle) => { return new RegExp(needle, "i"); });
    		const ids = this._slaveIDs((slave) => { return this._fragmentSearch([slave.career, slave.origin], needles); });
    		this._appendResultList(ids, frag);
    	}
    	return frag;
    
    };
    
    /**
     * Generate a slave list as the result of evaluating an expression
     * @param {string} query
     * @returns {DocumentFragment}
     */
    App.FindSlave.searchByExpression = function(query) {
    
    	const frag = document.createDocumentFragment();
    	if (query) {
    		const resultTitle = App.UI.DOM.appendNewElement("p", frag, "Query results from expression: ");
    		App.UI.DOM.appendNewElement("code", resultTitle, query);
    		const pred = new Function("slave", "return (" + query + ");");
    		const ids = runWithReadonlyProxy(() => { return this._slaveIDs(pred); });
    		this._appendResultList(ids, frag);
    	}
    	return frag;