0

I have some inline-javascript containing large datasets which are hard-coded into my PHP site:

var statsData = {
    "times"  : [1369008000,1369094400,1369180800,],
    "counts" : [49,479,516,]
};

I'd like to refactor my code so that my variables are served with this structure:

[
    [1369008000, 49],
    [1369094400, 479],
    [1369180800, 516],
]

However I have many files to update - are there any tools that would help automate this process?

Emissary
  • 9,954
  • 8
  • 54
  • 65
Avi Zolty
  • 52
  • 5
  • 1
    This is JavaScript, isn't it? – kero Dec 28 '13 at 21:09
  • Yes it is. I tagged it as PHP because I figured that's how i'd be able to parse/reorganize it. – Avi Zolty Dec 28 '13 at 21:17
  • By this do you mean it's being generated by *PHP* beforehand? - in which case you'd be as well showing us *that* code. – Emissary Dec 28 '13 at 21:19
  • @Emissary no, it's not being generated - I have hundreds of pages of this javascript. Want to run it through (anything, really but assumed PHP) to reorganize it. – Avi Zolty Dec 28 '13 at 21:24
  • You're still keeping us guessing... Are we talking javascript files with nothing but valid *JS* within? If so you'd be better writing a batch script in the same language, you can run JS on the command line with [Node](http://nodejs.org/) - see [this post on how to read/write to files](http://stackoverflow.com/a/2497040/1238344); combined with `eval` first then `JSON.stringify` this should cover what you need. If on the other hand this is data mixed in with *HTML* or *PHP* or other stuff then it's a much bigger, messier, hackier task. – Emissary Dec 28 '13 at 21:40
  • I have pages of this Javascript code. It is mixed with php/html. I'm just trying to make a script that i can Copy & Paste code from my saved files into an it will export it in the format I want. A nother way to word this is [a,b,c,d,e,f,g,][1,2,3,4,5,6] how do i make change to [a,1][b,2][c,3]...? – Avi Zolty Dec 28 '13 at 21:58

2 Answers2

0

Just create a new array then loop through the original one, and place the values according to the indexes:

var statsData = {"times":[1369008000,1369094400,1369180800,],"counts":[49,479,516,]};

var result = [];//Create a new array for results.
for (var i = 0; i < statsData.times.length; ++i){//Loop the original object's times property from 0 to it's length.
    result.push([statsData.times[i], statsData.counts[i]]);//Push a new array to the result array in the new order that will contain the original values you acces throught the index in the loop variable.
}

console.log(result);

Also in your code you have two start [ in your object's counts attribute but only one ] closing it.

totymedli
  • 29,531
  • 22
  • 131
  • 165
0

Carrying on from the comments; Trying to parse JS from a mix of PHP/HTML is horrible so if you are prepared to do some copying and pasting then - if it were me - I'd opt for a simple command-line tool. As your Javascript won't validate as JSON it doesn't make much sense to try and parse it in any other language.

I've knocked up a quick script to work with your current example (I'll leave it up to you to extend it further as needed). To run it you will need to install Node.js

Next, save the following where ever you like to organise files - lets call it statsData.js:

process.stdin.resume();
process.stdin.setEncoding('utf8');

process.stdin.on('data', function(data){
    try { 
        eval(data+';global.data=statsData');
        processData(); 
    } catch(e) {
        process.stdout.write('Error: Invalid Javascript\n');
    }
});

function processData(){
    try {
        var i, out = [];
        while(i = data.times.shift())
            out.push([i, data.counts.shift()||0]);
        process.stdout.write('var statsData='+JSON.stringify(out)+';\n');
    } catch(e) {
        process.stdout.write('Error: Unexpected Javascript\n');
    }
}

Now you have a CLI tool that works with standard I/O, to use it open a terminal window and run:

$ node path/to/statsData.js 

It will then sit and wait for you to copy and paste valid javascript statements into the terminal, otherwise you could always pipe the input stream from a file where you have copied and pasted your JS to:

$ cat inputFile.js | node path/to/statsData.js > outputFile.js

cat is a unix command - if you are working on a windows machine I think the equivalent is type - but I'm unable to test that right now.

Emissary
  • 9,954
  • 8
  • 54
  • 65