lib/converters/v1.0.0/converter-v1-to-v2.js
function (collectionV1) {
        var self = this,
            items = [],
            itemGroupCache = {},
            retainEmpty = self.options.retainEmptyValues;

        // Read all folder data, and prep it so that we can throw subfolders in the right places
        itemGroupCache = _.reduce(collectionV1.folders, function (accumulator, folder) {
            if (!folder) { return accumulator; }

            var auth = self.auth(folder),
                event = self.event(folder),
                variable = self.variable(folder),
                result = {
                    name: folder.name,
                    item: []
                };

            self.options.retainIds && (result._postman_id = folder.id || util.uid());

            if (folder.description) { result.description = folder.description; }
            else if (retainEmpty) { result.description = null; }

            (auth || (auth === null)) && (result.auth = auth);
            event && (result.event = event);
            variable && variable.length && (result.variable = variable);

            accumulator[folder.id] = result;

            return accumulator;
        }, {});

        // Populate each ItemGroup with subfolders
        _.forEach(collectionV1.folders, function (folderV1) {
            if (!folderV1) { return; }

            var itemGroup = itemGroupCache[folderV1.id],
                hasSubfolders = folderV1.folders_order && folderV1.folders_order.length,
                hasRequests = folderV1.order && folderV1.order.length;

            // Add subfolders
            hasSubfolders && _.forEach(folderV1.folders_order, function (subFolderId) {
                if (!itemGroupCache[subFolderId]) {
                    // todo: figure out what to do when a collection contains a subfolder ID,
                    // but the subfolder is not actually there.
                    return;
                }

                itemGroupCache[subFolderId]._postman_isSubFolder = true;

                itemGroup.item.push(itemGroupCache[subFolderId]);
            });

            // Add items
            hasRequests && _.forEach(folderV1.order, function (requestId) {
                if (!self.cache[requestId]) {
                    // todo: what do we do here??
                    return;
                }

                itemGroup.item.push(self.singleItem(self.cache[requestId]));
            });
        });

        // This compromises some self-healing, which was originally present, but the performance cost of
        // doing self-healing the right way is high, so we directly rely on collectionV1.folders_order
        // The self-healing way would be to iterate over itemGroupCache directly, but preserving the right order
        // becomes a pain in that case.
        _.forEach(_.uniq(collectionV1.folders_order || _.map(collectionV1.folders, 'id')), function (folderId) {
            var itemGroup = itemGroupCache[folderId];

            itemGroup && !_.get(itemGroup, '_postman_isSubFolder') && items.push(itemGroup);
        });

        // This is useful later
        self.itemGroupCache = itemGroupCache;

        return _.compact(items);
    }
function (collectionV1) {
        var self = this,
            items = [],
            itemGroupCache = {},
            retainEmpty = self.options.retainEmptyValues;

        // Read all folder data, and prep it so that we can throw subfolders in the right places
        itemGroupCache = _.reduce(collectionV1.folders, function (accumulator, folder) {
            if (!folder) { return accumulator; }

            var auth = self.auth(folder),
                event = self.event(folder),
                variable = self.variable(folder),
                result = {
                    name: folder.name,
                    item: []
                };

            self.options.retainIds && (result._postman_id = folder.id || util.uid());

            if (folder.description) { result.description = folder.description; }
            else if (retainEmpty) { result.description = null; }

            (auth || (auth === null)) && (result.auth = auth);
            event && (result.event = event);
            variable && variable.length && (result.variable = variable);

            accumulator[folder.id] = result;

            return accumulator;
        }, {});

        // Populate each ItemGroup with subfolders
        _.forEach(collectionV1.folders, function (folderV1) {
            if (!folderV1) { return; }

            var itemGroup = itemGroupCache[folderV1.id],
                hasSubfolders = folderV1.folders_order && folderV1.folders_order.length,
                hasRequests = folderV1.order && folderV1.order.length;

            // Add subfolders
            hasSubfolders && _.forEach(folderV1.folders_order, function (subFolderId) {
                if (!itemGroupCache[subFolderId]) {
                    // todo: figure out what to do when a collection contains a subfolder ID,
                    // but the subfolder is not actually there.
                    return;
                }

                itemGroupCache[subFolderId]._postman_isSubFolder = true;

                itemGroup.item.push(itemGroupCache[subFolderId]);
            });

            // Add items
            hasRequests && _.forEach(folderV1.order, function (requestId) {
                if (!self.cache[requestId]) {
                    // todo: what do we do here??
                    return;
                }

                itemGroup.item.push(self.singleItem(self.cache[requestId]));
            });
        });

        // This compromises some self-healing, which was originally present, but the performance cost of
        // doing self-healing the right way is high, so we directly rely on collectionV1.folders_order
        // The self-healing way would be to iterate over itemGroupCache directly, but preserving the right order
        // becomes a pain in that case.
        _.forEach(_.uniq(collectionV1.folders_order || _.map(collectionV1.folders, 'id')), function (folderId) {
            var itemGroup = itemGroupCache[folderId];

            itemGroup && !_.get(itemGroup, '_postman_isSubFolder') && items.push(itemGroup);
        });

        // This is useful later
        self.itemGroupCache = itemGroupCache;

        return _.compact(items);
    }
