read-excel-file
Version:
Read `.xlsx` files in a web browser or in Node.js
92 lines (89 loc) • 3.81 kB
JavaScript
// Sidenote: `strFromU8()` function from `fflate` is not "self-contained",
// i.e. it references some variables outside of its scope,
// which doesn't work with `worker-f` package and throws an error: "td is not defined".
//
// But `woker-f` package is currently not used, so this `import` is not commented out.
//
import { strFromU8 as strFromU8_ } from 'fflate';
import checkpoint from './checkpoint.js';
/**
* @param {Record<string,Uint8Array} entries
* @returns {Record<string,string>}
*/
export default function convertValuesFromUint8ArraysToStrings(entries) {
checkpoint('convert files to strings');
var convertedEntries = {};
for (var _i = 0, _Object$keys = Object.keys(entries); _i < _Object$keys.length; _i++) {
var key = _Object$keys[_i];
convertedEntries[key] = strFromU8(entries[key]);
}
return convertedEntries;
}
// There's a strange thing with using `fflate`'s `strFromU8()` function:
// when `import`ed directly from `fflate` package, the benchmark runs about 10% slower
// then when using `new TextDecoder().decode(data)`, even though `strFromU8()`
// does the same thing internally. So it's a bit mysterious.
//
// Because manually calling `new TextDecoder().decode(data)` is measurably faster for some reason,
// `fflate`'s `strFromU8()` is used as a backup for old web browsers that don't support `TextDecoder`.
//
/**
* Converts a Uint8Array to a string
* @param data The data to decode to string
* @param latin1 Whether or not to interpret the data as Latin-1. This should
* not need to be true unless encoding to binary string.
* @returns The original UTF-8/Latin-1 string
*/
export function strFromU8(data) {
// https://caniuse.com/?search=TextDecoder
if (typeof TextDecoder !== 'undefined') {
return new TextDecoder().decode(data);
} else {
return strFromU8_(data);
// Alternatively, it could call a copy-pasted "rest" part of the `strFromU8()` function:
// const [string, leftovers] = decodeUtf8String(data)
// console.log(leftovers)
// if (leftovers.length) {
// throw new Error('Decode UTF8')
// }
// return string
}
}
// This function's code is a copy-paste of `dutf8()` function in `fflate` repository from Jul 12th, 2026.
// https://github.com/101arrowz/fflate/blob/master/src/index.ts
//
// By the way, this function errors when running test case:
// "made-in-macos-excel-2011-with-custom-font".
// The leftover is `Uint8Array(2) [ 255, 217 ]`.
// To reproduce the error, replace the entire `strFromU8()` function
// in `convertValuesFromUint8ArraysToStrings()` with this function and see what happens.
//
// /**
// * Decodes a `Uint8Array` into a UTF8 string.
// * @param {Uint8Array} d
// * @returns {[string, Uint8Array]} — The decoded string and what's left undecoded
// */
// export function decodeUtf8String(d) {
// for (let s = '', i = 0;;) {
// let c = d[i++]
// const charType = (c > 127) + (c > 223) + (c > 239)
// if (i + charType > d.length) {
// // Returns a slice of an array.
// // Allows garbage collector to free the original reference.
// // This function is browser-compatible than `Uint8Array.slice()`.
// const leftovers = new Uint8Array(d.subarray(i - 1))
// return [s, leftovers]
// }
// if (!charType) {
// s += String.fromCharCode(c)
// } else if (charType == 3) {
// c = ((c & 15) << 18 | (d[i++] & 63) << 12 | (d[i++] & 63) << 6 | (d[i++] & 63)) - 65536
// s += String.fromCharCode(55296 | (c >> 10), 56320 | (c & 1023))
// } else if (charType & 1) {
// s += String.fromCharCode((c & 31) << 6 | (d[i++] & 63))
// } else {
// s += String.fromCharCode((c & 15) << 12 | (d[i++] & 63) << 6 | (d[i++] & 63))
// }
// }
// }
//# sourceMappingURL=convertValuesFromUint8ArraysToStrings.js.map