Skip to content

Commit

Permalink
feat: utils add winEOL (umijs#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
ycjcl868 committed Feb 12, 2020
1 parent 6e164eb commit e83dae6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export { default as mergeConfig } from './mergeConfig/mergeConfig';
export { default as isLernaPackage } from './isLernaPackage/isLernaPackage';
export { default as getFile } from './getFile/getFile';
export { default as winPath } from './winPath/winPath';
export { default as winEOL } from './winEOL/winEOL';
export { default as parseRequireDeps } from './parseRequireDeps/parseRequireDeps';
export { default as BabelRegister } from './BabelRegister/BabelRegister';
export { default as Generator } from './Generator/Generator';
Expand Down
16 changes: 16 additions & 0 deletions packages/utils/src/winEOL/winEOL.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import winEOL from './winEOL';

const isWindows =
typeof process !== 'undefined' && process.platform === 'win32';

test('normal', () => {
if (isWindows) {
expect(winEOL('a \r\n b')).toEqual('a \n b');
expect(winEOL('成 \r\n b')).toEqual('成 \n b');
expect(winEOL('🍟 \r\n 🍔')).toEqual('🍟 \n 🍔');
} else {
expect(winEOL('a \n b')).toEqual('a \n b');
expect(winEOL('成 \n b')).toEqual('成 \n b');
expect(winEOL('🍟 \n 🍔')).toEqual('🍟 \n 🍔');
}
});
16 changes: 16 additions & 0 deletions packages/utils/src/winEOL/winEOL.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// 在windows环境下,很多工具都会把换行符lf自动改成crlf
// 为了测试精准需要将换行符转化一下
// https://github.com/cssmagic/blog/issues/22

/**
* Convert Windows crlf to lf (\r\n to \n)
* @param content
*/
export default (content: string | undefined) => {
const isWindows =
typeof process !== 'undefined' && process.platform === 'win32';
if (typeof content !== 'string') {
return content;
}
return isWindows ? content.replace(/\r/g, '') : content;
};

0 comments on commit e83dae6

Please sign in to comment.