Skip to content

Commit

Permalink
adds updates for optimal truncation
Browse files Browse the repository at this point in the history
  • Loading branch information
yowainwright committed Oct 19, 2016
1 parent f916e9b commit addbfba
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
3 changes: 2 additions & 1 deletion dist/truncated.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ function truncated(target, maxHeight, symbol, cName) {
var lastSpace = trimmedText.lastIndexOf(' ');
if (lastSpace < 0) break;
trimmedText = trimmedText.substr(0, lastSpace);
} while (text.offsetHeight > maxHeight);
el.textContent = trimmedText;
} while (el.offsetHeight > maxHeight);
var k = 0;
var diff = '';
for (var j = 0; j < text.length; j++) {
Expand Down
3 changes: 2 additions & 1 deletion src/truncated.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ export default function truncated(target, maxHeight, symbol, cName) {
const lastSpace = trimmedText.lastIndexOf(' ');
if (lastSpace < 0) break;
trimmedText = trimmedText.substr(0, lastSpace);
} while (text.offsetHeight > maxHeight);
el.textContent = trimmedText;
} while (el.offsetHeight > maxHeight);
let k = 0;
let diff = '';
for (let j = 0; j < text.length; j++) {
Expand Down
9 changes: 9 additions & 0 deletions tests/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@
<p class="test">
To surpass others is fucking tough, if you only do as you are told you don’t have it in you to succeed. When you design, you have to draw on your own fucking life experiences. If it’s not something you would want to read/look at/use then why fucking bother? Intuition is fucking important. Design is all about fucking relationships—the relationship of form and content, the relationship of elements, the relationship of designer and user. Why are you fucking reading all of this? Get back to work. If you’re not being fucking honest with yourself how could you ever hope to communicate something meaningful to someone else? Never, never assume that what you have achieved is fucking good enough. A good fucking composition is the result of a hierarchy consisting of clearly contrasting elements set with distinct alignments containing irregular intervals of negative space. Someday is not a fucking day of the week. Make your work consistent but not fucking predictable. What’s important is the fucking drive to see a project through no matter what. This design is fucking brilliant. The details are not the details. They make the fucking design. Nothing of value comes to you without fucking working
</p>
<p id="test">
To surpass others is fucking tough, if you only do as you are told you don’t have it in you to succeed. When you design, you have to draw on your own fucking life experiences. If it’s not something you would want to read/look at/use then why fucking bother? Intuition is fucking important. Design is all about fucking relationships—the relationship of form and content, the relationship of elements, the relationship of designer and user. Why are you fucking reading all of this? Get back to work. If you’re not being fucking honest with yourself how could you ever hope to communicate something meaningful to someone else? Never, never assume that what you have achieved is fucking good enough. A good fucking composition is the result of a hierarchy consisting of clearly contrasting elements set with distinct alignments containing irregular intervals of negative space. Someday is not a fucking day of the week. Make your work consistent but not fucking predictable. What’s important is the fucking drive to see a project through no matter what. This design is fucking brilliant. The details are not the details. They make the fucking design. Nothing of value comes to you without fucking working
</p>
<p id="test-2">
To surpass others is fucking tough, if you only do as you are told you don’t have it in you to succeed. When you design, you have to draw on your own fucking life experiences. If it’s not something you would want to read/look at/use then why fucking bother? Intuition is fucking important. Design is all about fucking relationships—the relationship of form and content, the relationship of elements, the relationship of designer and user. Why are you fucking reading all of this? Get back to work. If you’re not being fucking honest with yourself how could you ever hope to communicate something meaningful to someone else? Never, never assume that what you have achieved is fucking good enough. A good fucking composition is the result of a hierarchy consisting of clearly contrasting elements set with distinct alignments containing irregular intervals of negative space. Someday is not a fucking day of the week. Make your work consistent but not fucking predictable. What’s important is the fucking drive to see a project through no matter what. This design is fucking brilliant. The details are not the details. They make the fucking design. Nothing of value comes to you without fucking working
</p>
<!-- <p id="test-3">
To surpass others is fucking tough, if you only do as you are told you don’t have it in you to succeed. When you design, you have to draw on your own fucking life experiences. If it’s not something you would want to read/look at/use then why fucking bother? Intuition is fucking important. Design is all about fucking relationships—the relationship of form and content, the relationship of elements, the relationship of designer and user. Why are you fucking reading all of this? Get back to work. If you’re not being fucking honest with yourself how could you ever hope to communicate something meaningful to someone else? Never, never assume that what you have achieved is fucking good enough. A good fucking composition is the result of a hierarchy consisting of clearly contrasting elements set with distinct alignments containing irregular intervals of negative space. Someday is not a fucking day of the week. Make your work consistent but not fucking predictable. What’s important is the fucking drive to see a project through no matter what. This design is fucking brilliant. The details are not the details. They make the fucking design. Nothing of value comes to you without fucking working
</p> -->
<script src="https://code.jquery.com/jquery-3.1.0.slim.min.js" integrity="sha256-cRpWjoSOw5KcyIOaZNo4i6fZ9tKPhYYb6i5T9RSVJG8=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/qunit/2.0.1/qunit.js"></script>
<script src="../dist/truncated.js"></script>
Expand Down
10 changes: 9 additions & 1 deletion tests/tests.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
QUnit.test("select a paragraph with a class", function(assert) {
truncated('.test', 70);
truncated('.test', 50);
assert.equal(document.querySelectorAll('.js-truncated').length, 1, 'there should be 1 truncated thing');
});
QUnit.test("select a paragraph with an id & unique class", function(assert) {
truncated('#test', 70, '&hellip;', 'js-test');
assert.equal(document.querySelectorAll('.js-test').length, 1, 'there should be 1 truncated thing');
});
QUnit.test("select a paragraph with an id & no class & a special hellip", function(assert) {
truncated('#test-2', 70, '🐔', 'js-chicken');
assert.equal(document.querySelectorAll('.js-chicken').length, 1, 'there should be 1 truncated thing');
});

0 comments on commit addbfba

Please sign in to comment.