Skip to content

Commit

Permalink
Merge pull request #239 from sasjs/lineEnding
Browse files Browse the repository at this point in the history
feat(line-ending): added utility to get line ending
  • Loading branch information
YuryShkoda authored Jul 20, 2023
2 parents 2c74626 + 66b11b1 commit cc1604f
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 4 deletions.
4 changes: 4 additions & 0 deletions PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
9 changes: 9 additions & 0 deletions src/file/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean> {
return fs.promises
Expand Down Expand Up @@ -268,3 +269,11 @@ export const createReadStream = async (filePath: string) =>
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
3 changes: 2 additions & 1 deletion src/file/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ export {
base64EncodeFile,
getRealPath,
isTestFile,
testFileRegExp
testFileRegExp,
getLineEnding
} from './file'

export { updateCsv, createCsv, readCsv } from './csvFile'
Expand Down
22 changes: 19 additions & 3 deletions src/file/spec/file.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -500,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())
Expand All @@ -525,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())
Expand Down Expand Up @@ -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)
})
})
4 changes: 4 additions & 0 deletions src/types/file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export enum LineEndings {
CRLF = `\r\n`,
LF = `\n`
}
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ export * from './serverError'
export * from './serverType'
export * from './servicePack'
export * from './target'
export * from './file'

0 comments on commit cc1604f

Please sign in to comment.