roll-parser 2.3.2 → 3.0.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (109) hide show
  1. package/CHANGELOG.md +91 -0
  2. package/README.md +175 -170
  3. package/dist/cli/args.d.ts +35 -0
  4. package/dist/cli/args.d.ts.map +1 -0
  5. package/dist/cli/format.d.ts +20 -0
  6. package/dist/cli/format.d.ts.map +1 -0
  7. package/dist/cli/index.d.ts +8 -0
  8. package/dist/cli/index.d.ts.map +1 -0
  9. package/dist/cli.js +2608 -0
  10. package/dist/cli.js.map +28 -0
  11. package/dist/errors.d.ts +42 -0
  12. package/dist/errors.d.ts.map +1 -0
  13. package/dist/evaluator/evaluator.d.ts +121 -0
  14. package/dist/evaluator/evaluator.d.ts.map +1 -0
  15. package/dist/evaluator/index.d.ts +8 -0
  16. package/dist/evaluator/index.d.ts.map +1 -0
  17. package/dist/evaluator/modifiers/compare.d.ts +15 -0
  18. package/dist/evaluator/modifiers/compare.d.ts.map +1 -0
  19. package/dist/evaluator/modifiers/crit-threshold.d.ts +28 -0
  20. package/dist/evaluator/modifiers/crit-threshold.d.ts.map +1 -0
  21. package/dist/evaluator/modifiers/explode.d.ts +48 -0
  22. package/dist/evaluator/modifiers/explode.d.ts.map +1 -0
  23. package/dist/evaluator/modifiers/keep-drop.d.ts +44 -0
  24. package/dist/evaluator/modifiers/keep-drop.d.ts.map +1 -0
  25. package/dist/evaluator/modifiers/reroll.d.ts +33 -0
  26. package/dist/evaluator/modifiers/reroll.d.ts.map +1 -0
  27. package/dist/evaluator/modifiers/sort.d.ts +23 -0
  28. package/dist/evaluator/modifiers/sort.d.ts.map +1 -0
  29. package/dist/evaluator/modifiers/success-count.d.ts +28 -0
  30. package/dist/evaluator/modifiers/success-count.d.ts.map +1 -0
  31. package/dist/index.d.ts +20 -0
  32. package/dist/index.d.ts.map +1 -0
  33. package/dist/index.js +2560 -0
  34. package/dist/index.js.map +26 -0
  35. package/dist/lexer/lexer.d.ts +90 -0
  36. package/dist/lexer/lexer.d.ts.map +1 -0
  37. package/dist/lexer/tokens.d.ts +107 -0
  38. package/dist/lexer/tokens.d.ts.map +1 -0
  39. package/dist/parser/ast.d.ts +368 -0
  40. package/dist/parser/ast.d.ts.map +1 -0
  41. package/dist/parser/parser.d.ts +145 -0
  42. package/dist/parser/parser.d.ts.map +1 -0
  43. package/dist/rng/index.d.ts +8 -0
  44. package/dist/rng/index.d.ts.map +1 -0
  45. package/dist/rng/mock.d.ts +36 -0
  46. package/dist/rng/mock.d.ts.map +1 -0
  47. package/dist/rng/seeded.d.ts +40 -0
  48. package/dist/rng/seeded.d.ts.map +1 -0
  49. package/dist/rng/types.d.ts +24 -0
  50. package/dist/rng/types.d.ts.map +1 -0
  51. package/dist/roll.d.ts +51 -0
  52. package/dist/roll.d.ts.map +1 -0
  53. package/dist/testing.d.ts +11 -0
  54. package/dist/testing.d.ts.map +1 -0
  55. package/dist/testing.js +41 -0
  56. package/dist/testing.js.map +11 -0
  57. package/dist/types.d.ts +246 -0
  58. package/dist/types.d.ts.map +1 -0
  59. package/package.json +66 -43
  60. package/src/cli/args.ts +73 -0
  61. package/src/cli/format.ts +49 -0
  62. package/src/cli/index.ts +92 -0
  63. package/src/errors.ts +90 -0
  64. package/src/evaluator/evaluator.ts +1552 -0
  65. package/src/evaluator/index.ts +14 -0
  66. package/src/evaluator/modifiers/compare.ts +29 -0
  67. package/src/evaluator/modifiers/crit-threshold.ts +59 -0
  68. package/src/evaluator/modifiers/explode.ts +219 -0
  69. package/src/evaluator/modifiers/keep-drop.ts +151 -0
  70. package/src/evaluator/modifiers/reroll.ts +165 -0
  71. package/src/evaluator/modifiers/sort.ts +30 -0
  72. package/src/evaluator/modifiers/success-count.ts +59 -0
  73. package/src/index.ts +90 -0
  74. package/src/lexer/lexer.ts +374 -0
  75. package/src/lexer/tokens.ts +187 -0
  76. package/src/parser/ast.ts +628 -0
  77. package/src/parser/parser.ts +1239 -0
  78. package/src/rng/index.ts +8 -0
  79. package/src/rng/mock.ts +65 -0
  80. package/src/rng/seeded.ts +157 -0
  81. package/src/rng/types.ts +25 -0
  82. package/src/roll.ts +74 -0
  83. package/src/testing.ts +16 -0
  84. package/src/types.ts +237 -0
  85. package/bin/help.txt +0 -19
  86. package/bin/roll-parser.js +0 -23
  87. package/dist/roll-parser.js +0 -697
  88. package/dist/roll-parser.min.js +0 -1
  89. package/index.js +0 -24
  90. package/src/complex/parse.js +0 -37
  91. package/src/complex/parseAndRoll.js +0 -21
  92. package/src/complex/parseAndRollClassic.js +0 -20
  93. package/src/complex/parseAndRollSimple.js +0 -19
  94. package/src/complex/parseAndRollWod.js +0 -20
  95. package/src/complex/parseClassicRoll.js +0 -22
  96. package/src/complex/parseSimpleRoll.js +0 -21
  97. package/src/complex/parseWodRoll.js +0 -21
  98. package/src/converter.js +0 -45
  99. package/src/grammar.js +0 -14
  100. package/src/mapper.js +0 -50
  101. package/src/normalizer.js +0 -51
  102. package/src/object/Result.js +0 -23
  103. package/src/object/Roll.js +0 -26
  104. package/src/object/Type.js +0 -5
  105. package/src/object/WodRoll.js +0 -29
  106. package/src/parser.js +0 -43
  107. package/src/random.js +0 -22
  108. package/src/roller.js +0 -106
  109. package/src/stringifier.js +0 -33
