@effected/yaml 0.3.1 → 0.4.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 +1 -1
- package/Yaml.js +45 -0
- package/YamlEdit.js +7 -1
- package/index.d.ts +57 -2
- package/package.json +4 -3
- package/tsdoc-metadata.json +1 -1
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
[](https://www.npmjs.com/package/@effected/yaml)
|
|
4
4
|
[](https://opensource.org/licenses/MIT)
|
|
5
5
|
[](https://nodejs.org/)
|
|
6
|
-
[](https://www.typescriptlang.org/)
|
|
7
7
|
|
|
8
8
|
Zero-dependency YAML 1.2 parsing, editing and formatting expressed as Effect schemas and pure functions. Parse a single document or a multi-document stream into plain values or an offset-preserving AST, resolve anchors and aliases, strip comments, compute byte-minimal edits, format, modify by path, walk a document as a `Stream` and decode straight into a validated domain schema.
|
|
9
9
|
|
package/Yaml.js
CHANGED
|
@@ -527,6 +527,51 @@ var Yaml = class Yaml {
|
|
|
527
527
|
static schema(target, options) {
|
|
528
528
|
return Yaml.fromString(options).pipe(Schema.decodeTo(target));
|
|
529
529
|
}
|
|
530
|
+
/**
|
|
531
|
+
* Bind a target schema to the YAML codec once, yielding the composed
|
|
532
|
+
* schema plus pre-derived `decode`/`encode` directions — the
|
|
533
|
+
* {@link Yaml.schema} composition without the generic `Schema` machinery
|
|
534
|
+
* at every use site. Binds the plain single-document form only: default
|
|
535
|
+
* {@link YamlParseOptions} on decode, default stringify options on encode;
|
|
536
|
+
* for multi-document streams compose over {@link Yaml.allFromString}
|
|
537
|
+
* directly.
|
|
538
|
+
*
|
|
539
|
+
* Both directions fail with `Schema.SchemaError`, exactly as
|
|
540
|
+
* `Schema.decodeEffect`/`Schema.encodeEffect` over {@link Yaml.schema}
|
|
541
|
+
* would; the target's decoding/encoding service requirements flow through.
|
|
542
|
+
*
|
|
543
|
+
* @remarks
|
|
544
|
+
* Schema-producing: each call composes a fresh schema and derives both
|
|
545
|
+
* directions from it. Bind the result to a `const` — that single binding is
|
|
546
|
+
* the point.
|
|
547
|
+
*
|
|
548
|
+
* @example
|
|
549
|
+
* ```ts
|
|
550
|
+
* import { Yaml } from "@effected/yaml";
|
|
551
|
+
* import { Effect, Schema } from "effect";
|
|
552
|
+
*
|
|
553
|
+
* const Config = Schema.Struct({ port: Schema.Number });
|
|
554
|
+
* const config = Yaml.bind(Config);
|
|
555
|
+
*
|
|
556
|
+
* const program = Effect.gen(function* () {
|
|
557
|
+
* const value = yield* config.decode("port: 3000");
|
|
558
|
+
* const text = yield* config.encode(value);
|
|
559
|
+
* return [value, text] as const;
|
|
560
|
+
* });
|
|
561
|
+
* ```
|
|
562
|
+
*
|
|
563
|
+
* @param target - The domain schema decoded values must satisfy.
|
|
564
|
+
* @returns A {@link YamlBoundCodec} carrying the composed schema and its
|
|
565
|
+
* two pre-bound directions.
|
|
566
|
+
*/
|
|
567
|
+
static bind(target) {
|
|
568
|
+
const schema = Yaml.schema(target);
|
|
569
|
+
return {
|
|
570
|
+
schema,
|
|
571
|
+
decode: Schema.decodeEffect(schema),
|
|
572
|
+
encode: Schema.encodeEffect(schema)
|
|
573
|
+
};
|
|
574
|
+
}
|
|
530
575
|
};
|
|
531
576
|
/**
|
|
532
577
|
* Parse for `equals`/`equalsValue`: any recorded error — fatal or not — or a
|
package/YamlEdit.js
CHANGED
|
@@ -31,10 +31,16 @@ var YamlEdit = class extends Schema.Class("YamlEdit")({
|
|
|
31
31
|
/**
|
|
32
32
|
* Apply `edits` to `text`, producing a new string. Edits are applied in
|
|
33
33
|
* reverse-offset order so earlier offsets stay valid; the input `edits`
|
|
34
|
-
* array is not mutated.
|
|
34
|
+
* array is not mutated. Overlapping edits are a programmer error and throw
|
|
35
|
+
* as a defect — `YamlFormat` never produces them.
|
|
35
36
|
*/
|
|
36
37
|
static applyAll(text, edits) {
|
|
37
38
|
const sorted = [...edits].sort((a, b) => b.offset - a.offset);
|
|
39
|
+
for (let i = 0; i + 1 < sorted.length; i++) {
|
|
40
|
+
const upper = sorted[i];
|
|
41
|
+
const lower = sorted[i + 1];
|
|
42
|
+
if (lower.offset + lower.length > upper.offset) throw new Error(`YamlEdit.applyAll received overlapping edits at offsets ${lower.offset} and ${upper.offset} — overlapping edits are a programmer error`);
|
|
43
|
+
}
|
|
38
44
|
let result = text;
|
|
39
45
|
for (const edit of sorted) result = result.substring(0, edit.offset) + edit.content + result.substring(edit.offset + edit.length);
|
|
40
46
|
return result;
|
package/index.d.ts
CHANGED
|
@@ -237,6 +237,22 @@ declare const YamlStringifyError_base: Schema.Class<YamlStringifyError, Schema.T
|
|
|
237
237
|
declare class YamlStringifyError extends YamlStringifyError_base {
|
|
238
238
|
get message(): string;
|
|
239
239
|
}
|
|
240
|
+
/**
|
|
241
|
+
* A domain codec pre-bound to its two directions, returned by
|
|
242
|
+
* {@link Yaml.bind}: the composed `schema` (what {@link Yaml.schema} returns)
|
|
243
|
+
* plus `decode` and `encode` functions derived from it once, so callers need
|
|
244
|
+
* no generic `Schema` machinery at the use site.
|
|
245
|
+
*
|
|
246
|
+
* @public
|
|
247
|
+
*/
|
|
248
|
+
interface YamlBoundCodec<T, RD = never, RE = never> {
|
|
249
|
+
/** The composed codec decoding a YAML `string` straight into `T`. */
|
|
250
|
+
readonly schema: Schema.Codec<T, string, RD, RE>;
|
|
251
|
+
/** Decode a single-document YAML string into a validated `T`. */
|
|
252
|
+
readonly decode: (text: string) => Effect.Effect<T, Schema.SchemaError, RD>;
|
|
253
|
+
/** Encode a `T` back to YAML text with default stringify options. */
|
|
254
|
+
readonly encode: (value: T) => Effect.Effect<string, Schema.SchemaError, RE>;
|
|
255
|
+
}
|
|
240
256
|
/**
|
|
241
257
|
* Static entry points for YAML parsing, stringification, comment stripping,
|
|
242
258
|
* semantic equality and the schema factories. Not instantiable.
|
|
@@ -404,6 +420,44 @@ declare class Yaml {
|
|
|
404
420
|
* {@link Yaml.fromString}).
|
|
405
421
|
*/
|
|
406
422
|
static schema<T, E, RD = never, RE = never>(target: Schema.Codec<T, E, RD, RE>, options?: YamlParseOptions): Schema.Codec<T, string, RD, RE>;
|
|
423
|
+
/**
|
|
424
|
+
* Bind a target schema to the YAML codec once, yielding the composed
|
|
425
|
+
* schema plus pre-derived `decode`/`encode` directions — the
|
|
426
|
+
* {@link Yaml.schema} composition without the generic `Schema` machinery
|
|
427
|
+
* at every use site. Binds the plain single-document form only: default
|
|
428
|
+
* {@link YamlParseOptions} on decode, default stringify options on encode;
|
|
429
|
+
* for multi-document streams compose over {@link Yaml.allFromString}
|
|
430
|
+
* directly.
|
|
431
|
+
*
|
|
432
|
+
* Both directions fail with `Schema.SchemaError`, exactly as
|
|
433
|
+
* `Schema.decodeEffect`/`Schema.encodeEffect` over {@link Yaml.schema}
|
|
434
|
+
* would; the target's decoding/encoding service requirements flow through.
|
|
435
|
+
*
|
|
436
|
+
* @remarks
|
|
437
|
+
* Schema-producing: each call composes a fresh schema and derives both
|
|
438
|
+
* directions from it. Bind the result to a `const` — that single binding is
|
|
439
|
+
* the point.
|
|
440
|
+
*
|
|
441
|
+
* @example
|
|
442
|
+
* ```ts
|
|
443
|
+
* import { Yaml } from "@effected/yaml";
|
|
444
|
+
* import { Effect, Schema } from "effect";
|
|
445
|
+
*
|
|
446
|
+
* const Config = Schema.Struct({ port: Schema.Number });
|
|
447
|
+
* const config = Yaml.bind(Config);
|
|
448
|
+
*
|
|
449
|
+
* const program = Effect.gen(function* () {
|
|
450
|
+
* const value = yield* config.decode("port: 3000");
|
|
451
|
+
* const text = yield* config.encode(value);
|
|
452
|
+
* return [value, text] as const;
|
|
453
|
+
* });
|
|
454
|
+
* ```
|
|
455
|
+
*
|
|
456
|
+
* @param target - The domain schema decoded values must satisfy.
|
|
457
|
+
* @returns A {@link YamlBoundCodec} carrying the composed schema and its
|
|
458
|
+
* two pre-bound directions.
|
|
459
|
+
*/
|
|
460
|
+
static bind<T, E, RD = never, RE = never>(target: Schema.Codec<T, E, RD, RE>): YamlBoundCodec<T, RD, RE>;
|
|
407
461
|
}
|
|
408
462
|
//#endregion
|
|
409
463
|
//#region src/YamlEdit.d.ts
|
|
@@ -453,7 +507,8 @@ declare class YamlEdit extends YamlEdit_base {
|
|
|
453
507
|
/**
|
|
454
508
|
* Apply `edits` to `text`, producing a new string. Edits are applied in
|
|
455
509
|
* reverse-offset order so earlier offsets stay valid; the input `edits`
|
|
456
|
-
* array is not mutated.
|
|
510
|
+
* array is not mutated. Overlapping edits are a programmer error and throw
|
|
511
|
+
* as a defect — `YamlFormat` never produces them.
|
|
457
512
|
*/
|
|
458
513
|
static applyAll(text: string, edits: ReadonlyArray<YamlEdit>): string;
|
|
459
514
|
}
|
|
@@ -1466,5 +1521,5 @@ declare class YamlVisitor {
|
|
|
1466
1521
|
static visit(text: string, options?: YamlParseOptions): Stream.Stream<YamlVisitorEvent>;
|
|
1467
1522
|
}
|
|
1468
1523
|
//#endregion
|
|
1469
|
-
export { CollectionStyle, ScalarChomp, ScalarStyle, Yaml, YamlAlias, YamlComposerErrorCode, YamlDiagnostic, YamlDirective, YamlDocument, YamlEdit, YamlErrorCode, YamlFormat, YamlFormattingOptions, YamlLexErrorCode, YamlMap, YamlModificationError, YamlModifyErrorCode, YamlNode, YamlPair, YamlParseError, YamlParseErrorCode, YamlParseOptions, type YamlPath, YamlRange, type YamlRangeLike, YamlScalar, type YamlSegment, YamlSeq, YamlStringifyError, YamlStringifyErrorCode, YamlStringifyOptions, YamlVisitor, YamlVisitorEvent };
|
|
1524
|
+
export { CollectionStyle, ScalarChomp, ScalarStyle, Yaml, YamlAlias, type YamlBoundCodec, YamlComposerErrorCode, YamlDiagnostic, YamlDirective, YamlDocument, YamlEdit, YamlErrorCode, YamlFormat, YamlFormattingOptions, YamlLexErrorCode, YamlMap, YamlModificationError, YamlModifyErrorCode, YamlNode, YamlPair, YamlParseError, YamlParseErrorCode, YamlParseOptions, type YamlPath, YamlRange, type YamlRangeLike, YamlScalar, type YamlSegment, YamlSeq, YamlStringifyError, YamlStringifyErrorCode, YamlStringifyOptions, YamlVisitor, YamlVisitorEvent };
|
|
1470
1525
|
//# sourceMappingURL=index.d.ts.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@effected/yaml",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Zero-dependency YAML parsing, editing and formatting as Effect schemas.",
|
|
6
6
|
"keywords": [
|
|
@@ -34,12 +34,13 @@
|
|
|
34
34
|
"exports": {
|
|
35
35
|
".": {
|
|
36
36
|
"types": "./index.d.ts",
|
|
37
|
-
"import": "./index.js"
|
|
37
|
+
"import": "./index.js",
|
|
38
|
+
"default": "./index.js"
|
|
38
39
|
},
|
|
39
40
|
"./package.json": "./package.json"
|
|
40
41
|
},
|
|
41
42
|
"peerDependencies": {
|
|
42
|
-
"effect": "4.0.0-beta.
|
|
43
|
+
"effect": "4.0.0-beta.99"
|
|
43
44
|
},
|
|
44
45
|
"engines": {
|
|
45
46
|
"node": ">=24.11.0"
|