assertRecord.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var $TypeError = GetIntrinsic('%TypeError%');
  4. var $SyntaxError = GetIntrinsic('%SyntaxError%');
  5. var has = require('has');
  6. var isMatchRecord = require('./isMatchRecord');
  7. var predicates = {
  8. // https://262.ecma-international.org/6.0/#sec-property-descriptor-specification-type
  9. 'Property Descriptor': function isPropertyDescriptor(Desc) {
  10. var allowed = {
  11. '[[Configurable]]': true,
  12. '[[Enumerable]]': true,
  13. '[[Get]]': true,
  14. '[[Set]]': true,
  15. '[[Value]]': true,
  16. '[[Writable]]': true
  17. };
  18. for (var key in Desc) { // eslint-disable-line
  19. if (has(Desc, key) && !allowed[key]) {
  20. return false;
  21. }
  22. }
  23. var isData = has(Desc, '[[Value]]');
  24. var IsAccessor = has(Desc, '[[Get]]') || has(Desc, '[[Set]]');
  25. if (isData && IsAccessor) {
  26. throw new $TypeError('Property Descriptors may not be both accessor and data descriptors');
  27. }
  28. return true;
  29. },
  30. // https://262.ecma-international.org/13.0/#sec-match-records
  31. 'Match Record': isMatchRecord
  32. };
  33. module.exports = function assertRecord(Type, recordType, argumentName, value) {
  34. var predicate = predicates[recordType];
  35. if (typeof predicate !== 'function') {
  36. throw new $SyntaxError('unknown record type: ' + recordType);
  37. }
  38. if (Type(value) !== 'Object' || !predicate(value)) {
  39. throw new $TypeError(argumentName + ' must be a ' + recordType);
  40. }
  41. };