algebraic-captcha
Advanced tools
Comparing version 0.0.1 to 0.1.0
import { IParams } from './i-params'; | ||
export declare class Config { | ||
private _validated; | ||
static readonly MODE_FORMULA: string; | ||
static readonly MODE_EQUATION: string; | ||
constructor(params: IParams); | ||
@@ -13,3 +15,5 @@ readonly width: number; | ||
readonly operandTypes: string[]; | ||
isFormulaMode(): boolean; | ||
readonly targetSymbol: string; | ||
protected readonly schema: object; | ||
} |
@@ -36,2 +36,8 @@ "use strict"; | ||
} | ||
isFormulaMode() { | ||
return this._validated.mode === Config.MODE_FORMULA; | ||
} | ||
get targetSymbol() { | ||
return this._validated.targetSymbol; | ||
} | ||
get schema() { | ||
@@ -79,7 +85,17 @@ const colorRegexp = /^#([0-9a-f]{3}){1,2}$/i; | ||
.items(Joi.string()) | ||
.default(['+', '-']) | ||
.default(['+', '-']), | ||
mode: Joi.string() | ||
.valid([Config.MODE_FORMULA, Config.MODE_EQUATION]) | ||
.default(Config.MODE_FORMULA), | ||
targetSymbol: Joi.string() | ||
.min(1) | ||
.max(3) | ||
.trim() | ||
.default('?') | ||
}); | ||
} | ||
} | ||
Config.MODE_FORMULA = 'formula'; | ||
Config.MODE_EQUATION = 'equation'; | ||
exports.Config = Config; | ||
//# sourceMappingURL=config.js.map |
@@ -0,1 +1,2 @@ | ||
import { IFormula } from './i-formula'; | ||
import { Config } from './config'; | ||
@@ -5,6 +6,22 @@ export declare class FormulaBuilder { | ||
constructor(config: Config); | ||
generateFormula(): { | ||
formula: string[]; | ||
answer: number; | ||
}; | ||
generateFormula(): IFormula; | ||
private generateFormulaChunks; | ||
/** | ||
* Returns formula in classic presentation | ||
* @example 5 + 1 = ? | ||
* @param {String[]} formula | ||
* @param {Number} answer | ||
* @returns {Object} | ||
* @private | ||
*/ | ||
private makeFormula; | ||
/** | ||
* Returns formula in equation presentation | ||
* @example 5 + ? = 6 | ||
* @param {String[]} formula | ||
* @param {Number} answer | ||
* @returns {IFormula} | ||
* @private | ||
*/ | ||
private makeEquation; | ||
} |
@@ -9,2 +9,10 @@ "use strict"; | ||
generateFormula() { | ||
const formulaChunks = this.generateFormulaChunks(); | ||
const answer = new Function(`return ${formulaChunks.join(' ')}`)(); | ||
const formula = formulaChunks.concat('=').concat(this.config.targetSymbol); | ||
return this.config.isFormulaMode() | ||
? this.makeFormula(formula, answer) | ||
: this.makeEquation(formula, answer); | ||
} | ||
generateFormulaChunks() { | ||
const { minValue, maxValue, operandAmount, operandTypes } = this.config; | ||
@@ -14,16 +22,42 @@ const formulaParts = []; | ||
while (index < 2 * operandAmount + 1) { | ||
if (index % 2 === 0) { | ||
formulaParts.push(lodash_1.random(minValue, maxValue).toString()); | ||
} | ||
else { | ||
formulaParts.push(lodash_1.sample(operandTypes)); | ||
} | ||
index % 2 === 0 | ||
? formulaParts.push(lodash_1.random(minValue, maxValue).toString()) | ||
: formulaParts.push(lodash_1.sample(operandTypes)); | ||
index++; | ||
} | ||
const answer = new Function(`return ${formulaParts.join(' ')}`)(); | ||
const formula = formulaParts.concat('=').concat('?'); | ||
return formulaParts; | ||
} | ||
/** | ||
* Returns formula in classic presentation | ||
* @example 5 + 1 = ? | ||
* @param {String[]} formula | ||
* @param {Number} answer | ||
* @returns {Object} | ||
* @private | ||
*/ | ||
makeFormula(formula, answer) { | ||
return { formula, answer }; | ||
} | ||
/** | ||
* Returns formula in equation presentation | ||
* @example 5 + ? = 6 | ||
* @param {String[]} formula | ||
* @param {Number} answer | ||
* @returns {IFormula} | ||
* @private | ||
*/ | ||
makeEquation(formula, answer) { | ||
const operands = formula | ||
.slice(0, formula.length - 2) | ||
.filter((item, index) => index % 2 === 0) | ||
.map(Number); | ||
const target = lodash_1.sample(operands); | ||
const targetIndex = formula.indexOf(target.toString()); | ||
formula[targetIndex] = this.config.targetSymbol; | ||
formula[formula.length - 1] = answer.toString(); | ||
answer = target; | ||
return { formula, answer }; | ||
} | ||
} | ||
exports.FormulaBuilder = FormulaBuilder; | ||
//# sourceMappingURL=formula-builder.js.map |
@@ -10,2 +10,4 @@ export interface IParams { | ||
operandTypes?: string[]; | ||
mode?: string; | ||
targetSymbol?: string; | ||
} |
@@ -25,5 +25,5 @@ "use strict"; | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const generatedEquation = this.formulaBuilder.generateFormula(); | ||
const formula = generatedEquation.formula; | ||
const answer = generatedEquation.answer; | ||
const generatedFormula = this.formulaBuilder.generateFormula(); | ||
const formula = generatedFormula.formula; | ||
const answer = generatedFormula.answer; | ||
const image = yield this.formulaDrawer.draw(formula); | ||
@@ -30,0 +30,0 @@ const streamBuffer = new stream_buffers_1.WritableStreamBuffer({ |
{ | ||
"name": "algebraic-captcha", | ||
"version": "0.0.1", | ||
"version": "0.1.0", | ||
"description": "NodeJS math captcha package with algebraic formulas", | ||
@@ -20,4 +20,5 @@ "main": "build/index.js", | ||
"cm": "git-cz", | ||
"watch": "tsc -w", | ||
"demo": "node demo/generate.js && node_modules/.bin/gh-pages -d demo", | ||
"prepublish": "publish-please guard", | ||
"watch": "tsc -w", | ||
"publish-please": "publish-please" | ||
@@ -63,5 +64,8 @@ }, | ||
"@types/mocha": "^5.2.5", | ||
"bluebird": "^3.5.3", | ||
"chai": "^4.2.0", | ||
"commitizen": "^3.0.5", | ||
"cz-conventional-changelog": "^2.1.0", | ||
"gh-pages": "^2.0.1", | ||
"handlebars": "^4.0.12", | ||
"husky": "^1.2.1", | ||
@@ -68,0 +72,0 @@ "lint-staged": "^8.1.0", |
@@ -10,2 +10,4 @@ # algebraic-captcha | ||
[DEMO](https://tormozz48.github.io/algebraic-captcha) | ||
## Install | ||
@@ -41,3 +43,5 @@ | ||
operandAmount: 1, | ||
operandTypes: ['+', '-'] | ||
operandTypes: ['+', '-'], | ||
mode: 'formula', | ||
targetSymbol: '?' | ||
}); | ||
@@ -137,2 +141,26 @@ | ||
#### `mode` | ||
Can have two available values: `formula`, `equation` | ||
In `formula` mode anwer placeholder will be put to the last position of generated string, such as: | ||
``` | ||
5 + 2 = ? | ||
``` | ||
In `equation` mode answer placeholder will be put on random operand position except last: | ||
``` | ||
5 + ? = 7 //(or ? + 2 = 7) | ||
``` | ||
- default value: 'formula' | ||
#### `targetSymbol` | ||
Symbol which is used as placeholder for answer | ||
- default value: '?' | ||
## Development | ||
@@ -157,3 +185,2 @@ | ||
- `npm test` - run tests with [mocha](https://mochajs.org) | ||
(https://github.com/gotwarlost/istanbul) | ||
- `npm run watch` - launch watcher for compile source files during development | ||
@@ -160,0 +187,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Uses eval
Supply chain riskPackage uses eval() which is a dangerous function. This prevents the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses eval() which is a dangerous function. This prevents the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
188288
22
354
195
19