Add a maxLength option #2

Open
dusan.bratusa wants to merge 3 commits from feature/max-length into main

Cuts a long slug at the last separator before the limit.

Opened by Claude to test Pillar's review feedback.

Cuts a long slug at the last separator before the limit. Opened by Claude to test Pillar's review feedback.
dusan.bratusa left a comment

A first pass over the option:

  • the cut
  • the default
A first pass over the option: - the cut - the default
@ -57,3 +63,3 @@
export default function slugify(string, options) {
if (typeof string !== 'string') {
throw new TypeError(`Expected a string, got \`${typeof string}\``);
Author
Owner

The old message matched the wording elsewhere in the library; keep it?

The old message matched the wording elsewhere in the library; keep it?
@ -57,0 +57,4 @@
// Cut the slug to `maxLength` characters, preferring the last separator before the limit so a word is not split.
const truncateAtSeparator = (string, maxLength, separator) => {
const cut = string.slice(0, maxLength);
const lastSeparator = separator ? cut.lastIndexOf(separator) : -1;
Author
Owner

What if the separator is longer than one character, like --?

slugify("a b c", {separator: "--", maxLength: 4})
What if the separator is longer than one character, like `--`? ```js slugify("a b c", {separator: "--", maxLength: 4}) ```
Author
Owner

Good catch: it should split on the whole separator, not its first character.

Good catch: it should split on the whole separator, not its first character.
index.js Outdated
@ -68,6 +75,7 @@ export default function slugify(string, options) {
preserveTrailingDash: false,
preserveCharacters: [],
transliterate: true,
maxLength: Infinity,
Author
Owner

Should 0 mean no limit, as it does in some libraries?

Should `0` mean **no limit**, as it does in some libraries?
@ -154,0 +156,4 @@
Type: `number`\
Default: `Infinity`
The longest slug to return. A longer one is cut at the last separator before the limit, so words stay whole.
Author
Owner

Say what happens when there is no separator before the limit: the slug is cut mid-word.

Say what happens when there is no separator before the limit: the slug is cut mid-word.
@ -57,0 +57,4 @@
// Cut the slug to `maxLength` characters, preferring the last separator before the limit so a word is not split.
const truncateAtSeparator = (string, maxLength, separator) => {
const cut = string.slice(0, maxLength);
const lastSeparator = separator ? cut.lastIndexOf(separator) : -1;
Author
Owner

A second look at this line, in a review of its own.

A second look at this line, in a review of its own.
dusan.bratusa left a comment

Not submitted yet

Not submitted yet
@ -57,0 +59,4 @@
// Cut the slug to `maxLength` characters, preferring the last separator before the limit so a word is not split.
const truncateAtSeparator = (string, maxLength, separator) => {
const cut = string.slice(0, maxLength);
const lastSeparator = separator ? cut.lastIndexOf(separator) : -1;
Author
Owner

This moved two lines down; is it still right?

This moved two lines down; is it still right?
@ -57,0 +58,4 @@
const truncateAtSeparator = (string, maxLength, separator) => {
const cut = string.slice(0, maxLength);
const lastSeparator = separator ? cut.lastIndexOf(separator) : -1;
return lastSeparator > 0 ? cut.slice(0, lastSeparator) : cut;
Author
Owner

A draft thought that nobody else can see.

A draft thought that nobody else can see.
Author
Owner

Could this ship this week?

Could this ship **this week**?
dusan.bratusa left a comment

After the latest push

After the latest push
@ -57,0 +59,4 @@
// Cut the slug to `maxLength` characters, preferring the last separator before the limit so a word is not split.
const truncateAtSeparator = (string, maxLength, separator) => {
const cut = string.slice(0, maxLength);
const lastSeparator = separator ? cut.lastIndexOf(separator) : -1;
Author
Owner

This moved two lines down; is it still right?

This moved two lines down; is it still right?
@ -57,0 +58,4 @@
const truncateAtSeparator = (string, maxLength, separator) => {
const cut = string.slice(0, maxLength);
const lastSeparator = separator ? cut.lastIndexOf(separator) : -1;
return lastSeparator > 0 ? cut.slice(0, lastSeparator) : cut;
Author
Owner

A draft thought that nobody else can see.

A draft thought that nobody else can see.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin feature/max-length:feature/max-length
git switch feature/max-length

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch main
git merge --no-ff feature/max-length
git switch feature/max-length
git rebase main
git switch main
git merge --ff-only feature/max-length
git switch feature/max-length
git rebase main
git switch main
git merge --no-ff feature/max-length
git switch main
git merge --squash feature/max-length
git switch main
git merge --ff-only feature/max-length
git switch main
git merge feature/max-length
git push origin main
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
dusan.bratusa/test!2
No description provided.