<commit_before>﻿var Q = require('q');
var extend = require('extend');
var jsonPathEngine = require('JSONPath');

var Patch = function (obj) {
    this.id = null;
    this.data = {};
    this.applyAtJsonPath = "$.data"; //apply at the "data" node of the context by default
    this.rule = null;

    for (var prop in obj) {
        this[prop] = obj[prop];
    }

    //Setup expected prototype for the rule
    if (this.rule && this.rule.name) {
        var RuleConstructor = require('./rules')[this.rule.name];
        this.rule = new RuleConstructor(this.rule);
    }
};

Patch.prototype.shouldApply = function (context) {
    if (!this.rule) {
        throw new Error("A rule must be specified. Unable to evaluate.");
    }
    
    return this.rule.evaluate(context);
};

Patch.prototype.apply = function (context) {
    var matchingNodes = jsonPathEngine.eval(context, this.applyAtJsonPath);
    if (matchingNodes.length == 0) {
        throw new Error('Nothing found in context at "' + this.applyAtJsonPath + '". Unable to apply patch.')
    }

    //go patch all the paths it found
    var len = matchingNodes.length;
    for (var i = 0; i < len; i++) {
        extend(true, matchingNodes[i], this.data);
    }

    return Q(context);
};

module.exports = Patch;<commit_msg>Apply instead of error when no rule<commit_after>﻿var Q = require('q');
var extend = require('extend');
var jsonPathEngine = require('JSONPath');

var Patch = function (obj) {
    this.id = null;
    this.data = {};
    this.applyAtJsonPath = "$.data"; //apply at the "data" node of the context by default
    this.rule = null;

    for (var prop in obj) {
        this[prop] = obj[prop];
    }

    //Setup expected prototype for the rule
    if (this.rule && this.rule.name) {
        var RuleConstructor = require('./rules')[this.rule.name];
        this.rule = new RuleConstructor(this.rule);
    }
};

Patch.prototype.shouldApply = function (context) {
    //No rule means apply no matter what, for simplicity of the JSON.
    if (!this.rule) {
        return Q(true);
    }
    
    return this.rule.evaluate(context);
};

Patch.prototype.apply = function (context) {
    var matchingNodes = jsonPathEngine.eval(context, this.applyAtJsonPath);
    if (matchingNodes.length == 0) {
        throw new Error('Nothing found in context at "' + this.applyAtJsonPath + '". Unable to apply patch.')
    }

    //go patch all the paths it found
    var len = matchingNodes.length;
    for (var i = 0; i < len; i++) {
        extend(true, matchingNodes[i], this.data);
    }

    return Q(context);
};

module.exports = Patch;
{'fraction_non_alphanumeric': 0.13758647194465795, 'fraction_numerical': 0.0015372790161414297, 'mean_word_length': 2.8055555555555554, 'pattern_counts': {'":': 0, '<': 5, '<?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': '6224713', 'n_tokens_mistral': 804, 'n_tokens_neox': 779, 'n_words': 239}