assertRecord.js 1.1 KB

1234567891011121314151617181920212223242526272829303132
  1. 'use strict';
  2. // TODO, semver-major: delete this
  3. var $TypeError = require('es-errors/type');
  4. var $SyntaxError = require('es-errors/syntax');
  5. var isMatchRecord = require('./records/match-record');
  6. var isPropertyDescriptor = require('./records/property-descriptor');
  7. var isIteratorRecord = require('./records/iterator-record');
  8. var isPromiseCapabilityRecord = require('./records/promise-capability-record');
  9. var isAsyncGeneratorRequestRecord = require('./records/async-generator-request-record');
  10. var isRegExpRecord = require('./records/regexp-record');
  11. var predicates = {
  12. 'Property Descriptor': isPropertyDescriptor,
  13. 'Match Record': isMatchRecord,
  14. 'Iterator Record': isIteratorRecord,
  15. 'PromiseCapability Record': isPromiseCapabilityRecord,
  16. 'AsyncGeneratorRequest Record': isAsyncGeneratorRequestRecord,
  17. 'RegExp Record': isRegExpRecord
  18. };
  19. module.exports = function assertRecord(Type, recordType, argumentName, value) {
  20. var predicate = predicates[recordType];
  21. if (typeof predicate !== 'function') {
  22. throw new $SyntaxError('unknown record type: ' + recordType);
  23. }
  24. if (!predicate(value)) {
  25. throw new $TypeError(argumentName + ' must be a ' + recordType);
  26. }
  27. };