frightfulactions

polyfills

Apr 20th, 2026
8
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. (function(arr){
  2. arr.forEach(function(proto){
  3. if (!proto.hasOwnProperty('append')) {
  4. Object.defineProperty(proto, 'append', {
  5. configurable: true,
  6. enumerable: true,
  7. writable: true,
  8. value: function append() {
  9. var argArr = Array.prototype.slice.call(arguments),
  10. docFrag = document.createDocumentFragment();
  11. argArr.forEach(function(argItem){
  12. docFrag.appendChild(
  13. argItem instanceof Node ? argItem : document.createTextNode(String(argItem))
  14. );
  15. });
  16. this.appendChild(docFrag);
  17. }
  18. });
  19. }
  20. });
  21. })([Element.prototype, Document.prototype, DocumentFragment.prototype]);
  22.  
  23. (function(arr) {
  24. arr.forEach(function(proto) {
  25. if (!proto.hasOwnProperty('before')) {
  26. Object.defineProperty(proto, 'before', {
  27. configurable: true,
  28. enumerable: true,
  29. writable: true,
  30. value: function before() {
  31. var argArr = Array.prototype.slice.call(arguments),
  32. docFrag = document.createDocumentFragment();
  33. argArr.forEach(function(argItem) {
  34. docFrag.appendChild(
  35. argItem instanceof Node ? argItem : document.createTextNode(String(argItem))
  36. );
  37. });
  38. this.parentNode.insertBefore(docFrag, this);
  39. }
  40. });
  41. }
  42. });
  43. })([Element.prototype, CharacterData.prototype, DocumentType.prototype]);
  44.  
  45. if (typeof queueMicrotask !== 'function') {
  46. window.queueMicrotask = function (callback) {
  47. Promise.resolve()
  48. .then(callback)
  49. .catch(e => setTimeout(() => { throw e; }, 0))
  50. }
  51. }
  52.  
  53. if (!Array.prototype.flat) {
  54. Array.prototype.flat = function(depth = 1) {
  55. return this.reduce(function (flat, toFlatten) {
  56. return flat.concat(
  57. (Array.isArray(toFlatten) && depth > 1)
  58. ? toFlatten.flat(depth - 1)
  59. : toFlatten
  60. );
  61. }, []);
  62. };
  63. }
  64.  
  65. if (!Object.fromEntries) {
  66. Object.fromEntries = function (entries) {
  67. if (!entries || !entries[Symbol.iterator]) {
  68. throw new Error('Object.fromEntries() requires a single iterable argument');
  69. }
  70.  
  71. const obj = {};
  72. for (const [key, value] of entries) {
  73. obj[key] = value;
  74. }
  75. return obj;
  76. };
  77. }
  78.  
  79. if (!Array.prototype.findLast) {
  80. Array.prototype.findLast = function (predicate, thisArg) {
  81. if (this == null) throw new TypeError('Array.prototype.findLast called on null or undefined');
  82. if (typeof predicate !== 'function') throw new TypeError('predicate must be a function');
  83.  
  84. const list = Object(this);
  85. const length = list.length >>> 0;
  86.  
  87. for (let i = length - 1; i >= 0; i--) {
  88. const value = list[i];
  89. if (predicate.call(thisArg, value, i, list)) {
  90. return value;
  91. }
  92. }
  93.  
  94. return undefined;
  95. };
  96. }
Advertisement
Add Comment
Please, Sign In to add comment