mirror of
https://github.com/strapi/strapi.git
synced 2025-12-25 14:14:10 +00:00
change common beginning to common path (#10509)
* change common beginning to common path * get common path * add more tests * add some space Co-authored-by: Pierre Noël <petersg83@users.noreply.github.com>
This commit is contained in:
parent
1fe4b5ecad
commit
7a80bdaa8e
@ -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);
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user