From b7423c50ef701bf1ce2c42002dccdb81ae8ebdd7 Mon Sep 17 00:00:00 2001 From: Yury Shkoda Date: Thu, 20 Jul 2023 15:16:08 +0300 Subject: [PATCH 1/4] feat(line-ending): added utility to get line ending --- src/file/file.ts | 4 ++++ src/file/index.ts | 3 ++- src/file/spec/file.spec.ts | 18 +++++++++++++++++- src/types/file.ts | 4 ++++ src/types/index.ts | 1 + 5 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 src/types/file.ts diff --git a/src/file/file.ts b/src/file/file.ts index 95b5857..b1c94c4 100644 --- a/src/file/file.ts +++ b/src/file/file.ts @@ -3,6 +3,7 @@ import rimraf from 'rimraf' import path from 'path' import { asyncForEach } from '../utils' import * as file from '.' +import { LineEndings } from '../types' export async function fileExists(filePath: string): Promise { return fs.promises @@ -268,3 +269,6 @@ export const createReadStream = async (filePath: string) => export const testFileRegExp = /\.test\.(\d+\.)?sas$/i export const isTestFile = (fileName: string) => testFileRegExp.test(fileName) + +export const getLineEnding = (content: string) => + new RegExp(LineEndings.CRLF).test(content) ? LineEndings.CRLF : LineEndings.LF diff --git a/src/file/index.ts b/src/file/index.ts index 78c64d0..18436d0 100644 --- a/src/file/index.ts +++ b/src/file/index.ts @@ -24,7 +24,8 @@ export { base64EncodeFile, getRealPath, isTestFile, - testFileRegExp + testFileRegExp, + getLineEnding } from './file' export { updateCsv, createCsv, readCsv } from './csvFile' diff --git a/src/file/spec/file.spec.ts b/src/file/spec/file.spec.ts index d1b6b4e..b522b24 100644 --- a/src/file/spec/file.spec.ts +++ b/src/file/spec/file.spec.ts @@ -22,12 +22,14 @@ import { base64EncodeImageFile, getRealPath, createWriteStream, - createReadStream + createReadStream, + getLineEnding } from '../file' import * as fileModule from '../file' import { generateTimestamp } from '../../time' import { svgBase64EncodedUnix, svgBase64EncodedWin } from './expectedOutputs' import { isWindows } from '../../utils' +import { LineEndings } from '../../types' const content = 'test content' @@ -606,3 +608,17 @@ describe('deleteFolder', () => { expect(isFolderPresent).toBeFalsy() }) }) + +describe('getLineEnding', () => { + it('should return correct line ending', () => { + let lineEnding = LineEndings.CRLF + let line: LineEndings = lineEnding + + expect(getLineEnding(line)).toEqual(lineEnding) + + lineEnding = LineEndings.LF + line = lineEnding + + expect(getLineEnding(line)).toEqual(lineEnding) + }) +}) diff --git a/src/types/file.ts b/src/types/file.ts new file mode 100644 index 0000000..10f4c31 --- /dev/null +++ b/src/types/file.ts @@ -0,0 +1,4 @@ +export enum LineEndings { + CRLF = `\r\n`, + LF = `\n` +} diff --git a/src/types/index.ts b/src/types/index.ts index 4c322c6..09c1ac6 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -12,3 +12,4 @@ export * from './serverError' export * from './serverType' export * from './servicePack' export * from './target' +export * from './file' From a5ca7712f2479963d9cfddd75342f97c6656a5fc Mon Sep 17 00:00:00 2001 From: Yury Shkoda Date: Thu, 20 Jul 2023 15:18:21 +0300 Subject: [PATCH 2/4] chore(pr): improved PR template --- PULL_REQUEST_TEMPLATE.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/PULL_REQUEST_TEMPLATE.md b/PULL_REQUEST_TEMPLATE.md index d195921..c509f87 100644 --- a/PULL_REQUEST_TEMPLATE.md +++ b/PULL_REQUEST_TEMPLATE.md @@ -16,3 +16,7 @@ What code changes have been made to achieve the intent. - [ ] All unit tests are passing (`npm test`). - [ ] All `sasjs-cli` unit tests are passing (`npm test`). - [ ] Reviewer is assigned. + +### Reviewer checks + +- [ ] Any new code is documented. From ec656b585d5cf3d8ecfe9b8c3709090f6c1d9c20 Mon Sep 17 00:00:00 2001 From: Yury Shkoda Date: Thu, 20 Jul 2023 15:25:38 +0300 Subject: [PATCH 3/4] chore(lint): fixed lint issue --- src/file/spec/file.spec.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/file/spec/file.spec.ts b/src/file/spec/file.spec.ts index b522b24..f0a9805 100644 --- a/src/file/spec/file.spec.ts +++ b/src/file/spec/file.spec.ts @@ -502,7 +502,7 @@ describe('createReadStream', () => { jest.mock('../file') jest .spyOn(fs, 'createReadStream') - .mockImplementation(() => ({} as unknown as ReadStream)) + .mockImplementation(() => ({}) as unknown as ReadStream) jest .spyOn(fileModule, 'createFile') .mockImplementation(() => Promise.resolve()) @@ -527,7 +527,7 @@ describe('createWriteStream', () => { jest.mock('../file') jest .spyOn(fs, 'createWriteStream') - .mockImplementation(() => ({} as unknown as WriteStream)) + .mockImplementation(() => ({}) as unknown as WriteStream) jest .spyOn(fileModule, 'createFile') .mockImplementation(() => Promise.resolve()) From 66b11b17e4874d91eee17995e89d2d319c41c25a Mon Sep 17 00:00:00 2001 From: Yury Shkoda Date: Thu, 20 Jul 2023 15:31:03 +0300 Subject: [PATCH 4/4] docs(get-line-ending): added func description --- src/file/file.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/file/file.ts b/src/file/file.ts index b1c94c4..b3a7da0 100644 --- a/src/file/file.ts +++ b/src/file/file.ts @@ -270,5 +270,10 @@ export const testFileRegExp = /\.test\.(\d+\.)?sas$/i export const isTestFile = (fileName: string) => testFileRegExp.test(fileName) +/** + * Returns end of line sequence + * @param content to get end of line sequence. + * @returns CRLF(\r\n) or LF(\n) end of line sequence + */ export const getLineEnding = (content: string) => new RegExp(LineEndings.CRLF).test(content) ? LineEndings.CRLF : LineEndings.LF