Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the truncation of unicode strings #108

Merged
merged 1 commit into from
Feb 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions helpers/truncate.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,37 @@
'use strict';

const substring = require('stringz').substring;

/* 2017-02-14
*
*
* FUNCTION
* truncate(str, length)
*
* DESCRIPTION (WHAT)
* Returns the first X characters in a string (unless it reaches the end
* of the string first, in which case it will return fewer). Returns a
* Returns the first X characters in a string (unless it reaches the end
* of the string first, in which case it will return fewer). Returns a
* new string that is truncated to the given length.
*
* USE CASE (WHY)
* As a merchant, I want to display a card, at the bottom of
* my home page that highlights the most recent blog post. In the card,
* I want to display the blog thumbnail, title and the first 40 characters
* of the post's body. In order to extract the first 40 characters, I need
* a Handlebars helper that works like the javascript substring() function.
*
* a Handlebars helper that works like the javascript substring() function.
*
* USAGE
*
* {{lang (truncate 'blog.post.body.' 40) }}
*/
function helper(paper) {
paper.handlebars.registerHelper('truncate', function (s, length) {
if (typeof s !== 'string') {
return s;
paper.handlebars.registerHelper('truncate', function (string, length) {
if (typeof string !== 'string') {
return string;
}
var str = s.substring(0, length)
return new paper.handlebars.SafeString(str);

const truncatedString = substring(string, 0, length);

return new paper.handlebars.SafeString(truncatedString);
});
}

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"hoek": "^2.12.0",
"lodash": "^3.6.0",
"messageformat": "^0.2.2",
"stringz": "^0.1.1",
"tarjan-graph": "^0.3.0"
},
"devDependencies": {
Expand Down
24 changes: 23 additions & 1 deletion test/helpers/truncate.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ function c(template, context) {
describe('truncate helper', function() {

var context = {
chinese_string: '𠜎𠜱𠝹𠱓𠱸𠲖𠳏',
number: 2,
spanish_string: 'mañana',
string: 'hello world',
number: 2
unicode_string: 'She ❤️️ this',
};

it('should return the entire string if length is longer than the input string', function(done) {
Expand All @@ -37,4 +40,23 @@ describe('truncate helper', function() {
.to.be.equal('2');
done();
});

it('should handle non-English strings', function(done) {

expect(c('{{truncate spanish_string 3}}', context))
.to.be.equal('mañ');

expect(c('{{truncate chinese_string 3}}', context))
.to.be.equal('𠜎𠜱𠝹');

done();
});

it('should handle unicode strings', function(done) {

expect(c('{{truncate unicode_string 5}}', context))
.to.be.equal('She ❤️');

done();
});
});