MCPcopy Create free account
hub / github.com/denoland/std / stringifyNode

Method stringifyNode

yaml/_dumper_state.ts:748–816  ·  view source on GitHub ↗
(value: unknown, { level, block, compact, isKey }: {
    level: number;
    block: boolean;
    compact: boolean;
    isKey: boolean;
  })

Source from the content-addressed store, hash-verified

746 // Serializes `object` and writes it to global `result`.
747 // Returns true on success, or false on invalid object.
748 stringifyNode(value: unknown, { level, block, compact, isKey }: {
749 level: number;
750 block: boolean;
751 compact: boolean;
752 isKey: boolean;
753 }): string | null {
754 const result = this.detectType(value);
755 const tag = result.tag;
756 value = result.value;
757
758 if (block) {
759 block = this.flowLevel < 0 || this.flowLevel > level;
760 }
761
762 if (typeof value === "string" || value instanceof String) {
763 value = value instanceof String ? value.valueOf() : value;
764 if (tag !== "?") {
765 value = this.stringifyScalar(value as string, { level, isKey });
766 }
767 return stringifyValue(value, tag);
768 }
769
770 if (isObject(value)) {
771 const duplicateIndex = this.duplicates.indexOf(value);
772 const duplicate = duplicateIndex !== -1;
773
774 if (duplicate) {
775 if (this.usedDuplicates.has(value)) return `*ref_${duplicateIndex}`;
776 this.usedDuplicates.add(value);
777 }
778
779 if (
780 (tag !== null && tag !== "?") ||
781 duplicate ||
782 (this.indent !== 2 && level > 0)
783 ) {
784 compact = false;
785 }
786
787 if (Array.isArray(value)) {
788 const arrayLevel = !this.arrayIndent && level > 0 ? level - 1 : level;
789 if (block && value.length !== 0) {
790 value = this.stringifyBlockSequence(value, {
791 level: arrayLevel,
792 compact,
793 });
794 if (duplicate) value = `&ref_${duplicateIndex}${value}`;
795 return stringifyValue(value, tag);
796 }
797
798 value = this.stringifyFlowSequence(value, { level: arrayLevel });
799 if (duplicate) value = `&ref_${duplicateIndex} ${value}`;
800 return stringifyValue(value, tag);
801 }
802
803 if (block && Object.keys(value).length !== 0) {
804 value = this.stringifyBlockMapping(value, { tag, level, compact });
805 if (duplicate) value = `&ref_${duplicateIndex}${value}`;

Callers 5

stringifyFlowSequenceMethod · 0.95
stringifyFlowMappingMethod · 0.95
stringifyBlockMappingMethod · 0.95
stringifyMethod · 0.95

Calls 11

detectTypeMethod · 0.95
stringifyScalarMethod · 0.95
stringifyFlowSequenceMethod · 0.95
stringifyBlockMappingMethod · 0.95
stringifyFlowMappingMethod · 0.95
isObjectFunction · 0.90
stringifyValueFunction · 0.85
keysMethod · 0.80
hasMethod · 0.65
addMethod · 0.45

Tested by

no test coverage detected