Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .eslintignore

This file was deleted.

14 changes: 7 additions & 7 deletions .github/workflows/ci-module.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
name: ci

on:
push:
branches:
- master
pull_request:
workflow_dispatch:
push:
branches:
- master
pull_request:
workflow_dispatch:

jobs:
test:
uses: hapijs/.github/.github/workflows/ci-module.yml@master
test:
uses: hapijs/.github/.github/workflows/ci-module.yml@min-node-22-hapi-21
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
**/node_modules
**/package-lock.json

coverage.*
coverage/

**/.DS_Store
**/._*
Expand Down
13 changes: 6 additions & 7 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,30 @@
```js
const Formula = require('@hapi/formula');


const functions = {
x: (value) => value + 10
x: (value) => value + 10,
};

const constants = {
Z: 100
Z: 100,
};

const reference = function (name) {

return (context) => context[name];
};


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

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

## Methods

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

Creates a new formula parser object where:

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

Evaluate the formula where:

- `context` - optional object with runtime formula context used to resolve variables.

Returns the string or number outcome of the resolved formula.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# @sideway/formula
# @hapi/formula

#### Math and string formula parser.

Expand Down
52 changes: 0 additions & 52 deletions lib/index.d.ts

This file was deleted.

8 changes: 8 additions & 0 deletions oxfmt.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import DefaultOxfmtConfig from '@hapi/oxc-plugin/oxfmt';
import { defineConfig } from 'oxfmt';

import type { OxfmtConfig } from 'oxfmt';

export default defineConfig({
...DefaultOxfmtConfig,
}) as OxfmtConfig;
11 changes: 11 additions & 0 deletions oxlint.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import HapiRecommended from '@hapi/oxc-plugin/oxlint';
import { defineConfig } from 'oxlint';

import type { OxlintConfig } from 'oxlint';

export default defineConfig({
extends: [HapiRecommended],
env: {
...HapiRecommended.env,
},
}) as OxlintConfig;
46 changes: 31 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,29 +1,45 @@
{
"name": "@hapi/formula",
"description": "Math and string formula parser.",
"version": "3.0.2",
"repository": "git://github.com/hapijs/formula",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"description": "Math and string formula parser.",
"keywords": [
"formula",
"parser",
"math",
"parser",
"string"
],
"license": "BSD-3-Clause",
"repository": "git://github.com/hapijs/formula",
"files": [
"lib"
"src",
"API.md"
],
"dependencies": {},
"devDependencies": {
"@hapi/code": "^9.0.3",
"@hapi/lab": "^25.1.2",
"@types/node": "^14.18.36",
"typescript": "4.0.x"
"type": "module",
"types": "src/index.d.ts",
"exports": {
".": {
"types": "./src/index.d.ts",
"default": "./src/index.js"
}
},
"scripts": {
"test": "lab -a @hapi/code -t 100 -L -Y",
"test-cov-html": "lab -a @hapi/code -t 100 -L -r html -o coverage.html"
"test": "vitest run --coverage",
"typecheck": "tsc --noEmit",
"lint": "oxlint",
"lint:fix": "oxlint --fix",
"fmt": "oxfmt --check",
"fmt:fix": "oxfmt",
"check": "npm run lint && npm run fmt && npm run typecheck && npm test"
},
"devDependencies": {
"@hapi/oxc-plugin": "^1.0.1",
"@vitest/coverage-v8": "^4.1.9",
"oxfmt": "^0.57.0",
"oxlint": "^1.72.0",
"typescript": "^6.0.3",
"vitest": "^4.1.9"
},
"license": "BSD-3-Clause"
"engines": {
"node": ">=22"
}
}
36 changes: 36 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/** Formula parser */
export class Parser<T extends string | number> {
/**
* Create a new formula parser.
*
* @param formula - The formula string to parse.
* @param options - Optional settings.
*/
constructor(formula: string, options?: Options);

/**
* Evaluate the formula.
*
* @param context - Optional object with runtime formula context used to resolve variables.
* @returns The string or number outcome of the resolved formula.
*/
evaluate(context?: any): T;
}

export interface Options {
/** A hash of key - value pairs used to convert constants to values. */
readonly constants?: Record<string, any>;

/** A regular expression used to validate token variables. */
readonly tokenRx?: RegExp;

/** A variable resolver factory function. */
readonly reference?: Options.Reference;

/** A hash of key-value pairs used to resolve formula functions. */
readonly functions?: Record<string, Function>;
}

export namespace Options {
type Reference = (name: string) => (context: any) => any;
}
Loading
Loading