From 423084cc8b7a4b790ca5acce157f6615b6cc4fc3 Mon Sep 17 00:00:00 2001 From: Vincent Date: Sun, 4 Oct 2020 08:45:43 +0200 Subject: [PATCH 1/3] feat: add `slugify` and use const in `yesterday` --- snippets/slugify.md | 27 +++++++++++++++++++++++++++ snippets/yesterday.md | 2 +- 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 snippets/slugify.md diff --git a/snippets/slugify.md b/snippets/slugify.md new file mode 100644 index 000000000..4bd803463 --- /dev/null +++ b/snippets/slugify.md @@ -0,0 +1,27 @@ +--- +title: slugify +tags: function,intermediate +--- + +Convert a string to a URL-friendly slug. + +- Make the string lowercase and remove leading or trailing whitespace. +- Remove all characters that are not words, whitespace or hyphens. +- Replace whitespace with a single hyphen (`-`). +- Remove any leading or trailing hyphens. + +```js +const slugify = (string) => { + const slug = string + .toLowerCase() + .trim() + .replace(/[^\w\s-]/g, '') + .replace(/[\s_-]+/g, '-') + .replace(/^-+|-+$/g, ''); + return slug; +}; +``` + +```js +slugify('Hello World!'); // 'hello-world' +``` diff --git a/snippets/yesterday.md b/snippets/yesterday.md index b343d4cb7..8fd6d3088 100644 --- a/snippets/yesterday.md +++ b/snippets/yesterday.md @@ -10,7 +10,7 @@ Results in a string representation of yesterday's date. ```js const yesterday = () => { - let t = new Date(); + const t = new Date(); t.setDate(t.getDate() - 1); return t.toISOString().split('T')[0]; }; From 0c705c1936bec47cf274ff1efed080304d08126c Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sun, 4 Oct 2020 10:30:14 +0300 Subject: [PATCH 2/3] Update yesterday.md --- snippets/yesterday.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/yesterday.md b/snippets/yesterday.md index 8fd6d3088..b343d4cb7 100644 --- a/snippets/yesterday.md +++ b/snippets/yesterday.md @@ -10,7 +10,7 @@ Results in a string representation of yesterday's date. ```js const yesterday = () => { - const t = new Date(); + let t = new Date(); t.setDate(t.getDate() - 1); return t.toISOString().split('T')[0]; }; From 68dd9e2966d427ea8d34fbac291f6d60d254216d Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sun, 4 Oct 2020 10:36:38 +0300 Subject: [PATCH 3/3] Update slugify.md --- snippets/slugify.md | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/snippets/slugify.md b/snippets/slugify.md index 4bd803463..75b6962d2 100644 --- a/snippets/slugify.md +++ b/snippets/slugify.md @@ -1,25 +1,21 @@ --- title: slugify -tags: function,intermediate +tags: string,regexp,intermediate --- -Convert a string to a URL-friendly slug. +Converts a string to a URL-friendly slug. -- Make the string lowercase and remove leading or trailing whitespace. -- Remove all characters that are not words, whitespace or hyphens. -- Replace whitespace with a single hyphen (`-`). -- Remove any leading or trailing hyphens. +- Use `String.prototype.toLowerCase()` and `String.prototype.trim()` to normalize the string. +- Use `String.prototype.replace()` to replace spaces, dashes and underscores with `-` and remove special characters. ```js -const slugify = (string) => { - const slug = string +const slugify = str => + str .toLowerCase() .trim() .replace(/[^\w\s-]/g, '') .replace(/[\s_-]+/g, '-') .replace(/^-+|-+$/g, ''); - return slug; -}; ``` ```js