From 3a529856c124303f7ad4206a0f994e6f4e6757f2 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Wed, 7 Feb 2024 17:05:44 -0800 Subject: [PATCH] [Refactor] move `into` static methods to prototype and rename See https://github.com/tc39/proposal-arraybuffer-base64/pull/45 --- README.md | 6 +- Uint8Array.fromBase64Into/polyfill.js | 7 -- Uint8Array.fromHexInto/polyfill.js | 7 -- .../auto.js | 0 .../implementation.js | 70 ++++++++++--------- .../index.js | 2 +- .../polyfill.js | 7 ++ .../shim.js | 8 +-- .../auto.js | 0 .../implementation.js | 40 ++++++----- .../index.js | 2 +- Uint8Array.prototype.setFromHex/polyfill.js | 7 ++ .../shim.js | 8 +-- aos/IsFixedLengthArrayBuffer.js | 24 +++++-- aos/IsResizableArrayBuffer.js | 38 ---------- index.json | 8 +-- shim.js | 8 +-- ... => Uint8Array.prototype.setFromBase64.js} | 31 ++++---- ....js => Uint8Array.prototype.setFromHex.js} | 23 +++--- 19 files changed, 138 insertions(+), 158 deletions(-) delete mode 100644 Uint8Array.fromBase64Into/polyfill.js delete mode 100644 Uint8Array.fromHexInto/polyfill.js rename {Uint8Array.fromBase64Into => Uint8Array.prototype.setFromBase64}/auto.js (100%) rename {Uint8Array.fromBase64Into => Uint8Array.prototype.setFromBase64}/implementation.js (64%) rename {Uint8Array.fromHexInto => Uint8Array.prototype.setFromBase64}/index.js (88%) create mode 100644 Uint8Array.prototype.setFromBase64/polyfill.js rename {Uint8Array.fromBase64Into => Uint8Array.prototype.setFromBase64}/shim.js (50%) rename {Uint8Array.fromHexInto => Uint8Array.prototype.setFromHex}/auto.js (100%) rename {Uint8Array.fromHexInto => Uint8Array.prototype.setFromHex}/implementation.js (68%) rename {Uint8Array.fromBase64Into => Uint8Array.prototype.setFromHex}/index.js (88%) create mode 100644 Uint8Array.prototype.setFromHex/polyfill.js rename {Uint8Array.fromHexInto => Uint8Array.prototype.setFromHex}/shim.js (51%) delete mode 100644 aos/IsResizableArrayBuffer.js rename test/{Uint8Array.fromBase64Into.js => Uint8Array.prototype.setFromBase64.js} (80%) rename test/{Uint8Array.fromHexInto.js => Uint8Array.prototype.setFromHex.js} (75%) diff --git a/README.md b/README.md index 1ed69a6..31fff6f 100644 --- a/README.md +++ b/README.md @@ -19,10 +19,12 @@ If `Uint8Array` is not present, the `shim` functions and `auto` entrypoints will ## Supported things - - [`Uint8Array.prototype.toBase64`](https://tc39.es/proposal-arraybuffer-base64/spec/#sec-uint8array.prototype.tobase64) - - [`Uint8Array.prototype.toHex`](https://tc39.es/proposal-arraybuffer-base64/spec/#sec-uint8array.prototype.tohex) - [`Uint8Array.fromBase64`](https://tc39.es/proposal-arraybuffer-base64/spec/#sec-uint8array.frombase64) - [`Uint8Array.fromHex`](https://tc39.es/proposal-arraybuffer-base64/spec/#sec-uint8array.fromhex) + - [`Uint8Array.prototype.toBase64`](https://tc39.es/proposal-arraybuffer-base64/spec/#sec-uint8array.prototype.tobase64) + - [`Uint8Array.prototype.toHex`](https://tc39.es/proposal-arraybuffer-base64/spec/#sec-uint8array.prototype.tohex) + - [`Uint8Array.prototype.setFromBase64`](https://tc39.es/proposal-arraybuffer-base64/spec/#sec-uint8array.prototype.setfrombase64) + - [`Uint8Array.prototype.setFromHex`](https://tc39.es/proposal-arraybuffer-base64/spec/#sec-uint8array.prototype.setfromhex) ## Getting started diff --git a/Uint8Array.fromBase64Into/polyfill.js b/Uint8Array.fromBase64Into/polyfill.js deleted file mode 100644 index a944fd6..0000000 --- a/Uint8Array.fromBase64Into/polyfill.js +++ /dev/null @@ -1,7 +0,0 @@ -'use strict'; - -var implementation = require('./implementation'); - -module.exports = function getPolyfill() { - return typeof Uint8Array === 'function' && typeof Uint8Array.fromBase64Into === 'function' ? Uint8Array.fromBase64Into : implementation; -}; diff --git a/Uint8Array.fromHexInto/polyfill.js b/Uint8Array.fromHexInto/polyfill.js deleted file mode 100644 index f9abd87..0000000 --- a/Uint8Array.fromHexInto/polyfill.js +++ /dev/null @@ -1,7 +0,0 @@ -'use strict'; - -var implementation = require('./implementation'); - -module.exports = function getPolyfill() { - return typeof Uint8Array === 'function' && typeof Uint8Array.fromHexInto === 'function' ? Uint8Array.fromHexInto : implementation; -}; diff --git a/Uint8Array.fromBase64Into/auto.js b/Uint8Array.prototype.setFromBase64/auto.js similarity index 100% rename from Uint8Array.fromBase64Into/auto.js rename to Uint8Array.prototype.setFromBase64/auto.js diff --git a/Uint8Array.fromBase64Into/implementation.js b/Uint8Array.prototype.setFromBase64/implementation.js similarity index 64% rename from Uint8Array.fromBase64Into/implementation.js rename to Uint8Array.prototype.setFromBase64/implementation.js index 3bd9749..413c245 100644 --- a/Uint8Array.fromBase64Into/implementation.js +++ b/Uint8Array.prototype.setFromBase64/implementation.js @@ -19,89 +19,91 @@ var ValidateUint8Array = require('../aos/ValidateUint8Array'); var typedArrayByteOffset = require('typed-array-byte-offset'); var typedArrayBuffer = require('typed-array-buffer'); -module.exports = function fromBase64Into(string, into) { +module.exports = function setFromBase64(string) { if (!$Uint8Array) { throw new $SyntaxError('This environment does not support Uint8Array'); } - if (typeof string !== 'string') { - throw new $TypeError('`string` is not a string: ' + string); // step 1 - } + var into = this; // step 1 ValidateUint8Array(into); // step 2 - var opts = GetOptionsObject(arguments.length > 2 ? arguments[2] : void undefined); // step 3 + if (typeof string !== 'string') { + throw new $TypeError('`string` is not a string: ' + string); // step 3 + } + + var opts = GetOptionsObject(arguments.length > 1 ? arguments[1] : void undefined); // step 4 - var alphabet = Get(opts, 'alphabet'); // step 4 + var alphabet = Get(opts, 'alphabet'); // step 5 if (typeof alphabet === 'undefined') { - alphabet = 'base64'; // step 5 + alphabet = 'base64'; // step 6 } if (typeof alphabet !== 'string') { - throw new $TypeError('`alphabet` is not a string: ' + alphabet); // step 6 + throw new $TypeError('`alphabet` is not a string: ' + alphabet); // step 7 } if (alphabet !== 'base64' && alphabet !== 'base64url') { - throw new $TypeError('Assertion failed: `alphabet` is not `\'base64\'` or `\'base64url\'`: ' + alphabet); // step 7 + throw new $TypeError('Assertion failed: `alphabet` is not `\'base64\'` or `\'base64url\'`: ' + alphabet); // step 8 } - var lastChunkHandling = Get(opts, 'lastChunkHandling'); // step 8 + var lastChunkHandling = Get(opts, 'lastChunkHandling'); // step 9 if (typeof lastChunkHandling === 'undefined') { - lastChunkHandling = 'loose'; // step 9 + lastChunkHandling = 'loose'; // step 10 } if (lastChunkHandling !== 'loose' && lastChunkHandling !== 'strict' && lastChunkHandling !== 'stop-before-partial') { - throw new $TypeError('`lastChunkHandling` must be `\'loose\'`, `\'strict\'`, or `\'stop-before-partial\'`'); // step 10 + throw new $TypeError('`lastChunkHandling` must be `\'loose\'`, `\'strict\'`, or `\'stop-before-partial\'`'); // step 11 } - var taRecord = MakeTypedArrayWithBufferWitnessRecord(into, 'SEQ-CST'); // step 11 + var taRecord = MakeTypedArrayWithBufferWitnessRecord(into, 'SEQ-CST'); // step 12 if (IsTypedArrayOutOfBounds(taRecord)) { - throw new $TypeError('typed array is out of bounds'); // step 12 + throw new $TypeError('typed array is out of bounds'); // step 13 } - var byteLength = TypedArrayByteLength(taRecord); // step 13 + var byteLength = TypedArrayByteLength(taRecord); // step 14 - var maxLength = byteLength; // step 14 + var maxLength = byteLength; // step 15 - var result = FromBase64(string, alphabet, lastChunkHandling, maxLength); // step 15 + var result = FromBase64(string, alphabet, lastChunkHandling, maxLength); // step 16 - var bytes = result['[[Bytes]]']; // step 16 + var bytes = result['[[Bytes]]']; // step 17 - var written = bytes.length; // step 17 + var written = bytes.length; // step 18 - // 18. NOTE: FromBase64 does not invoke any user code, so the ArrayBuffer backing into cannot have been detached or shrunk. + // 19. NOTE: FromBase64 does not invoke any user code, so the ArrayBuffer backing into cannot have been detached or shrunk. if (written > byteLength) { - throw new $TypeError('Assertion failed: written is not <= byteLength'); // step 19 + throw new $TypeError('Assertion failed: written is not <= byteLength'); // step 20 } - SetUint8ArrayBytes(into, bytes); // step 20 + SetUint8ArrayBytes(into, bytes); // step 21 - var offset = typedArrayByteOffset(into); // step 20 + var offset = typedArrayByteOffset(into); // step 22 - var index = 0; // step 21 + var index = 0; // step 23 var intoBuffer = typedArrayBuffer(into); - while (index < written) { // step 22 - var byte = bytes[index]; // step 22.a + while (index < written) { // step 24 + var byte = bytes[index]; // step 24.a - var byteIndexInBuffer = index + offset; // step 22.b + var byteIndexInBuffer = index + offset; // step 24.b - SetValueInBuffer(intoBuffer, byteIndexInBuffer, 'Uint8', byte, true, 'Unordered'); // step 22.c + SetValueInBuffer(intoBuffer, byteIndexInBuffer, 'Uint8', byte, true, 'Unordered'); // step 24.c - index += 1; // step 22.d + index += 1; // step 24.d } - // 23. Let resultObject be OrdinaryObjectCreate(%Object.prototype%). - // 24. Perform ! CreateDataPropertyOrThrow(resultObject, "read", 𝔽(result.[[Read]])). - // 25. Perform ! CreateDataPropertyOrThrow(resultObject, "written", 𝔽(written)). - // 26. Return resultObject. + // 25. Let resultObject be OrdinaryObjectCreate(%Object.prototype%). + // 26. Perform ! CreateDataPropertyOrThrow(resultObject, "read", 𝔽(result.[[Read]])). + // 27. Perform ! CreateDataPropertyOrThrow(resultObject, "written", 𝔽(written)). + // 28. Return resultObject. - return { // steps 23 - 26 + return { // steps 25 - 28 read: result['[[Read]]'], written: written }; diff --git a/Uint8Array.fromHexInto/index.js b/Uint8Array.prototype.setFromBase64/index.js similarity index 88% rename from Uint8Array.fromHexInto/index.js rename to Uint8Array.prototype.setFromBase64/index.js index 4c50b07..31f6bf4 100644 --- a/Uint8Array.fromHexInto/index.js +++ b/Uint8Array.prototype.setFromBase64/index.js @@ -7,7 +7,7 @@ var implementation = require('./implementation'); var getPolyfill = require('./polyfill'); var shim = require('./shim'); -var bound = callBind(getPolyfill(), null); +var bound = callBind(getPolyfill()); define(bound, { getPolyfill: getPolyfill, diff --git a/Uint8Array.prototype.setFromBase64/polyfill.js b/Uint8Array.prototype.setFromBase64/polyfill.js new file mode 100644 index 0000000..6331f62 --- /dev/null +++ b/Uint8Array.prototype.setFromBase64/polyfill.js @@ -0,0 +1,7 @@ +'use strict'; + +var implementation = require('./implementation'); + +module.exports = function getPolyfill() { + return typeof Uint8Array === 'function' && typeof Uint8Array.prototype.setFromBase64 === 'function' ? Uint8Array.prototype.setFromBase64 : implementation; +}; diff --git a/Uint8Array.fromBase64Into/shim.js b/Uint8Array.prototype.setFromBase64/shim.js similarity index 50% rename from Uint8Array.fromBase64Into/shim.js rename to Uint8Array.prototype.setFromBase64/shim.js index 9ceb69b..6f16f58 100644 --- a/Uint8Array.fromBase64Into/shim.js +++ b/Uint8Array.prototype.setFromBase64/shim.js @@ -3,14 +3,14 @@ var getPolyfill = require('./polyfill'); var define = require('define-properties'); -module.exports = function shimUint8ArrayFromBase64Into() { +module.exports = function shimUint8ArraySetFromBase64() { var polyfill = getPolyfill(); if (typeof Uint8Array === 'function') { define( - Uint8Array, - { fromBase64Into: polyfill }, - { fromBase64Into: function () { return Uint8Array.fromBase64Into !== polyfill; } } + Uint8Array.prototype, + { setFromBase64: polyfill }, + { setFromBase64: function () { return Uint8Array.prototype.setFromBase64 !== polyfill; } } ); } diff --git a/Uint8Array.fromHexInto/auto.js b/Uint8Array.prototype.setFromHex/auto.js similarity index 100% rename from Uint8Array.fromHexInto/auto.js rename to Uint8Array.prototype.setFromHex/auto.js diff --git a/Uint8Array.fromHexInto/implementation.js b/Uint8Array.prototype.setFromHex/implementation.js similarity index 68% rename from Uint8Array.fromHexInto/implementation.js rename to Uint8Array.prototype.setFromHex/implementation.js index 0963554..d741082 100644 --- a/Uint8Array.fromHexInto/implementation.js +++ b/Uint8Array.prototype.setFromHex/implementation.js @@ -13,47 +13,49 @@ var TypedArrayByteLength = require('../aos/TypedArrayByteLength'); var ValidateUint8Array = require('../aos/ValidateUint8Array'); var SetUint8ArrayBytes = require('../aos/SetUint8ArrayBytes'); -module.exports = function fromHexInto(string, into) { +module.exports = function setFromHex(string) { if (!$Uint8Array) { throw new $SyntaxError('This environment does not support Uint8Array'); // step 1 } - if (typeof string !== 'string') { - throw new $TypeError('`string` is not a string: ' + string); // step 1 - } + var into = this; // step 1 ValidateUint8Array(into); // step 2 - var taRecord = MakeTypedArrayWithBufferWitnessRecord(into, 'SEQ-CST'); // step 3 + if (typeof string !== 'string') { + throw new $TypeError('`string` is not a string: ' + string); // step 3 + } + + var taRecord = MakeTypedArrayWithBufferWitnessRecord(into, 'SEQ-CST'); // step 4 if (IsTypedArrayOutOfBounds(taRecord)) { - throw new $TypeError('fromHexInto called on Typed Array backed by detached buffer'); // step 4 + throw new $TypeError('fromHexInto called on Typed Array backed by detached buffer'); // step 5 } - var byteLength = TypedArrayByteLength(taRecord); // step 5 + var byteLength = TypedArrayByteLength(taRecord); // step 6 - var maxLength = byteLength; // step 6 + var maxLength = byteLength; // step 7 - var result = FromHex(string, maxLength); // step 7 + var result = FromHex(string, maxLength); // step 8 - var bytes = result['[[Bytes]]']; // step 8 + var bytes = result['[[Bytes]]']; // step 9 - var written = bytes.length; // step 9 + var written = bytes.length; // step 10 - // 10. NOTE: FromHex does not invoke any user code, so the ArrayBuffer backing into cannot have been detached or shrunk. + // 11. NOTE: FromHex does not invoke any user code, so the ArrayBuffer backing into cannot have been detached or shrunk. if (written > byteLength) { - throw new $TypeError('Assertion failed: written is not <= byteLength'); // step 11 + throw new $TypeError('Assertion failed: written is not <= byteLength'); // step 12 } - SetUint8ArrayBytes(into, bytes); // step 12 + SetUint8ArrayBytes(into, bytes); // step 13 - // var resultObject = {}; // step 13 // OrdinaryObjectCreate(%Object.prototype%) - // CreateDataPropertyOrThrow(resultObject, 'read', result['[[Read]]']); // step 14 - // CreateDataPropertyOrThrow(resultObject, 'written', written); // step 15 - // return resultObject; // step 16 + // var resultObject = {}; // step 14 // OrdinaryObjectCreate(%Object.prototype%) + // CreateDataPropertyOrThrow(resultObject, 'read', result['[[Read]]']); // step 15 + // CreateDataPropertyOrThrow(resultObject, 'written', written); // step 16 + // return resultObject; // step 17 - return { // steps 13 - 16 + return { // steps 14 - 17 read: result['[[Read]]'], written: written }; diff --git a/Uint8Array.fromBase64Into/index.js b/Uint8Array.prototype.setFromHex/index.js similarity index 88% rename from Uint8Array.fromBase64Into/index.js rename to Uint8Array.prototype.setFromHex/index.js index 4c50b07..31f6bf4 100644 --- a/Uint8Array.fromBase64Into/index.js +++ b/Uint8Array.prototype.setFromHex/index.js @@ -7,7 +7,7 @@ var implementation = require('./implementation'); var getPolyfill = require('./polyfill'); var shim = require('./shim'); -var bound = callBind(getPolyfill(), null); +var bound = callBind(getPolyfill()); define(bound, { getPolyfill: getPolyfill, diff --git a/Uint8Array.prototype.setFromHex/polyfill.js b/Uint8Array.prototype.setFromHex/polyfill.js new file mode 100644 index 0000000..4db444a --- /dev/null +++ b/Uint8Array.prototype.setFromHex/polyfill.js @@ -0,0 +1,7 @@ +'use strict'; + +var implementation = require('./implementation'); + +module.exports = function getPolyfill() { + return typeof Uint8Array === 'function' && typeof Uint8Array.prototype.setFromHex === 'function' ? Uint8Array.prototype.setFromHex : implementation; +}; diff --git a/Uint8Array.fromHexInto/shim.js b/Uint8Array.prototype.setFromHex/shim.js similarity index 51% rename from Uint8Array.fromHexInto/shim.js rename to Uint8Array.prototype.setFromHex/shim.js index 49613eb..c040caa 100644 --- a/Uint8Array.fromHexInto/shim.js +++ b/Uint8Array.prototype.setFromHex/shim.js @@ -3,14 +3,14 @@ var getPolyfill = require('./polyfill'); var define = require('define-properties'); -module.exports = function shimUint8ArrayFromHexInto() { +module.exports = function shimUint8ArraySetFromHex() { var polyfill = getPolyfill(); if (typeof Uint8Array === 'function') { define( - Uint8Array, - { fromHexInto: polyfill }, - { fromHexInto: function () { return Uint8Array.fromHexInto !== polyfill; } } + Uint8Array.prototype, + { setFromHex: polyfill }, + { setFromHex: function () { return Uint8Array.prototype.setFromHex !== polyfill; } } ); } diff --git a/aos/IsFixedLengthArrayBuffer.js b/aos/IsFixedLengthArrayBuffer.js index 41a8d91..0df150e 100644 --- a/aos/IsFixedLengthArrayBuffer.js +++ b/aos/IsFixedLengthArrayBuffer.js @@ -1,19 +1,29 @@ 'use strict'; -// var $TypeError = require('es-errors/type'); +var $TypeError = require('es-errors/type'); -var IsResizableArrayBuffer = require('./IsResizableArrayBuffer'); +var callBound = require('call-bind/callBound'); + +var $arrayBufferResizable = callBound('%ArrayBuffer.prototype.resizable%', true); +var $sharedArrayGrowable = callBound('%SharedArrayBuffer.prototype.growable%', true); var isArrayBuffer = require('is-array-buffer'); var isSharedArrayBuffer = require('is-shared-array-buffer'); -// https://tc39.es/ecma262/#sec-isfixedlengtharraybuffer +// https://262.ecma-international.org/15.0/#sec-isfixedlengtharraybuffer module.exports = function IsFixedLengthArrayBuffer(arrayBuffer) { - if (!isArrayBuffer(arrayBuffer) && !isSharedArrayBuffer(arrayBuffer)) { - // checked inside IsResizableArrayBuffer - // throw new $TypeError('Assertion failed: `arrayBuffer` must be an ArrayBuffer or a SharedArrayBuffer'); + var isAB = isArrayBuffer(arrayBuffer); + var isSAB = isSharedArrayBuffer(arrayBuffer); + if (!isAB && !isSAB) { + throw new $TypeError('Assertion failed: `arrayBuffer` must be an ArrayBuffer or SharedArrayBuffer'); } - return !IsResizableArrayBuffer(arrayBuffer); + if (isAB && $arrayBufferResizable) { + return !$arrayBufferResizable(arrayBuffer); // step 1 + } + if (isSAB && $sharedArrayGrowable) { + return !$sharedArrayGrowable(arrayBuffer); // step 1 + } + return true; // step 2 }; diff --git a/aos/IsResizableArrayBuffer.js b/aos/IsResizableArrayBuffer.js deleted file mode 100644 index 5040b96..0000000 --- a/aos/IsResizableArrayBuffer.js +++ /dev/null @@ -1,38 +0,0 @@ -'use strict'; - -var $TypeError = require('es-errors/type'); - -var callBound = require('call-bind/callBound'); - -var $arrayBufferResizable = callBound('%ArrayBuffer.prototype.resizable%', true); -var $sharedArrayGrowable = callBound('%SharedArrayBuffer.prototype.growable%', true); - -var isArrayBuffer = require('is-array-buffer'); -var isSharedArrayBuffer = require('is-shared-array-buffer'); - -module.exports = function IsResizableArrayBuffer(arrayBuffer) { - var isAB = isArrayBuffer(arrayBuffer); - var isSAB = isSharedArrayBuffer(arrayBuffer); - if (!isAB && !isSAB) { - throw new $TypeError('Assertion failed: `arrayBuffer` must be an ArrayBuffer or SharedArrayBuffer'); - } - // 1. If arrayBuffer has an [[ArrayBufferMaxByteLength]] internal slot, return true. - - // 2. Return false. - - if (isAB) { - try { - return !!$arrayBufferResizable && $arrayBufferResizable(arrayBuffer); // step 1 - } catch (e) { - return false; // step 2 - } - } - if (isSAB) { - try { - return !!$sharedArrayGrowable && $sharedArrayGrowable(arrayBuffer); // step 1 - } catch (e) { - return false; // step 2 - } - } - return false; // step 2 -}; diff --git a/index.json b/index.json index bf2564b..7ed0bc8 100644 --- a/index.json +++ b/index.json @@ -1,8 +1,8 @@ [ - "Uint8Array.prototype.toBase64", - "Uint8Array.prototype.toHex", "Uint8Array.fromBase64", - "Uint8Array.fromBase64Into", "Uint8Array.fromHex", - "Uint8Array.fromHexInto" + "Uint8Array.prototype.setFromBase64", + "Uint8Array.prototype.setFromHex", + "Uint8Array.prototype.toBase64", + "Uint8Array.prototype.toHex" ] diff --git a/shim.js b/shim.js index 3881e82..cddd903 100644 --- a/shim.js +++ b/shim.js @@ -1,17 +1,17 @@ 'use strict'; var fromBase64 = require('./Uint8Array.fromBase64/shim'); -var fromBase64Into = require('./Uint8Array.fromBase64Into/shim'); var fromHex = require('./Uint8Array.fromHex/shim'); -var fromHexInto = require('./Uint8Array.fromHexInto/shim'); +var setFromBase64 = require('./Uint8Array.prototype.setFromBase64/shim'); +var setFromHex = require('./Uint8Array.prototype.setFromHex/shim'); var toBase64 = require('./Uint8Array.prototype.toBase64/shim'); var toHex = require('./Uint8Array.prototype.toHex/shim'); module.exports = function shimArrayBufferBase64() { fromBase64(); - fromBase64Into(); fromHex(); - fromHexInto(); + setFromBase64(); + setFromHex(); toBase64(); toHex(); }; diff --git a/test/Uint8Array.fromBase64Into.js b/test/Uint8Array.prototype.setFromBase64.js similarity index 80% rename from test/Uint8Array.fromBase64Into.js rename to test/Uint8Array.prototype.setFromBase64.js index 129487a..7665153 100644 --- a/test/Uint8Array.fromBase64Into.js +++ b/test/Uint8Array.prototype.setFromBase64.js @@ -2,15 +2,16 @@ var defineProperties = require('define-properties'); var test = require('tape'); +var callBind = require('call-bind'); -var index = require('../Uint8Array.fromBase64Into'); -var impl = require('../Uint8Array.fromBase64Into/implementation'); +var index = require('../Uint8Array.prototype.setFromBase64'); +var impl = require('../Uint8Array.prototype.setFromBase64/implementation'); -var polyfill = require('../Uint8Array.fromBase64Into/polyfill')(); +var polyfill = require('../Uint8Array.prototype.setFromBase64/polyfill')(); var isEnumerable = Object.prototype.propertyIsEnumerable; -var shimName = 'Uint8Array.fromBase64Into'; +var shimName = 'Uint8Array.prototype.setFromBase64'; module.exports = { tests: function (t, method) { @@ -29,14 +30,14 @@ module.exports = { arr[0] = 1; st['throws']( - function () { return method('F', arr); }, + function () { return method(arr, 'F'); }, SyntaxError, 'throws on odd-numbered length base64 strings' ); var expectedArr = new Uint8Array(12); expectedArr[0] = 1; - st.deepEqual(method('', arr), { read: 0, written: 0 }, 'empty string makes no changes'); + st.deepEqual(method(arr, ''), { read: 0, written: 0 }, 'empty string makes no changes'); st.deepEqual(arr, expectedArr, '`arr`, no changes'); var helloWorld = 'aGVsbG8gd29ybGQ='; @@ -52,7 +53,7 @@ module.exports = { var expected = new Uint8Array([104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 0]); st.deepEqual( - method(helloWorld, arr), + method(arr, helloWorld), { read: 16, written: 11 }, 'return object is as expected' ); @@ -63,7 +64,7 @@ module.exports = { ); st.deepEqual( - method(sentenceHelloWorld, arr), + method(arr, sentenceHelloWorld), { read: 16, written: 11 }, 'return object is as expected' ); @@ -74,7 +75,7 @@ module.exports = { ); st.deepEqual( - method('+/+/', arr), + method(arr, '+/+/'), { read: 4, written: 3 }, 'return object is as expected' ); @@ -85,7 +86,7 @@ module.exports = { ); st.deepEqual( - method('+/+/', arr, { alphabet: 'base64' }), + method(arr, '+/+/', { alphabet: 'base64' }), { read: 4, written: 3 }, 'return object is as expected' ); @@ -96,7 +97,7 @@ module.exports = { ); st.deepEqual( - method('-_-_', arr, { alphabet: 'base64url' }), + method(arr, '-_-_', { alphabet: 'base64url' }), { read: 4, written: 3 }, 'return object is as expected' ); @@ -107,7 +108,7 @@ module.exports = { ); st['throws']( - function () { method('-_-_', arr, { alphabet: 'base64' }); }, + function () { method(arr, '-_-_', { alphabet: 'base64' }); }, SyntaxError, 'base64url string with base64 alphabet throws' ); @@ -135,7 +136,7 @@ module.exports = { test(shimName + ': implementation', function (t) { t.equal(impl, polyfill, 'implementation is polyfill itself'); - module.exports.tests(t, impl); + module.exports.tests(t, callBind(impl)); t.end(); }); @@ -143,11 +144,11 @@ module.exports = { shimmed: function () { test(shimName + ': shimmed', function (t) { t.test('enumerability', { skip: !defineProperties.supportsDescriptors }, function (et) { - et.equal(false, isEnumerable.call(Uint8Array, 'fromBase64Into'), shimName + ' is not enumerable'); + et.equal(false, isEnumerable.call(Uint8Array.prototype, 'setFromBase64'), shimName + ' is not enumerable'); et.end(); }); - module.exports.tests(t, Uint8Array.fromBase64Into); + module.exports.tests(t, callBind(Uint8Array.prototype.setFromBase64)); t.end(); }); diff --git a/test/Uint8Array.fromHexInto.js b/test/Uint8Array.prototype.setFromHex.js similarity index 75% rename from test/Uint8Array.fromHexInto.js rename to test/Uint8Array.prototype.setFromHex.js index 974bd46..865e24f 100644 --- a/test/Uint8Array.fromHexInto.js +++ b/test/Uint8Array.prototype.setFromHex.js @@ -2,15 +2,16 @@ var defineProperties = require('define-properties'); var test = require('tape'); +var callBind = require('call-bind'); -var index = require('../Uint8Array.fromHexInto'); -var impl = require('../Uint8Array.fromHexInto/implementation'); +var index = require('../Uint8Array.prototype.setFromHex'); +var impl = require('../Uint8Array.prototype.setFromHex/implementation'); -var polyfill = require('../Uint8Array.fromHexInto/polyfill')(); +var polyfill = require('../Uint8Array.prototype.setFromHex/polyfill')(); var isEnumerable = Object.prototype.propertyIsEnumerable; -var shimName = 'Uint8Array.fromHexInto'; +var shimName = 'Uint8Array.prototype.setFromHex'; module.exports = { tests: function (t, method) { @@ -29,13 +30,13 @@ module.exports = { arr[0] = 1; st['throws']( - function () { return method('F', arr); }, + function () { return method(arr, 'F'); }, SyntaxError, 'throws on odd-numbered length hex strings' ); st['throws']( - function () { return method('FG', arr); }, + function () { return method(arr, 'FG'); }, SyntaxError, 'throws on invalid hex string characters' ); @@ -43,7 +44,7 @@ module.exports = { var expectedArr = new Uint8Array(256); expectedArr[0] = 1; st.deepEqual(arr, expectedArr, 'initial `arr`'); - st.deepEqual(method('', arr), { read: 0, written: 0 }, 'empty string makes no changes'); + st.deepEqual(method(arr, ''), { read: 0, written: 0 }, 'empty string makes no changes'); st.deepEqual(arr, expectedArr, '`arr`, no changes'); var hex = ''; @@ -54,7 +55,7 @@ module.exports = { } st.deepEqual( - method(hex, arr), + method(arr, hex), { read: 512, written: 256 }, 'return object is as expected' ); @@ -87,7 +88,7 @@ module.exports = { test(shimName + ': implementation', function (t) { t.equal(impl, polyfill, 'implementation is polyfill itself'); - module.exports.tests(t, impl); + module.exports.tests(t, callBind(impl)); t.end(); }); @@ -95,11 +96,11 @@ module.exports = { shimmed: function () { test(shimName + ': shimmed', function (t) { t.test('enumerability', { skip: !defineProperties.supportsDescriptors }, function (et) { - et.equal(false, isEnumerable.call(Uint8Array, 'fromHexInto'), shimName + ' is not enumerable'); + et.equal(false, isEnumerable.call(Uint8Array.prototype, 'setFromHex'), shimName + ' is not enumerable'); et.end(); }); - module.exports.tests(t, Uint8Array.fromHexInto); + module.exports.tests(t, callBind(Uint8Array.prototype.setFromHex)); t.end(); });