JD2022-TU1/data/EngineData/SubmitAssistant/util/utils.js

75 lines
No EOL
2.2 KiB
JavaScript

define({
/**
* Get a parameter by name from window.location.search
*
* @param name - the name of the parameter
*
* @return - the value of the parameter
*/
getParameterByName: function(name) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
},
/**
* Parse text file used to store tags.
*
* @param txt_file - the text file to parse
* @param treatment_callback - function to apply to a tag.
* Take as parameter the tag to treat.
* Return the treated tag (See trimOfficialMap).
*
* @return - an array containing all the valid tags found
*/
parseTags: function(txt_file, treatment_callback){
var tab_tags = [];
//get all lines of txt file in a tab
var tab_lines = txt_file.split("\n").sort();
for(var i = 0, length = tab_lines.length ; i < length ; i++){
var tag = $.trim(tab_lines[i]);
//test if tag is correct
if(/^[\w,/.]+$/.test(tag)){
if(treatment_callback){
var value = treatment_callback(tag);
if (!value) {
continue;
}
tag = value;
}
tab_tags.push(tag);
}
}
return tab_tags;
},
/**
* Trim the useless parts of official map name.
*
* @param map_name - the name of the map to trim.
*
* @return the treated map_name
*/
trimOfficialMap: function(map_name){
//remove path
map_name = map_name.substr(map_name.lastIndexOf("/") + 1, map_name.length);
//remove extension
map_name = map_name.substr(0, map_name.indexOf("."));
//remove useless end of name ("MAIN_SCENE")
if(map_name.split("_").length > 2){
map_name = map_name.substr(0, map_name.indexOf("_"));
}
return map_name;
},
});