Manipulating paths in JS LiveAPI.

Nov 29, 2011 at 1:36am

Manipulating paths in JS LiveAPI.

It seems like there’s some fairly gnarly string operations going on in the JS LiveAPI paths.
Let’s say I want to get a path from something, and prepare a path, based on what I find…

function test(){
var api = new LiveAPI();
api.path = 'this_device'; //returns sth like "live_set tracks 0 devices 0"
}

If api.path is
"live_set tracks 0 devices 0"
What I’m interested in is the device to the right of this_device. How can I unravel that result and change that 0, then add a few more things to the path, for example:
"live_set tracks 0 devices 1 chains 0"
??
I’ve tried various tricks of unravelling the string and packing it together into something that I can use to set to api.path, but I keep getting errors.
Any hints?
Peter

#60327
Dec 7, 2011 at 8:24am

I don’t know how efficient this is compared to good old fashioned string manipulation, but I’ve always found arrays to be more intuitive.

var oldString = "live_set tracks 0 devices 0";
var strArray = oldString.split(" ");

So now it’s a list of words (as previously separated by spaces).

being array members now, we can alter the parameters individually. The “0″ we want to change was the 6th list item, but we’re counting up from 0, so it’s #5…

strArray[5] = "1";

Then, when we’re done, turn the array back into a string.

newString = strArray.join(" ");

(each element is separated by a spacebar.

And then, appending strings is just the plus sign.

So, something like this:

var oldString = "live_set tracks 0 devices 0";
var strArray = oldString.split(" ");
strArray[5] = "1";
var newString = strArray.join(" ") + " chains 0";

…but you knew all that.

The real question is “why am I getting errors?” and the answer is “because you’re trying to perform math operations on a string.”

“30″ + 1 does not equal 31. It equals “301″. That’s probably out of range, so your code’s going to behave unpredictably.

likewise…

var fakeNumber = "30";
fakeNumber++;

You can’t increment a string like this. That’s an error.

This, however, works:

var fakeNumber = "30";
fakeNumber = Number(fakeNumber) + 1;

Does that help?

#217116
Dec 7, 2011 at 9:13am

Actually, it looks like api.path also has quotation marks baked in. That’ll get in your way.

So, something like this might be less fragile:

function apiTest() {
	var api = new LiveAPI();
	api.path= 'this_device';

	var nextPath = removeQuotes(api.path);
	nextPath = moveToTheRight(nextPath);
	nextPath = appendStuff(nextPath);
	nextPath = replaceQuotes(nextPath);

	post(nextPath);
}

function moveToTheRight(devicePath){
	//split into a more usable array
	var pathArray = devicePath.split(" ");

	// increment the device number
	pathArray[4] = Number(pathArray[4]) + 1;

	//rebuild the string
	return pathArray.join(" ");
}

function removeQuotes(str) {
	var tmpArray = str.split('"');
	return tmpArray[1];
}

function replaceQuotes(str) {
	return '"' + str + '"';
}

function appendStuff(str) {
	return str + " chain 10";
}

I’m not sure how to actually pipe that new path back into the api and make it useful to you, but there it is.

(worth pointing out: I’ve got no experience coding for the Live API, and every part of this is probably wrong. we learn by doing…)

#217117
Dec 7, 2011 at 9:25am

Okay, that last part was easily researched. This may not be at all efficient, but it should at least be usable. I think.

(Doesn’t look like the quotation marks are actually needed, so we’re not adding them back anymore. And my “removeQuotes” function was embarrassingly bad, so I rewrote that as well.)

function apiTest(){
	var api = new LiveAPI();
	api.path= 'this_device';

	var nextPath = removeQuotes(api.path);
	nextPath = moveToTheRight(nextPath) + " chain 10";

	//pipe that string back into the LiveAPI
	api = new LiveAPI(, nextPath);

	//and confirm that this worked
	post(api.path);
}

function moveToTheRight(devicePath){
	//split into a more usable array
	var pathArray = devicePath.split(" ");

	// increment the device number
	pathArray[4] = Number(pathArray[4]) + 1;

	//rebuild the string
	return pathArray.join(" ");
}

function removeQuotes(str){
	return str.split('"').join("");
}
#217118

You must be logged in to reply to this topic.