mirror of
https://github.com/iib0011/omni-tools.git
synced 2025-09-20 06:29:32 +02:00
rotate tool, testCases then updated index file
This commit is contained in:
43
src/pages/string/rotate/service.ts
Normal file
43
src/pages/string/rotate/service.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
export function rotate(input: string, step: number, right: boolean): string {
|
||||
const array: string[] = input.split('');
|
||||
const length = array.length;
|
||||
|
||||
if (length === 0) {
|
||||
return input; // Return early if the input is an empty string
|
||||
}
|
||||
|
||||
// Normalize the step to be within the bounds of the array length
|
||||
const normalizedPositions = ((step % length) + length) % length;
|
||||
|
||||
if (right) {
|
||||
// Rotate right
|
||||
return array
|
||||
.slice(-normalizedPositions)
|
||||
.concat(array.slice(0, -normalizedPositions))
|
||||
.join('');
|
||||
} else {
|
||||
// Rotate left
|
||||
return array
|
||||
.slice(normalizedPositions)
|
||||
.concat(array.slice(0, normalizedPositions))
|
||||
.join('');
|
||||
}
|
||||
}
|
||||
|
||||
export function rotateString(
|
||||
input: string,
|
||||
step: number,
|
||||
right: boolean,
|
||||
multiLine: boolean
|
||||
) {
|
||||
const result: string[] = [];
|
||||
if (multiLine) {
|
||||
const array: string[] = input.split('\n');
|
||||
for (const word of array) {
|
||||
result.push(rotate(word, step, right));
|
||||
}
|
||||
} else {
|
||||
result.push(rotate(input, step, right));
|
||||
}
|
||||
return result.join('\n');
|
||||
}
|
Reference in New Issue
Block a user