Skip to content

Commit

Permalink
feat(utils): normalizeLib
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 b61f9ae commit b3f2126
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/utils/__tests__/normalize-lib.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* @file Unit Tests - normalizeLib
* @module tsconfig-utils/utils/tests/unit/normalizeLib
*/

import LIB from '../lib'
import testSubject from '../normalize-lib'

describe('unit:utils/normalizeLib', () => {
it('should return empty array if option cannot be normalized', () => {
expect(testSubject(faker.string.sample())).to.be.empty
})

it('should return normalized compiler option', () => {
expect(testSubject([...LIB.keys()])).to.deep.equal([...LIB.values()])
})
})
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ export { default as loadPluginConfigs } from './load-plugin-configs'
export { default as loadTsconfig } from './load-tsconfig'
export { default as normalizeImportsNotUsed } from './normalize-imports-not-used'
export { default as normalizeJsx } from './normalize-jsx'
export { default as normalizeLib } from './normalize-lib'
29 changes: 29 additions & 0 deletions src/utils/normalize-lib.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* @file Utilities - normalizeLib
* @module tsconfig-utils/utils/normalizeLib
*/

import type { Lib, LibFile } from '@flex-development/tsconfig-types'
import { isString, isUndefined } from '@flex-development/tutils'
import LIB from './lib'

/**
* Converts the given `option` to an array containing **programmatic**
* [`lib`][1] options.
*
* TypeScript programs expect values defined in `compilerOptions.lib` to match
* filenames in `node_modules/typescript/lib` exactly.
*
* [1]: https://www.typescriptlang.org/tsconfig#lib
*
* @param {unknown} option - Option to evaluate
* @return {LibFile[]} Lib filename array
*/
const normalizeLib = (option: unknown): LibFile[] => {
return (Array.isArray(option) ? option : [])
.filter(item => isString(item))
.map((name: string) => LIB.get(name as Lowercase<Lib>))
.filter(file => !isUndefined(file)) as LibFile[]
}

export default normalizeLib

0 comments on commit b3f2126

Please sign in to comment.