-
Notifications
You must be signed in to change notification settings - Fork 22
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
3 changed files
with
42 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { toSingleLineString } from './toSingleLineString' | ||
|
||
describe('toSingleLineString', () => { | ||
test('ok', () => { | ||
expect(toSingleLineString('a\rb')).toBe('a b') | ||
expect(toSingleLineString('a\r\n\n\r\nb')).toBe('a b') | ||
expect(toSingleLineString('a\r\n\n\r\nb\r\n')).toBe('a b') | ||
expect(toSingleLineString('a\r\n\n\r\nb\r\nc')).toBe('a b c') | ||
expect(toSingleLineString('\r\n\n\r\n')).toBe('') | ||
expect(toSingleLineString('a\rb\ncvldlddd', 5)).toBe('a ...') | ||
expect( | ||
toSingleLineString('a\rb\ncvldlddd', { | ||
length: 5, | ||
}), | ||
).toBe('a ...') | ||
}) | ||
}) |
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,24 @@ | ||
import { truncate } from 'lodash-uni' | ||
import type { TruncateOptions } from 'lodash' | ||
|
||
/** | ||
* 将多行字符串转换为单行字符串。 | ||
* | ||
* @param value 要转换的字符串 | ||
* @returns 返回结果 | ||
*/ | ||
export function toSingleLineString( | ||
value: string, | ||
truncateOptions?: number | TruncateOptions, | ||
): string { | ||
let res = value.replace(/[\r\n]+/g, ' ').trim() | ||
if (truncateOptions) { | ||
res = truncate( | ||
res, | ||
typeof truncateOptions === 'number' | ||
? { length: truncateOptions } | ||
: truncateOptions, | ||
) | ||
} | ||
return res | ||
} |