Commit f6fea305 by Sam Padgett Committed by GitHub

Merge pull request #107 from cdcabrera/issue-truncate

Truncate Update
parents 329b46fe 965881b2
...@@ -1682,7 +1682,7 @@ angular.module('openshiftCommonUI') ...@@ -1682,7 +1682,7 @@ angular.module('openshiftCommonUI')
angular.module('openshiftCommonUI') angular.module('openshiftCommonUI')
.filter('truncate', function() { .filter('truncate', function() {
return function(str, charLimit, useWordBoundary, newlineLimit) { return function(str, charLimit, useWordBoundary, newlineLimit) {
if (!str) { if (typeof str !== 'string') {
return str; return str;
} }
...@@ -1698,11 +1698,10 @@ angular.module('openshiftCommonUI') ...@@ -1698,11 +1698,10 @@ angular.module('openshiftCommonUI')
} }
if (useWordBoundary !== false) { if (useWordBoundary !== false) {
// Find the last word break, but don't look more than 10 characters back.
// Make sure we show at least the first 5 characters.
var startIndex = Math.max(4, charLimit - 10); var startIndex = Math.max(4, charLimit - 10);
var lastSpace = truncated.lastIndexOf(/\s/, startIndex); var lastSpace = truncated.lastIndexOf(' ');
if (lastSpace !== -1) { if (lastSpace >= startIndex && lastSpace !== -1) {
truncated = truncated.substring(0, lastSpace); truncated = truncated.substring(0, lastSpace);
} }
} }
......
...@@ -1853,7 +1853,7 @@ angular.module('openshiftCommonUI') ...@@ -1853,7 +1853,7 @@ angular.module('openshiftCommonUI')
angular.module('openshiftCommonUI') angular.module('openshiftCommonUI')
.filter('truncate', function() { .filter('truncate', function() {
return function(str, charLimit, useWordBoundary, newlineLimit) { return function(str, charLimit, useWordBoundary, newlineLimit) {
if (!str) { if (typeof str !== 'string') {
return str; return str;
} }
...@@ -1869,11 +1869,10 @@ angular.module('openshiftCommonUI') ...@@ -1869,11 +1869,10 @@ angular.module('openshiftCommonUI')
} }
if (useWordBoundary !== false) { if (useWordBoundary !== false) {
// Find the last word break, but don't look more than 10 characters back.
// Make sure we show at least the first 5 characters.
var startIndex = Math.max(4, charLimit - 10); var startIndex = Math.max(4, charLimit - 10);
var lastSpace = truncated.lastIndexOf(/\s/, startIndex); var lastSpace = truncated.lastIndexOf(' ');
if (lastSpace !== -1) { if (lastSpace >= startIndex && lastSpace !== -1) {
truncated = truncated.substring(0, lastSpace); truncated = truncated.substring(0, lastSpace);
} }
} }
......
...@@ -719,15 +719,15 @@ return -1 === index ? !1 :ignoreTrailing ? index !== str.length - 1 :!0; ...@@ -719,15 +719,15 @@ return -1 === index ? !1 :ignoreTrailing ? index !== str.length - 1 :!0;
}; };
}), angular.module("openshiftCommonUI").filter("truncate", function() { }), angular.module("openshiftCommonUI").filter("truncate", function() {
return function(str, charLimit, useWordBoundary, newlineLimit) { return function(str, charLimit, useWordBoundary, newlineLimit) {
if (!str) return str; if ("string" != typeof str) return str;
var truncated = str; var truncated = str;
if (charLimit && (truncated = truncated.substring(0, charLimit)), newlineLimit) { if (charLimit && (truncated = truncated.substring(0, charLimit)), newlineLimit) {
var nthNewline = str.split("\n", newlineLimit).join("\n").length; var nthNewline = str.split("\n", newlineLimit).join("\n").length;
truncated = truncated.substring(0, nthNewline); truncated = truncated.substring(0, nthNewline);
} }
if (useWordBoundary !== !1) { if (useWordBoundary !== !1) {
var startIndex = Math.max(4, charLimit - 10), lastSpace = truncated.lastIndexOf(/\s/, startIndex); var startIndex = Math.max(4, charLimit - 10), lastSpace = truncated.lastIndexOf(" ");
-1 !== lastSpace && (truncated = truncated.substring(0, lastSpace)); lastSpace >= startIndex && -1 !== lastSpace && (truncated = truncated.substring(0, lastSpace));
} }
return truncated; return truncated;
}; };
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
angular.module('openshiftCommonUI') angular.module('openshiftCommonUI')
.filter('truncate', function() { .filter('truncate', function() {
return function(str, charLimit, useWordBoundary, newlineLimit) { return function(str, charLimit, useWordBoundary, newlineLimit) {
if (!str) { if (typeof str !== 'string') {
return str; return str;
} }
...@@ -19,11 +19,10 @@ angular.module('openshiftCommonUI') ...@@ -19,11 +19,10 @@ angular.module('openshiftCommonUI')
} }
if (useWordBoundary !== false) { if (useWordBoundary !== false) {
// Find the last word break, but don't look more than 10 characters back.
// Make sure we show at least the first 5 characters.
var startIndex = Math.max(4, charLimit - 10); var startIndex = Math.max(4, charLimit - 10);
var lastSpace = truncated.lastIndexOf(/\s/, startIndex); var lastSpace = truncated.lastIndexOf(' ');
if (lastSpace !== -1) { if (lastSpace >= startIndex && lastSpace !== -1) {
truncated = truncated.substring(0, lastSpace); truncated = truncated.substring(0, lastSpace);
} }
} }
......
describe("TruncateFilter", function() {
var $scope;
var $filter;
beforeEach(inject(function (_$rootScope_, _$filter_) {
$scope = _$rootScope_;
$filter = _$filter_;
}));
beforeEach(function () {
$scope.params = {
str: 'Operation cannot be fulfilled on namespaces\n"ups-broker": The system is ensuring all content is removed\nfrom this namespace. Upon completion, this\nnamespace will automatically be purged by the system.\n',
charLimit: 200,
useWordBoundary: false,
newlineLimit: null
};
});
it('should have a minimum of two parameters defined', function() {
var str = null;
var charLimit = null;
var useWordBoundary = $scope.params.useWordBoundary;
var newlineLimit = $scope.params.newlineLimit;
var filteredString = $filter('truncate')(str, charLimit, useWordBoundary, newlineLimit);
expect(str).toBeFalsy();
expect(charLimit).toBeFalsy();
expect(useWordBoundary).toEqual(false);
expect(newlineLimit).toBeFalsy();
expect(filteredString).toEqual(null);
});
it('should return a string with a specific length', function() {
var str = $scope.params.str;
var charLimit = $scope.params.charLimit;
var useWordBoundary = $scope.params.useWordBoundary;
var newlineLimit = $scope.params.newlineLimit;
var filteredString = $filter('truncate')(str, charLimit, useWordBoundary, newlineLimit);
expect(filteredString.length).toEqual(200);
});
it('should return a string that ends on a word boundary', function() {
var str = $scope.params.str;
var charLimit = $scope.params.charLimit;
var useWordBoundary = true;
var newlineLimit = $scope.params.newlineLimit;
var filteredString = $filter('truncate')(str, charLimit, useWordBoundary, newlineLimit);
expect(filteredString.length).toEqual(192);
expect(/^\s/.test(str.split(filteredString)[1])).toEqual(true);
});
it('should return a string with a limited number of new lines', function() {
var str = $scope.params.str;
var charLimit = $scope.params.charLimit;
var useWordBoundary = $scope.params.useWordBoundary;
var newlineLimit = 1;
var filteredString = $filter('truncate')(str, charLimit, useWordBoundary, newlineLimit);
expect((filteredString.match(/\n/g)||[]).length).toEqual(0);
newlineLimit = 2;
filteredString = $filter('truncate')(str, charLimit, useWordBoundary, newlineLimit);
expect((filteredString.match(/\n/g)||[]).length).toEqual(1);
newlineLimit = 3;
filteredString = $filter('truncate')(str, charLimit, useWordBoundary, newlineLimit);
expect((filteredString.match(/\n/g)||[]).length).toEqual(2);
newlineLimit = 4;
filteredString = $filter('truncate')(str, charLimit, useWordBoundary, newlineLimit);
expect((filteredString.match(/\n/g)||[]).length).toEqual(3);
});
it('should return a string that both ends on a word boundary, and has a limited number of new lines', function() {
var str = $scope.params.str;
var charLimit = $scope.params.charLimit;
var useWordBoundary = true;
var newlineLimit = 3;
var filteredString = $filter('truncate')(str, charLimit, useWordBoundary, newlineLimit);
expect(filteredString.length).toEqual(146);
expect(/^\s/.test(str.split(filteredString)[1])).toEqual(true);
expect((filteredString.match(/\n/g)||[]).length).toEqual(2);
});
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment