lib/srt.js
function(data) {
        var json = {},
            index = 0,
            id,
            text,
            startTimeMicro,
            durationMicro,
            invalidText = /^\s+$/,
            endTimeMicro,
            time,
            lastNonEmptyLine;

        function getLastNonEmptyLine(linesArray) {
            var idx = linesArray.length - 1;
            while (idx >= 0 && !linesArray[idx]) {
                idx--;
            }
            return idx;
        }

        json.captions = [];
        lastNonEmptyLine = getLastNonEmptyLine(data) + 1;

        while (index < lastNonEmptyLine) {
            if (data[index]) {
                text = [];
                //Find the ID line..
                if (/^[0-9]+$/.test(data[index])) {
                    //found id line
                    id = parseInt(data[index], 10);
                    index++;
                }
                if (!data[index].split) {
                    // for some reason this is not a string
                    index++;
                    continue;
                }
                //next line has to be timestamp right? right?
                time = data[index].split(/[\t ]*-->[\t ]*/);
                startTimeMicro = module.exports.translateTime(time[0]);
                endTimeMicro = module.exports.translateTime(time[1]);
                durationMicro = parseInt(parseInt(endTimeMicro, 10) - parseInt(startTimeMicro, 10), 10);
                if (!startTimeMicro || !endTimeMicro) {
                    // no valid timestamp
                    index++;
                    continue;
                }
                index++;
                while (data[index]) {
                    text.push(data[index]);
                    index++;
                    if (!data[index] && !invalidText.test(text.join('\n'))) {
                        json.captions.push({
                            id: id,
                            text: module.exports.addMacros(text.join('\n')),
                            startTimeMicro: startTimeMicro,
                            durationSeconds: parseInt(durationMicro / 1000, 10) / 1000,
                            endTimeMicro: endTimeMicro
                        });
                        break;
                    }
                }
            }
            index++;
        }
        return json.captions;

    }
function(data) {
        var json = {},
            index = 0,
            id,
            text,
            startTimeMicro,
            durationMicro,
            invalidText = /^\s+$/,
            endTimeMicro,
            time,
            lastNonEmptyLine;

        function getLastNonEmptyLine(linesArray) {
            var idx = linesArray.length - 1;
            while (idx >= 0 && !linesArray[idx]) {
                idx--;
            }
            return idx;
        }

        json.captions = [];
        lastNonEmptyLine = getLastNonEmptyLine(data) + 1;

        while (index < lastNonEmptyLine) {
            if (data[index]) {
                text = [];
                //Find the ID line..
                if (/^[0-9]+$/.test(data[index])) {
                    //found id line
                    id = parseInt(data[index], 10);
                    index++;
                }
                if (!data[index].split) {
                    // for some reason this is not a string
                    index++;
                    continue;
                }
                //next line has to be timestamp right? right?
                time = data[index].split(/[\t ]*-->[\t ]*/);
                startTimeMicro = module.exports.translateTime(time[0]);
                endTimeMicro = module.exports.translateTime(time[1]);
                durationMicro = parseInt(parseInt(endTimeMicro, 10) - parseInt(startTimeMicro, 10), 10);
                if (!startTimeMicro || !endTimeMicro) {
                    // no valid timestamp
                    index++;
                    continue;
                }
                index++;
                while (data[index]) {
                    text.push(data[index]);
                    index++;
                    if (!data[index] && !invalidText.test(text.join('\n'))) {
                        json.captions.push({
                            id: id,
                            text: module.exports.addMacros(text.join('\n')),
                            startTimeMicro: startTimeMicro,
                            durationSeconds: parseInt(durationMicro / 1000, 10) / 1000,
                            endTimeMicro: endTimeMicro
                        });
                        break;
                    }
                }
            }
            index++;
        }
        return json.captions;

    }
function ( data ) { var json = { } , index = 0 , id , text , startTimeMicro , durationMicro , invalidText = / ^\s+$ / , endTimeMicro , time , lastNonEmptyLine ; function getLastNonEmptyLine ( linesArray ) { var idx = linesArray . length - 1 ; while ( idx >= 0 && ! linesArray [ idx ] ) { idx -- ; } return idx ; } json . captions = [ ] ; lastNonEmptyLine = getLastNonEmptyLine ( data ) + 1 ; while ( index < lastNonEmptyLine ) { if ( data [ index ] ) { text = [ ] ; //Find the ID line.. if ( / ^[0-9]+$ / . test ( data [ index ] ) ) { //found id line id = parseInt ( data [ index ] , 10 ) ; index ++ ; } if ( ! data [ index ] . split ) { // for some reason this is not a string index ++ ; continue ; } //next line has to be timestamp right? right? time = data [ index ] . split ( / [\t ]*-->[\t ]* / ) ; startTimeMicro = module . exports . translateTime ( time [ 0 ] ) ; endTimeMicro = module . exports . translateTime ( time [ 1 ] ) ; durationMicro = parseInt ( parseInt ( endTimeMicro , 10 ) - parseInt ( startTimeMicro , 10 ) , 10 ) ; if ( ! startTimeMicro || ! endTimeMicro ) { // no valid timestamp index ++ ; continue ; } index ++ ; while ( data [ index ] ) { text . push ( data [ index ] ) ; index ++ ; if ( ! data [ index ] && ! invalidText . test ( text . join ( '\n' ) ) ) { json . captions . push ( { id : id , text : module . exports . addMacros ( text . join ( '\n' ) ) , startTimeMicro : startTimeMicro , durationSeconds : parseInt ( durationMicro / 1000 , 10 ) / 1000 , endTimeMicro : endTimeMicro } ) ; break ; } } } index ++ ; } return json . captions ; }
converts SRT to JSON format
@function
@param {array} data - output from read usually
@public
converts SRT to JSON format