Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function doSort(arrayname, sorting) {
- /*Function: Receives an array from a calling statement, format: js doSort("<arrayname>", 0/1);
- The function will sort the array in the following orders: 0 - ascending, 1 - descending
- pre: arrayname as a String, sorting as an Integer - 0 or 1
- post: Variable array with arrayname will be sorted in either ascending or descending order
- */
- var list = getVar(arrayname).toString().split("|");
- switch(sorting)
- {
- case 0:
- for(i = 0; i < list.length() - 1; i++)
- {
- if(list[i].localeCompare(list[i+1]) == 1) {
- var temp = list[i];
- list[i] = list[i+1];
- list[i+1] = temp;
- i = -1;
- }
- }
- break;
- case 1:
- for(i = 0; i < list.length() - 1; i++)
- {
- if(list[i].localeCompare(list[i+1]) == -1) {
- var temp = list[i];
- list[i] = list[i+1];
- list[i+1] = temp;
- i = -1;
- }
- }
- break;
- }
- setVar(arrayname,list.join("|"));
- }
- function findIndex(arrayname, srch) {
- /*Function: Receives an array from a calling statement, format: jscall <variable> findIndex("<arrayname>", "search string");
- The function will search the array for the matching string and return its index or -1 if not found
- pre: arrayname as a String, srch as a String
- post: function will return the index of the search string in the variable array with array name or -1 if string is not found
- */
- var list = getVar(arrayname).toString().split("|");
- for(i = 0; i < list.length();i++)
- {
- if(list[i].localeCompare(srch) == 0)
- {
- return i;
- }
- }
- return -1;
- }
- function checkExists(arrayname, srch) {
- /*Function: Receives an array from a calling statement, format: jscall <variable> checkExists("<arrayname>", "search string");
- The function will search the array for the matching string and return 1 if it exists or 0 if it does not.
- pre: arrayname as a String, srch as a String
- post: function will return 1 if the search string exists in the variable array with arrayname or 0 if string is not found
- */
- var list = getVar(arrayname).toString().split("|");
- for(i = 0; i < list.length();i++)
- {
- if(list[i].localeCompare(srch) == 0)
- {
- return 1;
- }
- }
- return 0;
- }
- function doInsert(arrayname, items, position) {
- /*Function: Receives an array from a calling statement, format: js doInsert("<arrayname>", "item(s) to insert", position to insert);
- The function will insert items into the array at the specified position.
- pre: arrayname as a String, items as a String or List (separated by |), position as Integer
- post: function will insert all items starting at specified index into the array and store the list in the variable array called arrayname.
- */
- var list = getVar(arrayname).toString().split("|");
- items = items.toString().split("|");
- var temp = list.slice(0,position);
- for(i = 0; i < items.length();i++)
- {
- temp.push(items[i]);
- }
- for(i = position; i < list.length();i++)
- {
- temp.push(list[i]);
- }
- list = temp;
- setVar(arrayname,list.join("|"));
- }
- function doRemove(arrayname, srch, amount) {
- /*Function: Receives an array from a calling statement, format: js doRemove("<arrayname>", String to remove, number of items to remove);
- The function will remove the specified number of items starting at the specified string (the first occurance of that string!).
- pre: arrayname as a String, srch as String, amount as Integer
- post: function will remove the specified number of items starting at index of the search string from array and store list in the variable array called arrayname.
- */
- var index = findIndex(arrayname, srch);
- var list = getVar(arrayname).toString().split("|");
- if(index == 0)
- {
- list = list.slice(1,list.length());
- } else if(index == -1){
- return;
- } else {
- var temp = list.slice(0,index);
- }
- for(i = index + 1; i < list.length();i++)
- {
- temp.push(list[i]);
- }
- list = temp;
- setVar(arrayname,list.join("|"));
- }
- function doConcat(arrayname, array) {
- /*Function: Receives two arrays from a calling statement, format: js doConcat("<arrayname>", "<array>")
- The function concats the second array onto the first and saves it in the array variable with arrayname. The second array can be an array or a list string.
- pre: arrayname as a String, array as String
- post: the second array will be concatenated onto the first array and stored in the array variable with arrayname
- */
- var list = getVar(arrayname).toString().split("|");
- array = array.toString().split("|");
- list = list.concat(array);
- setVar(arrayname,list.join("|"));
- }
- function doXConcat(arrayname, array) {
- /*Function: Receives two arrays from a calling statement, format: js doXConcat("<arrayname>", "<array>")
- The function concats items in the second array onto the first, provided they do not exist in the first array, and saves it in the array variable with arrayname.
- The second array can be an array or a list string.
- pre: arrayname as a String, array as String
- post: the second array will be concatenated onto the first array and stored in the array variable with arrayname, skipping duplicates
- */
- var list = getVar(arrayname).toString().split("|");
- array = array.toString().split("|");
- for(i = 0; i < array.length(); i++)
- {
- if(!checkExists(arrayname, array[i]))
- {
- list.push(array[i]);
- }
- }
- setVar(arrayname,list.join("|"));
- }
- function doXCompare(arraysrc, arraytrg, srch) {
- /*Function: Receives two arrays from a calling statement and a search string, format: jscall <variable> doXCompare("<arraysrc>", "<arraytrg>", "search string")
- The function will search the first array for the search string and set return the matching result from the target array to be stored in the specified variable.
- The funtion returns -1 if the search string does not exist.
- pre: arraysrc as a String, arraytrg as String, srch as String
- post: the value returned will be the position in the second array that corresponds to the position of the search string in the first array
- */
- var list = getVar(arraysrc).toString().split("|");
- var targ = getVar(arraytrg).toString().split("|");
- for(i = 0; i < list.length();i++)
- {
- if(list[i].localeCompare(srch) == 0)
- {
- return targ[i];
- }
- }
- return -1;
- }
- function doPush(arrayname, item) {
- /*Function: Receives an array and a string, format: js doPush("<arrayname>", "new item")
- The function will add the item string to the end of the array and store the new list in the variable with the name arrayname
- pre: arrayname as a String, item as a String
- post: the string item will be added to the array in the last position
- */
- var list = getVar(arrayname).toString().split("|");
- if (list[0] == "undefined")
- {
- list[0] = item;
- } else {
- list.push(item);
- }
- setVar(arrayname,list.join("|"));
- }
- function doPop(arrayname) {
- /*Function: Receives an array, format: jscall <variable> doPop("<arrayname>")
- The function will remove the last item from the array and return the value to be stored in the specified variable.
- The function will return 0 if the array is already empty.
- pre: arrayname as a String
- post: the last item removed from the array and stored in the variable from the calling statement
- */
- var list = getVar(arrayname).toString().split("|");
- if (list.length() > 0 && list[0] != "")
- {
- var temp = list[list.length() - 1];
- list.pop();
- setVar(arrayname,list.join("|"));
- return temp;
- } else {
- return 0;
- }
- }
- function doUnshift(arrayname, item) {
- /*Function: Receives an array and a string, format: js doUnshift("<arrayname>", "new item")
- The function will add the item string to the beginning of the array and store the new list in the variable with the name arrayname
- pre: arrayname as a String, item as a String
- post: the string item will be added to the array in the first position
- */
- var list = getVar(arrayname).toString().split("|");
- if (list[0] == "undefined")
- {
- list[0] = item;
- } else {
- list.unshift(item);
- }
- setVar(arrayname,list.join("|"));
- }
- function doShift(arrayname) {
- /*Function: Receives an array, format: jscall <variable> doShift("<arrayname>")
- The function will remove the first item from the array and return the value to be stored in the specified variable.
- The function will return 0 if the array is already empty.
- pre: arrayname as a String
- post: the first item removed from the array and stored in the variable from the calling statement
- */
- var list = getVar(arrayname).toString().split("|");
- if (list.length() > 0 && list[0] != "")
- {
- var temp = list[0];
- list.shift();
- setVar(arrayname,list.join("|"));
- return temp;
- } else {
- return 0;
- }
- }
- function buildArray(arrayname, raw) {
- /*Function: Receives string separated by commas, 'a's, 'an's and ands to build array, format: js buildArray("<arrayname>", "list")
- *Note* To build from action data you -must- capture the whole string at the same time to be sent to the function!
- Actions with Javascript in them will only trigger -once- per instance of the pattern in a line.
- pre: arrayname as a String, list as a String
- post: the list is returned as a Genie formatted array stored in the array with arrayname
- */
- raw = raw.toString().replace(/, /g , "|").replace(/ and (a|an) /, "|").replace(/\ban? /g, "");
- setVar(arrayname,raw);
- }
- function buildArrayStr(arrayname, raw, pattern) {
- /*Function: Receives string separated by commas, 'a's, 'an's and ands to build array and a string, format: js buildArrayStr("<arrayname>", "list", "pattern")
- *Note* To build from action data you -must- capture the whole string at the same time to be sent to the function!
- Actions with Javascript in them will only trigger -once- per instance of the pattern in a line.
- Function will return an array of only the items that contain the target string. Must not be regex!
- pre: arrayname as a String, list as a String, pattern as a String.
- post: the list of items with the selected string is returned as a Genie formatted array stored in the array with arrayname
- */
- raw = raw.toString().replace(/, /g , "|").replace(/ and (a|an) /, "|").replace(/\ban? /g, "");
- raw = raw.split("|");
- var list = [];
- pattern = new RegExp(pattern,"i");
- for(i = 0; i < raw.length();i++)
- {
- if(pattern.test(raw[i]))
- {
- list.push(raw[i]);
- }
- }
- setVar(arrayname,list.join("|"));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement