Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ce collect rc #184

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions lib/compile/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ var createRuleFromObject = (function () {
function parseRule(rule, conditions, identifiers, defined, name) {
if (rule.length) {
var r0 = rule[0];
if (r0 === "not" || r0 === "exists") {
if (r0 === "not" || r0 === "exists" || r0 === "collect" ) {
var temp = [];
rule.shift();
__resolveRule(rule, identifiers, temp, defined, name);
Expand Down Expand Up @@ -135,7 +135,10 @@ var createRuleFromObject = (function () {
forEach(constraints, function (rule) {
parseRule(rule, conditions, identifiers, defined, name);
});
return rules.createRule(name, options, conditions, parseAction(action, identifiers, defined, scope));
var ruleScope = merge(defined, scope);
var copyOptions = merge({}, options);
copyOptions.scope = ruleScope;
return rules.createRule(name, copyOptions, conditions, parseAction(action, identifiers, defined, scope));
};
})();

Expand All @@ -158,7 +161,7 @@ exports.compile = function (flowObj, options, cb, Container) {
throw new Error("Name must be present in JSON or options");
}
var flow = new Container(name);
var defined = merge({Array: Array, String: String, Number: Number, Boolean: Boolean, RegExp: RegExp, Date: Date, Object: Object}, options.define || {});
var defined = merge({Array: Array, String: String, Number: Number, Boolean: Boolean, RegExp: RegExp, Date: Date, Object: Object}, ( (options.define || options.defines || options.defined) || {}));
if (typeof Buffer !== "undefined") {
defined.Buffer = Buffer;
}
Expand Down
77 changes: 15 additions & 62 deletions lib/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ var extd = require("./extended"),
indexOf = extd.indexOf,
pPush = Array.prototype.push;

var Match = require('./match.js');

function createContextHash(paths, hashCode) {
var ret = "",
i = -1,
Expand All @@ -16,77 +18,28 @@ function createContextHash(paths, hashCode) {
return ret;
}

function merge(h1, h2, aliases) {
var i = -1, l = aliases.length, alias;
while (++i < l) {
alias = aliases[i];
h1[alias] = h2[alias];
}
}

function unionRecency(arr, arr1, arr2) {
pPush.apply(arr, arr1);
var i = -1, l = arr2.length, val, j = arr.length;
function createPathstHash(paths, hashCode) {
var ret = "",
i = -1,
l = paths.length;
while (++i < l) {
val = arr2[i];
if (indexOf(arr, val) === -1) {
arr[j++] = val;
}
ret += paths[i].id + ":";
}
return ret;
}

var Match = declare({
instance: {

isMatch: true,
hashCode: "",
facts: null,
factIds: null,
factHash: null,
recency: null,
aliases: null,

constructor: function () {
this.facts = [];
this.factIds = [];
this.factHash = {};
this.recency = [];
this.aliases = [];
},

addFact: function (assertable) {
pPush.call(this.facts, assertable);
pPush.call(this.recency, assertable.recency);
pPush.call(this.factIds, assertable.id);
this.hashCode = this.factIds.join(":");
return this;
},

merge: function (mr) {
var ret = new Match();
ret.isMatch = mr.isMatch;
pPush.apply(ret.facts, this.facts);
pPush.apply(ret.facts, mr.facts);
pPush.apply(ret.aliases, this.aliases);
pPush.apply(ret.aliases, mr.aliases);
ret.hashCode = this.hashCode + ":" + mr.hashCode;
merge(ret.factHash, this.factHash, this.aliases);
merge(ret.factHash, mr.factHash, mr.aliases);
unionRecency(ret.recency, this.recency, mr.recency);
return ret;
}
}
});

var Context = declare({
instance: {
match: null,
factHash: null,
aliases: null,
fact: null,
hashCode: null,
paths: null,
pathsHash: null,
match: null,
factHash: null,
aliases: null,
fact: null,
hashCode: null,
paths: null,
pathsHash: null,

constructor: function (fact, paths, mr) {
this.fact = fact;
Expand Down
23 changes: 23 additions & 0 deletions lib/extended.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var arr = require("array-extended"),
is = require("is-extended"),
unique = arr.unique,
indexOf = arr.indexOf,
map = arr.map,
Expand Down Expand Up @@ -126,6 +127,27 @@ function diffHash(h1, h2) {
function union(arr1, arr2) {
return unique(arr1.concat(arr2));
}

function findIndex(coll, item) {
var target = -1;
if( 'function' === typeof item ) {
coll.some(function(e, i) {
if(item(e,i)) {
target = i;
return true;
};
});
}
else {
coll.some(function(e, i) {
if(is.deepEqual(e, item)) {
target = i;
return true;
};
});
}
return target;
}

module.exports = require("extended")()
.register(require("date-extended"))
Expand All @@ -145,5 +167,6 @@ module.exports = require("extended")()
.register("HashTable", require("ht"))
.register("declare", require("declare.js"))
.register(require("leafy"))
.register('findIndex', findIndex)
.register("LinkedList", require("./linkedList"));

73 changes: 73 additions & 0 deletions lib/match.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
"use strict";
var extd = require("./extended"),
isBoolean = extd.isBoolean,
declare = extd.declare,
indexOf = extd.indexOf,
pPush = Array.prototype.push;


function merge(h1, h2, aliases) {
var i = -1, l = aliases.length, alias;
while (++i < l) {
alias = aliases[i];
h1[alias] = h2[alias];
}
}

function unionRecency(arr, arr1, arr2) {
pPush.apply(arr, arr1);
var i = -1, l = arr2.length, val, j = arr.length;
while (++i < l) {
val = arr2[i];
if (indexOf(arr, val) === -1) {
arr[j++] = val;
}
}
}

var Match = declare({
instance: {

isMatch: true,
hashCode: "",
facts: null,
factIds: null,
factHash: null,
recency: null,
aliases: null,

constructor: function () {
this.facts = [];
this.factIds = [];
this.factHash = {};
this.recency = [];
this.aliases = [];
},

addFact: function (assertable) {
pPush.call(this.facts, assertable);
pPush.call(this.recency, assertable.recency);
pPush.call(this.factIds, assertable.id);
this.hashCode = this.factIds.join(":");
return this;
},
merge: function (mr) {
var ret = new Match();
ret.isMatch = mr.isMatch;
pPush.apply(ret.facts, this.facts);
pPush.apply(ret.facts, mr.facts);
pPush.apply(ret.factIds, this.factIds);
pPush.apply(ret.factIds, mr.factIds);
pPush.apply(ret.aliases, this.aliases);
pPush.apply(ret.aliases, mr.aliases);
ret.hashCode = this.hashCode + ":" + mr.hashCode;
merge(ret.factHash, this.factHash, this.aliases);
merge(ret.factHash, mr.factHash, mr.aliases);
unionRecency(ret.recency, this.recency, mr.recency);
return ret;
}
}
}).as(module);



Loading