<commit_before>'use strict';

const ch = require('./changes_hunter');
const glob = require('glob');
const fs = require('fs-extra');
const _ = require('lodash');
const lh = require('./lineage_hunter');
const Pattern = require('./object_factory').Pattern;
const path = require('path');
const addPattern = require('./addPattern');
const logger = require('./log');
const readDocumentation = require('./readDocumentation');
const lineage_hunter = new lh();
const changes_hunter = new ch();
const yaml = require('js-yaml');

const pseudopattern_hunter = function() {};

pseudopattern_hunter.prototype.find_pseudopatterns = function(
  currentPattern,
  patternlab
) {
  const paths = patternlab.config.paths;

  //look for a pseudo pattern by checking if there is a file containing same
  //name, with ~ in it, ending in .json, .yml or .yaml
  const needle =
    currentPattern.subdir +
    '/' +
    currentPattern.fileName +
    '~*.{json,yml,yaml}';
  const pseudoPatterns = glob.sync(needle, {
    cwd: paths.source.patterns,
    debug: false,
    nodir: true,
  });

  if (pseudoPatterns.length > 0) {
    for (let i = 0; i < pseudoPatterns.length; i++) {
      logger.debug(
        `Found pseudoPattern variant of ${currentPattern.patternPartial}`
      );

      //we want to do everything we normally would here, except instead read the pseudoPattern data
      let variantFileFullPath;
      let variantFileData;
      try {
        variantFileFullPath = path.resolve(
          paths.source.patterns,
          pseudoPatterns[i]
        );
        variantFileData = yaml.safeLoad(
          fs.readFileSync(variantFileFullPath, 'utf8')
        );
      } catch (err) {
        logger.warning(
          `There was an error parsing pseudopattern JSON for ${currentPattern.relPath}`
        );
        logger.warning(err);
      }

      //extend any existing data with variant data
      variantFileData = _.mergeWith(
        {},
        currentPattern.jsonFileData,
        variantFileData,
        (objValue, srcValue) => {
          if (
            _.isArray(objValue) &&
            patternlab.config.patternMergeVariantArrays
          ) {
            return objValue.concat(srcValue);
          }
          // Lodash will only check for "undefined" and eslint needs a consistent
          // return so do not remove
          return undefined;
        }
      );

      const variantName = pseudoPatterns[i]<commit_msg>#1199: Fix merge patterns when needed<commit_after>'use strict';

const ch = require('./changes_hunter');
const glob = require('glob');
const fs = require('fs-extra');
const _ = require('lodash');
const lh = require('./lineage_hunter');
const Pattern = require('./object_factory').Pattern;
const path = require('path');
const addPattern = require('./addPattern');
const logger = require('./log');
const readDocumentation = require('./readDocumentation');
const lineage_hunter = new lh();
const changes_hunter = new ch();
const yaml = require('js-yaml');

const pseudopattern_hunter = function() {};

pseudopattern_hunter.prototype.find_pseudopatterns = function(
  currentPattern,
  patternlab
) {
  const paths = patternlab.config.paths;

  //look for a pseudo pattern by checking if there is a file containing same
  //name, with ~ in it, ending in .json, .yml or .yaml
  const needle =
    currentPattern.subdir +
    '/' +
    currentPattern.fileName +
    '~*.{json,yml,yaml}';
  const pseudoPatterns = glob.sync(needle, {
    cwd: paths.source.patterns,
    debug: false,
    nodir: true,
  });

  if (pseudoPatterns.length > 0) {
    for (let i = 0; i < pseudoPatterns.length; i++) {
      logger.debug(
        `Found pseudoPattern variant of ${currentPattern.patternPartial}`
      );

      //we want to do everything we normally would here, except instead read the pseudoPattern data
      let variantFileFullPath;
      let variantFileData;
      try {
        variantFileFullPath = path.resolve(
          paths.source.patterns,
          pseudoPatterns[i]
        );
        variantFileData = yaml.safeLoad(
          fs.readFileSync(variantFileFullPath, 'utf8')
        );
      } catch (err) {
        logger.warning(
          `There was an error parsing pseudopattern JSON for ${currentPattern.relPath}`
        );
        logger.warning(err);
      }

      //extend any existing data with variant data
      variantFileData = _.mergeWith(
        {},
        currentPattern.jsonFileData,
        variantFileData,
        (objValue, srcValue) => {
          if (
            _.isArray(objValue) &&
            !patternlab.config.patternMergeVariantArrays
          ) {
            return srcValue;
          }
          // Lodash will only check for "undefined" and eslint needs a consistent
          // return so do not remove
          return undefined;
        }
      );

      const variantName = pseudoPatterns[i]
{'fraction_non_alphanumeric': 0.11252566735112936, 'fraction_numerical': 0.002053388090349076, 'mean_word_length': 2.6108228317272055, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '2881116', 'n_tokens_mistral': 1380, 'n_tokens_neox': 1357, 'n_words': 376}