1- import YAML from 'static-js-yaml' ;
1+ import { parse as parseYaml , stringify as stringifyYaml } from 'yaml' ;
2+
3+ const DEFAULT_PARSE_OPTIONS = {
4+ logLevel : 'error' ,
5+ merge : true ,
6+ } ;
27
38function retry ( fn , content , ...args ) {
49 try {
@@ -16,17 +21,73 @@ function retry(fn, content, ...args) {
1621 }
1722}
1823
24+ function toSerializable ( content ) {
25+ return JSON . parse ( JSON . stringify ( content ) ) ;
26+ }
27+
28+ function normalizeParseOptions ( options ) {
29+ return {
30+ ...DEFAULT_PARSE_OPTIONS ,
31+ ...( options || { } ) ,
32+ } ;
33+ }
34+
35+ function normalizeStringifyOptions ( options = { } ) {
36+ const {
37+ forceQuotes,
38+ lineWidth,
39+ noArrayIndent,
40+ noRefs,
41+ quotingType,
42+ sortKeys,
43+ ...rest
44+ } = options || { } ;
45+ const normalized = { ...rest } ;
46+
47+ if ( typeof lineWidth === 'number' ) {
48+ normalized . lineWidth = lineWidth <= 0 ? 0 : lineWidth ;
49+ }
50+ if ( typeof noArrayIndent === 'boolean' ) {
51+ normalized . indentSeq = ! noArrayIndent ;
52+ }
53+ if ( typeof noRefs === 'boolean' ) {
54+ normalized . aliasDuplicateObjects = ! noRefs ;
55+ }
56+ if ( typeof sortKeys !== 'undefined' ) {
57+ normalized . sortMapEntries = sortKeys ;
58+ }
59+ if ( quotingType === "'" ) {
60+ normalized . singleQuote = true ;
61+ } else if ( quotingType === '"' ) {
62+ normalized . singleQuote = false ;
63+ }
64+ if ( forceQuotes ) {
65+ normalized . defaultStringType =
66+ quotingType === "'" ? 'QUOTE_SINGLE' : 'QUOTE_DOUBLE' ;
67+ }
68+
69+ return normalized ;
70+ }
71+
72+ function parse ( content , options ) {
73+ return parseYaml ( content , normalizeParseOptions ( options ) ) ;
74+ }
75+
76+ function stringify ( content , options ) {
77+ return stringifyYaml ( content , normalizeStringifyOptions ( options ) ) ;
78+ }
79+
1980export function safeLoad ( content , ...args ) {
20- return retry ( YAML . safeLoad , JSON . parse ( JSON . stringify ( content ) ) , ...args ) ;
81+ return retry ( parse , toSerializable ( content ) , ...args ) ;
2182}
2283export function load ( content , ...args ) {
23- return retry ( YAML . load , JSON . parse ( JSON . stringify ( content ) ) , ...args ) ;
84+ return retry ( parse , toSerializable ( content ) , ...args ) ;
2485}
2586export function safeDump ( content , ...args ) {
26- return YAML . safeDump ( JSON . parse ( JSON . stringify ( content ) ) , ...args ) ;
87+ return stringify ( toSerializable ( content ) , ...args ) ;
2788}
2889export function dump ( content , ...args ) {
29- return YAML . dump ( JSON . parse ( JSON . stringify ( content ) ) , ...args ) ;
90+ return stringify ( toSerializable ( content ) , ...args ) ;
3091}
3192
3293export default {
0 commit comments