@leismore/insert_separator
Version:
A JavaScript function that converts a string into a series of fixed-length string chunks divided by a given separator.
37 lines (36 loc) • 1.28 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.insert_separator = void 0;
const ISError_1 = require("./ISError");
/**
* @param text {string} - The text waiting for being added with separators
* @param size {int} - The text chunk length
* @param separator {string}
* @returns {string} - The text with separators
* @throws {ISError}
* 1 invalid_text
* 2 invalid_size
* 3 invalid_separator
*/
function insert_separator(text, size, separator) {
let parts = [];
let maxStart;
if (typeof text !== 'string' || text === '') {
throw new ISError_1.ISError({ message: 'invalid_text', code: '1' });
}
if (typeof size !== 'number' || size < 1) {
throw new ISError_1.ISError({ message: 'invalid_size', code: '2' });
}
else {
size = Math.round(size);
}
if (typeof separator !== 'string' || separator === '') {
throw new ISError_1.ISError({ message: 'invalid_separator', code: '3' });
}
maxStart = (Math.ceil(text.length / size) - 1) * size;
for (let i = 0, j = 0; j <= maxStart; i++, j += size) {
parts[i] = text.substr(j, size);
}
return parts.join(separator);
}
exports.insert_separator = insert_separator;