@modernpoacher/rules-runner 2.3.174 → 2.3.187
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.
- package/eslint.config.mjs +92 -0
- package/husky.mjs +16 -0
- package/package.json +10 -13
- package/src/common/index.mjs +32 -2
- package/src/comparators/all-of.mjs +5 -0
- package/src/comparators/any-of.mjs +5 -0
- package/src/comparators/between.mjs +5 -0
- package/src/comparators/boolean.mjs +5 -0
- package/src/comparators/contains.mjs +5 -0
- package/src/comparators/equals.mjs +5 -0
- package/src/comparators/greater-than.mjs +5 -0
- package/src/comparators/less-than.mjs +5 -0
- package/src/comparators/matches.mjs +5 -0
- package/src/comparators/not.mjs +6 -0
- package/src/comparators/one-of.mjs +5 -0
- package/src/index.d.mts +7 -5
- package/src/index.mjs +8 -8
- package/tsconfig.json +10 -4
- package/babel.config.cjs +0 -38
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import globals from 'globals'
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
configs as standard
|
|
5
|
+
} from '@sequencemedia/eslint-config-standard'
|
|
6
|
+
|
|
7
|
+
import {
|
|
8
|
+
configs as typescript
|
|
9
|
+
} from '@sequencemedia/eslint-config-typescript'
|
|
10
|
+
|
|
11
|
+
export default [
|
|
12
|
+
{
|
|
13
|
+
...standard.recommended,
|
|
14
|
+
files: [
|
|
15
|
+
'**/*.{mjs,cjs,mts,cts}'
|
|
16
|
+
],
|
|
17
|
+
ignores: [
|
|
18
|
+
'test',
|
|
19
|
+
'src'
|
|
20
|
+
],
|
|
21
|
+
languageOptions: {
|
|
22
|
+
...standard.recommended.languageOptions,
|
|
23
|
+
globals: {
|
|
24
|
+
...globals.node
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
...standard.recommended,
|
|
30
|
+
files: [
|
|
31
|
+
'src/**/*.{mjs,cjs,mts,cts}'
|
|
32
|
+
],
|
|
33
|
+
languageOptions: {
|
|
34
|
+
...standard.recommended.languageOptions,
|
|
35
|
+
globals: {
|
|
36
|
+
...globals.browser
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
...standard.recommended,
|
|
42
|
+
files: [
|
|
43
|
+
'test/**/*.{mjs,cjs,mts,cts}'
|
|
44
|
+
],
|
|
45
|
+
languageOptions: {
|
|
46
|
+
...standard.recommended.languageOptions,
|
|
47
|
+
globals: {
|
|
48
|
+
...globals.mocha
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
...typescript.recommended,
|
|
54
|
+
files: [
|
|
55
|
+
'**/*.{mts,cts}'
|
|
56
|
+
],
|
|
57
|
+
ignores: [
|
|
58
|
+
'test',
|
|
59
|
+
'src'
|
|
60
|
+
],
|
|
61
|
+
languageOptions: {
|
|
62
|
+
...typescript.recommended.languageOptions,
|
|
63
|
+
globals: {
|
|
64
|
+
...globals.node
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
...typescript.recommended,
|
|
70
|
+
files: [
|
|
71
|
+
'src/**/*.{mts,cts}'
|
|
72
|
+
],
|
|
73
|
+
languageOptions: {
|
|
74
|
+
...typescript.recommended.languageOptions,
|
|
75
|
+
globals: {
|
|
76
|
+
...globals.browser
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
...typescript.recommended,
|
|
82
|
+
files: [
|
|
83
|
+
'test/**/*.{mts,cts}'
|
|
84
|
+
],
|
|
85
|
+
languageOptions: {
|
|
86
|
+
...typescript.recommended.languageOptions,
|
|
87
|
+
globals: {
|
|
88
|
+
...globals.mocha
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
]
|
package/husky.mjs
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// @ts-ignore
|
|
4
|
+
Promise.resolve(import('husky'))
|
|
5
|
+
.then(({ default: husky }) => { husky() })
|
|
6
|
+
.catch((e) => {
|
|
7
|
+
if (e instanceof Error) {
|
|
8
|
+
if ('code' in e) {
|
|
9
|
+
const { code } = e
|
|
10
|
+
if (code === 'ERR_MODULE_NOT_FOUND') return
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const { message } = e
|
|
14
|
+
if (message) console.error(`💥 ${message}`)
|
|
15
|
+
}
|
|
16
|
+
})
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@modernpoacher/rules-runner",
|
|
3
|
-
"version": "2.3.
|
|
3
|
+
"version": "2.3.187",
|
|
4
4
|
"description": "A business rules engine for Node",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"business rules",
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
],
|
|
32
32
|
"license": "ISC",
|
|
33
33
|
"engines": {
|
|
34
|
-
"node": ">=
|
|
34
|
+
"node": ">=22.23.4"
|
|
35
35
|
},
|
|
36
36
|
"repository": {
|
|
37
37
|
"type": "git",
|
|
@@ -44,7 +44,7 @@
|
|
|
44
44
|
"scripts": {
|
|
45
45
|
"lint": "eslint src test --ext .mjs,.cjs,.mts,.cts",
|
|
46
46
|
"lint:fix": "npm run lint -- --fix",
|
|
47
|
-
"prepare": "husky",
|
|
47
|
+
"prepare": "node husky.mjs",
|
|
48
48
|
"test": "cross-env NODE_ENV=test mocha test --recursive"
|
|
49
49
|
},
|
|
50
50
|
"dependencies": {
|
|
@@ -52,18 +52,15 @@
|
|
|
52
52
|
"object-path": "^0.11.8"
|
|
53
53
|
},
|
|
54
54
|
"devDependencies": {
|
|
55
|
-
"@
|
|
56
|
-
"@
|
|
57
|
-
"@
|
|
58
|
-
"
|
|
59
|
-
"@typescript-eslint/parser": "7.18.0",
|
|
60
|
-
"chai": "^6.2.1",
|
|
55
|
+
"@sequencemedia/eslint-config-standard": "0.3.2",
|
|
56
|
+
"@sequencemedia/eslint-config-typescript": "0.2.3",
|
|
57
|
+
"@types/debug": "^4.1.13",
|
|
58
|
+
"chai": "^6.3.0",
|
|
61
59
|
"cross-env": "^10.1.0",
|
|
62
|
-
"eslint": "
|
|
63
|
-
"
|
|
64
|
-
"eslint-config-standard": "^17.1.0",
|
|
60
|
+
"eslint": "10.14.0",
|
|
61
|
+
"globals": "^17.14.0",
|
|
65
62
|
"husky": "^9.1.7",
|
|
66
|
-
"mocha": "^
|
|
63
|
+
"mocha": "^12.0.3"
|
|
67
64
|
},
|
|
68
65
|
"imports": {
|
|
69
66
|
"#rules-runner": "./src/index.mjs",
|
package/src/common/index.mjs
CHANGED
|
@@ -4,10 +4,22 @@ const log = debug('@modernpoacher/rules-runner/common')
|
|
|
4
4
|
|
|
5
5
|
log('`@modernpoacher/rules-runner` is awake')
|
|
6
6
|
|
|
7
|
+
/**
|
|
8
|
+
* @param {unknown} v
|
|
9
|
+
* @returns {v is boolean | 'true' | 'false'} v is a boolean or string "true" or string "false"
|
|
10
|
+
*/
|
|
7
11
|
export const isBoolean = (v) => (typeof v === 'boolean' || v === 'true' || v === 'false')
|
|
8
12
|
|
|
13
|
+
/**
|
|
14
|
+
* @param {unknown} v
|
|
15
|
+
* @returns {v is object}
|
|
16
|
+
*/
|
|
9
17
|
export const isObject = (v) => (v || false).constructor === Object && !Array.isArray(v)
|
|
10
18
|
|
|
19
|
+
/**
|
|
20
|
+
* @param {unknown} v
|
|
21
|
+
* @returns {null}
|
|
22
|
+
*/
|
|
11
23
|
export function toNull (v) {
|
|
12
24
|
log('toNull')
|
|
13
25
|
|
|
@@ -16,6 +28,10 @@ export function toNull (v) {
|
|
|
16
28
|
throw new Error('Invalid `null`')
|
|
17
29
|
}
|
|
18
30
|
|
|
31
|
+
/**
|
|
32
|
+
* @param {unknown} v
|
|
33
|
+
* @returns {RegExp}
|
|
34
|
+
*/
|
|
19
35
|
export function toRegExp (v) {
|
|
20
36
|
log('toRegExp')
|
|
21
37
|
|
|
@@ -27,7 +43,7 @@ export function toRegExp (v) {
|
|
|
27
43
|
match,
|
|
28
44
|
expression,
|
|
29
45
|
flags
|
|
30
|
-
] = v.match(/^\/(.*)\/(\w*)$/)
|
|
46
|
+
] = v.match(/^\/(.*)\/(\w*)$/) ?? []
|
|
31
47
|
|
|
32
48
|
if (match) return new RegExp(expression, flags)
|
|
33
49
|
}
|
|
@@ -36,6 +52,10 @@ export function toRegExp (v) {
|
|
|
36
52
|
throw new Error('Invalid `reg exp`')
|
|
37
53
|
}
|
|
38
54
|
|
|
55
|
+
/**
|
|
56
|
+
* @param {unknown} v
|
|
57
|
+
* @returns {boolean}
|
|
58
|
+
*/
|
|
39
59
|
export function toBoolean (v) {
|
|
40
60
|
log('toBoolean')
|
|
41
61
|
|
|
@@ -46,15 +66,25 @@ export function toBoolean (v) {
|
|
|
46
66
|
throw new Error('Invalid `boolean`')
|
|
47
67
|
}
|
|
48
68
|
|
|
69
|
+
/**
|
|
70
|
+
* @param {unknown} v
|
|
71
|
+
* @returns {string}
|
|
72
|
+
*/
|
|
49
73
|
export function toString (v) {
|
|
50
74
|
log('toString')
|
|
51
75
|
|
|
52
76
|
if (typeof v === 'string') return v
|
|
53
77
|
if (typeof v === 'number') return String(v)
|
|
78
|
+
if (v === undefined) return 'undefined'
|
|
79
|
+
if (v instanceof Function || (v || false).constructor === Symbol) return String(v)
|
|
54
80
|
|
|
55
|
-
return JSON.stringify(v)
|
|
81
|
+
return String(JSON.stringify(v)) // `{ toJSON () { return undefined } }`
|
|
56
82
|
}
|
|
57
83
|
|
|
84
|
+
/**
|
|
85
|
+
* @param {unknown} v
|
|
86
|
+
* @returns {number}
|
|
87
|
+
*/
|
|
58
88
|
export function toNumber (v) {
|
|
59
89
|
log('toNumber')
|
|
60
90
|
|
|
@@ -4,6 +4,11 @@ const log = debug('@modernpoacher/rules-runner/comparators/all-of')
|
|
|
4
4
|
|
|
5
5
|
log('`@modernpoacher/rules-runner` is awake')
|
|
6
6
|
|
|
7
|
+
/**
|
|
8
|
+
* @param {{ allOf?: unknown[] | null }} expected
|
|
9
|
+
* @param {unknown} actual
|
|
10
|
+
* @returns {boolean}
|
|
11
|
+
*/
|
|
7
12
|
export default function allOf ({ allOf: values = null }, actual) {
|
|
8
13
|
log('allOf')
|
|
9
14
|
|
|
@@ -4,6 +4,11 @@ const log = debug('@modernpoacher/rules-runner/comparators/any-of')
|
|
|
4
4
|
|
|
5
5
|
log('`@modernpoacher/rules-runner` is awake')
|
|
6
6
|
|
|
7
|
+
/**
|
|
8
|
+
* @param {{ anyOf?: unknown[] | null }} expected
|
|
9
|
+
* @param {unknown} actual
|
|
10
|
+
* @returns {boolean}
|
|
11
|
+
*/
|
|
7
12
|
export default function anyOf ({ anyOf: values = null }, actual) {
|
|
8
13
|
log('anyOf')
|
|
9
14
|
|
|
@@ -6,6 +6,11 @@ const log = debug('@modernpoacher/rules-runner/comparators/between')
|
|
|
6
6
|
|
|
7
7
|
log('`@modernpoacher/rules-runner` is awake')
|
|
8
8
|
|
|
9
|
+
/**
|
|
10
|
+
* @param {{ between?: { lowerBound?: unknown, upperBound?: unknown } }} expected
|
|
11
|
+
* @param {unknown} actual
|
|
12
|
+
* @returns {boolean}
|
|
13
|
+
*/
|
|
9
14
|
export default function between ({ between: { lowerBound, upperBound } = {} }, actual) {
|
|
10
15
|
log('between')
|
|
11
16
|
|
|
@@ -6,6 +6,11 @@ const log = debug('@modernpoacher/rules-runner/comparators/boolean')
|
|
|
6
6
|
|
|
7
7
|
log('`@modernpoacher/rules-runner` is awake')
|
|
8
8
|
|
|
9
|
+
/**
|
|
10
|
+
* @param {unknown} expected
|
|
11
|
+
* @param {unknown} actual
|
|
12
|
+
* @returns {boolean}
|
|
13
|
+
*/
|
|
9
14
|
export default function boolean (expected, actual) {
|
|
10
15
|
log('boolean')
|
|
11
16
|
|
|
@@ -6,6 +6,11 @@ const log = debug('@modernpoacher/rules-runner/comparators/contains')
|
|
|
6
6
|
|
|
7
7
|
log('`@modernpoacher/rules-runner` is awake')
|
|
8
8
|
|
|
9
|
+
/**
|
|
10
|
+
* @param {{ contains: unknown }} expected
|
|
11
|
+
* @param {unknown} actual
|
|
12
|
+
* @returns {boolean}
|
|
13
|
+
*/
|
|
9
14
|
export default function contains ({ contains }, actual) {
|
|
10
15
|
log('contains')
|
|
11
16
|
|
|
@@ -4,6 +4,11 @@ const log = debug('@modernpoacher/rules-runner/comparators/equals')
|
|
|
4
4
|
|
|
5
5
|
log('`@modernpoacher/rules-runner` is awake')
|
|
6
6
|
|
|
7
|
+
/**
|
|
8
|
+
* @param {unknown} expected
|
|
9
|
+
* @param {unknown} actual
|
|
10
|
+
* @returns {boolean}
|
|
11
|
+
*/
|
|
7
12
|
export default function equals (expected, actual) {
|
|
8
13
|
log('equals')
|
|
9
14
|
|
|
@@ -6,6 +6,11 @@ const log = debug('@modernpoacher/rules-runner/comparators/greater-than')
|
|
|
6
6
|
|
|
7
7
|
log('`@modernpoacher/rules-runner` is awake')
|
|
8
8
|
|
|
9
|
+
/**
|
|
10
|
+
* @param {{ greaterThan: unknown }} expected
|
|
11
|
+
* @param {unknown} actual
|
|
12
|
+
* @returns {boolean}
|
|
13
|
+
*/
|
|
9
14
|
export default function greaterThan ({ greaterThan }, actual) {
|
|
10
15
|
log('greaterThan')
|
|
11
16
|
|
|
@@ -6,6 +6,11 @@ const log = debug('@modernpoacher/rules-runner/comparators/less-than')
|
|
|
6
6
|
|
|
7
7
|
log('`@modernpoacher/rules-runner` is awake')
|
|
8
8
|
|
|
9
|
+
/**
|
|
10
|
+
* @param {{ lessThan: unknown }} expected
|
|
11
|
+
* @param {unknown} actual
|
|
12
|
+
* @returns {boolean}
|
|
13
|
+
*/
|
|
9
14
|
export default function lessThan ({ lessThan }, actual) {
|
|
10
15
|
log('lessThan')
|
|
11
16
|
|
|
@@ -6,6 +6,11 @@ const log = debug('@modernpoacher/rules-runner/comparators/matches')
|
|
|
6
6
|
|
|
7
7
|
log('`@modernpoacher/rules-runner` is awake')
|
|
8
8
|
|
|
9
|
+
/**
|
|
10
|
+
* @param {{ matches: unknown }} expected
|
|
11
|
+
* @param {unknown} actual
|
|
12
|
+
* @returns {boolean}
|
|
13
|
+
*/
|
|
9
14
|
export default function matches ({ matches }, actual) {
|
|
10
15
|
log('matches')
|
|
11
16
|
|
package/src/comparators/not.mjs
CHANGED
|
@@ -4,6 +4,12 @@ const log = debug('@modernpoacher/rules-runner/comparators/not')
|
|
|
4
4
|
|
|
5
5
|
log('`@modernpoacher/rules-runner` is awake')
|
|
6
6
|
|
|
7
|
+
/**
|
|
8
|
+
* @this {{ runTest: (expectation: unknown, ...actual: unknown[]) => boolean }}
|
|
9
|
+
* @param {{ not: unknown }} expected
|
|
10
|
+
* @param {unknown} actual
|
|
11
|
+
* @returns {boolean}
|
|
12
|
+
*/
|
|
7
13
|
export default function not (expected, actual) {
|
|
8
14
|
log('not')
|
|
9
15
|
|
|
@@ -4,6 +4,11 @@ const log = debug('@modernpoacher/rules-runner/comparators/one-of')
|
|
|
4
4
|
|
|
5
5
|
log('`@modernpoacher/rules-runner` is awake')
|
|
6
6
|
|
|
7
|
+
/**
|
|
8
|
+
* @param {{ oneOf?: unknown[] | null }} expected
|
|
9
|
+
* @param {unknown} actual
|
|
10
|
+
* @returns {boolean}
|
|
11
|
+
*/
|
|
7
12
|
export default function oneOf ({ oneOf: values = null }, actual) {
|
|
8
13
|
log('oneOf')
|
|
9
14
|
|
package/src/index.d.mts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
+
type ObjectType = Record<PropertyKey, unknown>
|
|
2
|
+
|
|
1
3
|
declare class RulesRunner {
|
|
2
|
-
constructor (config
|
|
3
|
-
run (values?:
|
|
4
|
-
runTests (expectations?:
|
|
5
|
-
runTest (expectation
|
|
6
|
-
runOutcomes (outcomes?:
|
|
4
|
+
constructor (config: ObjectType)
|
|
5
|
+
run (values?: ObjectType, decisions?: ObjectType): ObjectType
|
|
6
|
+
runTests (expectations?: ObjectType, values?: ObjectType): boolean
|
|
7
|
+
runTest (expectation: unknown, ...actual: unknown[]): boolean
|
|
8
|
+
runOutcomes (outcomes?: ObjectType, accumulator?: ObjectType): ObjectType
|
|
7
9
|
}
|
|
8
10
|
|
|
9
11
|
declare module '@modernpoacher/rules-runner' {
|
package/src/index.mjs
CHANGED
|
@@ -33,18 +33,18 @@ export default class RulesRunner {
|
|
|
33
33
|
Object
|
|
34
34
|
.values(this.config)
|
|
35
35
|
.reduce((accumulator, rule) => {
|
|
36
|
-
if (
|
|
37
|
-
const IF =
|
|
36
|
+
if ('if' in rule) {
|
|
37
|
+
const { if: IF } = rule
|
|
38
38
|
|
|
39
39
|
if (this.runTests(IF, values)) {
|
|
40
|
-
if (
|
|
41
|
-
const THEN =
|
|
40
|
+
if ('then' in rule) {
|
|
41
|
+
const { then: THEN } = rule
|
|
42
42
|
|
|
43
43
|
return this.runOutcomes(THEN, accumulator)
|
|
44
44
|
}
|
|
45
45
|
} else {
|
|
46
|
-
if (
|
|
47
|
-
const OTHERWISE =
|
|
46
|
+
if ('otherwise' in rule) {
|
|
47
|
+
const { otherwise: OTHERWISE } = rule
|
|
48
48
|
|
|
49
49
|
return this.runOutcomes(OTHERWISE, accumulator)
|
|
50
50
|
}
|
|
@@ -83,8 +83,8 @@ export default class RulesRunner {
|
|
|
83
83
|
Object
|
|
84
84
|
.entries(expectation)
|
|
85
85
|
.some(([key, value]) => {
|
|
86
|
-
if (
|
|
87
|
-
const comparator =
|
|
86
|
+
if (key in comparators) {
|
|
87
|
+
const comparator = comparators[key]
|
|
88
88
|
|
|
89
89
|
return comparator.call(this, { [key]: value }, ...actual)
|
|
90
90
|
}
|
package/tsconfig.json
CHANGED
|
@@ -2,10 +2,16 @@
|
|
|
2
2
|
"compilerOptions": {
|
|
3
3
|
"module": "NodeNext",
|
|
4
4
|
"target": "ESNext",
|
|
5
|
+
"allowJs": true,
|
|
6
|
+
"checkJs": true,
|
|
5
7
|
"noEmit": true,
|
|
6
8
|
"strict": true,
|
|
7
|
-
"isolatedModules": true
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
"isolatedModules": true
|
|
10
|
+
},
|
|
11
|
+
"include": [
|
|
12
|
+
"src"
|
|
13
|
+
],
|
|
14
|
+
"exclude": [
|
|
15
|
+
"test"
|
|
16
|
+
]
|
|
11
17
|
}
|
package/babel.config.cjs
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
const debug = require('debug')
|
|
2
|
-
|
|
3
|
-
const log = debug('@modernpoacher/rules-runner')
|
|
4
|
-
|
|
5
|
-
const {
|
|
6
|
-
env: {
|
|
7
|
-
NODE_ENV = 'development'
|
|
8
|
-
}
|
|
9
|
-
} = process
|
|
10
|
-
|
|
11
|
-
log('`@modernpoacher/rules-runner` is awake')
|
|
12
|
-
|
|
13
|
-
function env () {
|
|
14
|
-
log({ NODE_ENV })
|
|
15
|
-
|
|
16
|
-
return (
|
|
17
|
-
NODE_ENV === 'production'
|
|
18
|
-
)
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
module.exports = (api) => {
|
|
22
|
-
if (api) api.cache.using(env)
|
|
23
|
-
|
|
24
|
-
return {
|
|
25
|
-
presets: [
|
|
26
|
-
[
|
|
27
|
-
'@babel/env',
|
|
28
|
-
{
|
|
29
|
-
targets: {
|
|
30
|
-
node: 'current'
|
|
31
|
-
},
|
|
32
|
-
useBuiltIns: 'usage',
|
|
33
|
-
corejs: 3
|
|
34
|
-
}
|
|
35
|
-
]
|
|
36
|
-
]
|
|
37
|
-
}
|
|
38
|
-
}
|