@stll/agent-input 0.1.8 → 0.2.0
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/README.md +14 -0
- package/dist/numeric-token.d.ts +18 -0
- package/dist/numeric-token.js +45 -0
- package/package.json +6 -3
package/README.md
CHANGED
|
@@ -82,3 +82,17 @@ their own contracts. Ordinary strings are never normalized heuristically.
|
|
|
82
82
|
## License
|
|
83
83
|
|
|
84
84
|
Apache-2.0
|
|
85
|
+
|
|
86
|
+
## Machine numeric tokens
|
|
87
|
+
|
|
88
|
+
`@stll/agent-input/numeric-token` reads protocol counters and numeric strings that
|
|
89
|
+
must consume the entire input. `parseIntegerToken` accepts ASCII digits and an
|
|
90
|
+
optional leading minus, and requires a safe integer. `parseDecimalToken` uses
|
|
91
|
+
`-?[0-9]+(\.[0-9]+)?` and requires a finite value. Neither trims whitespace nor
|
|
92
|
+
accepts plus signs, exponents, hexadecimal, grouping or incomplete fractions.
|
|
93
|
+
Both return `Result<number, InvalidNumericTokenError>` and accept optional
|
|
94
|
+
`sign: "unsigned"`, `min` and `max` bounds. Unsigned mode also rejects `-0`.
|
|
95
|
+
|
|
96
|
+
Callers choose their invalid-input policy explicitly. Human-authored localized
|
|
97
|
+
values continue through `normalizeNumber`; this strict subpath does not replace
|
|
98
|
+
its intentional normalization rules.
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Result } from "better-result";
|
|
2
|
+
//#region src/numeric-token.d.ts
|
|
3
|
+
declare const InvalidNumericTokenError_base: import("better-result").TaggedErrorClass<"InvalidNumericTokenError">;
|
|
4
|
+
export declare class InvalidNumericTokenError extends InvalidNumericTokenError_base<{
|
|
5
|
+
message: string;
|
|
6
|
+
reason: "syntax" | "unsafe-integer" | "non-finite" | "out-of-range";
|
|
7
|
+
}> {}
|
|
8
|
+
export type NumericTokenOptions = {
|
|
9
|
+
/** Signed by default; unsigned tokens reject even the spelling `-0`. */
|
|
10
|
+
sign?: "signed" | "unsigned";
|
|
11
|
+
min?: number;
|
|
12
|
+
max?: number;
|
|
13
|
+
};
|
|
14
|
+
/** Parse an entire ASCII decimal integer token; no trimming or prefix coercion. */
|
|
15
|
+
export declare const parseIntegerToken: (input: unknown, options?: NumericTokenOptions) => Result<number, InvalidNumericTokenError>;
|
|
16
|
+
/** Grammar: `-?[0-9]+(\.[0-9]+)?`; no grouping, exponent or implicit whitespace. */
|
|
17
|
+
export declare const parseDecimalToken: (input: unknown, options?: NumericTokenOptions) => Result<number, InvalidNumericTokenError>;
|
|
18
|
+
//#endregion
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { Result, TaggedError } from "better-result";
|
|
2
|
+
//#region src/numeric-token.ts
|
|
3
|
+
var InvalidNumericTokenError = class extends TaggedError("InvalidNumericTokenError") {};
|
|
4
|
+
const SIGNED_INTEGER = /^-?[0-9]+(?![\s\S])/u;
|
|
5
|
+
const UNSIGNED_INTEGER = /^[0-9]+(?![\s\S])/u;
|
|
6
|
+
/** Base-ten digits, with an optional fraction containing at least one digit. */
|
|
7
|
+
const SIGNED_DECIMAL = /^-?[0-9]+(?:\.[0-9]+)?(?![\s\S])/u;
|
|
8
|
+
const UNSIGNED_DECIMAL = /^[0-9]+(?:\.[0-9]+)?(?![\s\S])/u;
|
|
9
|
+
const inRange = (value, { min, max }) => {
|
|
10
|
+
if (min !== void 0 && value < min || max !== void 0 && value > max) return Result.err(new InvalidNumericTokenError({
|
|
11
|
+
reason: "out-of-range",
|
|
12
|
+
message: "Numeric token is outside the permitted range"
|
|
13
|
+
}));
|
|
14
|
+
return Result.ok(value);
|
|
15
|
+
};
|
|
16
|
+
/** Parse an entire ASCII decimal integer token; no trimming or prefix coercion. */
|
|
17
|
+
const parseIntegerToken = (input, options = {}) => {
|
|
18
|
+
const grammar = options.sign === "unsigned" ? UNSIGNED_INTEGER : SIGNED_INTEGER;
|
|
19
|
+
if (typeof input !== "string" || !grammar.test(input)) return Result.err(new InvalidNumericTokenError({
|
|
20
|
+
reason: "syntax",
|
|
21
|
+
message: "Expected a whole decimal integer token"
|
|
22
|
+
}));
|
|
23
|
+
const value = Number(input);
|
|
24
|
+
if (!Number.isSafeInteger(value)) return Result.err(new InvalidNumericTokenError({
|
|
25
|
+
reason: "unsafe-integer",
|
|
26
|
+
message: "Integer token exceeds the safe integer range"
|
|
27
|
+
}));
|
|
28
|
+
return inRange(value, options);
|
|
29
|
+
};
|
|
30
|
+
/** Grammar: `-?[0-9]+(\.[0-9]+)?`; no grouping, exponent or implicit whitespace. */
|
|
31
|
+
const parseDecimalToken = (input, options = {}) => {
|
|
32
|
+
const grammar = options.sign === "unsigned" ? UNSIGNED_DECIMAL : SIGNED_DECIMAL;
|
|
33
|
+
if (typeof input !== "string" || !grammar.test(input)) return Result.err(new InvalidNumericTokenError({
|
|
34
|
+
reason: "syntax",
|
|
35
|
+
message: "Expected a whole decimal token"
|
|
36
|
+
}));
|
|
37
|
+
const value = Number(input);
|
|
38
|
+
if (!Number.isFinite(value)) return Result.err(new InvalidNumericTokenError({
|
|
39
|
+
reason: "non-finite",
|
|
40
|
+
message: "Decimal token must be finite"
|
|
41
|
+
}));
|
|
42
|
+
return inRange(value, options);
|
|
43
|
+
};
|
|
44
|
+
//#endregion
|
|
45
|
+
export { InvalidNumericTokenError, parseDecimalToken, parseIntegerToken };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stll/agent-input",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Lenient readers for values agents write: dates, date bounds, date-format specs, numbers, booleans, locales, countries, enum and vocabulary values, ids, ELIs, string lists",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"agent",
|
|
@@ -44,6 +44,10 @@
|
|
|
44
44
|
"types": "./dist/locale.d.ts",
|
|
45
45
|
"import": "./dist/locale.js"
|
|
46
46
|
},
|
|
47
|
+
"./numeric-token": {
|
|
48
|
+
"types": "./dist/numeric-token.d.ts",
|
|
49
|
+
"import": "./dist/numeric-token.js"
|
|
50
|
+
},
|
|
47
51
|
"./string-list": {
|
|
48
52
|
"types": "./dist/string-list.d.ts",
|
|
49
53
|
"import": "./dist/string-list.js"
|
|
@@ -61,7 +65,6 @@
|
|
|
61
65
|
"typecheck": "bun check --no-pretty --all --project=tsconfig.json",
|
|
62
66
|
"lint": "cd ../.. && bun --bun oxlint -c oxlint.config.ts --report-unused-disable-directives-severity=error --type-aware --type-check packages/agent-input",
|
|
63
67
|
"lint:fix": "cd ../.. && bun --bun oxlint -c oxlint.config.ts --type-aware --fix packages/agent-input",
|
|
64
|
-
"format": "bun ../../scripts/run-oxfmt.ts .",
|
|
65
68
|
"prepack": "bun run build"
|
|
66
69
|
},
|
|
67
70
|
"dependencies": {
|
|
@@ -75,7 +78,7 @@
|
|
|
75
78
|
"devDependencies": {
|
|
76
79
|
"@stll/property-testing": "0.0.0",
|
|
77
80
|
"@stll/typescript-config": "0.0.0",
|
|
78
|
-
"bun-types": "1.4.
|
|
81
|
+
"bun-types": "1.4.4",
|
|
79
82
|
"fast-check": "^4.10.2",
|
|
80
83
|
"tsdown": "0.23.0"
|
|
81
84
|
},
|