Skip to content

Commit

Permalink
refactor review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
OzakIOne committed Mar 29, 2024
1 parent 9734e79 commit 987472d
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 62 deletions.
3 changes: 1 addition & 2 deletions packages/docusaurus-plugin-client-redirects/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

import {removePrefix} from '@docusaurus/utils';
import {addLeadingSlash} from '@docusaurus/utils-common';
import {addLeadingSlash, removePrefix} from '@docusaurus/utils-common';
import collectRedirects from './collectRedirects';
import writeRedirectFiles, {
toRedirectFiles,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import applyTrailingSlash, {
addTrailingSlash,
type ApplyTrailingSlashParams,
addLeadingSlash,
removeSuffix,
removeTrailingSlash,
} from '../applyTrailingSlash';

Expand Down Expand Up @@ -207,17 +206,3 @@ describe('removeTrailingSlash', () => {
expect(removeTrailingSlash('/abcd/')).toBe('/abcd');
});
});

describe('removeSuffix', () => {
it("is no-op when suffix doesn't exist", () => {
expect(removeSuffix('abcdef', 'ijk')).toBe('abcdef');
expect(removeSuffix('abcdef', 'abc')).toBe('abcdef');
expect(removeSuffix('abcdef', '')).toBe('abcdef');
});
it('removes suffix', () => {
expect(removeSuffix('abcdef', 'ef')).toBe('abcd');
});
it('removes empty suffix', () => {
expect(removeSuffix('abcdef', '')).toBe('abcdef');
});
});
55 changes: 55 additions & 0 deletions packages/docusaurus-utils-common/src/__tests__/stringUtils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import {addPrefix, addSuffix, removePrefix, removeSuffix} from '../stringUtils';

describe('removePrefix', () => {
it("is no-op when prefix doesn't exist", () => {
expect(removePrefix('abcdef', 'ijk')).toBe('abcdef');
expect(removePrefix('abcdef', 'def')).toBe('abcdef');
expect(removePrefix('abcdef', '')).toBe('abcdef');
});
it('removes prefix', () => {
expect(removePrefix('prefix', 'pre')).toBe('fix');
});
});

describe('removeSuffix', () => {
it("is no-op when suffix doesn't exist", () => {
expect(removeSuffix('abcdef', 'ijk')).toBe('abcdef');
expect(removeSuffix('abcdef', 'abc')).toBe('abcdef');
expect(removeSuffix('abcdef', '')).toBe('abcdef');
});
it('removes suffix', () => {
expect(removeSuffix('abcdef', 'ef')).toBe('abcd');
});
it('removes empty suffix', () => {
expect(removeSuffix('abcdef', '')).toBe('abcdef');
});
});

describe('addPrefix', () => {
it('is no-op when prefix already exists', () => {
expect(addPrefix('abcdef', 'abc')).toBe('abcdef');
expect(addPrefix('abc', '')).toBe('abc');
expect(addPrefix('', '')).toBe('');
});
it('adds prefix', () => {
expect(addPrefix('def', 'abc')).toBe('abcdef');
});
});

describe('addSuffix', () => {
it('is no-op when suffix already exists', () => {
expect(addSuffix('abcdef', 'def')).toBe('abcdef');
expect(addSuffix('abc', '')).toBe('abc');
expect(addSuffix('', '')).toBe('');
});
it('adds suffix', () => {
expect(addSuffix('abc', 'def')).toBe('abcdef');
});
});
15 changes: 1 addition & 14 deletions packages/docusaurus-utils-common/src/applyTrailingSlash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

import {addPrefix, removeSuffix} from './stringUtils';
import type {DocusaurusConfig} from '@docusaurus/types';

export type ApplyTrailingSlashParams = Pick<
Expand Down Expand Up @@ -53,20 +54,6 @@ export default function applyTrailingSlash(
return path.replace(pathname, newPathname);
}

/** Adds a given string prefix to `str`. */
export function addPrefix(str: string, prefix: string): string {
return str.startsWith(prefix) ? str : `${prefix}${str}`;
}

/** Removes a given string suffix from `str`. */
export function removeSuffix(str: string, suffix: string): string {
if (suffix === '') {
// str.slice(0, 0) is ""
return str;
}
return str.endsWith(suffix) ? str.slice(0, -suffix.length) : str;
}

