<commit_before>var resolve = require('resolve');
var copy = require('shallow-copy');
var concatMap = require('concat-map');
var path = require('path');
var parse = require('css').parse;
var fs = require('fs');

var ABS_URL = /^url\(|:\/\//,
    QUOTED = /^['"]|['"]$/g;

module.exports = reworkNPM;

function reworkNPM(opts) {
    opts = opts || {};
    var root = opts.root || process.cwd();
    var prefilter = opts.prefilter || identity;
    var shim = opts.shim || {};

    function inline(scope, style) {
        style.rules = concatMap(style.rules, function(rule) {
            return (
                (rule.type === 'import') ? getImport(scope, rule) :
                (rule.rules) ? inline(copy(scope), rule) :
                rule);
        });

        return style;
    }

    function getImport(scope, rule) {
        var file = resolveImport(rule);
        if (!file) {
            return rule;
        }

        if (scope.indexOf(file) !== -1) {
            return [];
        }
        scope.push(file);

        var contents = fs.readFileSync(file, 'utf8');
        contents = prefilter(contents, file);
        contents = parse(contents, { source: path.relative(root, file) });
        return inline(scope, contents.stylesheet).rules;
    }

    function resolveImport(rule) {
        var name = rule.import.replace(QUOTED, '');
        if (!isNpmImport(name)) {
            return null;
        }

        var alias = opts.alias ? opts.alias[name] : null;
        if (alias) {
            return path.resolve(root, alias);
        }

        var source = rule.position.source;
        var dir = source ? path.dirname(path.resolve(root, source)) : root;

        var file = resolve.sync(name, {
            basedir: dir,
            extensions: ['.css'],
            packageFilter: processPackage
        });

        return path.normalize(file);
    }

    function processPackage(pkg) {
        pkg.main =
            (hasOwn(shim, pkg.name) && shim[pkg.name]) ||
            pkg.style || 'index.css';
        return pkg;
    }

    return function(style) {
        return inline([], style);
    };
}

function identity(value) {
    return value;
}

function hasOwn(obj, prop) {
    return Object.prototype.hasOwnProperty.call(obj, prop);
}

function isNpmImport(path) {
    // Do not import absolute URLs */
    return !ABS_URL.test(path);
}
<commit_msg>Use hasOwn to check for alias path<commit_after>var resolve = require('resolve');
var copy = require('shallow-copy');
var concatMap = require('concat-map');
var path = require('path');
var parse = require('css').parse;
var fs = require('fs');

var ABS_URL = /^url\(|:\/\//,
    QUOTED = /^['"]|['"]$/g;

module.exports = reworkNPM;

function reworkNPM(opts) {
    opts = opts || {};
    var root = opts.root || process.cwd();
    var prefilter = opts.prefilter || identity;
    var shim = opts.shim || {};
    var alias = opts.alias || {};

    function inline(scope, style) {
        style.rules = concatMap(style.rules, function(rule) {
            return (
                (rule.type === 'import') ? getImport(scope, rule) :
                (rule.rules) ? inline(copy(scope), rule) :
                rule);
        });

        return style;
    }

    function getImport(scope, rule) {
        var file = resolveImport(rule);
        if (!file) {
            return rule;
        }

        if (scope.indexOf(file) !== -1) {
            return [];
        }
        scope.push(file);

        var contents = fs.readFileSync(file, 'utf8');
        contents = prefilter(contents, file);
        contents = parse(contents, { source: path.relative(root, file) });
        return inline(scope, contents.stylesheet).rules;
    }

    function resolveImport(rule) {
        var name = rule.import.replace(QUOTED, '');
        if (!isNpmImport(name)) {
            return null;
        }

        if (hasOwn(alias, name)) {
            return path.resolve(root, alias[name]);
        }

        var source = rule.position.source;
        var dir = source ? path.dirname(path.resolve(root, source)) : root;

        var file = resolve.sync(name, {
            basedir: dir,
            extensions: ['.css'],
            packageFilter: processPackage
        });

        return path.normalize(file);
    }

    function processPackage(pkg) {
        pkg.main =
            (hasOwn(shim, pkg.name) && shim[pkg.name]) ||
            pkg.style || 'index.css';
        return pkg;
    }

    return function(style) {
        return inline([], style);
    };
}

function identity(value) {
    return value;
}

function hasOwn(obj, prop) {
    return Object.prototype.hasOwnProperty.call(obj, prop);
}

function isNpmImport(path) {
    // Do not import absolute URLs */
    return !ABS_URL.test(path);
}

{'fraction_non_alphanumeric': 0.14887817152442862, 'fraction_numerical': 0.000838750262109457, 'mean_word_length': 2.0953926022063594, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1604607', 'n_tokens_mistral': 1461, 'n_tokens_neox': 1418, 'n_words': 325}