From 1fe4b5ecad162e487fa7dd26a9422569915b4d9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20de=20Juvigny?= Date: Thu, 12 Aug 2021 18:44:20 +0200 Subject: [PATCH 1/2] Remove netlify mention from readme (#10725) --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ba47a45d8e..e2fe84aa6e 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ Strapi is a free and open-source headless CMS delivering your content anywhere you need. - **Keep control over your data**. With Strapi, you know where your data is stored, and you keep full control at all times. -- **Self-hosted**. You can host and scale Strapi projects the way you want. You can choose any hosting platform you want: AWS, Render, Netlify, Heroku, a VPS, or a dedicated server. You can scale as you grow, 100% independent. +- **Self-hosted**. You can host and scale Strapi projects the way you want. You can choose any hosting platform you want: AWS, Render, Heroku, a VPS, or a dedicated server. You can scale as you grow, 100% independent. - **Database agnostic**. You can choose the database you prefer. Strapi works with SQL & NoSQL databases: MongoDB, PostgreSQL, MySQL, MariaDB, and SQLite. - **Customizable**. You can quickly build your logic by fully customizing APIs, routes, or plugins to fit your needs perfectly. From 7a80bdaa8e4e4b194853b338a30495c8a1050f1e Mon Sep 17 00:00:00 2001 From: GQ Date: Tue, 17 Aug 2021 19:25:14 +0800 Subject: [PATCH 2/2] change common beginning to common path (#10509) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * change common beginning to common path * get common path * add more tests * add some space Co-authored-by: Pierre Noël --- .../lib/__tests__/string-formatting.test.js | 30 ++++++++++++++++++- packages/strapi-utils/lib/config.js | 4 +-- .../strapi-utils/lib/string-formatting.js | 25 ++++++++-------- 3 files changed, 43 insertions(+), 16 deletions(-) diff --git a/packages/strapi-utils/lib/__tests__/string-formatting.test.js b/packages/strapi-utils/lib/__tests__/string-formatting.test.js index fc7c3c0159..e90e462a5a 100644 --- a/packages/strapi-utils/lib/__tests__/string-formatting.test.js +++ b/packages/strapi-utils/lib/__tests__/string-formatting.test.js @@ -1,6 +1,6 @@ 'use strict'; -const { escapeQuery, stringIncludes, stringEquals } = require('../string-formatting'); +const { escapeQuery, stringIncludes, stringEquals, getCommonBeginning, getCommonPath } = require('../string-formatting'); describe('string-formatting', () => { describe('Escape Query', () => { @@ -64,4 +64,32 @@ describe('string-formatting', () => { expect(result).toBe(expectedResult); }); }); + + describe('getCommonBeginning', () => { + const tests = [ + [['abcd', 'abc', 'ab'], 'ab'], + [['abcd', 'abc'], 'abc'], + [['ab/cd', 'ab/c'], 'ab/c'], + [['abc', 'abc'], 'abc'], + ]; + test.each(tests)('%p has common beginning: %p', (a, expectedResult) => { + const result = getCommonBeginning(...a); + expect(result).toBe(expectedResult); + }); + }); + + describe('getCommonPath', () => { + const tests = [ + [['abc', 'ab'], ''], + [['http://ab.com/cd', 'http://ab.com/c'], 'http://ab.com'], + [['http://ab.com/admin', 'http://ab.com/api'], 'http://ab.com'], + [['http://ab.com/admin', 'http://ab.com/admin/'], 'http://ab.com/admin'], + [['http://ab.com/admin', 'http://ab.com/admin'], 'http://ab.com/admin'], + ]; + test.each(tests)('%p has common path: %p', (a, expectedResult) => { + const result = getCommonPath(...a); + expect(result).toBe(expectedResult); + }); + }); + }); diff --git a/packages/strapi-utils/lib/config.js b/packages/strapi-utils/lib/config.js index b36e746706..0e1f2ae1d5 100644 --- a/packages/strapi-utils/lib/config.js +++ b/packages/strapi-utils/lib/config.js @@ -1,7 +1,7 @@ 'use strict'; const _ = require('lodash'); -const { getCommonBeginning } = require('./string-formatting'); +const { getCommonPath } = require('./string-formatting'); const getConfigUrls = (serverConfig, forAdminBuild = false) => { // Defines serverUrl value @@ -46,7 +46,7 @@ const getConfigUrls = (serverConfig, forAdminBuild = false) => { new URL(adminUrl).origin === new URL(serverUrl).origin && !forAdminBuild ) { - adminPath = adminUrl.replace(getCommonBeginning(serverUrl, adminUrl), ''); + adminPath = adminUrl.replace(getCommonPath(serverUrl, adminUrl), ''); adminPath = `/${_.trim(adminPath, '/')}`; } else if (adminUrl.startsWith('http')) { adminPath = new URL(adminUrl).pathname; diff --git a/packages/strapi-utils/lib/string-formatting.js b/packages/strapi-utils/lib/string-formatting.js index 1ee95f2874..250aaf4b47 100644 --- a/packages/strapi-utils/lib/string-formatting.js +++ b/packages/strapi-utils/lib/string-formatting.js @@ -1,23 +1,21 @@ 'use strict'; - +const _ = require('lodash'); const slugify = require('@sindresorhus/slugify'); const nameToSlug = (name, options = { separator: '-' }) => slugify(name, options); const nameToCollectionName = name => slugify(name, { separator: '_' }); -const getCommonBeginning = (str1 = '', str2 = '') => { - let common = ''; - let index = 0; - while (index < str1.length && index < str2.length) { - if (str1[index] === str2[index]) { - common += str1[index]; - index += 1; - } else { - break; - } - } - return common; +const getCommonBeginning = (...strings) => _.takeWhile( + strings[0], + (char, index) => strings.every(string => string[index] === char) +).join(''); + +const getCommonPath = (...paths) => { + const [segments, ...otherSegments] = paths.map(it => _.split(it, '/')); + return _.join( + _.takeWhile(segments, (str, index) => otherSegments.every(it => it[index] === str)) + , '/'); }; const escapeQuery = (query, charsToEscape, escapeChar = '\\') => { @@ -39,6 +37,7 @@ module.exports = { nameToSlug, nameToCollectionName, getCommonBeginning, + getCommonPath, escapeQuery, stringIncludes, stringEquals,