@shgysk8zer0/polyfills 0.3.11 → 0.3.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/array.js CHANGED
@@ -280,7 +280,10 @@ if (! (Array.isTemplateObject instanceof Function)) {
280
280
  */
281
281
  if (! (Uint8Array.prototype.toHex instanceof Function)) {
282
282
  Uint8Array.prototype.toHex = function toHex() {
283
- return Array.from(this).map(n => n.toString(16).padStart('0', 2)).join('');
283
+ return Array.from(
284
+ this,
285
+ n => n.toString(16).padStart('0', 2)
286
+ ).join('');
284
287
  };
285
288
  }
286
289
 
@@ -288,7 +291,16 @@ if (! (Uint8Array.prototype.toBase64 instanceof Function)) {
288
291
  Uint8Array.prototype.toBase64 = function toBase64({ alphabet = 'base64' } = {}) {
289
292
  if (alphabet === 'base64') {
290
293
  // @todo Figure out encoding specifics
291
- return btoa(String.fromCodePoint(...this));
294
+ //return btoa(String.fromCodePoint(...this));
295
+ const chunkSize = 0x8000; // 32,768 bytes per chunk
296
+ let str = '';
297
+
298
+ for (let i = 0; i < this.length; i += chunkSize) {
299
+ const chunk = this.subarray(i, i + chunkSize);
300
+ str += String.fromCharCode.apply(null, chunk);
301
+ }
302
+
303
+ return btoa(str);
292
304
  // return btoa(new TextDecoder().decode(this));
293
305
  } else if (alphabet === 'base64url') {
294
306
  return this.toBase64({ alphabet: 'base64' }).replaceAll('+', '-').replaceAll('/', '_');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shgysk8zer0/polyfills",
3
- "version": "0.3.11",
3
+ "version": "0.3.13",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "description": "A collection of JavaScript polyfills",
@@ -78,6 +78,7 @@
78
78
  },
79
79
  "dependencies": {
80
80
  "@aegisjsproject/sanitizer": "^0.1.0",
81
- "@aegisjsproject/trusted-types": "^1.0.1"
81
+ "@aegisjsproject/trusted-types": "^1.0.1",
82
+ "string-dedent": "^3.0.1"
82
83
  }
83
84
  }
package/promise.js CHANGED
@@ -67,4 +67,25 @@ if ('Promise' in globalThis) {
67
67
  return def;
68
68
  };
69
69
  }
70
+
71
+ if (! (Promise.fromEntries instanceof Function)) {
72
+ Promise.fromEntries = async function fromEntries(entries) {
73
+ const keys = [];
74
+ const values = [];
75
+
76
+ for (const [key, value] of entries) {
77
+ keys.push(key);
78
+ values.push(value);
79
+ }
80
+
81
+ return await Promise.all(values)
82
+ .then(values => Object.fromEntries(values.map((value, i) => [keys[i], value])));
83
+ };
84
+ }
85
+
86
+ if (!(Promise.ownProperties instanceof Function)) {
87
+ Promise.ownProperties = async function ownProperties(obj) {
88
+ return await Promise.fromEntries(Object.entries(obj));
89
+ };
90
+ }
70
91
  }
package/string.js ADDED
@@ -0,0 +1,5 @@
1
+ import dedent from './node_modules/string-dedent/dist/dedent.mjs';
2
+
3
+ if (! (String.dedent instanceof Function)) {
4
+ String.dedent = dedent;
5
+ }
package/symbols.js CHANGED
@@ -1,4 +1,12 @@
1
+ import { polyfillMethod } from './utils.js';
2
+
1
3
  if ('Symbol' in globalThis) {
4
+ const known = new Set(
5
+ Reflect.ownKeys(Symbol)
6
+ .filter(item => typeof Symbol[item] === 'symbol')
7
+ .map(key => Symbol[key])
8
+ );
9
+
2
10
  if (typeof Symbol.toStringTag === 'undefined') {
3
11
  Symbol.toStringTag = Symbol('Symbol.toStringTag');
4
12
  }
@@ -6,4 +14,12 @@ if ('Symbol' in globalThis) {
6
14
  if (typeof Symbol.iterator === 'undefined') {
7
15
  Symbol.iterator = Symbol('Symbol.iterator');
8
16
  }
17
+
18
+ polyfillMethod(Symbol, 'isRegistered', function(symbol) {
19
+ return typeof symbol === 'symbol' && typeof Symbol.keyFor(symbol) === 'string';
20
+ });
21
+
22
+ polyfillMethod(Symbol, 'isWellKnown', function(symbol) {
23
+ return known.has(symbol);
24
+ });
9
25
  }
package/url.js CHANGED
@@ -1,9 +1,16 @@
1
1
  import { polyfillMethod } from './utils.js';
2
2
 
3
- polyfillMethod(URL, 'canParse', (str, base) => {
3
+ polyfillMethod(URL, 'parse', (url, base) => {
4
4
  try {
5
- new URL(str, base);
6
- return true;
5
+ return new URL(url, base);
6
+ } catch {
7
+ return null;
8
+ }
9
+ });
10
+
11
+ polyfillMethod(URL, 'canParse', (url, base) => {
12
+ try {
13
+ return new URL(url, base) instanceof URL;
7
14
  } catch {
8
15
  return false;
9
16
  }