@modernpoacher/rules-runner 2.3.174 → 2.3.188

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.
@@ -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,18 @@
1
+ #!/usr/bin/env node
2
+
3
+ if (process.env.CI === 'true') process.exit(0)
4
+
5
+ // @ts-ignore
6
+ Promise.resolve(import('husky'))
7
+ .then(({ default: husky }) => { husky() })
8
+ .catch((e) => {
9
+ if (e instanceof Error) {
10
+ if ('code' in e) {
11
+ const { code } = e
12
+ if (code === 'ERR_MODULE_NOT_FOUND') return
13
+ }
14
+
15
+ const { message } = e
16
+ if (message) console.error(`💥 ${message}`)
17
+ }
18
+ })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@modernpoacher/rules-runner",
3
- "version": "2.3.174",
3
+ "version": "2.3.188",
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": ">=18.12.0"
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
- "@babel/core": "^7.28.5",
56
- "@babel/eslint-parser": "^7.28.5",
57
- "@babel/preset-env": "^7.28.5",
58
- "@typescript-eslint/eslint-plugin": "7.18.0",
59
- "@typescript-eslint/parser": "7.18.0",
60
- "chai": "^6.2.1",
55
+ "@sequencemedia/eslint-config-standard": "0.4.2",
56
+ "@sequencemedia/eslint-config-typescript": "0.2.13",
57
+ "@types/debug": "^4.1.13",
58
+ "chai": "^6.3.0",
61
59
  "cross-env": "^10.1.0",
62
- "eslint": "8.57.1",
63
- "eslint-config-love": "47.0.0",
64
- "eslint-config-standard": "^17.1.0",
60
+ "eslint": "10.15.0",
61
+ "globals": "^17.14.0",
65
62
  "husky": "^9.1.7",
66
- "mocha": "^11.7.5"
63
+ "mocha": "^12.0.3"
67
64
  },
68
65
  "imports": {
69
66
  "#rules-runner": "./src/index.mjs",
@@ -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
 
@@ -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?: object)
3
- run (values?: object, decisions?: object): object
4
- runTests (expectations?: object, values?: object): object
5
- runTest (expectation?: object): object
6
- runOutcomes (outcomes?: object, accumulator?: object): object
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
@@ -16,9 +16,7 @@ log('`@modernpoacher/rules-runner` is awake')
16
16
  const hasArrayKey = (key) => /^(.*)\[\]$/.test(key)
17
17
 
18
18
  function getArrayKey (key) {
19
- const [,
20
- arrayKey
21
- ] = key.match(/^(.*)\[\]$/)
19
+ const [, arrayKey] = key.match(/^(.*)\[\]$/)
22
20
 
23
21
  return arrayKey
24
22
  }
@@ -33,18 +31,18 @@ export default class RulesRunner {
33
31
  Object
34
32
  .values(this.config)
35
33
  .reduce((accumulator, rule) => {
36
- if (Reflect.has(rule, 'if')) {
37
- const IF = Reflect.get(rule, 'if')
34
+ if ('if' in rule) {
35
+ const { if: IF } = rule
38
36
 
39
37
  if (this.runTests(IF, values)) {
40
- if (Reflect.has(rule, 'then')) {
41
- const THEN = Reflect.get(rule, 'then')
38
+ if ('then' in rule) {
39
+ const { then: THEN } = rule
42
40
 
43
41
  return this.runOutcomes(THEN, accumulator)
44
42
  }
45
43
  } else {
46
- if (Reflect.has(rule, 'otherwise')) {
47
- const OTHERWISE = Reflect.get(rule, 'otherwise')
44
+ if ('otherwise' in rule) {
45
+ const { otherwise: OTHERWISE } = rule
48
46
 
49
47
  return this.runOutcomes(OTHERWISE, accumulator)
50
48
  }
@@ -83,8 +81,8 @@ export default class RulesRunner {
83
81
  Object
84
82
  .entries(expectation)
85
83
  .some(([key, value]) => {
86
- if (Reflect.has(comparators, key)) {
87
- const comparator = Reflect.get(comparators, key)
84
+ if (key in comparators) {
85
+ const comparator = comparators[key]
88
86
 
89
87
  return comparator.call(this, { [key]: value }, ...actual)
90
88
  }
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
- "moduleResolution": "NodeNext",
9
- "baseUrl": "."
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
- }