@@ -1 +0,0 @@
1
- (function(a){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=a();else if("function"==typeof define&&define.amd)define([],a);else{var b;b="undefined"==typeof window?"undefined"==typeof global?"undefined"==typeof self?this:self:global:window,b.rollParser=a()}})(function(){return function b(c,d,e){function a(h,i){if(!d[h]){if(!c[h]){var j="function"==typeof require&&require;if(!i&&j)return j(h,!0);if(g)return g(h,!0);var k=new Error("Cannot find module '"+h+"'");throw k.code="MODULE_NOT_FOUND",k}var f=d[h]={exports:{}};c[h][0].call(f.exports,function(b){var d=c[h][1][b];return a(d?d:b)},f,f.exports,b,c,d,e)}return d[h].exports}for(var g="function"==typeof require&&require,f=0;f<e.length;f++)a(e[f]);return a}({1:[function(a,b){b.exports={Roll:a("./src/object/Roll"),WodRoll:a("./src/object/WodRoll"),Result:a("./src/object/Result"),parse:a("./src/complex/parse"),parseClassicRoll:a("./src/complex/parseClassicRoll"),parseSimpleRoll:a("./src/complex/parseSimpleRoll"),parseWodRoll:a("./src/complex/parseWodRoll"),roll:a("./src/roller").rollAny,rollClassic:a("./src/roller").rollClassic,rollWod:a("./src/roller").rollWod,parseAndRoll:a("./src/complex/parseAndRoll"),parseAndRollSimple:a("./src/complex/parseAndRollSimple"),parseAndRollClassic:a("./src/complex/parseAndRollClassic"),parseAndRollWod:a("./src/complex/parseAndRollWod"),random:a("./src/random").randomRoll,convert:a("./src/converter").convertToAnyRoll}},{"./src/complex/parse":2,"./src/complex/parseAndRoll":3,"./src/complex/parseAndRollClassic":4,"./src/complex/parseAndRollSimple":5,"./src/complex/parseAndRollWod":6,"./src/complex/parseClassicRoll":7,"./src/complex/parseSimpleRoll":8,"./src/complex/parseWodRoll":9,"./src/converter":10,"./src/object/Result":14,"./src/object/Roll":15,"./src/object/WodRoll":17,"./src/random":19,"./src/roller":20}],2:[function(a,b){const{parseAny:c}=a("../parser"),{mapToRoll:d,mapToWodRoll:e}=a("../mapper"),f=a("../object/Type");b.exports=function(a){const b=c(a),g=b?b.type:"";return g===f.simple?d(b):g===f.classic?d(b):g===f.wod?e(b):null}},{"../mapper":12,"../object/Type":16,"../parser":18}],3:[function(a,b){const{rollAny:c}=a("../roller"),d=a("./parse");b.exports=(a)=>c(d(a))},{"../roller":20,"./parse":2}],4:[function(a,b){const{rollClassic:c}=a("../roller"),d=a("./parseClassicRoll");b.exports=(a)=>c(d(a))},{"../roller":20,"./parseClassicRoll":7}],5:[function(a,b){const{rollClassic:c}=a("../roller"),d=a("./parseSimpleRoll");b.exports=(a)=>c(d(a))},{"../roller":20,"./parseSimpleRoll":8}],6:[function(a,b){const{rollWod:c}=a("../roller"),d=a("./parseWodRoll");b.exports=(a)=>c(d(a))},{"../roller":20,"./parseWodRoll":9}],7:[function(a,b){const{parseClassic:c}=a("../parser"),{mapToRoll:d}=a("../mapper");b.exports=(a)=>d(c(a))},{"../mapper":12,"../parser":18}],8:[function(a,b){const{parseSimple:c}=a("../parser"),{mapToRoll:d}=a("../mapper");b.exports=(a)=>d(c(a))},{"../mapper":12,"../parser":18}],9:[function(a,b){const{parseWod:c}=a("../parser"),{mapToWodRoll:d}=a("../mapper");b.exports=(a)=>d(c(a))},{"../mapper":12,"../parser":18}],10:[function(a,b){function c(a={}){const{dice:b,count:c,modifier:d}=a||{};return new f(b,c,d)}function d(a={}){const{dice:b,count:c,again:d,success:e,fail:f}=a||{};return new g(b,c,d,e,f)}const{isAbsent:e}=a("./normalizer"),f=a("./object/Roll"),g=a("./object/WodRoll");b.exports={convertToRoll:c,convertToWodRoll:d,convertToAnyRoll:function(a={}){const{again:b,success:f,fail:g}=a||{};return e(b)&&e(f)&&e(g)?c(a):d(a)}}},{"./normalizer":13,"./object/Roll":15,"./object/WodRoll":17}],11:[function(a,b){b.exports={simple:[/^(\d+)(?:\s+(\d+)(?:\s+([+-]?\d+))?)?$/],classic:[/^(\d*)(?:[dD])(\d+)([+-]\d+)?$/],wod:[/^(\d*)(?:[dD])(\d+)(!?)$/,/^(\d*)(?:[dD])(\d+)(!?)(?:>)(\d+)(?:f(\d+))?$/]}},{}],12:[function(a,b){function c(a){const b=!a||2>a.length;return b?null:a.slice(1).map(d)}const{normalizeRegexResult:d,isAbsent:e}=a("./normalizer"),f=a("./object/Roll"),g=a("./object/WodRoll"),h=(a)=>(b)=>{if(!b||0===b.length)return null;const c=1===b.length||b.slice(1).every(e);return c?[b[0]]:[b[1],b[0],...b.slice(2,a)]},i=h(3),j=h(5);b.exports={map:c,orderArguments:h,mapToRoll:(a)=>{const b=i(c(a));return b?new f(...b):null},mapToWodRoll:(a)=>{const b=j(c(a));return b?new g(...b):null}}},{"./normalizer":13,"./object/Roll":15,"./object/WodRoll":17}],13:[function(a,b){var c=Math.min,d=Number.isInteger;const e=(a)=>d(a)&&0<a,f=(a)=>(b)=>e(b)?b:a,g=f(1),h=(a)=>(b)=>c(f(Number.MAX_SAFE_INTEGER)(b),a),i=(a)=>(b)=>c(f(0)(b),a),j=(a)=>a===void 0||null===a||""===a,k=(a)=>parseInt(a,10)||a;b.exports={isAbsent:j,isDefined:(a)=>a!==void 0&&null!==a,fixInvalid:f,normalizeInteger:(a)=>d(a)?a:0,normalizeRollResult:g,normalizeTop:h,normalizeBottom:i,normalizeWodBorders:(c,d,e)=>{const f=h(e)(d),b=i(f-1)(c);return[b,f]},normalizeRegexResult:(a)=>j(a)?null:k(a)}},{}],14:[function(a,b){function c(a,b,c){this.notation=a,this.value=b,this.rolls=c}const{resultNotation:d}=a("../stringifier");c.prototype.toString=function(){return d(this)},b.exports=c},{"../stringifier":21}],15:[function(a,b){function c(a=20,b=1,c=0){this.dice=g(a),this.count=g(b),this.modifier=e(c)}const{fixInvalid:d,normalizeInteger:e}=a("../normalizer"),{classicNotation:f}=a("../stringifier"),g=d(1);c.prototype.toString=function(){return f(this)},b.exports=c},{"../normalizer":13,"../stringifier":21}],16:[function(a,b){b.exports={simple:"simple",classic:"classic",wod:"wod"}},{}],17:[function(a,b){function c(a=10,b=1,c=!1,d=6,f){this.dice=g(a),this.count=g(b),this.again=!!c,[this.fail,this.success]=e(f,d,this.dice)}const{fixInvalid:d,normalizeWodBorders:e}=a("../normalizer"),{wodNotation:f}=a("../stringifier"),g=d(1);c.prototype.toString=function(){return f(this)},b.exports=c},{"../normalizer":13,"../stringifier":21}],18:[function(a,b){const c=a("./grammar"),d=a("./object/Type"),e=(a)=>1===a.length,f=(a,b)=>{return e(a)?(c)=>{const d=a[0].exec(c);return d&&(d.type=b),d}:(c)=>{let d=null;return a.some((a)=>d=a.exec(c)),d&&(d.type=b),d}},g=f(c.simple,d.simple),h=f(c.classic,d.classic),i=f(c.wod,d.wod);b.exports={makeParser:f,parseSimple:g,parseClassic:h,parseWod:i,parseAny:(a)=>h(a)||g(a)||i(a)}},{"./grammar":11,"./object/Type":16}],19:[function(a,b){const c=(a)=>(b)=>Math.floor(Math.random()*(b+1-a))+a,d=c(1);b.exports={random:c,randomRoll:d}},{}],20:[function(a,b){function c(a){const b=a instanceof m?a:i(a),{dice:c,count:d,modifier:e}=b,h=[...Array(d)].map(()=>f(c)),j=h.reduce((a,b)=>a+b,0),k=g(j+e);return new l(b.toString(),k,h)}function d(a){const b=a instanceof n?a:j(a),{dice:c,count:d,again:e,success:g,fail:h}=b,k=[];for(let a=d;0<a;){const b=f(c);k.push(b);(b!==c||!e||k.length>100)&&(a-=1)}const i=k.reduce((a,b)=>{if(b>=g)return a+1;return b<=h?a-1:a},0);return new l(b.toString(),Math.max(i,0),k)}function e(a){if(a instanceof m)return c(a);return a instanceof n?d(a):h(a)?e(k(a)):null}const{randomRoll:f}=a("./random"),{normalizeRollResult:g,isDefined:h}=a("./normalizer"),{convertToRoll:i,convertToWodRoll:j,convertToAnyRoll:k}=a("./converter"),l=a("./object/Result"),m=a("./object/Roll"),n=a("./object/WodRoll");b.exports={rollClassic:c,rollWod:d,rollAny:e}},{"./converter":10,"./normalizer":13,"./object/Result":14,"./object/Roll":15,"./object/WodRoll":17,"./random":19}],21:[function(a,b){b.exports={simpleNotation:function(a){const b=1<a.count?`${a.count} `:"",c=a.modifier?` ${a.modifier}`:"";return`${b}${a.dice}${c}`},classicNotation:function(a){const b=1<a.count?a.count:"",c=0<a.modifier?`+${a.modifier}`:a.modifier||"";return`${b}d${a.dice}${c}`},wodNotation:function(a){const b=1<a.count?a.count:"",c=a.again?"!":"",d=0<a.fail?`f${a.fail}`:"";return`${b}d${a.dice}${c}>${a.success}${d}`},resultNotation:function(a){const{notation:b,value:c,rolls:d}=a;return`(${b}) ${c} [${d}]`}}},{}]},{},[1])(1)});
package/index.js DELETED
@@ -1,24 +0,0 @@
1
- /* eslint global-require: "off" */
2
- module.exports = {
3
- Roll: require( './src/object/Roll' ),
4
- WodRoll: require( './src/object/WodRoll' ),
5
- Result: require( './src/object/Result' ),
6
-
7
- parse: require( './src/complex/parse' ),
8
- parseClassicRoll: require( './src/complex/parseClassicRoll' ),
9
- parseSimpleRoll: require( './src/complex/parseSimpleRoll' ),
10
- parseWodRoll: require( './src/complex/parseWodRoll' ),
11
-
12
- roll: require( './src/roller' ).rollAny,
13
- rollClassic: require( './src/roller' ).rollClassic,
14
- rollWod: require( './src/roller' ).rollWod,
15
-
16
- parseAndRoll: require( './src/complex/parseAndRoll' ),
17
- parseAndRollSimple: require( './src/complex/parseAndRollSimple' ),
18
- parseAndRollClassic: require( './src/complex/parseAndRollClassic' ),
19
- parseAndRollWod: require( './src/complex/parseAndRollWod' ),
20
-
21
- random: require( './src/random' ).randomRoll,
22
-
23
- convert: require( './src/converter' ).convertToAnyRoll,
24
- };
@@ -1,37 +0,0 @@
1
- const { parseAny } = require( '../parser' );
2
- const { mapToRoll, mapToWodRoll } = require( '../mapper' );
3
- const Type = require( '../object/Type' );
4
-
5
- /**
6
- * Parses simplified, classic or WoD roll notation.
7
- *
8
- * @func
9
- * @since v2.0.0
10
- * @param {String} roll
11
- * @return {Roll|WodRoll|null}
12
- * @see parseSimpleRoll
13
- * @see parseClassicRoll
14
- * @see parseWodRoll
15
- * @example
16
- * parse('2 10 -1'); //=> { dice: 10, count: 2, modifier: -1 }
17
- * parse('2d10+1'); //=> { dice: 10, count: 2, modifier: 1 }
18
- * parse('4d10!>8f1'); //=> { dice: 10, count: 4, again: true, success: 8, fail: 1 }
19
- * parse('xyz'); //=> null
20
- */
21
- function parse( roll ) {
22
- const result = parseAny( roll );
23
- const type = result ? result.type : '';
24
-
25
- switch ( type ) {
26
- case Type.simple:
27
- return mapToRoll( result );
28
- case Type.classic:
29
- return mapToRoll( result );
30
- case Type.wod:
31
- return mapToWodRoll( result );
32
- default:
33
- return null;
34
- }
35
- }
36
-
37
- module.exports = parse;
@@ -1,21 +0,0 @@
1
- const { rollAny } = require( '../roller' );
2
- const parse = require( './parse' );
3
-
4
- /**
5
- * Parses simplified, classic or WoD roll notation and then rolls the dice.
6
- *
7
- * @func
8
- * @since v2.0.0
9
- * @param {String} roll
10
- * @return {Result|null} Returns `Result` for defined parameters, otherwise returns `null`.
11
- * @see parseAndRollSimple
12
- * @see parseAndRollClassic
13
- * @see parseAndRollWod
14
- * @example
15
- * parseAndRoll('2 10 -1'); //=> { notation: '2d10-1', value: 14, rolls: [ 7, 8 ] }
16
- * parseAndRoll('2d10+1'); //=> { notation: '2d10+1', value: 9, rolls: [ 2, 6 ] }
17
- * parseAndRoll('4d10!>8f1'); //=> { notation: '4d10!>8f1', value: 2, rolls: [ 3, 10, 7, 9, 5 ] }
18
- */
19
- const parseAndRoll = roll => rollAny( parse( roll ));
20
-
21
- module.exports = parseAndRoll;
@@ -1,20 +0,0 @@
1
- const { rollClassic } = require( '../roller' );
2
- const parseClassicRoll = require( './parseClassicRoll' );
3
-
4
- /**
5
- * Parses classic notation and then rolls the dice.
6
- *
7
- * @func
8
- * @since v2.0.0
9
- * @param {String} roll
10
- * @return {Result}
11
- * @see parseAndRoll
12
- * @see parseAndRollSimple
13
- * @see parseAndRollWod
14
- * @example
15
- * parseAndRollClassic('2d10+1'); //=> { notation: '2d10+1', value: 9, rolls: [ 2, 6 ] }
16
- * parseAndRollClassic('d6'); //=> { notation: 'd6', value: 3, rolls: [ 3 ] }
17
- */
18
- const parseAndRollClassic = roll => rollClassic( parseClassicRoll( roll ));
19
-
20
- module.exports = parseAndRollClassic;
@@ -1,19 +0,0 @@
1
- const { rollClassic } = require( '../roller' );
2
- const parseSimpleRoll = require( './parseSimpleRoll' );
3
-
4
- /**
5
- * Parses simple notation and then rolls the dice.
6
- *
7
- * @func
8
- * @since v2.0.0
9
- * @param {String} roll
10
- * @return {Result}
11
- * @see parseAndRoll
12
- * @see parseAndRollSimple
13
- * @see parseAndRollWod
14
- * @example
15
- * parseAndRollSimple('2 10 -1'); //=> { notation: '2d10-1', value: 14, rolls: [ 7, 8 ] }
16
- */
17
- const parseAndRollSimple = roll => rollClassic( parseSimpleRoll( roll ));
18
-
19
- module.exports = parseAndRollSimple;
@@ -1,20 +0,0 @@
1
- const { rollWod } = require( '../roller' );
2
- const parseWodRoll = require( './parseWodRoll' );
3
-
4
- /**
5
- * Parses WoD roll notation and then rolls the dice.
6
- *
7
- * @func
8
- * @since v2.0.0
9
- * @param {String} roll
10
- * @return {Result}
11
- * @see parseAndRoll
12
- * @see parseAndRollSimple
13
- * @see parseAndRollClassic
14
- * @example
15
- * parseAndRollWod('2d10>6'); //=> { notation: '2d10>6', value: 1, rolls: [ 5, 10 ] }
16
- * parseAndRollWod('4d10!>8f1'); //=> { notation: '4d10!>8f1', value: 2, rolls: [ 3, 10, 7, 9, 5 ] }
17
- */
18
- const parseAndRollWod = roll => rollWod( parseWodRoll( roll ));
19
-
20
- module.exports = parseAndRollWod;
@@ -1,22 +0,0 @@
1
- const { parseClassic } = require( '../parser' );
2
- const { mapToRoll } = require( '../mapper' );
3
-
4
- /**
5
- * Parses classic DnD roll notation.
6
- *
7
- * @func
8
- * @since v2.0.0
9
- * @param {String} roll
10
- * @return {Roll}
11
- * @see parse
12
- * @see parseSimpleRoll
13
- * @see parseWodRoll
14
- * @example
15
- * parseClassicRoll('d10'); //=> { dice: 10, count: 1, modifier: 0 }
16
- * parseClassicRoll('2d10'); //=> { dice: 10, count: 2, modifier: 0 }
17
- * parseClassicRoll('d10+1'); //=> { dice: 10, count: 1, modifier: 1 }
18
- * parseClassicRoll('2d10-1'); //=> { dice: 10, count: 2, modifier: -1 }
19
- */
20
- const parseClassicRoll = roll => mapToRoll( parseClassic( roll ));
21
-
22
- module.exports = parseClassicRoll;
@@ -1,21 +0,0 @@
1
- const { parseSimple } = require( '../parser' );
2
- const { mapToRoll } = require( '../mapper' );
3
-
4
- /**
5
- * Parses simple roll notation (space separated values).
6
- *
7
- * @func
8
- * @since v2.0.0
9
- * @param {String} roll
10
- * @return {Roll}
11
- * @see parse
12
- * @see parseClassicRoll
13
- * @see parseWodRoll
14
- * @example
15
- * parseSimpleRoll('10'); //=> { dice: 10, count: 1, modifier: 0 }
16
- * parseSimpleRoll('2 10'); //=> { dice: 10, count: 2, modifier: 0 }
17
- * parseSimpleRoll('2 10 -1'); //=> { dice: 10, count: 2, modifier: -1 }
18
- */
19
- const parseSimpleRoll = roll => mapToRoll( parseSimple( roll ));
20
-
21
- module.exports = parseSimpleRoll;
@@ -1,21 +0,0 @@
1
- const { parseWod } = require( '../parser' );
2
- const { mapToWodRoll } = require( '../mapper' );
3
-
4
- /**
5
- * Parses World of Darkness (WoD) roll notation.
6
- *
7
- * @func
8
- * @since v2.0.0
9
- * @param {String} roll
10
- * @return {WodRoll}
11
- * @see parse
12
- * @see parseSimpleRoll
13
- * @see parseClassicRoll
14
- * @example
15
- * parseWodRoll('d10>6'); //=> { dice: 10, count: 1, again: false, success: 6, fail: 0 }
16
- * parseWodRoll('2d10!>6'); //=> { dice: 10, count: 2, again: true, success: 6, fail: 0 }
17
- * parseWodRoll('4d10!>8f1'); //=> { dice: 10, count: 4, again: true, success: 8, fail: 1 }
18
- */
19
- const parseWodRoll = roll => mapToWodRoll( parseWod( roll ));
20
-
21
- module.exports = parseWodRoll;
package/src/converter.js DELETED
@@ -1,45 +0,0 @@
1
- const { isAbsent } = require( './normalizer' );
2
- const Roll = require( './object/Roll' );
3
- const WodRoll = require( './object/WodRoll' );
4
-
5
- function convertToRoll( object = {}) {
6
- const { dice, count, modifier } = object || {};
7
-
8
- return new Roll( dice, count, modifier );
9
- }
10
-
11
- function convertToWodRoll( object = {}) {
12
- const { dice, count, again, success, fail } = object || {};
13
-
14
- return new WodRoll( dice, count, again, success, fail );
15
- }
16
-
17
- /**
18
- * Converts any arguments to `Roll` or `WodRoll` object.
19
- * If passed argument has `again`, `success` or `fail` property, the function will return `WodRoll`.
20
- * Otherwise, `Roll` will be returned.
21
- *
22
- * @func
23
- * @alias convert
24
- * @since v2.1.0
25
- * @param {Object} object - `Roll`, `WodRoll` or similar object.
26
- * @return {Roll|WodRoll} Result of converion.
27
- * @example
28
- * convert({ dice: 6 }); //=> new Roll( 6 )
29
- * convert({ modifier: 6 }); //=> new Roll( undefined, undefined, 6 )
30
- * convert({ dice: 10, count: 5, success: 5 }); //=> new WodRoll( 10, 5, undefined, 5 )
31
- */
32
- function convertToAnyRoll( object = {}) {
33
- const { again, success, fail } = object || {};
34
-
35
- if ( isAbsent( again ) && isAbsent( success ) && isAbsent( fail )) {
36
- return convertToRoll( object );
37
- } // else
38
- return convertToWodRoll( object );
39
- }
40
-
41
- module.exports = {
42
- convertToRoll,
43
- convertToWodRoll,
44
- convertToAnyRoll,
45
- };
package/src/grammar.js DELETED
@@ -1,14 +0,0 @@
1
- // Multiple instances of each notation exists for purposes of readability
2
- // Each grammar is an array of RegExps
3
- module.exports = {
4
- simple: [
5
- /^(\d+)(?:\s+(\d+)(?:\s+([+-]?\d+))?)?$/, // 1 20 4 || 1 20 +4 5 || 1 20 -4 5 20
6
- ],
7
- classic: [
8
- /^(\d*)(?:[dD])(\d+)([+-]\d+)?$/, // d20 || 1d20 || d20+4 || 1d20-4
9
- ],
10
- wod: [
11
- /^(\d*)(?:[dD])(\d+)(!?)$/, // d10 || d10! || 1d10 || 1d10!
12
- /^(\d*)(?:[dD])(\d+)(!?)(?:>)(\d+)(?:f(\d+))?$/, // d10>6 || 1d10>6 || 1d10!>6 || 1d10!>6f1
13
- ],
14
- };
package/src/mapper.js DELETED
@@ -1,50 +0,0 @@
1
- const { normalizeRegexResult, isAbsent } = require( './normalizer' );
2
- const Roll = require( './object/Roll' );
3
- const WodRoll = require( './object/WodRoll' );
4
-
5
- // map :: Object -> [Array | null]
6
- // Takes a result of RegExp.prototype.exec() and returns an Array of integers, strings, and null
7
- function map( result ) {
8
- const invalid = !result || result.length < 2;
9
- return invalid ? null : ( result.slice( 1 ).map( normalizeRegexResult ));
10
- }
11
-
12
- const orderArguments = limit => ( values ) => {
13
- if ( !values || values.length === 0 ) {
14
- return null;
15
- }
16
-
17
- // Minimum grammar accepts 2 values. If second is not set, it is mapped to `null`
18
- const sinlge = values.length === 1 || values.slice( 1 ).every( isAbsent );
19
- if ( sinlge ) {
20
- return [ values[ 0 ] ];
21
- }
22
-
23
- return [ values[ 1 ], values[ 0 ], ...values.slice( 2, limit ) ];
24
- };
25
-
26
- const orderRollArguments = orderArguments( 3 );
27
- const orderWodRollArguments = orderArguments( 5 );
28
-
29
- // mapToRoll :: Object -> [Object | null]
30
- // Orders map() values with orderArguments(), takes the result
31
- // and returns a Roll object or null
32
- const mapToRoll = ( result ) => {
33
- const values = orderRollArguments( map( result ));
34
- return values ? new Roll( ...values ) : null;
35
- };
36
-
37
- // mapToWodRoll :: Object -> [Object | null]
38
- // Orders map() values with orderArguments(), takes the result
39
- // and returns a WodRoll object or null
40
- const mapToWodRoll = ( result ) => {
41
- const values = orderWodRollArguments( map( result ));
42
- return values ? new WodRoll( ...values ) : null;
43
- };
44
-
45
- module.exports = {
46
- map,
47
- orderArguments,
48
- mapToRoll,
49
- mapToWodRoll,
50
- };
package/src/normalizer.js DELETED
@@ -1,51 +0,0 @@
1
- // isPositiveInteger :: Number -> Boolean
2
- const isPositiveInteger = value => Number.isInteger( value ) && value > 0;
3
-
4
- // fixInvalid :: Number -> Any -> Number
5
- const fixInvalid = backup => value => ( isPositiveInteger( value ) ? value : backup );
6
-
7
- // normalizeInteger :: Any -> Number
8
- const normalizeInteger = value => ( Number.isInteger( value ) ? value : 0 );
9
-
10
- // normalizeRollResult :: Any -> Number
11
- const normalizeRollResult = fixInvalid( 1 );
12
-
13
- // normalizeTop :: Number -> Number -> Number
14
- const normalizeTop = max => top => Math.min( fixInvalid( Number.MAX_SAFE_INTEGER )( top ), max );
15
- // normalizeTop :: Number -> Number -> Number
16
- const normalizeBottom = max => bottom => Math.min( fixInvalid( 0 )( bottom ), max );
17
-
18
- // normalizeWodBorders :: ( Number, Number, Number ) -> [ Number, Number ]
19
- const normalizeWodBorders = ( bottom, top, max ) => {
20
- const b = normalizeTop( max )( top );
21
- const a = normalizeBottom( b - 1 )( bottom );
22
- return [ a, b ];
23
- };
24
-
25
- // isDefined :: Any -> Boolean
26
- // Checks for the existence of the value
27
- const isDefined = value => value !== undefined && value !== null;
28
-
29
- // isAbsent :: Any -> Boolean
30
- // Checks for absence of the values from RegExp execution result.
31
- const isAbsent = value => value === undefined || value === null || value === '';
32
-
33
- // toInteger :: a -> Number | a
34
- // a = String | Any
35
- const toInteger = value => parseInt( value, 10 ) || value;
36
-
37
-
38
- // normalizeRegexResult :: Any -> null | Number | String
39
- const normalizeRegexResult = value => ( isAbsent( value ) ? null : toInteger( value ));
40
-
41
- module.exports = {
42
- isAbsent,
43
- isDefined,
44
- fixInvalid,
45
- normalizeInteger,
46
- normalizeRollResult,
47
- normalizeTop,
48
- normalizeBottom,
49
- normalizeWodBorders,
50
- normalizeRegexResult,
51
- };
@@ -1,23 +0,0 @@
1
- const { resultNotation } = require( '../stringifier' );
2
- /**
3
- * A class that represents a dice roll result
4
- * @class
5
- * @classdesc A class that represents a dice roll result
6
- * @since v2.0.0
7
- * @param {String} notation - A roll notation
8
- * @param {Number} value - A numeric representation of roll result, like total summ or success count
9
- * @param {Array} rolls - An array of rolls dome
10
- * @see Roll
11
- * @see WodRoll
12
- */
13
- function Result( notation, value, rolls ) {
14
- this.notation = notation;
15
- this.value = value;
16
- this.rolls = rolls;
17
- }
18
-
19
- Result.prototype.toString = function toString() {
20
- return resultNotation( this );
21
- };
22
-
23
- module.exports = Result;
@@ -1,26 +0,0 @@
1
- const { fixInvalid, normalizeInteger } = require( '../normalizer' );
2
- const { classicNotation } = require( '../stringifier' );
3
-
4
- const positiveInteger = fixInvalid( 1 );
5
-
6
- /**
7
- * A class that represents a dice roll from D&D setting
8
- * @class
9
- * @classdesc A class that represents a dice roll from D&D setting
10
- * @since v2.0.0
11
- * @param {Number} dice - A number of dice faces
12
- * @param {Number} count - A number of dices
13
- * @param {Number} modifier - A modifier, that should be added/sustracted from result
14
- * @see WodRoll
15
- */
16
- function Roll( dice = 20, count = 1, modifier = 0 ) {
17
- this.dice = positiveInteger( dice );
18
- this.count = positiveInteger( count );
19
- this.modifier = normalizeInteger( modifier );
20
- }
21
-
22
- Roll.prototype.toString = function toString() {
23
- return classicNotation( this );
24
- };
25
-
26
- module.exports = Roll;
@@ -1,5 +0,0 @@
1
- module.exports = {
2
- simple: 'simple',
3
- classic: 'classic',
4
- wod: 'wod',
5
- };
@@ -1,29 +0,0 @@
1
- const { fixInvalid, normalizeWodBorders } = require( '../normalizer' );
2
- const { wodNotation } = require( '../stringifier' );
3
-
4
- const positiveInteger = fixInvalid( 1 );
5
-
6
- /**
7
- * A class that represents a dice roll from World of Darkness setting
8
- * @class
9
- * @classdesc A class that represents a dice roll from World of Darkness setting
10
- * @since v2.0.0
11
- * @param {Number} dice - A number of dice faces
12
- * @param {Number} count - A number of dices
13
- * @param {Boolean} again - A flag for "10 Again" rolls policy
14
- * @param {Number} success - A minimum value, that counts as success
15
- * @param {Number} fail - A maximum value, that counts as failure
16
- * @see Roll
17
- */
18
- function WodRoll( dice = 10, count = 1, again = false, success = 6, fail ) {
19
- this.dice = positiveInteger( dice );
20
- this.count = positiveInteger( count );
21
- this.again = !!again;
22
- [ this.fail, this.success ] = normalizeWodBorders( fail, success, this.dice );
23
- }
24
-
25
- WodRoll.prototype.toString = function toString() {
26
- return wodNotation( this );
27
- };
28
-
29
- module.exports = WodRoll;
package/src/parser.js DELETED
@@ -1,43 +0,0 @@
1
- const grammar = require( './grammar' );
2
- const Type = require( './object/Type' );
3
-
4
- // isUnary :: Array -> Boolean
5
- const isUnary = array => array.length === 1;
6
-
7
- // makeParser :: ( [ RegExp ], String ) -> ( String -> [ String | Undefined ] )
8
- const makeParser = ( grammarSet, type ) => {
9
- if ( isUnary( grammarSet )) {
10
- return ( roll ) => {
11
- const result = grammarSet[ 0 ].exec( roll );
12
- if ( result ) {
13
- result.type = type;
14
- }
15
- return result;
16
- };
17
- }
18
- return ( roll ) => {
19
- let result = null;
20
- grammarSet.some( regex => ( result = regex.exec( roll )));
21
- if ( result ) {
22
- result.type = type;
23
- }
24
- return result;
25
- };
26
- };
27
-
28
- // parseSimple :: String -> [ String | Undefined ]
29
- const parseSimple = makeParser( grammar.simple, Type.simple );
30
- // parseClassic :: String -> [ String | Undefined ]
31
- const parseClassic = makeParser( grammar.classic, Type.classic );
32
- // parseWod :: String -> [ String | Undefined ]
33
- const parseWod = makeParser( grammar.wod, Type.wod );
34
- // parseAny :: String -> [ String | Undefined ]
35
- const parseAny = roll => parseClassic( roll ) || parseSimple( roll ) || parseWod( roll );
36
-
37
- module.exports = {
38
- makeParser,
39
- parseSimple,
40
- parseClassic,
41
- parseWod,
42
- parseAny,
43
- };
package/src/random.js DELETED
@@ -1,22 +0,0 @@
1
- // random :: Number -> Number -> Number
2
- // Generates random integer from the range of [ min, max ] values
3
- const random = min => max => Math.floor( Math.random() * (( max + 1 ) - min )) + min;
4
-
5
- /**
6
- * Generates random positive integer from `1` to `max`.
7
- *
8
- * @func
9
- * @alias random
10
- * @since v2.0.0
11
- * @param {Number} max - maximum possible generated value
12
- * @return {Number} Positive integer, from `1` to `max`
13
- * @example
14
- * random(100); //=> 77 - random number from 1 to 100
15
- * random(1); //=> 1 - always rolls 1
16
- */
17
- const randomRoll = random( 1 );
18
-
19
- module.exports = {
20
- random,
21
- randomRoll,
22
- };
package/src/roller.js DELETED
@@ -1,106 +0,0 @@
1
- const { randomRoll } = require( './random' );
2
- const { normalizeRollResult, isDefined } = require( './normalizer' );
3
- const { convertToRoll, convertToWodRoll, convertToAnyRoll } = require( './converter' );
4
- const Result = require( './object/Result' );
5
- const Roll = require( './object/Roll' );
6
- const WodRoll = require( './object/WodRoll' );
7
-
8
-
9
- /**
10
- * Rolls the dice from `Roll` object.
11
- *
12
- * @func
13
- * @since v2.0.0
14
- * @param {Roll} roll - `Roll` object or similar
15
- * @return {Result}
16
- * @see roll
17
- * @see rollWod
18
- * @example
19
- * rollClassic(new Roll(10, 2, -1)); //=> { notation: '2d10-1', value: 14, rolls: [ 7, 8 ] }
20
- * rollClassic({ dice: 6 }); //=> { notation: 'd6', value: 4, rolls: [ 4 ] }
21
- */
22
- function rollClassic( roll ) {
23
- const data = roll instanceof Roll ? roll : convertToRoll( roll );
24
- const { dice, count, modifier } = data;
25
-
26
- const rolls = [ ...new Array( count ) ].map(() => randomRoll( dice ));
27
- const summ = rolls.reduce(( prev, curr ) => prev + curr, 0 );
28
- const result = normalizeRollResult( summ + modifier );
29
-
30
- return new Result( data.toString(), result, rolls );
31
- }
32
-
33
- /**
34
- * Rolls the dice from `WodRoll` object.
35
- *
36
- * @func
37
- * @since v2.0.0
38
- * @param {WodRoll} roll - `WodRoll` object or similar
39
- * @return {Result}
40
- * @see roll
41
- * @see rollClassic
42
- * @example
43
- * rollWod(new WodRoll(10, 4, true, 8)); //=> { notation: '4d10!>8', value: 2, rolls: [3,10,7,9,5] }
44
- * rollWod({ dice: 8, count: 3 }); //=> { notation: '3d8>6', value: 2, rolls: [ 7, 3, 9 ] }
45
- */
46
- function rollWod( roll ) {
47
- const data = roll instanceof WodRoll ? roll : convertToWodRoll( roll );
48
- const { dice, count, again, success, fail } = data;
49
-
50
- const rolls = [];
51
-
52
- let i = count;
53
- while ( i > 0 ) {
54
- const value = randomRoll( dice );
55
- rolls.push( value );
56
- // Check for "10 Again" flag
57
- // `repeatLimit` will prevent infinite loop, for cases like `d1!>1`
58
- const repeatLimit = 100;
59
- if ( value !== dice || !again || rolls.length > repeatLimit ) {
60
- i -= 1;
61
- }
62
- }
63
-
64
- const result = rolls.reduce(( suc, val ) => {
65
- if ( val >= success ) {
66
- return suc + 1;
67
- } else if ( val <= fail ) {
68
- return suc - 1;
69
- }
70
- return suc;
71
- }, 0 );
72
-
73
- return new Result( data.toString(), Math.max( result, 0 ), rolls );
74
- }
75
-
76
- /**
77
- * Rolls the dice from `Roll` or `WodRoll` objects.
78
- *
79
- * @func
80
- * @alias roll
81
- * @since v2.0.0
82
- * @param {Roll|WodRoll|Object} roll - `Roll`, `WodRoll` or similar object.
83
- * @return {Result} Returns `Result` for defined parameters, otherwise returns `null`.
84
- * @see rollClassic
85
- * @see rollWod
86
- * @example
87
- * roll(new Roll(10, 2, -1)); //=> { notation: '2d10-1', value: 14, rolls: [ 7, 8 ] }
88
- * roll({ dice: 6 }); //=> { notation: 'd6', value: 4, rolls: [ 4 ] }
89
- * roll(new WodRoll(10, 4, true, 8)); //=> { notation: '4d10!>8', value: 2, rolls: [3,10,7,9,5] }
90
- * roll({ dice: 8, count: 3, again: true }); //=> { notation: '3d8!>6', value: 2, rolls: [7,3,9 ] }
91
- * roll( null ); //=> null
92
- */
93
- function rollAny( roll ) {
94
- if ( roll instanceof Roll ) {
95
- return rollClassic( roll );
96
- } else if ( roll instanceof WodRoll ) {
97
- return rollWod( roll );
98
- }
99
- return isDefined( roll ) ? rollAny( convertToAnyRoll( roll )) : null;
100
- }
101
-
102
- module.exports = {
103
- rollClassic,
104
- rollWod,
105
- rollAny,
106
- };