/** Appends a leading slash to `str`, if one doesn't exist. */
export function addLeadingSlash(str: string): string {
return addPrefix(str, '/');
Expand Down
3 changes: 1 addition & 2 deletions packages/docusaurus-utils-common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ export {
default as applyTrailingSlash,
addTrailingSlash,
addLeadingSlash,
addPrefix,
removeSuffix,
removeTrailingSlash,
type ApplyTrailingSlashParams,
} from './applyTrailingSlash';
export {addPrefix, removeSuffix, addSuffix, removePrefix} from './stringUtils';
export {getErrorCausalChain} from './errorUtils';
30 changes: 30 additions & 0 deletions packages/docusaurus-utils-common/src/stringUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

/** Adds a given string prefix to `str`. */
export function addPrefix(str: string, prefix: string): string {
return str.startsWith(prefix) ? str : `${prefix}${str}`;
}

/** Removes a given string suffix from `str`. */
export function removeSuffix(str: string, suffix: string): string {
if (suffix === '') {
// str.slice(0, 0) is ""
return str;
}
return str.endsWith(suffix) ? str.slice(0, -suffix.length) : str;
}

/** Adds a given string suffix to `str`. */
export function addSuffix(str: string, suffix: string): string {
return str.endsWith(suffix) ? str : `${str}${suffix}`;
}

/** Removes a given string prefix from `str`. */
export function removePrefix(str: string, prefix: string): string {
return str.startsWith(prefix) ? str.slice(prefix.length) : str;
}
1 change: 1 addition & 0 deletions packages/docusaurus-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"license": "MIT",
"dependencies": {
"@docusaurus/logger": "3.0.0",
"@docusaurus/utils-common": "3.0.0",
"@svgr/webpack": "^6.5.1",
"escape-string-regexp": "^4.0.0",
"file-loader": "^6.2.0",
Expand Down
17 changes: 1 addition & 16 deletions packages/docusaurus-utils/src/__tests__/jsUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,7 @@

import {jest} from '@jest/globals';
import _ from 'lodash';
import {
removePrefix,
mapAsyncSequential,
findAsyncSequential,
} from '../jsUtils';

describe('removePrefix', () => {
it("is no-op when prefix doesn't exist", () => {
expect(removePrefix('abcdef', 'ijk')).toBe('abcdef');
expect(removePrefix('abcdef', 'def')).toBe('abcdef');
expect(removePrefix('abcdef', '')).toBe('abcdef');
});
it('removes prefix', () => {
expect(removePrefix('prefix', 'pre')).toBe('fix');
});
});
import {mapAsyncSequential, findAsyncSequential} from '../jsUtils';

describe('mapAsyncSequential', () => {
function sleep(timeout: number): Promise<void> {
Expand Down
3 changes: 1 addition & 2 deletions packages/docusaurus-utils/src/globUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@

import path from 'path';
import Micromatch from 'micromatch'; // Note: Micromatch is used by Globby
import {addSuffix} from './jsUtils';

import {addSuffix} from '@docusaurus/utils-common';
/** A re-export of the globby instance. */
export {default as Globby} from 'globby';

Expand Down
2 changes: 1 addition & 1 deletion packages/docusaurus-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export {
getPluginI18nPath,
localizePath,
} from './i18nUtils';
export {removePrefix, mapAsyncSequential, findAsyncSequential} from './jsUtils';
export {mapAsyncSequential, findAsyncSequential} from './jsUtils';
export {
normalizeUrl,
getEditUrl,
Expand Down
10 changes: 0 additions & 10 deletions packages/docusaurus-utils/src/jsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,6 @@
* LICENSE file in the root directory of this source tree.
*/

/** Adds a given string suffix to `str`. */
export function addSuffix(str: string, suffix: string): string {
return str.endsWith(suffix) ? str : `${str}${suffix}`;
}

/** Removes a given string prefix from `str`. */
export function removePrefix(str: string, prefix: string): string {
return str.startsWith(prefix) ? str.slice(prefix.length) : str;
}

/**
* `Array#map` for async operations where order matters.
* @param array The array to traverse.
Expand Down

0 comments on commit 987472d

Please sign in to comment.