This repository has been archived by the owner on Jul 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgravityforms-exponents.js
83 lines (73 loc) · 2.73 KB
/
gravityforms-exponents.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
* Evaluate caret notation on front end
*
* Thanks to @Mohsen on Stack Overflow for the caret evaulation function
*
* @link http://stackoverflow.com/questions/15037805/javascript-fixing-that-dan-caret-symbol-for-calculator
* @version 0.1.0
* @since 0.1.0
*/
/**
* Extend the number prototype to have a pow method
*/
Number.prototype.pow = function(n) {return Math.pow(this,n)}
/**
* Javascript filter
*
* @param float result The calculation result
* @param object formulaField The current calculation field object
* @param int formId The ID of the form in use
* @param object calcObj The calculation object
* @link https://www.gravityhelp.com/documentation/article/gform_calculation_result/
* Description of `gform_calculation_result` filter
*/
gform.addFilter( 'gform_calculation_result', function( result, formulaField, formId, calcObj ) {
/**
* Only evaluate if the field has a caret in it
*
* Technically we should be able to run any formulas through this without
* breaking them, but this way we save some small amount of processing
*
* @link https://www.w3schools.com/jsref/jsref_indexof.asp
* Description of `indexOf` method
*/
if ( formulaField.formula.indexOf( '^' ) > -1 || formulaField.formula.indexOf( '**' ) > -1 ) {
/**
* Replace field tags with their associated values
*
* @param int formId The ID of the form in use
* @param string formula The value of the "Formula" field entered in
* the form admin
* @param object formulaField The current calculation field object
* @var string fieldFormula
*/
var fieldFormula = calcObj.replaceFieldTags( formId, formulaField.formula, formulaField );
/**
* Sanitize the formula in case we have malicious user inputs. This
* prevents malicious code getting passed to our `eval` call later in the
* function
*
* We are stripping out anything that is not a number, decimal, space,
* parentheses, or simple arithmetical operator.
*
* @link https://www.w3schools.com/jsref/jsref_replace.asp
* Description of `replace` method
*/
fieldFormula = fieldFormula.replace( /[^0-9\s\n\r\+\-\*\/\^\(\)\.]/g, '' );
/**
* Wrap every number with parentheses and replace the caret symbol with
* ".pow"
*/
fieldFormula = fieldFormula.replace(/[\d|\d.\d]+/g, function(n){
return '(' + n + ')'
}).replace(/\^|\*\*/g, '.pow');
/**
* Set calculation result equal to evaluated string
*
* @link https://www.w3schools.com/jsref/jsref_eval.asp
* Description of `eval` function
*/
result = eval(fieldFormula);
} // if ( formulaField.formula.indexOf( '^' ) > -1 || formulaField.formula.indexOf( '**' ) > -1 )
return result;
} );