Chuck Tomasi
Tera Patron

Last week I needed a way to take two lists of comma separated values and determine what the differences were. A customer was looking for a way to do approvals based on values added or removed from a proposed change to a glide_list on a CI. For example, if SOX was added to the glide_list field, they wanted a group approval based on SOX. If PCI was removed, then a different group was required to approve. With a little XML parsing, I was able to get the old and new list values. My script is written to work equally well on any list of comma separated values. I offer to you, free of charge, as-is my GlideListDiffer script include for your use.


GlideListDiffer takes two lists of comma separated values and returns an array of objects indicating which have been added (not found in the first list) and which have been removed (not found in the second list.) Here's an example use case:



var list1 = "1,2,3,4,5";
var list2 = "2,4,6,8,10";
var ld = new GlideListDiffer();

var o = ld.listChanges(list1, list2);

gs.print(o.length + ' changes found');
for (var i = 0; i < o.length; i++)
gs.print('action=' + o<i>.action + ' value=' + o<i>.value);


The above example wil produce the following output.


*** Script: 6 changes found
*** Script: action=delete value=1
*** Script: action=delete value=3
*** Script: action=delete value=5
*** Script: action=add value=6
*** Script: action=add value=8
*** Script: action=add value=10


Do yourself a favor and don't call your lists "old" and "new"! "new" is a reserved word in JavaScript and it cost me about 30 minutes. 🙂



Name: GlideListDiffer
Active: true
Client callable: false
Description: Take two lists (comma separate values - glide_list values) and determine what was added and what was removed.
Returns: array of objects
- properties
-- action: add/delete
-- value: list item added or removed



var GlideListDiffer = Class.create();

GlideListDiffer.prototype = {

initialize: function() {
this.debug = gs.getProperty('debug.GlideListDiffer') == 'true';
},

/*
* listChanges - go through each list to find what's been added/delete
*
* @parm - list1/list2: comma separated list values
* @return - array of objects with properties
* action: "add" or "delete"
* value: list value added or removed
*
*/
listChanges : function(list1, list2) {

var answer = [];
var a1 = [];
var a2 = [];
var au = new ArrayUtil();

if (list1 != '')
a1 = list1.split(',');

if (list2 != '')
a2 = list2.split(',');

this._debug('listChanges(): a1.length=' + a1.length + ' a2.length=' + a2.length);

this._debug('listChanges(): pass1...');
// Pass 1 - go through old list to see what's been removed
for (var i = 0; i < a1.length; i++) {

if (!au.contains(a2, a1<i>)) {
var o = {'action' : 'delete', 'value' : a1<i>};
answer.push(o);
this._debug('listChanges(): ' + a1<i> + ' deleted');
}
}

this._debug('listChanges(): pass2...');
// Pass 2 - go through new list and see what's been added
for (var i = 0; i < a2. length; i++) {

if (!au.contains(a1, a2<i>)) {
var o = {'action' : 'add', 'value' : a2<i>};
answer.push(o);
this._debug('listChanges(): ' + a2<i> + ' added');
}
}

this._debug('listChanges(): answer.length=' + answer.length);
return answer;
},

/*
* _debug - dump a debug to the log
*
* @parm - s: string to send
* @returns - NA
* @uses - this.debug
*
*/
_debug : function(s) {

if (this.debug)
gs.print('>>>DEBUG: GlideListDiffer: ' + s);
},

type: 'GlideListDiffer'
}

3 Comments