Comparing version 0.0.1 to 0.0.2
161
index.js
@@ -0,15 +1,45 @@ | ||
/**----------------------------------------------------- | ||
* | ||
* `7MM"""YMM `7MM"""Yb. `7MM"""Yp, | ||
* MM `7 MM `Yb. MM Yb | ||
* MM d `7MM `7MM `7MMpMMMb. MM `Mb MM dP | ||
* MM""MM MM MM MM MM MM MM MM"""bg. | ||
* MM Y MM MM MM MM MM ,MP MM `Y | ||
* MM MM MM MM MM MM ,dP' MM ,9 | ||
* .JMML. `Mbod"YML..JMML JMML..JMMmmmdP' .JMMmmmd9 | ||
* | ||
* A tiny Functional Programming library that works | ||
* like a database that supports (and|or) queries, sorting and indexing | ||
* | ||
* @author joe minichino <joe.minichino@gmail.com> | ||
* | ||
* ------------------------------------------------------*/ | ||
module.exports = (function () { | ||
var FunDB = {}; | ||
function query(coll, fun) { | ||
function curry() { | ||
var fun = arguments[0], | ||
arg = arguments[1]; | ||
return function () { | ||
var args = Array.prototype.slice.call(arguments); | ||
args.unshift(arg); | ||
return fun.apply(null, args); | ||
} | ||
} | ||
function query(fun, coll) { | ||
return coll.filter(fun); | ||
} | ||
function iquery(coll, fun) { | ||
return query(fun, coll); | ||
} | ||
function andTest(coll, filters) { | ||
var result = coll, | ||
var result = fork(coll), | ||
i = 0, | ||
len = coll.length; | ||
len = filters.length; | ||
for (i; i < len; i += 1) { | ||
result = query(result, filters[i]); | ||
result = query(filters[i], result); | ||
} | ||
@@ -19,27 +49,23 @@ return result; | ||
function arrayUnique(array) { | ||
var a = array.concat(); | ||
for (var i = 0; i < a.length; ++i) { | ||
for (var j = i + 1; j < a.length; ++j) { | ||
if (JSON.stringify(a[i]) === JSON.stringify(a[j])) | ||
a.splice(j--, 1); | ||
} | ||
} | ||
return a; | ||
} | ||
function orTest(coll, filters) { | ||
var result = [], | ||
i = 0, | ||
len = coll.length; | ||
len = filters.length; | ||
coll.forEach(function (elem) { | ||
for (i = 0; i < len; i += 1) { | ||
if (filters[i].apply(null, [elem])) { | ||
result.push(JSON.parse(JSON.stringify(elem))); | ||
break; | ||
} | ||
} | ||
}); | ||
return result; | ||
}; | ||
for (i; i < len; i += 1) { | ||
result.concat(query(coll, filters[i])); | ||
} | ||
return arrayUnique(result); | ||
function reduce(fun, coll) { | ||
return coll.reduce(fun); | ||
} | ||
function reduce(coll, fun) { | ||
return coll.reduce(fun); | ||
function ireduce(coll, fun) { | ||
return reduce(fun, coll); | ||
} | ||
@@ -55,5 +81,5 @@ | ||
function index(coll, sortFun) { | ||
function index(sortFun, coll) { | ||
var copy = fork(coll), | ||
index = [];; | ||
index = []; | ||
copy.forEach(addTestIndex); | ||
@@ -66,37 +92,80 @@ | ||
delete copy; | ||
return index; | ||
var i = {}; | ||
i.ids = index; | ||
i.first = function () { | ||
return coll[i.ids[0]]; | ||
}; | ||
i.last = function () { | ||
return coll[i.ids.length - 1]; | ||
}; | ||
i.get = function (pos) { | ||
return coll[pos]; | ||
}; | ||
i.insert = function (item, pos) { | ||
coll.push(item); | ||
i.ids.splice(pos, 0, coll.length - 1); | ||
}; | ||
return i; | ||
} | ||
function addData(data, coll, callback) { | ||
coll.push(data); | ||
callback(); | ||
function iindex(coll, sortFun) { | ||
return index(sortFun, coll); | ||
} | ||
function curryWrite(filename, writeFun) { | ||
return function (callback) { | ||
writeFun.apply(null, [filename, callback]); | ||
function curryFilter(filter) { | ||
return function (coll) { | ||
return query(filter, coll); | ||
} | ||
} | ||
function curryData(coll) { | ||
return function (filter) { | ||
return query(coll, filter); | ||
function searchSorted(coll, index, item, fun) { | ||
var lo = 0, | ||
hi = coll.length, | ||
compared, | ||
mid; | ||
if (!fun) { | ||
fun = function (a, b) { | ||
return (a < b) ? -1 : ((a > b) ? 1 : 0); | ||
} | ||
} | ||
} | ||
function curryQuery(filter) { | ||
return function (coll) { | ||
return query(coll, filter); | ||
while (lo < hi) { | ||
mid = ((lo + hi) / 2) | 0; | ||
compared = fun(item, coll[index.ids[mid]]); | ||
if (compared == 0) { | ||
return { | ||
found: true, | ||
index: mid | ||
}; | ||
} else if (compared < 0) { | ||
hi = mid; | ||
} else { | ||
lo = mid + 1; | ||
} | ||
} | ||
return { | ||
found: false, | ||
index: hi | ||
}; | ||
} | ||
var FunDB = {}; | ||
FunDB.query = query; | ||
FunDB.reduce = reduce; | ||
FunDB.andTest = andTest; | ||
FunDB.orTest = orTest; | ||
FunDB.and = andTest; | ||
FunDB.or = orTest; | ||
FunDB.fork = fork; | ||
FunDB.index = index; | ||
FunDB.addData = addData; | ||
FunDB.curryWrite = curryWrite; | ||
FunDB.curryQuery = curryQuery; | ||
FunDB._ = curry; | ||
FunDB.curryFilter = curryFilter; | ||
FunDB.search = searchSorted; | ||
FunDB.curry = function (coll) { | ||
var db = {}; | ||
db.query = curry(iquery, coll); | ||
db.index = curry(iindex, coll); | ||
db.search = curry(searchSorted, coll); | ||
db.or = curry(orTest, coll); | ||
db.and = curry(andTest, coll); | ||
db.reduce = curry(ireduce, coll); | ||
return db; | ||
}; | ||
@@ -103,0 +172,0 @@ return FunDB; |
{ | ||
"name": "fundb", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "funktional programming database", | ||
@@ -23,3 +23,7 @@ "main": "index.js", | ||
}, | ||
"homepage": "https://github.com/techfort/fdb" | ||
"homepage": "https://github.com/techfort/fdb", | ||
"dependencies": { | ||
"msgpack5": "^1.3.5", | ||
"stream": "0.0.2" | ||
} | ||
} |
@@ -1,7 +0,54 @@ | ||
# FunDB | ||
# Fun(ctional)DB (FunDB) | ||
a functional database | ||
Fun(ctional)DB is an experiment that follows the [Unix philosophy](http://en.wikipedia.org/wiki/Unix_philosophy) (small & specific). | ||
As most database systems in existence are monolithic, both in size and mentailty, this experiments aims at taking the opposite approach. | ||
As such it is not a drop-in replacement for any db system in existence, rather a "new" option to be considered. | ||
tl; dr FunDB is a micro functional programming library performing database functions such | ||
* querying / filtering | ||
* sorting | ||
* indexing | ||
* searching | ||
or better also, a micro-database that is storage agnostic and client agnostic. | ||
## Overview | ||
State is extracted from the db | ||
The point of FunDB is to extract state from the database as much as possible. You can manage writes to disk outside of FunDB (see an example in test.js) and only perform processing with FunDB. | ||
You can use FunDB in two ways: plain functions or curried around a data collection (which means you do not need to inject your data in the db functions anymore). | ||
## FunDB plain functions | ||
`curry(fun, arg)`: the heart of FunDB is a simple curry function, which takes a function and the first argument of that function. Given a function `f(a, b, c, d)` you can call `curry(f, 'hello')` which returns a function equivalent to `f('hello', b, c, d)`. Note that `curry` only curries one argument at a time (unlike the more advanced curry function in [ramda](http://ramdajs.com/docs/R.html#curry)) | ||
`query(filterFun, array)`: this effectively operating a `filter` on the array, and returning a filtered array. | ||
`reduce(reduceFun, array)`: again, operates a native `reduce` on the array, returns the reduced value. | ||
`index(sortFun, array)`: this function returns an object index, which contains a property `ids` which is an array of integers representing the order of the elements in `array` as sorted by the function `sortFun`. So if you have an array of objects, each object containing the property `age`, you could use a sort function like `function (a, b) { return a.age > b.age; }` (which sorts the array elements in ascending age order). Except, the originally array is untouched, while an object `index` is returned. This index object has the following properties: | ||
* `ids`: an array of integer representing the order of elements of the original array if they were to be sorted by `sortFun` | ||
* `first()`: gets the first element in the original array according to `sortFun` (i.e. the object with the lowest age value) | ||
* `last()`: get the last element (i.e. the oldest person in the array) | ||
* `get(position)`: gets the element in the original array corresponding to that position in the index | ||
* `insert(item, pos)`: inserts an element into the original array and updates the index of ids `ids` (*NOTE*: this is the only function with a side effect in that the original array is mutated). | ||
`searchSorted(array, index, item, compareFun)`: this function performs a binary search on `array`, utilising the index object `index`, on an item `item` utilising the (optional) comparison function `compareFun`. `compareFun` only needs to be there in case the concept of `less` or `equal` is not as obvious as comparing numbers or strings (i.e. you may want to compare the length of a string for some). `compareFun` takes two arguments `a` and `b` and compares them, returnning `-1` if a is "less" than b, `0` if the search is a match (and the position in the array will be included in the object returned), and `1` if a is "greater" than b. The function returns an object with two properties: `found` to indicate the item was found and `index` which indicates the position at which the item was found or the position at which the item would be inserted if you were to insert it in the original array. You would use the index method `insert(item, pos)` to operate that. | ||
`and(array, filtersArray)`: this function queries the original array against all the filter functions contained in the array `filtersArray`. Returns an array containing elements that passed all filter tests. Each filter function needs to return a boolean value (true for pass, false for fail). An example usage would be `fundb.and(array, [filterByAgeFunction, filterByLanguageFunction])`. | ||
`or(array, filtersArray)`: like `and` but it performs an `OR` operation between filters and returns an array containing all elements that passed at least one of the filter function tests. Filter function criteria are identical to `and`. | ||
`curryFilter(filterFunction)`: takes a filter function and returns a curried `query` function which can then be called on an array to apply the filter. I.e. `var byAge = function (obj) { return obj.age > 30; }` and then call `byAge(array)` to obtain an array of elements that pass the test. This is a utility method, you could just as easily run `fundb.curry(fundb.query, filterFunction)`. | ||
## Array-centric functions | ||
`FunDB.curry(array)`: this function returns an object with all the above functions as methods (it performs a curry on the array on all those functions and associates them to a single object), except you can skip referencing the data array. So for example after calling: `var db = FunDB.curry(array);` you can then do `db.index(sortFun)` or `db.query(filterFun)` etc.. In summary the object will have the following methods available: | ||
* `db.query(filterFun)` | ||
* `db.index(sortFun)` | ||
* `db.search(index, item, compareFun)` | ||
* `db.or(filtersArray)` | ||
* `db.and(filtersArray)` | ||
* `db.reduce(reduceFun)` |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12367
230
55
2
1
+ Addedmsgpack5@^1.3.5
+ Addedstream@0.0.2
+ Addedbl@0.9.5(transitive)
+ Addedcore-util-is@1.0.3(transitive)
+ Addedemitter-component@1.1.2(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedisarray@0.0.1(transitive)
+ Addedmsgpack5@1.6.0(transitive)
+ Addedreadable-stream@1.0.341.1.14(transitive)
+ Addedstream@0.0.2(transitive)
+ Addedstring_decoder@0.10.31(transitive)