From 987472d9a914333fa95982d968e78ddddb41032e Mon Sep 17 00:00:00 2001 From: ozakione <29860391+OzakIOne@users.noreply.github.com> Date: Fri, 29 Mar 2024 12:01:26 +0100 Subject: [PATCH] refactor review comments --- .../src/index.ts | 3 +- .../src/__tests__/applyTrailingSlash.test.ts | 15 ----- .../src/__tests__/stringUtils.test.ts | 55 +++++++++++++++++++ .../src/applyTrailingSlash.ts | 15 +---- packages/docusaurus-utils-common/src/index.ts | 3 +- .../src/stringUtils.ts | 30 ++++++++++ packages/docusaurus-utils/package.json | 1 + .../src/__tests__/jsUtils.test.ts | 17 +----- packages/docusaurus-utils/src/globUtils.ts | 3 +- packages/docusaurus-utils/src/index.ts | 2 +- packages/docusaurus-utils/src/jsUtils.ts | 10 ---- 11 files changed, 92 insertions(+), 62 deletions(-) create mode 100644 packages/docusaurus-utils-common/src/__tests__/stringUtils.test.ts create mode 100644 packages/docusaurus-utils-common/src/stringUtils.ts diff --git a/packages/docusaurus-plugin-client-redirects/src/index.ts b/packages/docusaurus-plugin-client-redirects/src/index.ts index 80612b23b22c..e7aa97961f37 100644 --- a/packages/docusaurus-plugin-client-redirects/src/index.ts +++ b/packages/docusaurus-plugin-client-redirects/src/index.ts @@ -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, diff --git a/packages/docusaurus-utils-common/src/__tests__/applyTrailingSlash.test.ts b/packages/docusaurus-utils-common/src/__tests__/applyTrailingSlash.test.ts index 225d235e07e7..c86013f7fd37 100644 --- a/packages/docusaurus-utils-common/src/__tests__/applyTrailingSlash.test.ts +++ b/packages/docusaurus-utils-common/src/__tests__/applyTrailingSlash.test.ts @@ -9,7 +9,6 @@ import applyTrailingSlash, { addTrailingSlash, type ApplyTrailingSlashParams, addLeadingSlash, - removeSuffix, removeTrailingSlash, } from '../applyTrailingSlash'; @@ -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'); - }); -}); diff --git a/packages/docusaurus-utils-common/src/__tests__/stringUtils.test.ts b/packages/docusaurus-utils-common/src/__tests__/stringUtils.test.ts new file mode 100644 index 000000000000..9d01520300bf --- /dev/null +++ b/packages/docusaurus-utils-common/src/__tests__/stringUtils.test.ts @@ -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'); + }); +}); diff --git a/packages/docusaurus-utils-common/src/applyTrailingSlash.ts b/packages/docusaurus-utils-common/src/applyTrailingSlash.ts index 172857c43e3b..f83e5ce9c076 100644 --- a/packages/docusaurus-utils-common/src/applyTrailingSlash.ts +++ b/packages/docusaurus-utils-common/src/applyTrailingSlash.ts @@ -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< @@ -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, '/'); diff --git a/packages/docusaurus-utils-common/src/index.ts b/packages/docusaurus-utils-common/src/index.ts index 504e834d2732..0c4aae428147 100644 --- a/packages/docusaurus-utils-common/src/index.ts +++ b/packages/docusaurus-utils-common/src/index.ts @@ -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'; diff --git a/packages/docusaurus-utils-common/src/stringUtils.ts b/packages/docusaurus-utils-common/src/stringUtils.ts new file mode 100644 index 000000000000..29dea13c3210 --- /dev/null +++ b/packages/docusaurus-utils-common/src/stringUtils.ts @@ -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; +} diff --git a/packages/docusaurus-utils/package.json b/packages/docusaurus-utils/package.json index b3fa5dee583d..9f7adc9cbe3b 100644 --- a/packages/docusaurus-utils/package.json +++ b/packages/docusaurus-utils/package.json @@ -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", diff --git a/packages/docusaurus-utils/src/__tests__/jsUtils.test.ts b/packages/docusaurus-utils/src/__tests__/jsUtils.test.ts index f45d2efbfc0c..fb3750e0eae7 100644 --- a/packages/docusaurus-utils/src/__tests__/jsUtils.test.ts +++ b/packages/docusaurus-utils/src/__tests__/jsUtils.test.ts @@ -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 { diff --git a/packages/docusaurus-utils/src/globUtils.ts b/packages/docusaurus-utils/src/globUtils.ts index 777eb84ccc50..72b65d75d7f9 100644 --- a/packages/docusaurus-utils/src/globUtils.ts +++ b/packages/docusaurus-utils/src/globUtils.ts @@ -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'; diff --git a/packages/docusaurus-utils/src/index.ts b/packages/docusaurus-utils/src/index.ts index c793de4afabb..405da5258dd4 100644 --- a/packages/docusaurus-utils/src/index.ts +++ b/packages/docusaurus-utils/src/index.ts @@ -35,7 +35,7 @@ export { getPluginI18nPath, localizePath, } from './i18nUtils'; -export {removePrefix, mapAsyncSequential, findAsyncSequential} from './jsUtils'; +export {mapAsyncSequential, findAsyncSequential} from './jsUtils'; export { normalizeUrl, getEditUrl, diff --git a/packages/docusaurus-utils/src/jsUtils.ts b/packages/docusaurus-utils/src/jsUtils.ts index 6564c6e2f301..240798b7ef82 100644 --- a/packages/docusaurus-utils/src/jsUtils.ts +++ b/packages/docusaurus-utils/src/jsUtils.ts @@ -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.