Code coverage report for lib/api.js

Statements: 100% (9 / 9)      Branches: 100% (6 / 6)      Functions: 100% (2 / 2)      Lines: 100% (7 / 7)      Ignored: none     

All files » lib/ » api.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 391       1                           15   14                 17     17   1        
var EOL = require('os').EOL,
    TOC_COMMENT = '<!-- TOC -->',
    TOC_END_COMMENT = '<!-- TOC END -->';
 
module.exports = {
    /**
     * @typedef {Object} TOC
     * @property {Number} index
     * @property {String} data
     */
 
    /**
     * Inserts a TOC object into a source
     * @param {String} source
     * @param {TOC} toc
     * @returns {String}
     */
    insert: function (source, toc) {
        if (toc.index === -1) return source;
 
        return source.substring(0, toc.index) + EOL + toc.data + EOL + TOC_END_COMMENT + source.substring(toc.index);
    },
 
    /**
     * Cleans a source from an existing TOC
     * @param {String} source
     * @returns {String}
     */
    clean: function (source) {
        var tocStartIndex = source.indexOf(TOC_COMMENT),
            tocEndIndex = source.indexOf(TOC_END_COMMENT, tocStartIndex);
 
        if (tocStartIndex === -1 || tocEndIndex === -1) return source;
 
        return source.substring(0, tocStartIndex + TOC_COMMENT.length) +
                source.substring(tocEndIndex + TOC_END_COMMENT.length);
    }
};