Advertisement
Guest User

Untitled

a guest
Feb 26th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.53 KB | None | 0 0
  1. function doSort(arrayname, sorting) {
  2. /*Function: Receives an array from a calling statement, format: js doSort("<arrayname>", 0/1);
  3. The function will sort the array in the following orders: 0 - ascending, 1 - descending
  4. pre: arrayname as a String, sorting as an Integer - 0 or 1
  5. post: Variable array with arrayname will be sorted in either ascending or descending order
  6. */
  7. var list = getVar(arrayname).toString().split("|");
  8. switch(sorting)
  9. {
  10. case 0:
  11. for(i = 0; i < list.length() - 1; i++)
  12. {
  13. if(list[i].localeCompare(list[i+1]) == 1) {
  14. var temp = list[i];
  15. list[i] = list[i+1];
  16. list[i+1] = temp;
  17. i = -1;
  18. }
  19. }
  20. break;
  21. case 1:
  22. for(i = 0; i < list.length() - 1; i++)
  23. {
  24. if(list[i].localeCompare(list[i+1]) == -1) {
  25. var temp = list[i];
  26. list[i] = list[i+1];
  27. list[i+1] = temp;
  28. i = -1;
  29. }
  30. }
  31. break;
  32. }
  33. setVar(arrayname,list.join("|"));
  34. }
  35.  
  36. function findIndex(arrayname, srch) {
  37. /*Function: Receives an array from a calling statement, format: jscall <variable> findIndex("<arrayname>", "search string");
  38. The function will search the array for the matching string and return its index or -1 if not found
  39. pre: arrayname as a String, srch as a String
  40. post: function will return the index of the search string in the variable array with array name or -1 if string is not found
  41. */
  42. var list = getVar(arrayname).toString().split("|");
  43. for(i = 0; i < list.length();i++)
  44. {
  45. if(list[i].localeCompare(srch) == 0)
  46. {
  47. return i;
  48. }
  49. }
  50. return -1;
  51. }
  52.  
  53. function checkExists(arrayname, srch) {
  54. /*Function: Receives an array from a calling statement, format: jscall <variable> checkExists("<arrayname>", "search string");
  55. The function will search the array for the matching string and return 1 if it exists or 0 if it does not.
  56. pre: arrayname as a String, srch as a String
  57. post: function will return 1 if the search string exists in the variable array with arrayname or 0 if string is not found
  58. */
  59. var list = getVar(arrayname).toString().split("|");
  60. for(i = 0; i < list.length();i++)
  61. {
  62. if(list[i].localeCompare(srch) == 0)
  63. {
  64. return 1;
  65. }
  66. }
  67. return 0;
  68. }
  69.  
  70. function doInsert(arrayname, items, position) {
  71. /*Function: Receives an array from a calling statement, format: js doInsert("<arrayname>", "item(s) to insert", position to insert);
  72. The function will insert items into the array at the specified position.
  73. pre: arrayname as a String, items as a String or List (separated by |), position as Integer
  74. post: function will insert all items starting at specified index into the array and store the list in the variable array called arrayname.
  75. */
  76. var list = getVar(arrayname).toString().split("|");
  77. items = items.toString().split("|");
  78. var temp = list.slice(0,position);
  79. for(i = 0; i < items.length();i++)
  80. {
  81. temp.push(items[i]);
  82. }
  83. for(i = position; i < list.length();i++)
  84. {
  85. temp.push(list[i]);
  86. }
  87. list = temp;
  88. setVar(arrayname,list.join("|"));
  89. }
  90.  
  91. function doRemove(arrayname, srch, amount) {
  92. /*Function: Receives an array from a calling statement, format: js doRemove("<arrayname>", String to remove, number of items to remove);
  93. The function will remove the specified number of items starting at the specified string (the first occurance of that string!).
  94. pre: arrayname as a String, srch as String, amount as Integer
  95. 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.
  96. */
  97. var index = findIndex(arrayname, srch);
  98. var list = getVar(arrayname).toString().split("|");
  99. if(index == 0)
  100. {
  101. list = list.slice(1,list.length());
  102. } else if(index == -1){
  103. return;
  104. } else {
  105. var temp = list.slice(0,index);
  106. }
  107. for(i = index + 1; i < list.length();i++)
  108. {
  109. temp.push(list[i]);
  110. }
  111. list = temp;
  112. setVar(arrayname,list.join("|"));
  113. }
  114.  
  115. function doConcat(arrayname, array) {
  116. /*Function: Receives two arrays from a calling statement, format: js doConcat("<arrayname>", "<array>")
  117. 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.
  118. pre: arrayname as a String, array as String
  119. post: the second array will be concatenated onto the first array and stored in the array variable with arrayname
  120. */
  121. var list = getVar(arrayname).toString().split("|");
  122. array = array.toString().split("|");
  123. list = list.concat(array);
  124. setVar(arrayname,list.join("|"));
  125. }
  126.  
  127. function doXConcat(arrayname, array) {
  128. /*Function: Receives two arrays from a calling statement, format: js doXConcat("<arrayname>", "<array>")
  129. 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.
  130. The second array can be an array or a list string.
  131. pre: arrayname as a String, array as String
  132. post: the second array will be concatenated onto the first array and stored in the array variable with arrayname, skipping duplicates
  133. */
  134. var list = getVar(arrayname).toString().split("|");
  135. array = array.toString().split("|");
  136. for(i = 0; i < array.length(); i++)
  137. {
  138. if(!checkExists(arrayname, array[i]))
  139. {
  140. list.push(array[i]);
  141. }
  142. }
  143. setVar(arrayname,list.join("|"));
  144. }
  145.  
  146. function doXCompare(arraysrc, arraytrg, srch) {
  147. /*Function: Receives two arrays from a calling statement and a search string, format: jscall <variable> doXCompare("<arraysrc>", "<arraytrg>", "search string")
  148. 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.
  149. The funtion returns -1 if the search string does not exist.
  150. pre: arraysrc as a String, arraytrg as String, srch as String
  151. 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
  152. */
  153. var list = getVar(arraysrc).toString().split("|");
  154. var targ = getVar(arraytrg).toString().split("|");
  155. for(i = 0; i < list.length();i++)
  156. {
  157. if(list[i].localeCompare(srch) == 0)
  158. {
  159. return targ[i];
  160. }
  161. }
  162. return -1;
  163. }
  164.  
  165. function doPush(arrayname, item) {
  166. /*Function: Receives an array and a string, format: js doPush("<arrayname>", "new item")
  167. 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
  168. pre: arrayname as a String, item as a String
  169. post: the string item will be added to the array in the last position
  170. */
  171. var list = getVar(arrayname).toString().split("|");
  172. if (list[0] == "undefined")
  173. {
  174. list[0] = item;
  175. } else {
  176. list.push(item);
  177. }
  178. setVar(arrayname,list.join("|"));
  179. }
  180.  
  181. function doPop(arrayname) {
  182. /*Function: Receives an array, format: jscall <variable> doPop("<arrayname>")
  183. The function will remove the last item from the array and return the value to be stored in the specified variable.
  184. The function will return 0 if the array is already empty.
  185. pre: arrayname as a String
  186. post: the last item removed from the array and stored in the variable from the calling statement
  187. */
  188. var list = getVar(arrayname).toString().split("|");
  189. if (list.length() > 0 && list[0] != "")
  190. {
  191. var temp = list[list.length() - 1];
  192. list.pop();
  193. setVar(arrayname,list.join("|"));
  194. return temp;
  195. } else {
  196. return 0;
  197. }
  198. }
  199.  
  200. function doUnshift(arrayname, item) {
  201. /*Function: Receives an array and a string, format: js doUnshift("<arrayname>", "new item")
  202. 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
  203. pre: arrayname as a String, item as a String
  204. post: the string item will be added to the array in the first position
  205. */
  206. var list = getVar(arrayname).toString().split("|");
  207. if (list[0] == "undefined")
  208. {
  209. list[0] = item;
  210. } else {
  211. list.unshift(item);
  212. }
  213. setVar(arrayname,list.join("|"));
  214. }
  215.  
  216. function doShift(arrayname) {
  217. /*Function: Receives an array, format: jscall <variable> doShift("<arrayname>")
  218. The function will remove the first item from the array and return the value to be stored in the specified variable.
  219. The function will return 0 if the array is already empty.
  220. pre: arrayname as a String
  221. post: the first item removed from the array and stored in the variable from the calling statement
  222. */
  223. var list = getVar(arrayname).toString().split("|");
  224. if (list.length() > 0 && list[0] != "")
  225. {
  226. var temp = list[0];
  227. list.shift();
  228. setVar(arrayname,list.join("|"));
  229. return temp;
  230. } else {
  231. return 0;
  232. }
  233. }
  234.  
  235. function buildArray(arrayname, raw) {
  236. /*Function: Receives string separated by commas, 'a's, 'an's and ands to build array, format: js buildArray("<arrayname>", "list")
  237. *Note* To build from action data you -must- capture the whole string at the same time to be sent to the function!
  238. Actions with Javascript in them will only trigger -once- per instance of the pattern in a line.
  239. pre: arrayname as a String, list as a String
  240. post: the list is returned as a Genie formatted array stored in the array with arrayname
  241. */
  242. raw = raw.toString().replace(/, /g , "|").replace(/ and (a|an) /, "|").replace(/\ban? /g, "");
  243. setVar(arrayname,raw);
  244. }
  245.  
  246. function buildArrayStr(arrayname, raw, pattern) {
  247. /*Function: Receives string separated by commas, 'a's, 'an's and ands to build array and a string, format: js buildArrayStr("<arrayname>", "list", "pattern")
  248. *Note* To build from action data you -must- capture the whole string at the same time to be sent to the function!
  249. Actions with Javascript in them will only trigger -once- per instance of the pattern in a line.
  250. Function will return an array of only the items that contain the target string. Must not be regex!
  251. pre: arrayname as a String, list as a String, pattern as a String.
  252. post: the list of items with the selected string is returned as a Genie formatted array stored in the array with arrayname
  253. */
  254.  
  255. raw = raw.toString().replace(/, /g , "|").replace(/ and (a|an) /, "|").replace(/\ban? /g, "");
  256. raw = raw.split("|");
  257. var list = [];
  258. pattern = new RegExp(pattern,"i");
  259. for(i = 0; i < raw.length();i++)
  260. {
  261. if(pattern.test(raw[i]))
  262. {
  263. list.push(raw[i]);
  264. }
  265. }
  266. setVar(arrayname,list.join("|"));
  267. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement