Skip to content

Commit

Permalink
Merge pull request #108 from davidchin/unicode_substr
Browse files Browse the repository at this point in the history
Fix the truncation of unicode strings
  • Loading branch information
davidchin authored Feb 20, 2017
2 parents 37453c9 + dc0f330 commit 3fd5974
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
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();
});
});

0 comments on commit 3fd5974

Please sign in to comment.