Skip to content

Commit

Permalink
Allow custom separator for strings
Browse files Browse the repository at this point in the history
  • Loading branch information
GoudekettingRM committed Oct 10, 2023
1 parent bc90385 commit 466ea15
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 57 deletions.
5 changes: 5 additions & 0 deletions .changeset/late-dancers-whisper.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"titleizejs": minor
---

Allow custom separator for slugs
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ test/
titleize.js
_config.yml
index.md
.changeset
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ titleize('i-am-a-slug'); // result: I-Am-a-Slug
titleize('i-am-a-slug', { isSlug: true }); // result: I Am a Slug
```

If you happen to have slugs that are use different separators, you can pass the separator for your string to `isSlug` and it will replace those. E.g.:

```js
titleize('i_am_a_slug', { isSlug: '_' }) // result: I Am a Slug
```

#### Support, bug-reports, feature suggestions

If you want to contribute, report a bug, or have a suggestion for a feature, contact me at [email protected] or contact me via Github or Twitter.
48 changes: 20 additions & 28 deletions test/stringTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,42 +19,31 @@ module.exports = [
},
{
it: 'should not capitalise any prepositions',
input:
'we are conjunctions via to on per for in of by at as yet so you see',
expectedOutput:
'We Are Conjunctions via to on per for in of by at as yet so You See',
input: 'we are conjunctions via to on per for in of by at as yet so you see',
expectedOutput: 'We Are Conjunctions via to on per for in of by at as yet so You See',
},
{
it:
'should always capitalise the first and last word of a sentence 1/2',
input:
'we are conjunctions via to on per for in of by at as yet so you see',
expectedOutput:
'We Are Conjunctions via to on per for in of by at as yet so You See',
it: 'should always capitalise the first and last word of a sentence 1/2',
input: 'we are conjunctions via to on per for in of by at as yet so you see',
expectedOutput: 'We Are Conjunctions via to on per for in of by at as yet so You See',
},
{
it:
'should always capitalise the first and last word of a sentence 2/2',
input:
'or we are conjunctions via to on per for in of by at as yet so you see and',
expectedOutput:
'Or We Are Conjunctions via to on per for in of by at as yet so You See And',
it: 'should always capitalise the first and last word of a sentence 2/2',
input: 'or we are conjunctions via to on per for in of by at as yet so you see and',
expectedOutput: 'Or We Are Conjunctions via to on per for in of by at as yet so You See And',
},
{
it:
'should capitalise the first letter of both words of a hyphenated word 1/3',
it: 'should capitalise the first letter of both words of a hyphenated word 1/3',
input: 'this sentence is self-explanatory',
expectedOutput: 'This Sentence Is Self-Explanatory',
},
{
it:
'should capitalise the first letter of both words of a hyphenated word 2/3',
it: 'should capitalise the first letter of both words of a hyphenated word 2/3',
input: 'self-explanatory is this sentence',
expectedOutput: 'Self-Explanatory Is This Sentence',
},
{
it:
'should capitalise the first letter of both words of a hyphenated word 3/3',
it: 'should capitalise the first letter of both words of a hyphenated word 3/3',
input: 'this sentence self-explanatory is',
expectedOutput: 'This Sentence Self-Explanatory Is',
},
Expand Down Expand Up @@ -87,15 +76,13 @@ module.exports = [
describe: 'keepUpperCaseLetters exceptions',
tests: [
{
it:
'should keep the upper case letters that are present in the provided string 1/2',
it: 'should keep the upper case letters that are present in the provided string 1/2',
input: 'This string will contain soME uPPerCase letters!',
setting: { keepUpperCaseLetters: true },
expectedOutput: 'This String Will Contain SoME UPPerCase Letters',
},
{
it:
'should keep the upper case letters that are present in the provided string 2/2',
it: 'should keep the upper case letters that are present in the provided string 2/2',
input: 'This string WILL Contain soME uPPerCase letters!',
setting: { keepUpperCaseLetters: true },
expectedOutput: 'This String WILL Contain SoME UPPerCase Letters',
Expand All @@ -112,8 +99,7 @@ module.exports = [
expectedOutput: 'This String WILL Contain Some Uppercase Letters',
},
{
it:
'should make the words lower case if there is one or more letters lower case in it',
it: 'should make the words lower case if there is one or more letters lower case in it',
input: 'THIs STRIng WILL CONTain some UPPERCASE LETters!',
setting: { keepUpperCaseWords: true },
expectedOutput: 'This String WILL Contain Some UPPERCASE Letters',
Expand All @@ -135,6 +121,12 @@ module.exports = [
setting: { isSlug: true },
expectedOutput: 'I Am Not a Slug for Your Url',
},
{
it: 'should de-slugify a slug with a custom separator',
input: 'i_am_a_slug_for_your_url',
setting: { isSlug: '_' },
expectedOutput: 'I Am a Slug for Your Url',
},
],
},
];
21 changes: 6 additions & 15 deletions titleize.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,13 @@ const titleize = (value, exceptions = {}) => {
}
}
const symbolsForRegex = exceptions.ignoreSymbols
? cleanseSymbolList(
exceptions.ignoreSymbols,
symbols.toString().slice(2, -2),
)
? cleanseSymbolList(exceptions.ignoreSymbols, symbols.toString().slice(2, -2))
: symbols.toString().slice(2, -2);

const symbolRegExp = new RegExp('[' + symbolsForRegex + ']', 'gi');

if (exceptions.isSlug) {
value = value.replace(/-/g, ' ');
value = typeof exceptions.isSlug === 'string' ? value.replaceAll(exceptions.isSlug, ' ') : value.replace(/-/g, ' ');
}

return value
Expand All @@ -89,7 +86,7 @@ const titleize = (value, exceptions = {}) => {
if (word) return word;
})
.map((word, index, array) => {
const regex = /([\w+])-([\w+])/gi
const regex = /([\w+])-([\w+])/gi;
const matches = [];
while (true) {
const match = regex.exec(word);
Expand All @@ -116,17 +113,11 @@ const titleize = (value, exceptions = {}) => {
(word.indexOf('-') !== -1 &&
word.length >= 3 &&
// !word.match(/(?<=[\w+])-(?=[\w+])/gi)
!matches.length
)
!matches.length)
) {
const v = word.replace(/-/g, '');
if (v.length > 0) {
return getWordForTitle(
word.replace(/-/g, ''),
index,
array,
exceptions,
);
return getWordForTitle(word.replace(/-/g, ''), index, array, exceptions);
}
} else {
return getWordForTitle(word, index, array, exceptions);
Expand All @@ -144,5 +135,5 @@ module.exports = titleize;
(ಠ _ ಠ)
_____(_____)____
| Robin |
| Goudeketting_|
|_Goudeketting_|
*/
18 changes: 4 additions & 14 deletions titleize.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 466ea15

Please sign in to comment.