Skip to content

Commit f374bd2

Browse files
authored
Merge pull request #18 from damusix/esm-conversion
Convert to native ESM (vitest + oxc toolchain)
2 parents f173633 + ae377f8 commit f374bd2

16 files changed

Lines changed: 671 additions & 687 deletions

.eslintignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

.github/workflows/ci-module.yml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
name: ci
22

33
on:
4-
push:
5-
branches:
6-
- master
7-
pull_request:
8-
workflow_dispatch:
4+
push:
5+
branches:
6+
- master
7+
pull_request:
8+
workflow_dispatch:
99

1010
jobs:
11-
test:
12-
uses: hapijs/.github/.github/workflows/ci-module.yml@master
11+
test:
12+
uses: hapijs/.github/.github/workflows/ci-module.yml@min-node-22-hapi-21

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
**/node_modules
22
**/package-lock.json
33

4-
coverage.*
4+
coverage/
55

66
**/.DS_Store
77
**/._*

API.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,30 @@
33
```js
44
const Formula = require('@hapi/formula');
55

6-
76
const functions = {
8-
x: (value) => value + 10
7+
x: (value) => value + 10,
98
};
109

1110
const constants = {
12-
Z: 100
11+
Z: 100,
1312
};
1413

1514
const reference = function (name) {
16-
1715
return (context) => context[name];
1816
};
1917

20-
2118
const formula = new Formula.Parser('1 + a.b.c.2.4.x + [b] + x([y + 4] + Z)', { functions, constants, reference });
2219

23-
formula.evaluate({ 'a.b.c.2.4.x': 2, b: 3, 'y + 4': 5 }); // 1 + 2 + 3 + 5 + 10 + 100
24-
formula.evaluate({ 'a.b.c.2.4.x': '2', b: 3, 'y + 4': '5' }); // '123510010'
20+
formula.evaluate({ 'a.b.c.2.4.x': 2, b: 3, 'y + 4': 5 }); // 1 + 2 + 3 + 5 + 10 + 100
21+
formula.evaluate({ 'a.b.c.2.4.x': '2', b: 3, 'y + 4': '5' }); // '123510010'
2522
```
2623

2724
## Methods
2825

2926
### `new Formula.Parser(formula, [options])`
3027

3128
Creates a new formula parser object where:
29+
3230
- `formula` - the formula string to parse.
3331
- `options` - optional settings:
3432
- `constants` - a hash of key-value pairs used to convert constants to values.
@@ -39,6 +37,7 @@ Creates a new formula parser object where:
3937
### `parser.evaluate([context])`
4038

4139
Evaluate the formula where:
40+
4241
- `context` - optional object with runtime formula context used to resolve variables.
4342

4443
Returns the string or number outcome of the resolved formula.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# @sideway/formula
1+
# @hapi/formula
22

33
#### Math and string formula parser.
44

lib/index.d.ts

Lines changed: 0 additions & 52 deletions
This file was deleted.

oxfmt.config.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import DefaultOxfmtConfig from '@hapi/oxc-plugin/oxfmt';
2+
import { defineConfig } from 'oxfmt';
3+
4+
import type { OxfmtConfig } from 'oxfmt';
5+
6+
export default defineConfig({
7+
...DefaultOxfmtConfig,
8+
}) as OxfmtConfig;

oxlint.config.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import HapiRecommended from '@hapi/oxc-plugin/oxlint';
2+
import { defineConfig } from 'oxlint';
3+
4+
import type { OxlintConfig } from 'oxlint';
5+
6+
export default defineConfig({
7+
extends: [HapiRecommended],
8+
env: {
9+
...HapiRecommended.env,
10+
},
11+
}) as OxlintConfig;

package.json

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,45 @@
11
{
22
"name": "@hapi/formula",
3-
"description": "Math and string formula parser.",
43
"version": "3.0.2",
5-
"repository": "git://github.com/hapijs/formula",
6-
"main": "lib/index.js",
7-
"types": "lib/index.d.ts",
4+
"description": "Math and string formula parser.",
85
"keywords": [
96
"formula",
10-
"parser",
117
"math",
8+
"parser",
129
"string"
1310
],
11+
"license": "BSD-3-Clause",
12+
"repository": "git://github.com/hapijs/formula",
1413
"files": [
15-
"lib"
14+
"src",
15+
"API.md"
1616
],
17-
"dependencies": {},
18-
"devDependencies": {
19-
"@hapi/code": "^9.0.3",
20-
"@hapi/lab": "^25.1.2",
21-
"@types/node": "^14.18.36",
22-
"typescript": "4.0.x"
17+
"type": "module",
18+
"types": "src/index.d.ts",
19+
"exports": {
20+
".": {
21+
"types": "./src/index.d.ts",
22+
"default": "./src/index.js"
23+
}
2324
},
2425
"scripts": {
25-
"test": "lab -a @hapi/code -t 100 -L -Y",
26-
"test-cov-html": "lab -a @hapi/code -t 100 -L -r html -o coverage.html"
26+
"test": "vitest run --coverage",
27+
"typecheck": "tsc --noEmit",
28+
"lint": "oxlint",
29+
"lint:fix": "oxlint --fix",
30+
"fmt": "oxfmt --check",
31+
"fmt:fix": "oxfmt",
32+
"check": "npm run lint && npm run fmt && npm run typecheck && npm test"
33+
},
34+
"devDependencies": {
35+
"@hapi/oxc-plugin": "^1.0.1",
36+
"@vitest/coverage-v8": "^4.1.9",
37+
"oxfmt": "^0.57.0",
38+
"oxlint": "^1.72.0",
39+
"typescript": "^6.0.3",
40+
"vitest": "^4.1.9"
2741
},
28-
"license": "BSD-3-Clause"
42+
"engines": {
43+
"node": ">=22"
44+
}
2945
}

src/index.d.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/** Formula parser */
2+
export class Parser<T extends string | number> {
3+
/**
4+
* Create a new formula parser.
5+
*
6+
* @param formula - The formula string to parse.
7+
* @param options - Optional settings.
8+
*/
9+
constructor(formula: string, options?: Options);
10+
11+
/**
12+
* Evaluate the formula.
13+
*
14+
* @param context - Optional object with runtime formula context used to resolve variables.
15+
* @returns The string or number outcome of the resolved formula.
16+
*/
17+
evaluate(context?: any): T;
18+
}
19+
20+
export interface Options {
21+
/** A hash of key - value pairs used to convert constants to values. */
22+
readonly constants?: Record<string, any>;
23+
24+
/** A regular expression used to validate token variables. */
25+
readonly tokenRx?: RegExp;
26+
27+
/** A variable resolver factory function. */
28+
readonly reference?: Options.Reference;
29+
30+
/** A hash of key-value pairs used to resolve formula functions. */
31+
readonly functions?: Record<string, Function>;
32+
}
33+
34+
export namespace Options {
35+
type Reference = (name: string) => (context: any) => any;
36+
}

0 commit comments

Comments
 (0)