javascript/Library.js
function organizer(flat, tags, treeDescriptor, leafDescriptor) {
    var tree = {};
    flat.forEach(function (song) {
        var treePtr = tree;
        var depth = 1;

        // strPossibleKeys can be like "albumArtist|artist", or just "album" for instance
        treeDescriptor.forEach(function (strPossibleKeys) {
            var possibleKeys = strPossibleKeys.split("|");
            var valueForKey = undefined;
            for (var key in possibleKeys) {
                valueForKey = song[possibleKeys[key]];
                if (valueForKey !== undefined && valueForKey !== "") {
                    break;
                }
            }
            if (valueForKey === undefined) {
                valueForKey = "";
            }
            if (!treePtr[valueForKey]) {
                if (depth === treeDescriptor.length) {
                    treePtr[valueForKey] = { tags: {}, mpd: [] };
                } else {
                    treePtr[valueForKey] = { tags: {}, mpd: {} };
                }
                var mostCommonKey = possibleKeys[possibleKeys.length - 1];
                if (tags[mostCommonKey] && tags[mostCommonKey][valueForKey]) {
                    treePtr[valueForKey].tags = tags[mostCommonKey][valueForKey];
                }
            }
            treePtr = treePtr[valueForKey].mpd;
            depth++;
        });
        var leaf = {};
        if (leafDescriptor) {
            leafDescriptor.forEach(function (key) {
                leaf[key] = song[key];
            });
        } else {
            leaf = song;
        }
        if (tags["song"] && tags["song"][song.file]) {
            leaf.tags = tags["song"][song.file];
        }
        treePtr.push(leaf);
    });
    return { root: tree };
}
function organizer(flat, tags, treeDescriptor, leafDescriptor) {
    var tree = {};
    flat.forEach(function (song) {
        var treePtr = tree;
        var depth = 1;

        // strPossibleKeys can be like "albumArtist|artist", or just "album" for instance
        treeDescriptor.forEach(function (strPossibleKeys) {
            var possibleKeys = strPossibleKeys.split("|");
            var valueForKey = undefined;
            for (var key in possibleKeys) {
                valueForKey = song[possibleKeys[key]];
                if (valueForKey !== undefined && valueForKey !== "") {
                    break;
                }
            }
            if (valueForKey === undefined) {
                valueForKey = "";
            }
            if (!treePtr[valueForKey]) {
                if (depth === treeDescriptor.length) {
                    treePtr[valueForKey] = { tags: {}, mpd: [] };
                } else {
                    treePtr[valueForKey] = { tags: {}, mpd: {} };
                }
                var mostCommonKey = possibleKeys[possibleKeys.length - 1];
                if (tags[mostCommonKey] && tags[mostCommonKey][valueForKey]) {
                    treePtr[valueForKey].tags = tags[mostCommonKey][valueForKey];
                }
            }
            treePtr = treePtr[valueForKey].mpd;
            depth++;
        });
        var leaf = {};
        if (leafDescriptor) {
            leafDescriptor.forEach(function (key) {
                leaf[key] = song[key];
            });
        } else {
            leaf = song;
        }
        if (tags["song"] && tags["song"][song.file]) {
            leaf.tags = tags["song"][song.file];
        }
        treePtr.push(leaf);
    });
    return { root: tree };
}
function organizer ( flat , tags , treeDescriptor , leafDescriptor ) { var tree = { } ; flat . forEach ( function ( song ) { var treePtr = tree ; var depth = 1 ; // strPossibleKeys can be like "albumArtist|artist", or just "album" for instance treeDescriptor . forEach ( function ( strPossibleKeys ) { var possibleKeys = strPossibleKeys . split ( "|" ) ; var valueForKey = undefined ; for ( var key in possibleKeys ) { valueForKey = song [ possibleKeys [ key ] ] ; if ( valueForKey !== undefined && valueForKey !== "" ) { break ; } } if ( valueForKey === undefined ) { valueForKey = "" ; } if ( ! treePtr [ valueForKey ] ) { if ( depth === treeDescriptor . length ) { treePtr [ valueForKey ] = { tags : { } , mpd : [ ] } ; } else { treePtr [ valueForKey ] = { tags : { } , mpd : { } } ; } var mostCommonKey = possibleKeys [ possibleKeys . length - 1 ] ; if ( tags [ mostCommonKey ] && tags [ mostCommonKey ] [ valueForKey ] ) { treePtr [ valueForKey ] . tags = tags [ mostCommonKey ] [ valueForKey ] ; } } treePtr = treePtr [ valueForKey ] . mpd ; depth ++ ; } ) ; var leaf = { } ; if ( leafDescriptor ) { leafDescriptor . forEach ( function ( key ) { leaf [ key ] = song [ key ] ; } ) ; } else { leaf = song ; } if ( tags [ "song" ] && tags [ "song" ] [ song . file ] ) { leaf . tags = tags [ "song" ] [ song . file ] ; } treePtr . push ( leaf ) ; } ) ; return { root : tree } ; }
Returns a custom object tree corresponding to the descriptor
Returns a custom object tree corresponding to the descriptor