string-from-charcodes
Version:
An alternative to String.fromCharCode that doesn't throw with many arguments, while still remaining fast.
22 lines (21 loc) • 762 B
JavaScript
/* IMPORT */
import { MAX_ARGUMENTS_LENGTH } from './constants.js';
/* MAIN */
const fromCharCodes = (charCodes) => {
if (!Array.isArray(charCodes))
return fromCharCodes(Array.from(charCodes));
const { length } = charCodes;
if (length <= MAX_ARGUMENTS_LENGTH) { // Small-enough length, no need to use chunks
return String.fromCharCode.apply(String, charCodes);
}
else { // Large-enough length, encoding in chunks
const chunks = [];
for (let ci = 0, i = 0; i < length; ci++) {
const slice = charCodes.slice(i, i += MAX_ARGUMENTS_LENGTH);
chunks[ci] = String.fromCharCode.apply(String, slice);
}
return chunks.join('');
}
};
/* EXPORT */
export default fromCharCodes;