From ab29ca18d50e82c4a31a9aa4d56e6f4c08ffdd30 Mon Sep 17 00:00:00 2001 From: Mark Bjerke Date: Fri, 29 Jan 2016 10:29:06 -0800 Subject: [PATCH] needed to expose the match class for feature work added findIndex to lib/extends.js --- lib/context.js | 77 ++++++++++--------------------------------------- lib/extended.js | 23 +++++++++++++++ lib/match.js | 73 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 111 insertions(+), 62 deletions(-) create mode 100644 lib/match.js diff --git a/lib/context.js b/lib/context.js index cc876aa..28b8ac9 100644 --- a/lib/context.js +++ b/lib/context.js @@ -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, @@ -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; diff --git a/lib/extended.js b/lib/extended.js index 3468567..9e800d9 100644 --- a/lib/extended.js +++ b/lib/extended.js @@ -1,4 +1,5 @@ var arr = require("array-extended"), + is = require("is-extended"), unique = arr.unique, indexOf = arr.indexOf, map = arr.map, @@ -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")) @@ -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")); diff --git a/lib/match.js b/lib/match.js new file mode 100644 index 0000000..c819cbb --- /dev/null +++ b/lib/match.js @@ -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); + + +