This commit is contained in:
Ilya Kantor 2016-03-09 00:16:22 +03:00
parent f1b7de040a
commit 2b874a73be
59 changed files with 1227 additions and 1232 deletions

View file

@ -1,4 +1,4 @@
function truncate(str, maxlength) {
return (str.length > maxlength) ?
str.slice(0, maxlength - 3) + '...' : str;
return (str.length > maxlength) ?
str.slice(0, maxlength - 1) + '…' : str;
}

View file

@ -1,15 +1,15 @@
describe("truncate", function() {
it("обрезает строку до указанной длины (включая троеточие)", function() {
it("truncate the long string to the given lenth (including the ellipsis)", function() {
assert.equal(
truncate("Вот, что мне хотелось бы сказать на эту тему:", 20),
"Вот, что мне хоте..."
truncate("What I'd like to tell on this topic is:", 20),
"What I'd like to te…"
);
});
it("не меняет короткие строки", function() {
it("doesn't change short strings", function() {
assert.equal(
truncate("Всем привет!", 20),
"Всем привет!"
truncate("Hi everyone!", 20),
"Hi everyone!"
);
});

View file

@ -1,26 +1,11 @@
Так как окончательная длина строки должна быть `maxlength`, то нужно её обрезать немного короче, чтобы дать место для троеточия.
The maximal length must be `maxlength`, so we need to cut it a little shorter, to give space for the ellipsis.
Note that there is actually a single unicode character for an ellipsis. That's not three dots.
```js run
function truncate(str, maxlength) {
if (str.length > maxlength) {
return str.slice(0, maxlength - 3) + '...';
// итоговая длина равна maxlength
}
return str;
}
alert( truncate("Вот, что мне хотелось бы сказать на эту тему:", 20) );
alert( truncate("Всем привет!", 20) );
```
Можно было бы написать этот код ещё короче:
```js run
function truncate(str, maxlength) {
return (str.length > maxlength) ?
str.slice(0, maxlength - 3) + '...' : str;
return (str.length > maxlength) ?
str.slice(0, maxlength - 1) + '…' : str;
}
```
P.S. Кстати, в кодироке Unicode существует специальный символ "троеточие": `…` (HTML: `…`), который можно использовать вместо трёх точек. Если его использовать, то можно отрезать только один символ.

View file

@ -2,18 +2,16 @@ importance: 5
---
# Усечение строки
# Truncate the text
Создайте функцию `truncate(str, maxlength)`, которая проверяет длину строки `str`, и если она превосходит `maxlength` -- заменяет конец `str` на `"..."`, так чтобы ее длина стала равна `maxlength`.
Create a function `truncate(str, maxlength)` that checks the length of the `str` and, if it exceeds `maxlength` -- replaces the end of `str` with the ellipsis character `"…"`, to make its length equal to `maxlength`.
Результатом функции должна быть (при необходимости) усечённая строка.
The result of the function should be the truncated (if needed) string.
Например:
For instance:
```js
truncate("Вот, что мне хотелось бы сказать на эту тему:", 20) = "Вот, что мне хоте..."
truncate("What I'd like to tell on this topic is:", 20) = "What I'd like to te…"
truncate("Всем привет!", 20) = "Всем привет!"
truncate("Hi everyone!", 20) = "Hi everyone!"
```
Эта функция имеет применение в жизни. Она используется, чтобы усекать слишком длинные темы сообщений.