LINUX.ORG.RU

История изменений

Исправление wandrien, (текущая версия) :

Я за 5 минут напишу функцию получше этой лапши. Которая корректно работает с многосимвольной выравнивающей вставкой и без говнокода.


let leftpad = (str, len, ch = ' ') => {
    str = str.toString();
    if (str.length >= len)
        return str;
    ch = ch.toString();
    if (ch.length == 0)
        return str;
    str = ch.repeat((len - str.length) / ch.length) + str;
    str = ch.substring(ch.length - len + str.length) + str;
    return str;
}


let test = (expected, f) => {
    let result = f();
    if (result === expected)
        console.log("Test OK: '" + expected + "'");
    else
        console.log("Test FAILED: '" + expected + "' !== '" + result + "'");
}

test("12345", () => leftpad("12345", 10, ''));

test("12345", () => leftpad("12345", 4));
test("12345", () => leftpad("12345", 5));
test(" 12345", () => leftpad("12345", 6));
test("  12345", () => leftpad("12345", 7));

test(" --> 12345", () => leftpad("12345", 10, '--> '));
test("> --> 12345", () => leftpad("12345", 11, '--> '));
test("-> --> 12345", () => leftpad("12345", 12, '--> '));
test("--> --> 12345", () => leftpad("12345", 13, '--> '));
test(" --> --> 12345", () => leftpad("12345", 14, '--> '));
Test OK: '12345'
Test OK: '12345'
Test OK: '12345'
Test OK: ' 12345'
Test OK: '  12345'
Test OK: ' --> 12345'
Test OK: '> --> 12345'
Test OK: '-> --> 12345'
Test OK: '--> --> 12345'
Test OK: ' --> --> 12345'

Исправление wandrien, :

Я за 5 минут напишу функцию получше этой лапши. Которая корректно работает с многосимвольной выравнивающей вставкой и без говнокода.


let leftpad = (str, len, ch = ' ') => {
    if (str.length >= len)
        return str;
    ch = ch.toString();
    if (ch.length == 0)
        return str;
    str = ch.repeat((len - str.length) / ch.length) + str;
    str = ch.substring(ch.length - len + str.length) + str;
    return str;
}


let test = (expected, f) => {
    let result = f();
    if (result === expected)
        console.log("Test OK: '" + expected + "'");
    else
        console.log("Test FAILED: '" + expected + "' !== '" + result + "'");
}

test("12345", () => leftpad("12345", 10, ''));

test("12345", () => leftpad("12345", 4));
test("12345", () => leftpad("12345", 5));
test(" 12345", () => leftpad("12345", 6));
test("  12345", () => leftpad("12345", 7));

test(" --> 12345", () => leftpad("12345", 10, '--> '));
test("> --> 12345", () => leftpad("12345", 11, '--> '));
test("-> --> 12345", () => leftpad("12345", 12, '--> '));
test("--> --> 12345", () => leftpad("12345", 13, '--> '));
test(" --> --> 12345", () => leftpad("12345", 14, '--> '));
Test OK: '12345'
Test OK: '12345'
Test OK: '12345'
Test OK: ' 12345'
Test OK: '  12345'
Test OK: ' --> 12345'
Test OK: '> --> 12345'
Test OK: '-> --> 12345'
Test OK: '--> --> 12345'
Test OK: ' --> --> 12345'

Исходная версия wandrien, :

Я за 5 минут лучше функцию напишу, которая корректно работает с многосимвольной выравнивающей вставкой и без говнокода.


let leftpad = (str, len, ch = ' ') => {
    if (str.length >= len)
        return str;
    ch = ch.toString();
    if (ch.length == 0)
        return str;
    str = ch.repeat((len - str.length) / ch.length) + str;
    str = ch.substring(ch.length - len + str.length) + str;
    return str;
}


let test = (expected, f) => {
    let result = f();
    if (result === expected)
        console.log("Test OK: '" + expected + "'");
    else
        console.log("Test FAILED: '" + expected + "' !== '" + result + "'");
}

test("12345", () => leftpad("12345", 10, ''));

test("12345", () => leftpad("12345", 4));
test("12345", () => leftpad("12345", 5));
test(" 12345", () => leftpad("12345", 6));
test("  12345", () => leftpad("12345", 7));

test(" --> 12345", () => leftpad("12345", 10, '--> '));
test("> --> 12345", () => leftpad("12345", 11, '--> '));
test("-> --> 12345", () => leftpad("12345", 12, '--> '));
test("--> --> 12345", () => leftpad("12345", 13, '--> '));
test(" --> --> 12345", () => leftpad("12345", 14, '--> '));
Test OK: '12345'
Test OK: '12345'
Test OK: '12345'
Test OK: ' 12345'
Test OK: '  12345'
Test OK: ' --> 12345'
Test OK: '> --> 12345'
Test OK: '-> --> 12345'
Test OK: '--> --> 12345'
Test OK: ' --> --> 12345'