Skip to content

Commit 62b4f4e

Browse files
committed
Add configurePrecision to the library
1 parent 7a74c0d commit 62b4f4e

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import Constants from './constants.js'
88
import defaultMethods from './defaultMethods.js'
99
import { asLogicSync, asLogicAsync } from './asLogic.js'
1010
import { splitPath, splitPathMemoized } from './utilities/splitPath.js'
11+
import { configurePrecision } from './precision/index.js'
1112

1213
export { splitPath, splitPathMemoized }
1314
export { LogicEngine }
@@ -17,5 +18,6 @@ export { Constants }
1718
export { defaultMethods }
1819
export { asLogicSync }
1920
export { asLogicAsync }
21+
export { configurePrecision }
2022

21-
export default { LogicEngine, AsyncLogicEngine, Compiler, Constants, defaultMethods, asLogicSync, asLogicAsync, splitPath, splitPathMemoized }
23+
export default { LogicEngine, AsyncLogicEngine, Compiler, Constants, defaultMethods, asLogicSync, asLogicAsync, splitPath, splitPathMemoized, configurePrecision }

precision/index.js

+30
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,34 @@
11

2+
/**
3+
* Allows you to configure precision for JSON Logic Engine.
4+
*
5+
* Essentially, pass the constructor for `decimal.js` in as the second argument.
6+
*
7+
* @example ```js
8+
* import { LogicEngine, configurePrecision } from 'json-logic-js'
9+
* import { Decimal } from 'decimal.js' // or decimal.js-light
10+
*
11+
* const engine = new LogicEngine()
12+
* configurePrecision(engine, Decimal)
13+
* ```
14+
*
15+
* The class this mechanism uses requires the following methods to be implemented:
16+
* - `eq`
17+
* - `gt`
18+
* - `gte`
19+
* - `lt`
20+
* - `lte`
21+
* - `plus`
22+
* - `minus`
23+
* - `mul`
24+
* - `div`
25+
* - `mod`
26+
* - `toNumber`
27+
*
28+
* @param {import('../logic.d.ts').default | import('../asyncLogic.d.ts').default} engine
29+
* @param {*} constructor
30+
* @param {Boolean} compatible
31+
*/
232
export function configurePrecision (engine, constructor, compatible = true) {
333
engine.precision = constructor
434

0 commit comments

Comments
 (0)