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

URL: Remove all of Lodash from the package #41687

Merged
merged 7 commits into from
Jun 14, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
7 changes: 6 additions & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion packages/url/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"sideEffects": false,
"dependencies": {
"@babel/runtime": "^7.16.0",
"lodash": "^4.17.21"
"remove-accents": "^0.4.2"
},
"publishConfig": {
"access": "public"
Expand Down
14 changes: 6 additions & 8 deletions packages/url/src/clean-for-slug.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { deburr, trim } from 'lodash';
import removeAccents from 'remove-accents';

/**
* Performs some basic cleanup of a string for use as a post slug.
Expand All @@ -23,11 +23,9 @@ export function cleanForSlug( string ) {
if ( ! string ) {
return '';
}
return trim(
deburr( string )
.replace( /[\s\./]+/g, '-' )
.replace( /[^\p{L}\p{N}_-]+/gu, '' )
.toLowerCase(),
'-'
);
return removeAccents( string )
Copy link
Contributor

@adamziel adamziel Jun 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this work too, @tyxla ?

const str = "Crème Brulée"
str.normalize("NFD").replace(/[\u0300-\u036f]/g, "")
> "Creme Brulee"

Found it on StackOverflow – perhaps we could do without the remove-accents dependency entirely? I saw it was based on a characters map so perhaps str.normalize would give us an even better coverage for free?

Copy link
Member Author

@tyxla tyxla Jun 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for asking @adamziel, it's actually a great question!

I'd be happy to go with that, it's always ideal to not rely on another package. The thing is, that solution works for most of the characters, but from what I've seen, it doesn't cover them all, or at least, doesn't cover as many as the package I've used covers.

Try this in your browser console - I've built it from the character map of the package:

"ÀÁÂÃÄÅẤẮẲẴẶÆẦẰȂÇḈÈÉÊËẾḖỀḔḜȆÌÍÎÏḮȊÐÑÒÓÔÕÖØỐṌṒȎÙÚÛÜÝàáâãäåấắẳẵặæầằȃçḉèéêëếḗềḕḝȇìíîïḯȋðñòóôõöøốṍṓȏùúûüýÿĀāĂ㥹ĆćĈĉĊċČčC̆c̆ĎďĐđĒēĔĕĖėĘęĚěĜǴĝǵĞğĠġĢģĤĥĦħḪḫĨĩĪīĬĭĮįİıIJijĴĵĶķḰḱK̆k̆ĹĺĻļĽľĿŀŁłḾḿM̆m̆ŃńŅņŇňʼnN̆n̆ŌōŎŏŐőŒœP̆p̆ŔŕŖŗŘřR̆r̆ȒȓŚśŜŝŞȘșşŠšŢţțȚŤťŦŧT̆t̆ŨũŪūŬŭŮůŰűŲųȖȗV̆v̆ŴŵẂẃX̆x̆ŶŷŸY̆y̆ŹźŻżŽžſƒƠơƯưǍǎǏǐǑǒǓǔǕǖǗǘǙǚǛǜỨứṸṹǺǻǼǽǾǿÞþṔṕṤṥX́x́ЃѓЌќA̋a̋E̋e̋I̋i̋ǸǹỒồṐṑỪừẀẁỲỳȀȁȄȅȈȉȌȍȐȑȔȕB̌b̌Č̣č̣Ê̌ê̌F̌f̌ǦǧȞȟJ̌ǰǨǩM̌m̌P̌p̌Q̌q̌Ř̩ř̩ṦṧV̌v̌W̌w̌X̌x̌Y̌y̌A̧a̧B̧b̧ḐḑȨȩƐ̧ɛ̧ḨḩI̧i̧Ɨ̧ɨ̧M̧m̧O̧o̧Q̧q̧U̧u̧X̧x̧Z̧z̧".normalize("NFD").replace(/[\u0300-\u036f]/g, "")

and you'll get:

AAAAAAAAAAAÆAAACCEEEEEEEEEEIIIIIIÐNOOOOOØOOOOUUUUYaaaaaaaaaaaæaaacceeeeeeeeeeiiiiiiðnoooooøoooouuuuyyAaAaAaCcCcCcCcCcDdĐđEeEeEeEeEeGGggGgGgGgHhĦħHhIiIiIiIiIıIJijJjKkKkKkLlLlLlĿŀŁłMmMmNnNnNnʼnNnOoOoOoŒœPpRrRrRrRrRrSsSsSSssSsTttTTtŦŧTtUuUuUuUuUuUuUuVvWwWwXxYyYYyZzZzZzſƒOoUuAaIiOoUuUuUuUuUuUuUuAaÆæØøÞþPpSsXxГгКкAaEeIiNnOoOoUuWwYyAaEeIiOoRrUuBbCcEeFfGgHhJjKkMmPpQqRrSsVvWwXxYyAaBbDdEeƐɛHhIiƗɨMmOoQqUuXxZz

where while the result is satisfactory, you will notice that there are quite a few that actually remain unchanged.

For the library above, we also used the corresponding WP function originally when compiling the character map, so this also gets us closer to a parity with the WordPress backend - a nice side benefit IMHO.

What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice test!

For the library above, we also used the corresponding WP function originally when compiling the character map, so this also gets us closer to a parity with the WordPress backend - a nice side benefit IMHO.

Alright, let's go with the package 👍

.replace( /[\s\./]+/g, '-' )
tyxla marked this conversation as resolved.
Show resolved Hide resolved
.replace( /[^\p{L}\p{N}_-]+/gu, '' )
.toLowerCase()
.replace( /(^-)|(-$)/g, '' );
tyxla marked this conversation as resolved.
Show resolved Hide resolved
}
65 changes: 31 additions & 34 deletions packages/url/src/test/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { every } from 'lodash';

/**
* Internal dependencies
*/
Expand Down Expand Up @@ -44,35 +39,37 @@ describe( 'isURL', () => {
} );

describe( 'isEmail', () => {
it( 'returns true when given things that look like an email', () => {
const emails = [
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
];

expect( every( emails, isEmail ) ).toBe( true );
} );

it( "returns false when given things that don't look like an email", () => {
const emails = [
'Abc.wordpress.org',
'A@b@[email protected]',
'a"b(c)d,e:f;g<h>i[jk][email protected]',
'just"not"[email protected]',
'this is"[email protected]',
'this still"not\\[email protected]',
'1234567890123456789012345678901234567890123456789012345678901234+x@wordpress.org',
];

expect( every( emails, isEmail ) ).toBe( false );
} );
it.each( [
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'1234567890123456789012345678901234567890123456789012345678901234+x@wordpress.org',
] )(
'returns true when given things that look like an email: %s',
( email ) => {
expect( isEmail( email ) ).toBe( true );
}
);

it.each( [
'Abc.wordpress.org',
'A@b@[email protected]',
'a"b(c)d,e:f;g<h>i[jk][email protected]',
'just"not"[email protected]',
'this is"[email protected]',
'this still"not\\[email protected]',
] )(
"returns false when given things that don't look like an email: %s",
( email ) => {
expect( isEmail( email ) ).toBe( false );
}
);
} );

describe( 'getProtocol', () => {
Expand Down