All files index.js

91.67% Statements 11/12
90.91% Branches 20/22
100% Functions 0/0
91.67% Lines 11/12
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 201x 1x   21x 7x         7x   7x 2x 5x 4x 4x   1x    
import substring from "unicode-substring";
import { length } from "./length";
 
export default function(str, limit = 16, padString = "#", padPosition = "right") {
    Iif (typeof str !== "string" || typeof limit !== "number") {
        throw new Error("Invalid arguments specified");
    }
    
    // Calculate string length considering astral code points
    const strLength = length(str);
 
    if (strLength > limit) {
        return substring(str, 0, limit);
    } else if (strLength < limit) {
        const padRepeats = padString.repeat(limit - strLength);
        return (padPosition === "left") ? padRepeats + str : str + padRepeats;
    }
    return str;
}