Compare commits
	
		
			5 Commits
		
	
	
		
			aecfdcec99
			...
			be09bf9dd3
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| be09bf9dd3 | |||
| e7e44e6c92 | |||
| 51d70a2b18 | |||
|  | a6fbbc45a9 | ||
|  | d1473483b2 | 
| @ -1,3 +1,8 @@ | ||||
| Unmaintained Mirror | ||||
| =============== | ||||
| 
 | ||||
| Please go to the real repo at <https://git.coolaj86.com/coolaj86/json2yaml.js> | ||||
| 
 | ||||
| json2yaml | ||||
| === | ||||
| 
 | ||||
|  | ||||
							
								
								
									
										156
									
								
								index.js
									
									
									
									
									
								
							
							
						
						
									
										156
									
								
								index.js
									
									
									
									
									
								
							| @ -1,99 +1,107 @@ | ||||
| (function () { | ||||
|   "use strict"; | ||||
| 
 | ||||
|   var typeOf = require('remedial').typeOf | ||||
|     ; | ||||
|   var typeOf = require('remedial').typeOf, | ||||
|     maxText = 60, | ||||
|     wrap = require('wordwrap')(maxText); | ||||
| 
 | ||||
|   function stringify(data) { | ||||
|     var handlers | ||||
|       , indentLevel = '' | ||||
|       ; | ||||
|     var handlers, indentLevel = ''; | ||||
| 
 | ||||
|     handlers = { | ||||
|         "undefined": function () { | ||||
|           // objects will not have `undefined` converted to `null`
 | ||||
|           // as this may have unintended consequences
 | ||||
|           // For arrays, however, this behavior seems appropriate
 | ||||
|           return 'null'; | ||||
|         } | ||||
|       , "null": function () { | ||||
|           return 'null'; | ||||
|         } | ||||
|       , "number": function (x) { | ||||
|           return x; | ||||
|         } | ||||
|       , "boolean": function (x) { | ||||
|           return x ? 'true' : 'false'; | ||||
|         } | ||||
|       , "string": function (x) { | ||||
|           // to avoid the string "true" being confused with the
 | ||||
|           // the literal `true`, we always wrap strings in quotes
 | ||||
|       "undefined": function () { | ||||
|         // objects will not have `undefined` converted to `null`
 | ||||
|         // as this may have unintended consequences
 | ||||
|         // For arrays, however, this behavior seems appropriate
 | ||||
|         return 'null'; | ||||
|       }, | ||||
|       "null": function () { | ||||
|         return 'null'; | ||||
|       }, | ||||
|       "number": function (x) { | ||||
|         return x; | ||||
|       }, | ||||
|       "boolean": function (x) { | ||||
|         return x ? 'true' : 'false'; | ||||
|       }, | ||||
|       "string": function (x) { | ||||
|         var output = '|'; | ||||
|         if (x.length <= maxText && x.indexOf('\n') === -1) { | ||||
|           return JSON.stringify(x); | ||||
|         } | ||||
|       , "array": function (x) { | ||||
|           var output = '' | ||||
|             ; | ||||
|         var text = wrap(x).split(/\\n|\n/); | ||||
|         indentLevel = indentLevel.replace(/$/, '  '); | ||||
|         text.forEach(function (y) { | ||||
|           output += '\n' + indentLevel + y; | ||||
| 
 | ||||
|           if (0 === x.length) { | ||||
|             output += '[]'; | ||||
|             return output; | ||||
|           } | ||||
|         }); | ||||
|         indentLevel = indentLevel.replace(/  /, ''); | ||||
| 
 | ||||
|           indentLevel = indentLevel.replace(/$/, '  '); | ||||
|           x.forEach(function (y) { | ||||
|             // TODO how should `undefined` be handled?
 | ||||
|             var handler = handlers[typeOf(y)] | ||||
|               ; | ||||
|         return output; | ||||
|       }, | ||||
|       "date": function (x) { | ||||
|         return x.toJSON(); | ||||
|       }, | ||||
|       "array": function (x) { | ||||
|         var output = ''; | ||||
| 
 | ||||
|             if (!handler) { | ||||
|               throw new Error('what the crap: ' + typeOf(y)); | ||||
|             } | ||||
| 
 | ||||
|             output += '\n' + indentLevel + '- ' + handler(y); | ||||
|               | ||||
|           }); | ||||
|           indentLevel = indentLevel.replace(/  /, ''); | ||||
|            | ||||
|         if (0 === x.length) { | ||||
|           output += '[]'; | ||||
|           return output; | ||||
|         } | ||||
|       , "object": function (x) { | ||||
|           var output = '' | ||||
|             ; | ||||
| 
 | ||||
|           if (0 === Object.keys(x).length) { | ||||
|             output += '{}'; | ||||
|             return output; | ||||
|         indentLevel = indentLevel.replace(/$/, '  '); | ||||
|         x.forEach(function (y) { | ||||
|           // TODO how should `undefined` be handled?
 | ||||
|           var handler = handlers[typeOf(y)]; | ||||
| 
 | ||||
|           if (!handler) { | ||||
|             throw new Error('what the crap: ' + typeOf(y)); | ||||
|           } | ||||
| 
 | ||||
|           indentLevel = indentLevel.replace(/$/, '  '); | ||||
|           Object.keys(x).forEach(function (k) { | ||||
|             var val = x[k] | ||||
|               , handler = handlers[typeOf(val)] | ||||
|               ; | ||||
|           output += '\n' + indentLevel + '- ' + handler(y); | ||||
| 
 | ||||
|             if ('undefined' === typeof val) { | ||||
|               // the user should do
 | ||||
|               // delete obj.key
 | ||||
|               // and not
 | ||||
|               // obj.key = undefined
 | ||||
|               // but we'll error on the side of caution
 | ||||
|               return; | ||||
|             } | ||||
|         }); | ||||
|         indentLevel = indentLevel.replace(/  /, ''); | ||||
| 
 | ||||
|             if (!handler) { | ||||
|               throw new Error('what the crap: ' + typeOf(val)); | ||||
|             } | ||||
| 
 | ||||
|             output += '\n' + indentLevel + k + ': ' + handler(val); | ||||
|           }); | ||||
|           indentLevel = indentLevel.replace(/  /, ''); | ||||
|         return output; | ||||
|       }, | ||||
|       "object": function (x) { | ||||
|         var output = ''; | ||||
| 
 | ||||
|         if (0 === Object.keys(x).length) { | ||||
|           output += '{}'; | ||||
|           return output; | ||||
|         } | ||||
|       , "function": function () { | ||||
|           // TODO this should throw or otherwise be ignored
 | ||||
|           return '[object Function]'; | ||||
|         } | ||||
| 
 | ||||
|         indentLevel = indentLevel.replace(/$/, '  '); | ||||
|         Object.keys(x).forEach(function (k) { | ||||
|           var val = x[k], | ||||
|             handler = handlers[typeOf(val)]; | ||||
| 
 | ||||
|           if ('undefined' === typeof val) { | ||||
|             // the user should do
 | ||||
|             // delete obj.key
 | ||||
|             // and not
 | ||||
|             // obj.key = undefined
 | ||||
|             // but we'll error on the side of caution
 | ||||
|             return; | ||||
|           } | ||||
| 
 | ||||
|           if (!handler) { | ||||
|             throw new Error('what the crap: ' + typeOf(val)); | ||||
|           } | ||||
| 
 | ||||
|           output += '\n' + indentLevel + k + ': ' + handler(val); | ||||
|         }); | ||||
|         indentLevel = indentLevel.replace(/  /, ''); | ||||
| 
 | ||||
|         return output; | ||||
|       }, | ||||
|       "function": function () { | ||||
|         // TODO this should throw or otherwise be ignored
 | ||||
|         return '[object Function]'; | ||||
|       } | ||||
|     }; | ||||
| 
 | ||||
|     return '---' + handlers[typeOf(data)](data) + '\n'; | ||||
|  | ||||
| @ -9,7 +9,7 @@ | ||||
|     "cli", | ||||
|     "util" | ||||
|   ], | ||||
|   "version": "1.1.0", | ||||
|   "version": "1.2.0", | ||||
|   "license": "Apache-2.0", | ||||
|   "main": "index.js", | ||||
|   "repository": { | ||||
| @ -27,7 +27,8 @@ | ||||
|   }, | ||||
|   "test": "echo 'error no test'; exit 1", | ||||
|   "dependencies": { | ||||
|     "remedial": "1.x" | ||||
|     "remedial": "1.x", | ||||
|     "wordwrap": "^1.0.0" | ||||
|   }, | ||||
|   "devDependencies": {}, | ||||
|   "preferGlobal": true | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user