Scripty library script update

If you use the Scripty libraries, then you doubtless know this function:

function cleanLibs()
{
	fwPage.scriptyLibs.sort();
	// Get rid of duplicates and out of range values
	var i = 0;
	while(i < fwPage.scriptyLibs.length)
		if(fwPage.scriptyLibs[i+1] && fwPage.scriptyLibs[i] == fwPage.scriptyLibs[i+1])
			fwPage.scriptyLibs.splice(i+1, 1);
		else if(fwPage.scriptyLibs[i] > gLibraries.length-1 || fwPage.scriptyLibs[i] < 0)
			fwPage.scriptyLibs.splice(i, 1);
		else
			i++;
}

It’s meant to reduce the array of scriptyLibs to unique values. Except it doesn’t work at the 0 index for some reason (bonus points to anyone who can explain this to me, I can’t see it).

Here’s an implementation that works. Note that it needs another helper method to work around the problem that Freeway’s version of JavaScript does not include Array#indexOf, and adding on to the Array Prototype has explosive consequences for other code in the same document, as previously noted.

var include = function(arr, obj) {
	for(var i = -1, j = arr.length; ++i < j;)
	if(arr[i] === obj) return true;
	return false;
};
/* Clean up the list of libraries by sorting them in their correct order (according to 
	scripty specs) and removing dupes */
function cleanLibs(){
	var out = [];
	for (var i=0; i < fwPage.scriptyLibs.length; i++) {
		if( ! (include(out, fwPage.scriptyLibs[i])) && 
			(fwPage.scriptyLibs[i] < gLibraries.length) && 
			(fwPage.scriptyLibs[i] >= 0) ){
			out.push(fwPage.scriptyLibs[i]);
		}
	};
	fwPage.scriptyLibs = out.sort();
}

Hope this helps.

Walter


actionsdev mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

The original function is doing a check to see if there is a value at the index i+1 of the array of values:


if ( fwPage.scriptyLibs[i+1] ...

Unfortunately, if the value is 0 JavaScript will always return false. Here’s an updated version of that function that will work (with comments):


	// Clean up the list of libraries by sorting them in their correct order (according to scripty specs) and removing dupes
	function cleanLibs()
	{
		fwPage.scriptyLibs.sort();
		// Get rid of duplicates and out of range values
		var i = 0,
			l = gLibraries.length;
		while(i < fwPage.scriptyLibs.length)
			// if we're in range and the values of i and i+1 are the same, remove i+1
			if(i < l && fwPage.scriptyLibs[i] === fwPage.scriptyLibs[i+1])
				fwPage.scriptyLibs.splice(i+1, 1);
			// if someone has passed a bad value into the array, get rid of it
			else if(fwPage.scriptyLibs[i] >= l || fwPage.scriptyLibs[i] < 0)
				fwPage.scriptyLibs.splice(i, 1);
			// otherwise, increase the index
			else
				i++;
	}

Joe

On 15 May 2012, at 14:11, Walter Lee Davis wrote:

If you use the Scripty libraries, then you doubtless know this function:

function cleanLibs()
{
fwPage.scriptyLibs.sort();
// Get rid of duplicates and out of range values
var i = 0;
while(i < fwPage.scriptyLibs.length)
if(fwPage.scriptyLibs[i+1] && fwPage.scriptyLibs[i] == fwPage.scriptyLibs[i+1])
fwPage.scriptyLibs.splice(i+1, 1);
else if(fwPage.scriptyLibs[i] > gLibraries.length-1 || fwPage.scriptyLibs[i] < 0)
fwPage.scriptyLibs.splice(i, 1);
else
i++;
}

It’s meant to reduce the array of scriptyLibs to unique values. Except it doesn’t work at the 0 index for some reason (bonus points to anyone who can explain this to me, I can’t see it).

Here’s an implementation that works. Note that it needs another helper method to work around the problem that Freeway’s version of JavaScript does not include Array#indexOf, and adding on to the Array Prototype has explosive consequences for other code in the same document, as previously noted.

var include = function(arr, obj) {
for(var i = -1, j = arr.length; ++i < j;)
if(arr[i] === obj) return true;
return false;
};
/* Clean up the list of libraries by sorting them in their correct order (according to
scripty specs) and removing dupes */
function cleanLibs(){
var out = [];
for (var i=0; i < fwPage.scriptyLibs.length; i++) {
if( ! (include(out, fwPage.scriptyLibs[i])) &&
(fwPage.scriptyLibs[i] < gLibraries.length) &&
(fwPage.scriptyLibs[i] >= 0) ){
out.push(fwPage.scriptyLibs[i]);
}
};
fwPage.scriptyLibs = out.sort();
}

Hope this helps.

Walter


actionsdev mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options


actionsdev mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Ok - in the browser (which is what I use to read FWTalk), that last message gets mangled up horribly.

Serious request - can Freeway handle all of this Scripty library business natively?

I don’t want to waste time updating all my Actions that use it every time there’s a JavaScript change to what is a common library of code which is coming from in essence Softpress.


actionsdev mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Sorry about that. No idea what happened there. The short answer to your request is:

We’re not going to be able to do that in the immediate future, no.


actionsdev mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options