to-file.js 1.1 KB

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