For instance, the string “carrots,peas,lettuce,tomato” could be described as a “comma delimited” or “comma separated” list. Although commas are the most common delimiter for lists, any character can be used – “Monkey:Rabbit:Mouse:Elephant” is no less a list than the comma delimited list.
There are 13 functions included in the library. They are, in alphabetical order:
Concatenates an element to a list.
Deletes an element from a list.
Determines the index of the first list element in which a specified value occurs. The search is case-sensitive.
Determines the index of the first list element in which a specified value occurs. The search is case-insensitive.
Gets the first element of a list.
Gets a list element at a specified position.
Inserts an element into a list.
Gets the last element of a list.
Determines the number of elements in a list.
Inserts an element at the beginning of a list.
Inserts a string at the beginning and end of list elements.
Gets a list, without its first element.
Reverses the order of elements in a list.
Replaces the contents of a list element.
Sorts list elements according to a sort type and sort order.
Once the library is loaded then invoking the function you want is simply a matter of prefixing the function with “jList.”. The following example, which uses the listAppend and listSort functions, shows how:
<script src="jlist-min.js"></script> <script> var l, l2; l = "Susan,Janet,Steve,Bree,Michele,Jim,Mark,Elizabeth"; l = jList.listAppend(l, "David"); l2 = jList.listSort(l); alert(l2); </script>
This will show an alert with the text “Bree,David,Elizabeth,Janet,Jim,Mark,Michele,Steve,Susan”.
var url = "http://www.mysite.net/main.htm?id=1234"; var url_part = jList.listGetAt(url,3,"/"); // returns 'www.mysite.net' var tld = jList.listLast(url_part,"."); // returns 'net'
In line 2 we grab the 3rd item in the list, using a delimiter of “/”. In this case this is “www.mysite.net” because the first item is “http:” and the second item is empty. This is an important difference between this library and ColdFusion list processing – empty elements are not ignored in these functions. Notice also that we start counting the items from 1, not from zero.
Once we have the third item stored in url_part (line 2) we can then treat this as a “dot delimited” list. Using the listLast function (line 3) to retrieve the rightmost item in the list but this time specifying “.” as the delimiter will return “net” into the tld variable.
For this 1.0.0 release I have cleaned up the code, added detailed documentation and made it public on GitHub.