<commit_before>/**
 * Copyright (c) 2015-2016, fangstar.com
 * 
 * All rights reserved.
 */

/**
 * @file gulp plugin for header license.
 * 
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @version 0.1.4.1, Jan 19, 2016
 */

'use strict';

var extend = require('object-assign');
var through = require('through2');
var gutil = require('gulp-util');
var path = require('path');

/**
 * gulp-license plugin.
 * @param {string} license The license template string.
 * @param {object} [config] The JSON object used to populate the license template.
 * @param {float} [rate=0.8] Matching rate.
 * @returns {oject} Gulp extension in the pipline.
 */
module.exports = function (license, config, rate) {

    /**
     * According to rate, get matching.
     * 
     * @param {object} file nodeJS file object.
     * @param {string} license The license template string.
     * @param {float} rate Matching rate.
     * @param {string} srcNLReg new line char code.
     * @returns {boolean} dose match.
     */
    function isMatch(file, license, rate, srcNLReg) {
        var srcLines = file.contents.toString('utf8').split(/\r?\n/),
                templateLines = license.split(/\r?\n/),
                type = path.extname(file.path),
                matchRates = 0,
                removed = false;

        // after '<?php' has new line, remove it 
        switch (type) {
            case '.php':
                if (srcLines[1] === '') {
                    srcLines.splice(1, 1);
                    removed = true;
                }
                break;
            default:
                break;
        }

        // count match line
        var minLength = templateLines.length > srcLines.length ? srcLines.length : templateLines.length;
        for (var i = 0; i < minLength; i++) {
            if (templateLines[templateLines.length - 1] === '' && i === templateLines.length - 1) {
                matchRates += 1;
            } else {
                switch (type) {
                    case '.php':
                        matchRates += getMatchRate(srcLines[i + 1], templateLines[i]);
                        break;
                    default:
                        matchRates += getMatchRate(srcLines[i], templateLines[i]);
                        break;
                }
            }
        }

        // has similar license, remove the license.
        var matchPer = matchRates / templateLines.length;

        if (matchPer >= rate && matchPer < 1) {
            // remove
            switch (type) {
                case '.php':
                    if (srcLines[templateLines.length + 1].replace(/\s/, '') === '' || removed) {
                        // after license, should be have a blank line. if have not, we don't need remove blank line. || after '<?php' has new line, remove it.
                        srcLines.splice(1, templateLines.length - 1);
                    } else {
                        srcLines.splice(1, templateLines.length);
                    }
                    file.contents = new Buffer(srcLines.join(srcNLReg));
                    break;
                default:
                    // after license, should be have a blank line. if have not, we don't need remove blank line.
                    if (srcLines[templateLines.length - 1].replace(/\s/, '') === '') {
                        srcLines.splice(0, templateLines.length);
                    } else {
                        srcLines.splice(0, templateLines.length - 1);
                    }
                    file.contents = new Buffer(srcLines.join(srcNLReg));<commit_msg>bug fixed<commit_after>/**
 * Copyright (c) 2015-2016, fangstar.com
 * 
 * All rights reserved.
 */

/**
 * @file gulp plugin for header license.
 * 
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @version 0.1.4.1, Jan 19, 2016
 */

'use strict';

var extend = require('object-assign');
var through = require('through2');
var gutil = require('gulp-util');
var path = require('path');

/**
 * gulp-license plugin.
 * @param {string} license The license template string.
 * @param {object} [config] The JSON object used to populate the license template.
 * @param {float} [rate=0.8] Matching rate.
 * @returns {oject} Gulp extension in the pipline.
 */
module.exports = function (license, config, rate) {

    /**
     * According to rate, get matching.
     * 
     * @param {object} file nodeJS file object.
     * @param {string} license The license template string.
     * @param {float} rate Matching rate.
     * @param {string} srcNLReg new line char code.
     * @returns {boolean} dose match.
     */
    function isMatch(file, license, rate, srcNLReg) {
        var srcLines = file.contents.toString('utf8').split(/\r?\n/),
                templateLines = license.split(/\r?\n/),
                type = path.extname(file.path),
                matchRates = 0,
                removed = false;

        // after '<?php' has new line, remove it 
        switch (type) {
            case '.php':
                if (srcLines[1].replace(/\s/, '') === '') {
                    srcLines.splice(1, 1);
                    removed = true;
                }
                break;
            default:
                break;
        }

        // count match line
        var minLength = templateLines.length > srcLines.length ? srcLines.length : templateLines.length;
        for (var i = 0; i < minLength; i++) {
            if (templateLines[templateLines.length - 1] === '' && i === templateLines.length - 1) {
                matchRates += 1;
            } else {
                switch (type) {
                    case '.php':
                        matchRates += getMatchRate(srcLines[i + 1], templateLines[i]);
                        break;
                    default:
                        matchRates += getMatchRate(srcLines[i], templateLines[i]);
                        break;
                }
            }
        }

        // has similar license, remove the license.
        var matchPer = matchRates / templateLines.length;

        if (matchPer >= rate && matchPer < 1) {
            // remove
            switch (type) {
                case '.php':
                    if (srcLines[templateLines.length].replace(/\s/, '') === '' && !removed) {
                        // after license, should be have a blank line. if has not, we don't need remove blank line. && after '<?php' has not new line
                        srcLines.splice(1, templateLines.length);
                    } else {
                        srcLines.splice(1, templateLines.length - 1);
                    }
                    file.contents = new Buffer(srcLines.join(srcNLReg));
                    break;
                default:
                    // after license, should be have a blank line. if have not, we don't need remove blank line.
                    if (srcLines[templateLines.length - 1].replace(/\s/, '') === '') {
                        srcLines.splice(0, templateLines.length);
                    } else {
                        srcLines.splice(0, templateLines.length - 1);
                    }
                    file.contents = new Buffer(srcLines.join(srcNLReg));
{'fraction_non_alphanumeric': 0.12590428491930997, 'fraction_numerical': 0.01099053978853645, 'mean_word_length': 1.5127577770010485, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6373904', 'n_tokens_mistral': 2019, 'n_tokens_neox': 1874, 'n_words': 540}