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

Performance optimize option: splitEveryChars #46

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
21 changes: 19 additions & 2 deletions src/clamp.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
clamp: options.clamp || 2,
useNativeClamp: typeof(options.useNativeClamp) != 'undefined' ? options.useNativeClamp : true,
splitOnChars: options.splitOnChars || ['.', '-', '–', '—', ' '], //Split on sentences (periods), hypens, en-dashes, em-dashes, and words (spaces).
splitEveryChars: options.splitEveryChars || 0,
animate: options.animate || false,
truncationChar: options.truncationChar || '…',
truncationHTML: options.truncationHTML
Expand Down Expand Up @@ -189,20 +190,36 @@
lastChunk = null;
}

/**
* Split a string, at every nth position
*/
function splitEvery(str, n) {
var arr = new Array;
for (var i = 0; i < str.length; i += n)
{
arr.push(str.substr(i, n));
}
return arr;
}

var nodeValue = target.nodeValue.replace(opt.truncationChar, '');

//Grab the next chunks
if (!chunks) {
//If there are more characters to try, grab the next one
if (splitOnChars.length > 0) {
splitChar = splitOnChars.shift();
chunks = nodeValue.split(splitChar);
}
//No characters to chunk by. Go character-by-character
else {
splitChar = '';
if (opt.splitEveryChars) {
chunks = splitEvery(nodeValue, opt.splitEveryChars);
} else {
chunks = nodeValue.split(splitChar);
}
}

chunks = nodeValue.split(splitChar);
}

//If there are chunks left to remove, remove the last one and see if
Expand Down
3 changes: 3 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ export interface DotdotdotProps {
useNativeClamp?: boolean,
className?: string,
tagName?: string,
splitOnChars?: string[],
splitEveryChars?: number,
animate?: boolean,
}

export default class Dotdotdot extends React.Component<DotdotdotProps> { }
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Dotdotdot.prototype.dotdotdot = function(container) {
'animate',
'clamp',
'splitOnChars',
'splitEveryChars',
'truncationChar',
'truncationHTML',
'useNativeClamp'
Expand Down