encoding.js 831 B

12345678910111213141516171819
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.strToEncoding = exports.assertEncoding = exports.ENCODING_UTF8 = void 0;
  4. var buffer_1 = require("./internal/buffer");
  5. var errors = require("./internal/errors");
  6. exports.ENCODING_UTF8 = 'utf8';
  7. function assertEncoding(encoding) {
  8. if (encoding && !buffer_1.Buffer.isEncoding(encoding))
  9. throw new errors.TypeError('ERR_INVALID_OPT_VALUE_ENCODING', encoding);
  10. }
  11. exports.assertEncoding = assertEncoding;
  12. function strToEncoding(str, encoding) {
  13. if (!encoding || encoding === exports.ENCODING_UTF8)
  14. return str; // UTF-8
  15. if (encoding === 'buffer')
  16. return new buffer_1.Buffer(str); // `buffer` encoding
  17. return new buffer_1.Buffer(str).toString(encoding); // Custom encoding
  18. }
  19. exports.strToEncoding = strToEncoding;