xnl-core 0.1.1 → 0.1.3
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/dist/index.cjs +239 -42
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +10 -3
- package/dist/index.d.ts +10 -3
- package/dist/index.js +239 -43
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -65,7 +65,13 @@ interface StringifyOptions {
|
|
|
65
65
|
pretty?: boolean;
|
|
66
66
|
indent?: number | string;
|
|
67
67
|
}
|
|
68
|
-
declare function stringify(value: XnlDocument | XnlNode, options?: StringifyOptions): string;
|
|
68
|
+
declare function stringify$1(value: XnlDocument | XnlNode, options?: StringifyOptions): string;
|
|
69
|
+
|
|
70
|
+
interface LineBlockStringifyOptions {
|
|
71
|
+
indent?: number | string;
|
|
72
|
+
textMarkerFactory?: () => string;
|
|
73
|
+
}
|
|
74
|
+
declare function stringify(value: XnlDocument | XnlNode, options?: LineBlockStringifyOptions): string;
|
|
69
75
|
|
|
70
76
|
type PathItemType = "UniqueName" | "MetadataSelector" | "InstanceProperty" | "MapKey" | "ListIndex";
|
|
71
77
|
interface PathItem {
|
|
@@ -133,7 +139,8 @@ declare const XNL: {
|
|
|
133
139
|
parseMany: typeof parseXnl;
|
|
134
140
|
parseSingle: typeof parseXnlSingleNode;
|
|
135
141
|
parseUnique: typeof parseUniqueChildren;
|
|
136
|
-
stringify: typeof stringify;
|
|
142
|
+
stringify: typeof stringify$1;
|
|
143
|
+
stringifyLineBlock: typeof stringify;
|
|
137
144
|
path: {
|
|
138
145
|
parse: typeof parsePath;
|
|
139
146
|
resolve: typeof resolvePath;
|
|
@@ -151,4 +158,4 @@ declare const XNL: {
|
|
|
151
158
|
};
|
|
152
159
|
};
|
|
153
160
|
|
|
154
|
-
export { type AttributeMap, type CommentNode, type DataElementNode, type ElementNode, type ElementNodeKind, type ExtendBody, GetWordFullName, MakeWord, type MetadataIdMode, type MutationType, type ParseWarning, type PathItem, type PathItemType, type SingleNodeResult, type TextElementNode, type UniqueChildrenResult, type ValueLiteral, XNL, type XnlDocument, type XnlErrorCode, type XnlMutation, type XnlMutationOptions, type XnlNode, XnlParseError, type XnlPath, XnlPathError, type XnlWord, applyMutations, batchLoad, deleteAtPath, diffNodes, isWord, loadFromString, resolveNode as loadNode, parsePath, parseUniqueChildren, parseXnl, parseXnlSingleNode, resolvePath, setPathValue, wordToString };
|
|
161
|
+
export { type AttributeMap, type CommentNode, type DataElementNode, type ElementNode, type ElementNodeKind, type ExtendBody, GetWordFullName, MakeWord, type MetadataIdMode, type MutationType, type ParseWarning, type PathItem, type PathItemType, type SingleNodeResult, type TextElementNode, type UniqueChildrenResult, type ValueLiteral, XNL, type XnlDocument, type XnlErrorCode, type XnlMutation, type XnlMutationOptions, type XnlNode, XnlParseError, type XnlPath, XnlPathError, type XnlWord, applyMutations, batchLoad, deleteAtPath, diffNodes, isWord, loadFromString, resolveNode as loadNode, parsePath, parseUniqueChildren, parseXnl, parseXnlSingleNode, resolvePath, setPathValue, stringify as stringifyLineBlock, wordToString };
|
package/dist/index.d.ts
CHANGED
|
@@ -65,7 +65,13 @@ interface StringifyOptions {
|
|
|
65
65
|
pretty?: boolean;
|
|
66
66
|
indent?: number | string;
|
|
67
67
|
}
|
|
68
|
-
declare function stringify(value: XnlDocument | XnlNode, options?: StringifyOptions): string;
|
|
68
|
+
declare function stringify$1(value: XnlDocument | XnlNode, options?: StringifyOptions): string;
|
|
69
|
+
|
|
70
|
+
interface LineBlockStringifyOptions {
|
|
71
|
+
indent?: number | string;
|
|
72
|
+
textMarkerFactory?: () => string;
|
|
73
|
+
}
|
|
74
|
+
declare function stringify(value: XnlDocument | XnlNode, options?: LineBlockStringifyOptions): string;
|
|
69
75
|
|
|
70
76
|
type PathItemType = "UniqueName" | "MetadataSelector" | "InstanceProperty" | "MapKey" | "ListIndex";
|
|
71
77
|
interface PathItem {
|
|
@@ -133,7 +139,8 @@ declare const XNL: {
|
|
|
133
139
|
parseMany: typeof parseXnl;
|
|
134
140
|
parseSingle: typeof parseXnlSingleNode;
|
|
135
141
|
parseUnique: typeof parseUniqueChildren;
|
|
136
|
-
stringify: typeof stringify;
|
|
142
|
+
stringify: typeof stringify$1;
|
|
143
|
+
stringifyLineBlock: typeof stringify;
|
|
137
144
|
path: {
|
|
138
145
|
parse: typeof parsePath;
|
|
139
146
|
resolve: typeof resolvePath;
|
|
@@ -151,4 +158,4 @@ declare const XNL: {
|
|
|
151
158
|
};
|
|
152
159
|
};
|
|
153
160
|
|
|
154
|
-
export { type AttributeMap, type CommentNode, type DataElementNode, type ElementNode, type ElementNodeKind, type ExtendBody, GetWordFullName, MakeWord, type MetadataIdMode, type MutationType, type ParseWarning, type PathItem, type PathItemType, type SingleNodeResult, type TextElementNode, type UniqueChildrenResult, type ValueLiteral, XNL, type XnlDocument, type XnlErrorCode, type XnlMutation, type XnlMutationOptions, type XnlNode, XnlParseError, type XnlPath, XnlPathError, type XnlWord, applyMutations, batchLoad, deleteAtPath, diffNodes, isWord, loadFromString, resolveNode as loadNode, parsePath, parseUniqueChildren, parseXnl, parseXnlSingleNode, resolvePath, setPathValue, wordToString };
|
|
161
|
+
export { type AttributeMap, type CommentNode, type DataElementNode, type ElementNode, type ElementNodeKind, type ExtendBody, GetWordFullName, MakeWord, type MetadataIdMode, type MutationType, type ParseWarning, type PathItem, type PathItemType, type SingleNodeResult, type TextElementNode, type UniqueChildrenResult, type ValueLiteral, XNL, type XnlDocument, type XnlErrorCode, type XnlMutation, type XnlMutationOptions, type XnlNode, XnlParseError, type XnlPath, XnlPathError, type XnlWord, applyMutations, batchLoad, deleteAtPath, diffNodes, isWord, loadFromString, resolveNode as loadNode, parsePath, parseUniqueChildren, parseXnl, parseXnlSingleNode, resolvePath, setPathValue, stringify as stringifyLineBlock, wordToString };
|
package/dist/index.js
CHANGED
|
@@ -373,8 +373,15 @@ function parseWordLiteral(state) {
|
|
|
373
373
|
return { kind: "Word", namespace: parts, name };
|
|
374
374
|
}
|
|
375
375
|
function readOptionalMarker(state) {
|
|
376
|
-
if (!
|
|
377
|
-
return
|
|
376
|
+
if (!isMarkerStart(peek(state))) return void 0;
|
|
377
|
+
return readMarker(state);
|
|
378
|
+
}
|
|
379
|
+
function readMarker(state) {
|
|
380
|
+
const start = state.pos;
|
|
381
|
+
while (!eof(state) && isIdentifierChar(state.input[state.pos])) {
|
|
382
|
+
state.pos++;
|
|
383
|
+
}
|
|
384
|
+
return state.input.slice(start, state.pos);
|
|
378
385
|
}
|
|
379
386
|
function readKey(state, message) {
|
|
380
387
|
const ch = peek(state);
|
|
@@ -489,6 +496,9 @@ function isIdentifierChar(ch) {
|
|
|
489
496
|
if (!ch) return false;
|
|
490
497
|
return /[A-Za-z0-9_-]/.test(ch);
|
|
491
498
|
}
|
|
499
|
+
function isMarkerStart(ch) {
|
|
500
|
+
return isIdentifierStart(ch) || isDigit(ch);
|
|
501
|
+
}
|
|
492
502
|
function isWhitespace(ch) {
|
|
493
503
|
return ch === " " || ch === " " || ch === "\n" || ch === "\r";
|
|
494
504
|
}
|
|
@@ -533,24 +543,24 @@ function isDocument(value) {
|
|
|
533
543
|
}
|
|
534
544
|
function serializeNode(node, state) {
|
|
535
545
|
if (isComment(node)) {
|
|
536
|
-
const
|
|
537
|
-
return `${
|
|
546
|
+
const pad2 = state.pretty ? state.indent.repeat(state.depth) : "";
|
|
547
|
+
return `${pad2}<!-- ${node.value} -->`;
|
|
538
548
|
}
|
|
539
549
|
if (isElement(node)) {
|
|
540
|
-
const
|
|
550
|
+
const pad2 = state.pretty ? state.indent.repeat(state.depth) : "";
|
|
541
551
|
if (node.kind === "TextElement") {
|
|
542
552
|
const metaStr2 = serializeInlineAttributes(node.metadata, state);
|
|
543
553
|
const attrStr = node.attributes ? ` ${serializeAttributeBlock(node.attributes, state)}` : "";
|
|
544
554
|
const idPart2 = node.id ? ` #${formatWord(node.id)}` : "";
|
|
545
555
|
const marker = node.textMarker ?? "";
|
|
546
|
-
return `${
|
|
556
|
+
return `${pad2}<${node.tag}${idPart2}${metaStr2}${attrStr} ?${marker}>${node.text ?? ""}</?${marker}>`;
|
|
547
557
|
}
|
|
548
558
|
const metaStr = serializeInlineAttributes(node.metadata, state);
|
|
549
559
|
const attrPart = node.attributes ? ` ${serializeAttributeBlock(node.attributes, state)}` : "";
|
|
550
560
|
const bodyPart = node.body ? ` ${serializeArrayBlock(node.body, state)}` : "";
|
|
551
561
|
const extendPart = node.extend ? ` ${serializeExtendBlock(node.extend, state)}` : "";
|
|
552
562
|
const idPart = node.id ? ` #${formatWord(node.id)}` : "";
|
|
553
|
-
return `${
|
|
563
|
+
return `${pad2}<${node.tag}${idPart}${metaStr}${attrPart}${bodyPart}${extendPart}>`;
|
|
554
564
|
}
|
|
555
565
|
if (Array.isArray(node)) {
|
|
556
566
|
return serializeArrayLiteral(node, state);
|
|
@@ -573,9 +583,9 @@ function serializeAttributeBlock(attrs, state) {
|
|
|
573
583
|
return `{ ${entries} }`;
|
|
574
584
|
}
|
|
575
585
|
const nextDepth = state.depth + 1;
|
|
576
|
-
const
|
|
586
|
+
const pad2 = state.indent.repeat(nextDepth);
|
|
577
587
|
const lines = Object.entries(attrs).map(
|
|
578
|
-
([k, v]) => `${
|
|
588
|
+
([k, v]) => `${pad2}${serializeKey(k)} = ${serializeValueNode(v, { ...state, depth: nextDepth })}`
|
|
579
589
|
);
|
|
580
590
|
const closingPad = state.indent.repeat(state.depth);
|
|
581
591
|
return `{
|
|
@@ -588,8 +598,8 @@ function serializeArrayBlock(items, state) {
|
|
|
588
598
|
return `[ ${serialized} ]`;
|
|
589
599
|
}
|
|
590
600
|
const nextDepth = state.depth + 1;
|
|
591
|
-
const
|
|
592
|
-
const lines = items.map((item) => `${
|
|
601
|
+
const pad2 = state.indent.repeat(nextDepth);
|
|
602
|
+
const lines = items.map((item) => `${pad2}${serializeValueNode(item, { ...state, depth: nextDepth })}`);
|
|
593
603
|
const closingPad = state.indent.repeat(state.depth);
|
|
594
604
|
return `[
|
|
595
605
|
${lines.join("\n")}
|
|
@@ -601,8 +611,8 @@ function serializeExtendBlock(extend, state) {
|
|
|
601
611
|
return `( ${children} )`;
|
|
602
612
|
}
|
|
603
613
|
const nextDepth = state.depth + 1;
|
|
604
|
-
const
|
|
605
|
-
const childStrings = extend.order.map((name) => `${
|
|
614
|
+
const pad2 = state.indent.repeat(nextDepth);
|
|
615
|
+
const childStrings = extend.order.map((name) => `${pad2}${serializeNode(extend.children[name], { ...state, depth: nextDepth })}`);
|
|
606
616
|
const closingPad = state.indent.repeat(state.depth);
|
|
607
617
|
return `(
|
|
608
618
|
${childStrings.join("\n")}
|
|
@@ -626,9 +636,9 @@ function serializeObjectLiteral(obj, state) {
|
|
|
626
636
|
return `{ ${Object.entries(obj).map(([k, v]) => `${serializeKey(k)} = ${serializeValueNode(v, state)}`).join(" ")} }`;
|
|
627
637
|
}
|
|
628
638
|
const nextDepth = state.depth + 1;
|
|
629
|
-
const
|
|
639
|
+
const pad2 = state.indent.repeat(nextDepth);
|
|
630
640
|
const lines = Object.entries(obj).map(
|
|
631
|
-
([k, v]) => `${
|
|
641
|
+
([k, v]) => `${pad2}${serializeKey(k)} = ${serializeValueNode(v, { ...state, depth: nextDepth })}`
|
|
632
642
|
);
|
|
633
643
|
const closingPad = state.indent.repeat(state.depth);
|
|
634
644
|
return `{
|
|
@@ -640,8 +650,8 @@ function serializeArrayLiteral(arr, state) {
|
|
|
640
650
|
return `[${arr.map((v) => serializeValueNode(v, state)).join(" ")}]`;
|
|
641
651
|
}
|
|
642
652
|
const nextDepth = state.depth + 1;
|
|
643
|
-
const
|
|
644
|
-
const lines = arr.map((v) => `${
|
|
653
|
+
const pad2 = state.indent.repeat(nextDepth);
|
|
654
|
+
const lines = arr.map((v) => `${pad2}${serializeValueNode(v, { ...state, depth: nextDepth })}`);
|
|
645
655
|
const closingPad = state.indent.repeat(state.depth);
|
|
646
656
|
return `[
|
|
647
657
|
${lines.join("\n")}
|
|
@@ -667,6 +677,191 @@ function formatWord(word) {
|
|
|
667
677
|
return wordToString(word) ?? "";
|
|
668
678
|
}
|
|
669
679
|
|
|
680
|
+
// src/lineBlockFormatter.ts
|
|
681
|
+
var ULID_ENCODING = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
|
|
682
|
+
var lastUlidTime = -1;
|
|
683
|
+
var lastUlidRandom = [];
|
|
684
|
+
function stringify2(value, options = {}) {
|
|
685
|
+
const state = {
|
|
686
|
+
indent: typeof options.indent === "string" ? options.indent : " ".repeat(options.indent ?? 2),
|
|
687
|
+
depth: 0,
|
|
688
|
+
textMarkerFactory: options.textMarkerFactory ?? makeUlid
|
|
689
|
+
};
|
|
690
|
+
if (isDocument2(value)) {
|
|
691
|
+
return value.nodes.map((node) => serializeNode2(node, state)).join("\n");
|
|
692
|
+
}
|
|
693
|
+
return serializeNode2(value, state);
|
|
694
|
+
}
|
|
695
|
+
function serializeNode2(node, state) {
|
|
696
|
+
if (isComment2(node)) return `${pad(state)}<!-- ${node.value} -->`;
|
|
697
|
+
if (isElement2(node)) return serializeElement(node, state);
|
|
698
|
+
return `${pad(state)}${serializeInlineValue(node, state)}`;
|
|
699
|
+
}
|
|
700
|
+
function serializeElement(node, state) {
|
|
701
|
+
if (node.kind === "TextElement") return serializeTextElement(node, state);
|
|
702
|
+
return serializeDataElement(node, state);
|
|
703
|
+
}
|
|
704
|
+
function serializeTextElement(node, state) {
|
|
705
|
+
const marker = node.textMarker ?? state.textMarkerFactory();
|
|
706
|
+
const open = [
|
|
707
|
+
`<${node.tag}`,
|
|
708
|
+
serializeId(node.id),
|
|
709
|
+
serializeMetadata(node.metadata, state),
|
|
710
|
+
node.attributes ? ` ${serializeAttributeBlock2(node.attributes, state)}` : "",
|
|
711
|
+
` ?${marker}>`
|
|
712
|
+
].join("");
|
|
713
|
+
const text = node.text ?? "";
|
|
714
|
+
const textLines = text.length > 0 ? text.split(/\r?\n/).map((line) => `${state.indent.repeat(state.depth + 1)}${line}`) : [];
|
|
715
|
+
const close = `${pad(state)}</?${marker}>`;
|
|
716
|
+
return [`${pad(state)}${open}`, ...textLines, close].join("\n");
|
|
717
|
+
}
|
|
718
|
+
function serializeDataElement(node, state) {
|
|
719
|
+
const open = [
|
|
720
|
+
`<${node.tag}`,
|
|
721
|
+
serializeId(node.id),
|
|
722
|
+
serializeMetadata(node.metadata, state),
|
|
723
|
+
node.attributes ? ` ${serializeAttributeBlock2(node.attributes, state)}` : ""
|
|
724
|
+
].join("");
|
|
725
|
+
const sections = [];
|
|
726
|
+
if (node.body) sections.push(serializeArrayBlock2(node.body, state));
|
|
727
|
+
if (node.extend) sections.push(serializeExtendBlock2(node.extend, state));
|
|
728
|
+
if (sections.length === 0) return `${pad(state)}${open}>`;
|
|
729
|
+
if (sections.length === 1) return `${pad(state)}${open} ${sections[0]}>`;
|
|
730
|
+
return `${pad(state)}${open} ${sections.join(" ")}>`;
|
|
731
|
+
}
|
|
732
|
+
function serializeArrayBlock2(items, state) {
|
|
733
|
+
if (items.length === 0) return "[]";
|
|
734
|
+
const nextState = { ...state, depth: state.depth + 1 };
|
|
735
|
+
const lines = items.map((item) => serializeNode2(item, nextState));
|
|
736
|
+
return `[
|
|
737
|
+
${lines.join("\n")}
|
|
738
|
+
${pad(state)}]`;
|
|
739
|
+
}
|
|
740
|
+
function serializeExtendBlock2(extend, state) {
|
|
741
|
+
if (extend.order.length === 0) return "()";
|
|
742
|
+
const nextState = { ...state, depth: state.depth + 1 };
|
|
743
|
+
const lines = extend.order.map((name) => serializeElement(extend.children[name], nextState));
|
|
744
|
+
return `(
|
|
745
|
+
${lines.join("\n")}
|
|
746
|
+
${pad(state)})`;
|
|
747
|
+
}
|
|
748
|
+
function serializeMetadata(attrs, state) {
|
|
749
|
+
const entries = Object.entries(attrs);
|
|
750
|
+
if (entries.length === 0) return "";
|
|
751
|
+
return " " + entries.map(([key, value]) => `${serializeKey2(key)}=${serializeInlineValue(value, state)}`).join(" ");
|
|
752
|
+
}
|
|
753
|
+
function serializeAttributeBlock2(attrs, state) {
|
|
754
|
+
const entries = Object.entries(attrs).map(([key, value]) => `${serializeKey2(key)} = ${serializeInlineValue(value, state)}`).join(" ");
|
|
755
|
+
return `{ ${entries} }`;
|
|
756
|
+
}
|
|
757
|
+
function serializeInlineValue(value, state) {
|
|
758
|
+
if (isComment2(value)) return `<!-- ${value.value} -->`;
|
|
759
|
+
if (isElement2(value)) return serializeInlineElement(value, state);
|
|
760
|
+
if (isWord(value)) return formatWord2(value);
|
|
761
|
+
if (Array.isArray(value)) return serializeInlineArray(value, state);
|
|
762
|
+
if (isPlainObject2(value)) return serializeInlineObject(value, state);
|
|
763
|
+
return serializePrimitive2(value);
|
|
764
|
+
}
|
|
765
|
+
function serializeInlineElement(node, state) {
|
|
766
|
+
const marker = node.kind === "TextElement" ? node.textMarker ?? state.textMarkerFactory() : void 0;
|
|
767
|
+
const open = [
|
|
768
|
+
`<${node.tag}`,
|
|
769
|
+
serializeId(node.id),
|
|
770
|
+
serializeMetadata(node.metadata, state),
|
|
771
|
+
node.attributes ? ` ${serializeAttributeBlock2(node.attributes, state)}` : ""
|
|
772
|
+
].join("");
|
|
773
|
+
if (node.kind === "TextElement") return `${open} ?${marker}>${node.text ?? ""}</?${marker}>`;
|
|
774
|
+
const sections = [];
|
|
775
|
+
if (node.body) sections.push(serializeInlineArrayBlock(node.body, state));
|
|
776
|
+
if (node.extend) sections.push(serializeInlineExtendBlock(node.extend, state));
|
|
777
|
+
if (sections.length === 0) return `${open}>`;
|
|
778
|
+
return `${open} ${sections.join(" ")}>`;
|
|
779
|
+
}
|
|
780
|
+
function serializeInlineArrayBlock(items, state) {
|
|
781
|
+
return `[ ${items.map((item) => serializeInlineValue(item, state)).join(" ")} ]`;
|
|
782
|
+
}
|
|
783
|
+
function serializeInlineExtendBlock(extend, state) {
|
|
784
|
+
return `( ${extend.order.map((name) => serializeInlineElement(extend.children[name], state)).join(" ")} )`;
|
|
785
|
+
}
|
|
786
|
+
function serializeInlineObject(value, state) {
|
|
787
|
+
const entries = Object.entries(value).map(([key, child]) => `${serializeKey2(key)} = ${serializeInlineValue(child, state)}`).join(" ");
|
|
788
|
+
return `{ ${entries} }`;
|
|
789
|
+
}
|
|
790
|
+
function serializeInlineArray(value, state) {
|
|
791
|
+
return `[${value.map((child) => serializeInlineValue(child, state)).join(" ")}]`;
|
|
792
|
+
}
|
|
793
|
+
function serializePrimitive2(value) {
|
|
794
|
+
if (value === null) return "null";
|
|
795
|
+
if (typeof value === "string") return `"${escapeString2(value)}"`;
|
|
796
|
+
if (typeof value === "boolean") return value ? "true" : "false";
|
|
797
|
+
return String(value);
|
|
798
|
+
}
|
|
799
|
+
function serializeId(id) {
|
|
800
|
+
const value = formatWord2(id);
|
|
801
|
+
return value ? ` #${value}` : "";
|
|
802
|
+
}
|
|
803
|
+
function formatWord2(word) {
|
|
804
|
+
return wordToString(word) ?? "";
|
|
805
|
+
}
|
|
806
|
+
function serializeKey2(key) {
|
|
807
|
+
if (/^[A-Za-z_][A-Za-z0-9_-]*$/.test(key)) return key;
|
|
808
|
+
return `"${escapeString2(key)}"`;
|
|
809
|
+
}
|
|
810
|
+
function escapeString2(value) {
|
|
811
|
+
return value.replace(/\\/g, "\\\\").replace(/"/g, '\\"').replace(/\n/g, "\\n").replace(/\t/g, "\\t").replace(/\r/g, "\\r");
|
|
812
|
+
}
|
|
813
|
+
function pad(state) {
|
|
814
|
+
return state.indent.repeat(state.depth);
|
|
815
|
+
}
|
|
816
|
+
function isDocument2(value) {
|
|
817
|
+
return value && Array.isArray(value.nodes);
|
|
818
|
+
}
|
|
819
|
+
function isElement2(value) {
|
|
820
|
+
return typeof value === "object" && value !== null && (value.kind === "DataElement" || value.kind === "TextElement");
|
|
821
|
+
}
|
|
822
|
+
function isComment2(value) {
|
|
823
|
+
return typeof value === "object" && value !== null && value.kind === "Comment";
|
|
824
|
+
}
|
|
825
|
+
function isPlainObject2(value) {
|
|
826
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) return false;
|
|
827
|
+
const kind = value.kind;
|
|
828
|
+
return kind !== "DataElement" && kind !== "TextElement" && kind !== "Comment" && kind !== "Word";
|
|
829
|
+
}
|
|
830
|
+
function makeUlid(now = Date.now()) {
|
|
831
|
+
if (now > lastUlidTime) {
|
|
832
|
+
lastUlidTime = now;
|
|
833
|
+
lastUlidRandom = nextRandom();
|
|
834
|
+
} else {
|
|
835
|
+
lastUlidRandom = incrementRandom(lastUlidRandom);
|
|
836
|
+
}
|
|
837
|
+
return encodeTime(lastUlidTime, 10) + encodeRandom(lastUlidRandom);
|
|
838
|
+
}
|
|
839
|
+
function encodeTime(time, length) {
|
|
840
|
+
let out = "";
|
|
841
|
+
for (let index = length - 1; index >= 0; index--) {
|
|
842
|
+
out = ULID_ENCODING.charAt(time % 32) + out;
|
|
843
|
+
time = Math.floor(time / 32);
|
|
844
|
+
}
|
|
845
|
+
return out;
|
|
846
|
+
}
|
|
847
|
+
function encodeRandom(values) {
|
|
848
|
+
return values.map((value) => ULID_ENCODING.charAt(value)).join("");
|
|
849
|
+
}
|
|
850
|
+
function nextRandom() {
|
|
851
|
+
return Array.from({ length: 16 }, () => Math.random() * 32 | 0);
|
|
852
|
+
}
|
|
853
|
+
function incrementRandom(values) {
|
|
854
|
+
const next = [...values];
|
|
855
|
+
for (let index = next.length - 1; index >= 0; index--) {
|
|
856
|
+
if (next[index] < 31) {
|
|
857
|
+
next[index] += 1;
|
|
858
|
+
return next;
|
|
859
|
+
}
|
|
860
|
+
next[index] = 0;
|
|
861
|
+
}
|
|
862
|
+
return next;
|
|
863
|
+
}
|
|
864
|
+
|
|
670
865
|
// src/path/index.ts
|
|
671
866
|
var XnlPathError = class extends Error {
|
|
672
867
|
};
|
|
@@ -762,7 +957,7 @@ function resolvePath(target, path, options = {}) {
|
|
|
762
957
|
current = current[item.value];
|
|
763
958
|
} else if (current && isTextElement(current)) {
|
|
764
959
|
current = current[item.value];
|
|
765
|
-
} else if (
|
|
960
|
+
} else if (isPlainObject3(current) || isDocument3(current)) {
|
|
766
961
|
current = current[item.value];
|
|
767
962
|
} else {
|
|
768
963
|
if (strict) throw new XnlPathError(`InstanceProperty '${item.value}' not allowed on current node`);
|
|
@@ -780,7 +975,7 @@ function resolvePath(target, path, options = {}) {
|
|
|
780
975
|
current = child;
|
|
781
976
|
continue;
|
|
782
977
|
}
|
|
783
|
-
if (!
|
|
978
|
+
if (!isPlainObject3(current)) {
|
|
784
979
|
if (strict) throw new XnlPathError("MapKey requires a map/object target");
|
|
785
980
|
return void 0;
|
|
786
981
|
}
|
|
@@ -878,7 +1073,7 @@ function deleteAtPath(target, path, options = {}) {
|
|
|
878
1073
|
const { parent, last } = getParentAndLast(target, parsed, { strict, createMissing: false });
|
|
879
1074
|
switch (last.type) {
|
|
880
1075
|
case "InstanceProperty":
|
|
881
|
-
if (
|
|
1076
|
+
if (isPlainObject3(parent) || isDataElement(parent) || isTextElement(parent) || isDocument3(parent)) {
|
|
882
1077
|
if (!(last.value in parent) && strict) {
|
|
883
1078
|
throw new XnlPathError(`InstanceProperty '${last.value}' not found`);
|
|
884
1079
|
}
|
|
@@ -962,8 +1157,8 @@ function getParentAndLast(target, path, opts) {
|
|
|
962
1157
|
current = current[item.value];
|
|
963
1158
|
} else if (current && isTextElement(current)) {
|
|
964
1159
|
current = current[item.value];
|
|
965
|
-
} else if (
|
|
966
|
-
if (current[item.value] === void 0 && createMissing &&
|
|
1160
|
+
} else if (isPlainObject3(current) || isDocument3(current)) {
|
|
1161
|
+
if (current[item.value] === void 0 && createMissing && isPlainObject3(current)) {
|
|
967
1162
|
current[item.value] = {};
|
|
968
1163
|
}
|
|
969
1164
|
current = current[item.value];
|
|
@@ -1075,7 +1270,7 @@ function readWhile(input, start, pred) {
|
|
|
1075
1270
|
return out;
|
|
1076
1271
|
}
|
|
1077
1272
|
function findByUniqueName(target, id) {
|
|
1078
|
-
const roots =
|
|
1273
|
+
const roots = isDocument3(target) ? target.nodes : [target];
|
|
1079
1274
|
for (const root of roots) {
|
|
1080
1275
|
const found = findInNode(root, id);
|
|
1081
1276
|
if (found) return found;
|
|
@@ -1084,7 +1279,7 @@ function findByUniqueName(target, id) {
|
|
|
1084
1279
|
}
|
|
1085
1280
|
function findByMetadataSelector(target, selector) {
|
|
1086
1281
|
const parsed = parseMetadataSelectorValue(selector);
|
|
1087
|
-
const roots =
|
|
1282
|
+
const roots = isDocument3(target) ? target.nodes : [target];
|
|
1088
1283
|
for (const root of roots) {
|
|
1089
1284
|
const found = findInNodeByMeta(root, parsed.key, parsed.value);
|
|
1090
1285
|
if (found) return found;
|
|
@@ -1093,7 +1288,7 @@ function findByMetadataSelector(target, selector) {
|
|
|
1093
1288
|
}
|
|
1094
1289
|
function findAllByMetadataSelector(target, selector) {
|
|
1095
1290
|
const parsed = parseMetadataSelectorValue(selector);
|
|
1096
|
-
const roots =
|
|
1291
|
+
const roots = isDocument3(target) ? target.nodes : [target];
|
|
1097
1292
|
const out = [];
|
|
1098
1293
|
for (const root of roots) {
|
|
1099
1294
|
collectInNodeByMeta(root, parsed.key, parsed.value, out);
|
|
@@ -1129,7 +1324,7 @@ function findInNodeByMeta(node, key, value) {
|
|
|
1129
1324
|
const found = findInNodeByMeta(child, key, value);
|
|
1130
1325
|
if (found) return found;
|
|
1131
1326
|
}
|
|
1132
|
-
} else if (
|
|
1327
|
+
} else if (isPlainObject3(node)) {
|
|
1133
1328
|
for (const k of Object.keys(node)) {
|
|
1134
1329
|
const found = findInNodeByMeta(node[k], key, value);
|
|
1135
1330
|
if (found) return found;
|
|
@@ -1163,7 +1358,7 @@ function collectInNodeByMeta(node, key, value, out) {
|
|
|
1163
1358
|
for (const child of node) {
|
|
1164
1359
|
collectInNodeByMeta(child, key, value, out);
|
|
1165
1360
|
}
|
|
1166
|
-
} else if (
|
|
1361
|
+
} else if (isPlainObject3(node)) {
|
|
1167
1362
|
for (const k of Object.keys(node)) {
|
|
1168
1363
|
collectInNodeByMeta(node[k], key, value, out);
|
|
1169
1364
|
}
|
|
@@ -1210,7 +1405,7 @@ function findInNode(node, id) {
|
|
|
1210
1405
|
const found = findInNode(child, id);
|
|
1211
1406
|
if (found) return found;
|
|
1212
1407
|
}
|
|
1213
|
-
} else if (
|
|
1408
|
+
} else if (isPlainObject3(node)) {
|
|
1214
1409
|
for (const key of Object.keys(node)) {
|
|
1215
1410
|
const found = findInNode(node[key], id);
|
|
1216
1411
|
if (found) return found;
|
|
@@ -1230,14 +1425,14 @@ function isTextElement(node) {
|
|
|
1230
1425
|
function isExtendBody(value) {
|
|
1231
1426
|
return value && typeof value === "object" && Array.isArray(value.order) && value.children;
|
|
1232
1427
|
}
|
|
1233
|
-
function
|
|
1428
|
+
function isDocument3(value) {
|
|
1234
1429
|
return value && typeof value === "object" && Array.isArray(value.nodes);
|
|
1235
1430
|
}
|
|
1236
|
-
function
|
|
1431
|
+
function isPlainObject3(value) {
|
|
1237
1432
|
return value !== null && typeof value === "object" && !Array.isArray(value) && !isElementNode(value) && !isExtendBody(value) && !isWord(value);
|
|
1238
1433
|
}
|
|
1239
1434
|
function ensureMap(value, strict) {
|
|
1240
|
-
if (!
|
|
1435
|
+
if (!isPlainObject3(value)) {
|
|
1241
1436
|
if (strict) throw new XnlPathError("Target is not a map/object");
|
|
1242
1437
|
}
|
|
1243
1438
|
}
|
|
@@ -1276,13 +1471,13 @@ function diffNodes(oldNode, newNode, basePath = [], opts = {}) {
|
|
|
1276
1471
|
if (!sameKind(oldNode, newNode)) {
|
|
1277
1472
|
throw new XnlPathError("Root kinds must match to diff");
|
|
1278
1473
|
}
|
|
1279
|
-
if (isValueLiteral(oldNode) ||
|
|
1474
|
+
if (isValueLiteral(oldNode) || isComment3(oldNode)) {
|
|
1280
1475
|
return oldNode === newNode ? [] : [{ type: "OBJECT_UPDATE", path: pathItems, valueAfter: newNode }];
|
|
1281
1476
|
}
|
|
1282
1477
|
if (Array.isArray(oldNode) && Array.isArray(newNode)) {
|
|
1283
1478
|
return diffArray(oldNode, newNode, pathItems, void 0, void 0, opts);
|
|
1284
1479
|
}
|
|
1285
|
-
if (
|
|
1480
|
+
if (isPlainObject4(oldNode) && isPlainObject4(newNode)) {
|
|
1286
1481
|
return diffMap(oldNode, newNode, pathItems, void 0, void 0, opts);
|
|
1287
1482
|
}
|
|
1288
1483
|
if (isTextElement2(oldNode) && isTextElement2(newNode)) {
|
|
@@ -1574,7 +1769,7 @@ function sameKind(a, b) {
|
|
|
1574
1769
|
if (isDataElement2(a) && isDataElement2(b)) return true;
|
|
1575
1770
|
if (isTextElement2(a) && isTextElement2(b)) return true;
|
|
1576
1771
|
if (Array.isArray(a) && Array.isArray(b)) return true;
|
|
1577
|
-
if (
|
|
1772
|
+
if (isPlainObject4(a) && isPlainObject4(b)) return true;
|
|
1578
1773
|
if (isValueLiteral(a) && isValueLiteral(b)) return true;
|
|
1579
1774
|
return typeof a === typeof b;
|
|
1580
1775
|
}
|
|
@@ -1588,7 +1783,7 @@ function isEqual(a, b) {
|
|
|
1588
1783
|
if (a.length !== b.length) return false;
|
|
1589
1784
|
return a.every((item, idx) => isEqual(item, b[idx]));
|
|
1590
1785
|
}
|
|
1591
|
-
if (
|
|
1786
|
+
if (isPlainObject4(a) && isPlainObject4(b)) {
|
|
1592
1787
|
const keysA = Object.keys(a);
|
|
1593
1788
|
const keysB = Object.keys(b);
|
|
1594
1789
|
if (keysA.length !== keysB.length) return false;
|
|
@@ -1596,7 +1791,7 @@ function isEqual(a, b) {
|
|
|
1596
1791
|
}
|
|
1597
1792
|
return false;
|
|
1598
1793
|
}
|
|
1599
|
-
function
|
|
1794
|
+
function isPlainObject4(value) {
|
|
1600
1795
|
return value !== null && typeof value === "object" && !Array.isArray(value) && !isDataElement2(value) && !isTextElement2(value) && !isWord(value);
|
|
1601
1796
|
}
|
|
1602
1797
|
function isDataElement2(node) {
|
|
@@ -1608,7 +1803,7 @@ function isTextElement2(node) {
|
|
|
1608
1803
|
function isValueLiteral(node) {
|
|
1609
1804
|
return typeof node === "string" || typeof node === "number" || typeof node === "boolean" || node === null || isWord(node);
|
|
1610
1805
|
}
|
|
1611
|
-
function
|
|
1806
|
+
function isComment3(node) {
|
|
1612
1807
|
return node && node.kind === "Comment";
|
|
1613
1808
|
}
|
|
1614
1809
|
var ip = (value) => ({ type: "InstanceProperty", value });
|
|
@@ -1943,7 +2138,7 @@ function cloneNode(node) {
|
|
|
1943
2138
|
if (isWord(node)) {
|
|
1944
2139
|
return cloneWord(node);
|
|
1945
2140
|
}
|
|
1946
|
-
if (
|
|
2141
|
+
if (isPlainObject5(node)) {
|
|
1947
2142
|
const out = {};
|
|
1948
2143
|
for (const key of Object.keys(node)) {
|
|
1949
2144
|
out[key] = cloneNode(node[key]);
|
|
@@ -1969,7 +2164,7 @@ function mergeMaps(ctx, base, override, scope) {
|
|
|
1969
2164
|
continue;
|
|
1970
2165
|
}
|
|
1971
2166
|
const resolvedValue = resolveValue(ctx, value, scope);
|
|
1972
|
-
if (
|
|
2167
|
+
if (isPlainObject5(resolvedValue) && isPlainObject5(result[key])) {
|
|
1973
2168
|
result[key] = mergeMaps(ctx, result[key], resolvedValue, scope);
|
|
1974
2169
|
} else if (Array.isArray(resolvedValue) && Array.isArray(result[key])) {
|
|
1975
2170
|
result[key] = mergeArray(ctx, result[key], resolvedValue, scope);
|
|
@@ -1990,7 +2185,7 @@ function mergeArray(ctx, baseArr, overrideArr, scope) {
|
|
|
1990
2185
|
const resolved = resolveValue(ctx, value, scope);
|
|
1991
2186
|
if (i < result.length) {
|
|
1992
2187
|
const baseVal = result[i];
|
|
1993
|
-
if (
|
|
2188
|
+
if (isPlainObject5(baseVal) && isPlainObject5(resolved)) {
|
|
1994
2189
|
result[i] = mergeMaps(ctx, baseVal, resolved, scope);
|
|
1995
2190
|
} else if (Array.isArray(baseVal) && Array.isArray(resolved)) {
|
|
1996
2191
|
result[i] = mergeArray(ctx, baseVal, resolved, scope);
|
|
@@ -2045,7 +2240,7 @@ function isDataElement3(node) {
|
|
|
2045
2240
|
function isTextElement3(node) {
|
|
2046
2241
|
return node && node.kind === "TextElement";
|
|
2047
2242
|
}
|
|
2048
|
-
function
|
|
2243
|
+
function isPlainObject5(value) {
|
|
2049
2244
|
return value !== null && typeof value === "object" && !Array.isArray(value) && !isDataElement3(value) && !isWord(value);
|
|
2050
2245
|
}
|
|
2051
2246
|
function resolveValue(ctx, value, scope) {
|
|
@@ -2062,7 +2257,7 @@ function resolveValue(ctx, value, scope) {
|
|
|
2062
2257
|
if (Array.isArray(value)) {
|
|
2063
2258
|
return value.map((v) => resolveValue(ctx, v, scope));
|
|
2064
2259
|
}
|
|
2065
|
-
if (
|
|
2260
|
+
if (isPlainObject5(value)) {
|
|
2066
2261
|
const out = {};
|
|
2067
2262
|
for (const key of Object.keys(value)) {
|
|
2068
2263
|
out[key] = resolveValue(ctx, value[key], scope);
|
|
@@ -2134,6 +2329,7 @@ var XNL = {
|
|
|
2134
2329
|
parseSingle: parseXnlSingleNode,
|
|
2135
2330
|
parseUnique: parseUniqueChildren,
|
|
2136
2331
|
stringify,
|
|
2332
|
+
stringifyLineBlock: stringify2,
|
|
2137
2333
|
path: {
|
|
2138
2334
|
parse: parsePath,
|
|
2139
2335
|
resolve: resolvePath,
|
|
@@ -2151,6 +2347,6 @@ var XNL = {
|
|
|
2151
2347
|
}
|
|
2152
2348
|
};
|
|
2153
2349
|
|
|
2154
|
-
export { GetWordFullName, MakeWord, XNL, XnlParseError, XnlPathError, applyMutations, batchLoad, deleteAtPath, diffNodes, isWord, loadFromString, resolveNode as loadNode, parsePath, parseUniqueChildren, parseXnl, parseXnlSingleNode, resolvePath, setPathValue, wordToString };
|
|
2350
|
+
export { GetWordFullName, MakeWord, XNL, XnlParseError, XnlPathError, applyMutations, batchLoad, deleteAtPath, diffNodes, isWord, loadFromString, resolveNode as loadNode, parsePath, parseUniqueChildren, parseXnl, parseXnlSingleNode, resolvePath, setPathValue, stringify2 as stringifyLineBlock, wordToString };
|
|
2155
2351
|
//# sourceMappingURL=index.js.map
|
|
2156
2352
|
//# sourceMappingURL=index.js.map
|