to-file.js (1145B)
1 'use strict'; 2 3 const typeOf = require('kind-of'); 4 const stringify = require('./stringify'); 5 const utils = require('./utils'); 6 7 /** 8 * Normalize the given value to ensure an object is returned 9 * with the expected properties. 10 */ 11 12 module.exports = function(file) { 13 if (typeOf(file) !== 'object') { 14 file = { content: file }; 15 } 16 17 if (typeOf(file.data) !== 'object') { 18 file.data = {}; 19 } 20 21 // if file was passed as an object, ensure that 22 // "file.content" is set 23 if (file.contents && file.content == null) { 24 file.content = file.contents; 25 } 26 27 // set non-enumerable properties on the file object 28 utils.define(file, 'orig', utils.toBuffer(file.content)); 29 utils.define(file, 'language', file.language || ''); 30 utils.define(file, 'matter', file.matter || ''); 31 utils.define(file, 'stringify', function(data, options) { 32 if (options && options.language) { 33 file.language = options.language; 34 } 35 return stringify(file, data, options); 36 }); 37 38 // strip BOM and ensure that "file.content" is a string 39 file.content = utils.toString(file.content); 40 file.isEmpty = false; 41 file.excerpt = ''; 42 return file; 43 };