esnext.array.is-template-object.js 959 B

12345678910111213141516171819202122232425262728
  1. var $ = require('../internals/export');
  2. var isArray = require('../internals/is-array');
  3. // eslint-disable-next-line es-x/no-object-isfrozen -- safe
  4. var isFrozen = Object.isFrozen;
  5. var isFrozenStringArray = function (array, allowUndefined) {
  6. if (!isFrozen || !isArray(array) || !isFrozen(array)) return false;
  7. var index = 0;
  8. var length = array.length;
  9. var element;
  10. while (index < length) {
  11. element = array[index++];
  12. if (!(typeof element == 'string' || (allowUndefined && typeof element == 'undefined'))) {
  13. return false;
  14. }
  15. } return length !== 0;
  16. };
  17. // `Array.isTemplateObject` method
  18. // https://github.com/tc39/proposal-array-is-template-object
  19. $({ target: 'Array', stat: true, sham: true, forced: true }, {
  20. isTemplateObject: function isTemplateObject(value) {
  21. if (!isFrozenStringArray(value, true)) return false;
  22. var raw = value.raw;
  23. return raw.length === value.length && isFrozenStringArray(raw, false);
  24. }
  25. });