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

needed to expose the match class for feature work #180

Open
wants to merge 1 commit 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
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) {
Copy link
Collaborator

@DevSide DevSide Oct 1, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

forEach is better here

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);