1 // ♓🌟 Piscēs ∷ base64.js
2 // ====================================================================
4 // Copyright © 2020–2022 Lady [@ Lady’s Computer].
6 // This Source Code Form is subject to the terms of the Mozilla Public
7 // License, v. 2.0. If a copy of the MPL was not distributed with this
8 // file, You can obtain one at <https://mozilla.org/MPL/2.0/>.
10 import { fill
, map
, reduce
} from "./collection.js";
11 import { bind
, call
} from "./function.js";
12 import { ceil
, floor
} from "./numeric.js";
13 import { hasOwnProperty
, objectCreate
} from "./object.js";
21 const Buffer
= ArrayBuffer
;
22 const View
= DataView
;
23 const TypedArray
= Object
.getPrototypeOf(Uint8Array
);
24 const { prototype: arrayPrototype
} = Array
;
25 const { prototype: bufferPrototype
} = Buffer
;
26 const { iterator
: iteratorSymbol
} = Symbol
;
27 const { prototype: rePrototype
} = RegExp
;
28 const { prototype: typedArrayPrototype
} = TypedArray
;
29 const { prototype: viewPrototype
} = View
;
31 const { [iteratorSymbol
]: arrayIterator
} = arrayPrototype
;
33 next
: arrayIteratorNext
,
34 } = Object
.getPrototypeOf([][iteratorSymbol
]());
35 const getBufferByteLength
=
36 Object
.getOwnPropertyDescriptor(bufferPrototype
, "byteLength").get;
37 const getTypedArrayBuffer
=
38 Object
.getOwnPropertyDescriptor(typedArrayPrototype
, "buffer").get;
40 Object
.getOwnPropertyDescriptor(viewPrototype
, "buffer").get;
41 const { exec
: reExec
} = rePrototype
;
43 getUint8
: viewGetUint8
,
44 setUint8
: viewSetUint8
,
45 setUint16
: viewSetUint16
,
48 const base64CodeUnitIterablePrototype
= {
53 call(arrayIterator
, this, []),
61 * Returns an ArrayBuffer generated from the provided Base64.
63 * This function can also be used as a tag for a template literal. The
64 * literal will be interpreted akin to `String.raw`.
66 export const a2b
= ($, ...$s
) => {
67 const source
= stringReplace(
70 : hasOwnProperty($, "raw")
77 source
.length
% 4 == 0
78 ? stringReplace(source
, /={1,2}$/u, "")
81 const code
= getCodeUnit(ucsCharacter
, 0);
82 const result
= code
>= 0x41 && code
<= 0x5A
84 : code
>= 0x61 && code
<= 0x7A
86 : code
>= 0x30 && code
<= 0x39
95 `Piscēs: Invalid character in Base64: ${character}.`,
102 const { length
} = u6s
;
103 const dataView
= new View(new Buffer(floor(length
* 3 / 4)));
104 for (let index
= 0; index
< length
- 1;) {
105 const dataIndex
= ceil(index
* 3 / 4);
106 const remainder
= index
% 3;
107 if (remainder
== 0) {
108 call(viewSetUint8
, dataView
, [
110 (u6s
[index
] << 2) + (u6s
[++index
] >> 4),
112 } else if (remainder
== 1) {
113 call(viewSetUint8
, dataView
, [
115 ((u6s
[index
] & 0xF) << 4) + (u6s
[++index
] >> 2),
118 call(viewSetUint8
, dataView
, [
120 ((u6s
[index
] & 0x3) << 6) + u6s
[++index
],
124 return call(getViewBuffer
, dataView
, []);
128 * Returns a (big‐endian) base64 string created from a typed array,
131 * This function can also be used as a tag for a template literal. The
132 * literal will be interpreted akin to `String.raw`.
134 export const b2a
= ($, ...$s
) => {
135 const buffer
= $ instanceof Buffer
138 ? call(getViewBuffer
, $, [])
139 : $ instanceof TypedArray
140 ? call(getTypedArrayBuffer
, $, [])
146 (result
, ucsCharacter
, index
) => (
147 call(viewSetUint16
, result
, [
149 getCodeUnit(ucsCharacter
, 0),
152 new View(new Buffer(string
.length
* 2)),
158 : hasOwnProperty($, "raw")
159 ? rawString($, ...$s
)
162 const dataView
= new View(buffer
);
163 const byteLength
= call(getBufferByteLength
, buffer
, []);
164 const minimumLengthOfResults
= ceil(byteLength
* 4 / 3);
165 const resultingCodeUnits
= fill(
167 base64CodeUnitIterablePrototype
,
170 value
: minimumLengthOfResults
+
171 (4 - (minimumLengthOfResults
% 4)) % 4,
177 for (let index
= 0; index
< byteLength
;) {
178 const codeUnitIndex
= ceil(index
* 4 / 3);
179 const currentIndex
= codeUnitIndex
+ +(
180 index
% 3 == 0 && resultingCodeUnits
[codeUnitIndex
] != 0x3D
182 const remainder
= currentIndex
% 4;
183 const u6
= remainder
== 0
184 ? call(viewGetUint8
, dataView
, [index
]) >> 2
186 ? ((call(viewGetUint8
, dataView
, [index
++]) & 0x3) << 4) +
188 ? call(viewGetUint8
, dataView
, [index
]) >> 4
191 ? ((call(viewGetUint8
, dataView
, [index
++]) & 0xF) << 2) +
193 ? call(viewGetUint8
, dataView
, [index
]) >> 6
195 : call(viewGetUint8
, dataView
, [index
++]) & 0x3F;
196 const result
= u6
< 26
208 throw new RangeError(`Piscēs: Unexpected Base64 value: ${u6}.`);
210 resultingCodeUnits
[currentIndex
] = result
;
213 return stringFromCodeUnits(...resultingCodeUnits
);
217 * Returns whether the provided value is a Base64 string.
219 * Returns false if the provided value is not a string primitive.
221 export const isBase64
= ($) => {
222 if (typeof $ !== "string") {
225 const source
= stringReplace($, /[\t\n\f\r ]+/gu, "");
226 const trimmed
= source
.length
% 4 == 0
227 ? stringReplace(source
, /={1,2}$/u, "")
229 return trimmed
.length
% 4 != 1 &&
230 call(reExec
, /[^0-9A-Za-z+\/]/u, [trimmed
]) == null;