Skip to content

Commit

Permalink
feat(utils): normalizeNewLine
Browse files Browse the repository at this point in the history
Signed-off-by: Lexus Drumgold <unicornware@flexdevelopment.llc>
  • Loading branch information
unicornware committed Feb 6, 2023
1 parent 2d8d082 commit 8219ec8
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/utils/__tests__/normalize-new-line.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* @file Unit Tests - normalizeNewLine
* @module tsconfig-utils/utils/tests/unit/normalizeNewLine
*/

import { NewLineKind } from '@flex-development/tsconfig-types'
import ts from 'typescript'
import testSubject from '../normalize-new-line'

describe('unit:utils/normalizeNewLine', () => {
it('should return normalized compiler option', () => {
// Arrange
const cases: [...Parameters<typeof testSubject>, ts.NewLineKind][] = [
[
NewLineKind.CarriageReturnLineFeed,
ts.NewLineKind.CarriageReturnLineFeed
],
[
NewLineKind.CarriageReturnLineFeed.toUpperCase(),
ts.NewLineKind.CarriageReturnLineFeed
],
[NewLineKind.LineFeed, ts.NewLineKind.LineFeed],
[NewLineKind.LineFeed.toUpperCase(), ts.NewLineKind.LineFeed]
]

// Act + Expect
cases.forEach(([option, expected]) => {
expect(testSubject(option)).to.equal(expected)
})
})

it('should return undefined if option cannot be normalized', () => {
expect(testSubject(faker.string.sample())).to.be.undefined
})
})
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ export { default as normalizeLib } from './normalize-lib'
export { default as normalizeModule } from './normalize-module'
export { default as normalizeModuleDetection } from './normalize-module-detection'
export { default as normalizeModuleResolution } from './normalize-module-resolution'
export { default as normalizeNewLine } from './normalize-new-line'
52 changes: 52 additions & 0 deletions src/utils/normalize-new-line.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* @file Utilities - normalizeNewLine
* @module tsconfig-utils/utils/normalizeNewLine
*/

import { getPropertyValue } from '#src/internal'
import { NewLineKind } from '@flex-development/tsconfig-types'
import { isString } from '@flex-development/tutils'
import ts from 'typescript'

/**
* Converts the given `option` to a **programmatic** [`newLine`][1] option.
*
* TypeScript programs expect a {@linkcode ts.NewLineKind} value.
*
* If the `option` is already programmatic, it will be returned unmodified. If
* it cannot be converted, `undefined` will be returned instead.
*
* [1]: https://www.typescriptlang.org/tsconfig#newLine
*
* @param {unknown} option - Option to evaluate
* @return {ts.NewLineKind | undefined} `ts.NewLineKind` value or `undefined`
*/
const normalizeNewLine = (option: unknown): ts.NewLineKind | undefined => {
// lowercase user option if it is a string
if (isString(option)) option = option.toLowerCase()

/**
* TypeScript program compiler option value, if any.
*
* @var {ts.NewLineKind | undefined} ret
*/
let ret: ts.NewLineKind | undefined

// normalize user compiler option
switch (option as NewLineKind | ts.NewLineKind) {
case NewLineKind.CarriageReturnLineFeed:
case getPropertyValue(ts.NewLineKind, 'CarriageReturnLineFeed'):
ret = getPropertyValue(ts.NewLineKind, 'CarriageReturnLineFeed')
break
case NewLineKind.LineFeed:
case getPropertyValue(ts.NewLineKind, 'LineFeed'):
ret = getPropertyValue(ts.NewLineKind, 'LineFeed')
break
default:
break
}

return ret
}

export default normalizeNewLine

0 comments on commit 8219ec8

Please sign in to comment.