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

Split by Delimiters Passed To Shave Function #412

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
14 changes: 10 additions & 4 deletions src/shave.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export type Opts = {
spaces?: boolean
charclassname?: string
link?: Link
delimiters?: string
}

function generateArrayOfNodes(target: string | NodeList | Node): Array<Node> {
Expand Down Expand Up @@ -36,6 +37,7 @@ export default function shave(target: string | NodeList | Node, maxHeight: numbe
spaces: initialSpaces = true,
charclassname = 'js-shave-char',
link = {},
delimiters = ' ',
} = opts

/**
Expand Down Expand Up @@ -71,7 +73,7 @@ export default function shave(target: string | NodeList | Node, maxHeight: numbe
}

const fullText = el[textProp]
const words: string | string[] = spaces ? fullText.split(' ') : fullText
const words: string | string[] = spaces ? fullText.split(new RegExp('(?=[' + delimiters + ']+)|(?<=[' + delimiters + ']+)', 'g')) : fullText;
// If 0 or 1 words, we're done
if (words.length < 2) {
continue
Expand Down Expand Up @@ -116,7 +118,7 @@ export default function shave(target: string | NodeList | Node, maxHeight: numbe
pivot = (min + max + 1) >> 1 // eslint-disable-line no-bitwise
const wordItems = words.slice(0, pivot);
el[textProp] = spaces
? (wordItems as string[]).join(' ') as string
? (wordItems as string[]).join('') as string
: wordItems as string;
el.insertAdjacentElement('beforeend', shavedTextEl)
if (el.offsetHeight > maxHeight) {
Expand All @@ -126,11 +128,15 @@ export default function shave(target: string | NodeList | Node, maxHeight: numbe
}
}
const wordeItems = words.slice(0, max)
el[textProp] = spaces ? ((wordeItems as string[]).join(' ') as string) : wordeItems as string
// This removes trailing delimiters from the array before reconstructing the string.
while (wordeItems.length > 0 && wordeItems[wordeItems.length - 1].match(new RegExp('[' + delimiters + ']+', 'g'))) {
wordeItems.pop();
}
el[textProp] = spaces ? ((wordeItems as string[]).join('') as string) : wordeItems as string
el.insertAdjacentElement('beforeend', shavedTextEl)
const diffItems = words.slice(max)
const diff: string = spaces
? ' ' + (diffItems as string[]).join(' ')
? ' ' + (diffItems as string[]).join('')
: diffItems as string;
const shavedText = document.createTextNode(diff)
const elWithShavedText = document.createElement('span')
Expand Down