function ( collectionV1 ) { var self = this , items = [ ] , itemGroupCache = { } , retainEmpty = self . options . retainEmptyValues ; // Read all folder data, and prep it so that we can throw subfolders in the right places itemGroupCache = _ . reduce ( collectionV1 . folders , function ( accumulator , folder ) { if ( ! folder ) { return accumulator ; } var auth = self . auth ( folder ) , event = self . event ( folder ) , variable = self . variable ( folder ) , result = { name : folder . name , item : [ ] } ; self . options . retainIds && ( result . _postman_id = folder . id || util . uid ( ) ) ; if ( folder . description ) { result . description = folder . description ; } else if ( retainEmpty ) { result . description = null ; } ( auth || ( auth === null ) ) && ( result . auth = auth ) ; event && ( result . event = event ) ; variable && variable . length && ( result . variable = variable ) ; accumulator [ folder . id ] = result ; return accumulator ; } , { } ) ; // Populate each ItemGroup with subfolders _ . forEach ( collectionV1 . folders , function ( folderV1 ) { if ( ! folderV1 ) { return ; } var itemGroup = itemGroupCache [ folderV1 . id ] , hasSubfolders = folderV1 . folders_order && folderV1 . folders_order . length , hasRequests = folderV1 . order && folderV1 . order . length ; // Add subfolders hasSubfolders && _ . forEach ( folderV1 . folders_order , function ( subFolderId ) { if ( ! itemGroupCache [ subFolderId ] ) { // todo: figure out what to do when a collection contains a subfolder ID, // but the subfolder is not actually there. return ; } itemGroupCache [ subFolderId ] . _postman_isSubFolder = true ; itemGroup . item . push ( itemGroupCache [ subFolderId ] ) ; } ) ; // Add items hasRequests && _ . forEach ( folderV1 . order , function ( requestId ) { if ( ! self . cache [ requestId ] ) { // todo: what do we do here?? return ; } itemGroup . item . push ( self . singleItem ( self . cache [ requestId ] ) ) ; } ) ; } ) ; // This compromises some self-healing, which was originally present, but the performance cost of // doing self-healing the right way is high, so we directly rely on collectionV1.folders_order // The self-healing way would be to iterate over itemGroupCache directly, but preserving the right order // becomes a pain in that case. _ . forEach ( _ . uniq ( collectionV1 . folders_order || _ . map ( collectionV1 . folders , 'id' ) ) , function ( folderId ) { var itemGroup = itemGroupCache [ folderId ] ; itemGroup && ! _ . get ( itemGroup , '_postman_isSubFolder' ) && items . push ( itemGroup ) ; } ) ; // This is useful later self . itemGroupCache = itemGroupCache ; return _ . compact ( items ) ; }
Constructs an array of Items & ItemGroups compatible with the V2 format.

@param {Object} collectionV1 - The v1 collection to derive folder information from.
@returns {Object[]} - The list of item group definitions.
Constructs an array of Items & ItemGroups compatible with the V2 format .