{"size": 1307, "ext": "js", "max_stars_repo_path": "src/index.js", "max_stars_repo_name": "clownvary/wrap-less-loader", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/index.js", "max_issues_repo_name": "clownvary/wrap-less-loader", "max_issues_repo_issues_event_min_datetime": "2020-09-12T07:01:34.000Z", "max_issues_repo_issues_event_max_datetime": "2021-08-11T22:00:15.000Z", "max_forks_repo_path": "src/index.js", "max_forks_repo_name": "clownvary/less-wrapper-loader", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 6, "max_forks_count": null, "avg_line_length": 28.4130434783, "max_line_length": 103, "alphanum_fraction": 0.6595256312}
import path from 'path';
import find from 'lodash/find';
import isRegExp from 'lodash/isRegExp';

function wrapContent (content, nameSpace, dir) {
  let wrapName = '';
  let result = content;
  if (isRegExp(nameSpace)) {
    if (nameSpace.test(dir)) {
      const matchResults = dir.match(nameSpace);
      wrapName = matchResults[matchResults.length - 1];
    }
  } else {
    wrapName = nameSpace;
  }
  result = `.${wrapName} {\n ${content} \n}`;
  return result;
}

function lessWrapperLoader (content) {
  const defaultOptions = {
    nameSpace: /less/g, // Determine what prefix name should be added at content, support string/regex,
    whitePathList: [] // Just prefix file which it's path contains one of the white list value
  };
  const { resourcePath, query } = this;
  const options = { ...defaultOptions, ...query };
  const { nameSpace, whitePathList } = options;
  const { dir } = path.parse(resourcePath);
  let result = content;

  if (whitePathList && whitePathList.length > 0) {
    const isWhitePath = !!find(whitePathList, (item) => {
      return dir.indexOf(item) !== -1;
    });
    if (isWhitePath) {
      result = wrapContent(content, nameSpace, dir);
    }
  } else {
    result = wrapContent(content, nameSpace, dir);
  }

  return result;
}

export default lessWrapperLoader;
