Comparing version 0.0.1 to 0.1.1
@@ -19,3 +19,4 @@ { | ||
"fantasy-states": "0.2.x", | ||
"fantasy-tuples": "0.0.x" | ||
"fantasy-tuples": "0.0.x", | ||
"daggy": "0.0.x" | ||
}, | ||
@@ -32,4 +33,5 @@ "devDependencies": { | ||
}, | ||
"files": ["defenders.js", "src/*.js"], | ||
"main": "src/server.js", | ||
"version": "0.0.1" | ||
"version": "0.1.1" | ||
} |
@@ -9,2 +9,15 @@ # Defender | ||
### Build | ||
#### Browser | ||
You have a lot of alternatives (requirejs, AMD), but to get started | ||
quickly you can use [commonjseverywhere](https://github.com/michaelficarra/commonjs-everywhere). | ||
``` | ||
cjsify defenders.js --no-node --export defenders --source-map defenders.browser.js.map >defenders.browser.js | ||
``` | ||
* * * | ||
#### Demo | ||
@@ -97,3 +110,3 @@ | ||
The Fortune Teller job is to decipher what the input field could | ||
The Fortune Tellers job is to decipher what the input field could | ||
possibly look like on key down, before the text has been entered. | ||
@@ -118,1 +131,12 @@ From there it should be possible to pipe this into the Defenders | ||
``` | ||
* * * | ||
### Steward | ||
A Steward attends over the processing of a value, by looking in to | ||
the value, attending to it's various needs before putting the value | ||
back into it's original type. | ||
The role of the Steward is to preside over internal modifications | ||
of a value. |
@@ -1,8 +0,7 @@ | ||
var Validation = require('fantasy-validations'), | ||
combinators = require('fantasy-combinators'), | ||
var combinators = require('fantasy-combinators'), | ||
daggy = require('daggy'), | ||
IO = require('fantasy-io'), | ||
Maybe = require('fantasy-options'), | ||
State = require('fantasy-states'), | ||
Tuples = require('fantasy-tuples'), | ||
Either = require('fantasy-eithers'), | ||
@@ -12,11 +11,15 @@ compose = combinators.compose, | ||
Success = Validation.Success, | ||
Failure = Validation.Failure, | ||
Tuple2 = Tuples.Tuple2, | ||
Tuple4 = Tuples.Tuple4, | ||
Left = Either.Left, | ||
Right = Either.Right, | ||
Triad = daggy.taggedSum({ | ||
Success: ['x'], | ||
Maybe: ['flow'], | ||
Failure: ['errors'] | ||
}), | ||
Flow = daggy.taggedSum({ | ||
Underflow: ['underflow', 'position'], | ||
Overflow: ['overflow', 'position'] | ||
}), | ||
Tuple2 = Tuples.Tuple2, | ||
Tuple3 = Tuples.Tuple3, | ||
executeExpr = function(a) { | ||
@@ -28,46 +31,67 @@ return function(b) { | ||
push = function(a, b) { | ||
var x = a.slice(); | ||
x.push(b); | ||
return x; | ||
}, | ||
validateString = function(program, index, accum, original, modified) { | ||
return function(possible) { | ||
if(possible) { | ||
return constant( | ||
Tuple4( | ||
program, | ||
original, | ||
modified.slice(possible[0].length), | ||
accum | ||
) | ||
); | ||
} else { | ||
return constant( | ||
Tuple4( | ||
program, | ||
original, | ||
modified, | ||
push(accum, Tuple2(modified, index)) | ||
) | ||
); | ||
} | ||
}; | ||
}, | ||
consume = function(a) { | ||
var string = a, | ||
M = State.StateT(IO); | ||
var M = State.StateT(IO), | ||
return function(b) { | ||
var accum = [], | ||
value, | ||
match, | ||
possible, | ||
position, | ||
i; | ||
rec = function(index, guards) { | ||
return function(tuple) { | ||
var program = tuple._1, | ||
original = tuple._2, | ||
modified = tuple._3, | ||
maybes = tuple._4, | ||
guard; | ||
/* Re-factor this to become recursive and only perform unsafe at the end */ | ||
for(i = 0; i < b.length; i++) { | ||
value = b[i]; | ||
if (index < guards.length) { | ||
guard = guards[index]; | ||
possible = | ||
M.lift(value) | ||
.chain(compose(M.modify)(executeExpr(string))) | ||
.chain(constant(M.get)) | ||
.exec('') | ||
.unsafePerform(); | ||
if(possible) { | ||
match = possible[0]; | ||
string = string.slice(match.length); | ||
} else { | ||
position = (a.length - string.length) + 1; | ||
accum.push(Tuple3(string, Maybe.Some(value), position)); | ||
/* Let's move the string onwards */ | ||
if (string.length < 1) | ||
break; | ||
string = string.slice(1); | ||
} | ||
} | ||
return Tuple3(a, string, accum); | ||
return program | ||
.chain(constant(M.lift(guard))) | ||
.chain(compose(M.modify)(executeExpr(modified))) | ||
.chain(constant(M.get)) | ||
.chain(compose(M.modify)(validateString(program, index, maybes, original, modified))) | ||
.chain(constant(M.get)) | ||
.chain(rec(index + 1, guards)); | ||
} else { | ||
return program; | ||
} | ||
}; | ||
}; | ||
return function(b) { | ||
return rec(0, b)(Tuple4(M.of(''), a, a, [])); | ||
}; | ||
}, | ||
containsError = function(x) { | ||
contains = function(x, f) { | ||
var i; | ||
for(i = 0; i < x.length; i++) { | ||
if (x[i]._1 !== '') | ||
for (i = 0; i < x.length; i++) { | ||
if (f(x[i])) | ||
return true; | ||
@@ -78,27 +102,38 @@ } | ||
createFailure = function(string, position) { | ||
return Failure([Tuple3(string, Maybe.None, position)]); | ||
filter = function(x, f) { | ||
var accum = [], | ||
i; | ||
for (i = 0; i < x.length; i++) { | ||
if (f(x[i])) | ||
accum.push(x[i]); | ||
} | ||
return accum; | ||
}, | ||
output = function() { | ||
return function(x) { | ||
var string = x._2, | ||
possibleErrors = x._3; | ||
empty = function(x) { | ||
return x._1 !== ''; | ||
}, | ||
/* Re-factor this, as it's really messy */ | ||
if (possibleErrors.length < 1 && string.length > 0) { | ||
/* Check for overflows */ | ||
return Left(createFailure(string, (x._1.length - string.length) + 1)); | ||
output = function(a) { | ||
return function() { | ||
var original = a._2, | ||
modified = a._3, | ||
possibleErrors = a._4, | ||
actualErrors = filter(possibleErrors, empty); | ||
if (possibleErrors.length < 1 && modified.length > 0) { | ||
// Check for overflows | ||
return Triad.Maybe(Flow.Overflow(modified, original.length - modified.length)); | ||
} else if(possibleErrors.length > 0) { | ||
if (string.length < 1 && !containsError(possibleErrors)) { | ||
/* Check for underflows */ | ||
return Right(createFailure(string, string.length)); | ||
if (modified.length < 1 && !contains(possibleErrors, empty)) { | ||
// Check for underflows | ||
return Triad.Maybe(Flow.Underflow(modified, modified.length)); | ||
} else | ||
return Left(Failure(possibleErrors)); | ||
return Triad.Failure(actualErrors); | ||
} else | ||
return Right(Success(x._1)); | ||
return Triad.Success(original); | ||
}; | ||
@@ -115,7 +150,14 @@ }, | ||
.chain(constant(M.lift(stream))) | ||
.chain(compose(M.modify)(consume)) | ||
.chain(function(a) { | ||
return M.modify(function(b) { | ||
return consume(a)(b).exec(''); | ||
}); | ||
}) | ||
.chain(constant(M.get)) | ||
.chain(function(a) { | ||
return M.lift(a); | ||
}) | ||
.chain(compose(M.modify)(output)) | ||
.chain(constant(M.get)); | ||
return program.exec([]); | ||
@@ -122,0 +164,0 @@ }; |
var combinators = require('fantasy-combinators'), | ||
IO = require('fantasy-io'), | ||
State = require('fantasy-states'), | ||
steward = require('./steward'), | ||
@@ -10,8 +11,2 @@ ap = combinators.apply, | ||
dimap = function(a, b) { | ||
return function(c) { | ||
return compose(compose(b)(c))(a); | ||
}; | ||
}, | ||
split = function(a) { | ||
@@ -74,3 +69,3 @@ return function(b) { | ||
return function(b) { | ||
var list = dimap(split(''), join('')); | ||
var list = steward(split(''), join('')); | ||
return list(replace(a))(b); | ||
@@ -96,3 +91,3 @@ }; | ||
var M = State.StateT(IO), | ||
values = | ||
@@ -104,5 +99,5 @@ M.lift(key) | ||
.chain(constant(M.get)), | ||
possible = values.exec(''), | ||
fortune = | ||
@@ -119,3 +114,3 @@ M.lift(io) | ||
.chain(constant(M.get)); | ||
return fortune.exec(''); | ||
@@ -122,0 +117,0 @@ }; |
var combinators = require('fantasy-combinators'), | ||
IO = require('fantasy-io'), | ||
State = require('fantasy-states'), | ||
steward = require('./steward'), | ||
@@ -9,8 +10,2 @@ ap = combinators.apply, | ||
dimap = function(a, b) { | ||
return function(c) { | ||
return compose(compose(b)(c))(a); | ||
}; | ||
}, | ||
split = function(a) { | ||
@@ -40,3 +35,3 @@ return function(b) { | ||
return function(x) { | ||
return dimap(split(''), join(''))(f)(x); | ||
return steward(split(''), join(''))(f)(x); | ||
}; | ||
@@ -60,4 +55,3 @@ }, | ||
program = | ||
M.lift(f) | ||
program = M.lift(f) | ||
.chain(compose(M.modify)( | ||
@@ -64,0 +58,0 @@ function(a) { |
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
140
0
17355
9
9
417
1
+ Addeddaggy@0.0.x