-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
92 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
packages/docusaurus-utils-common/src/__tests__/stringUtils.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters