diff --git a/.github/workflows/node.js.yml b/.github/workflows/node.js.yml index 802aae3..4677c23 100644 --- a/.github/workflows/node.js.yml +++ b/.github/workflows/node.js.yml @@ -4,10 +4,10 @@ jobs: test: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Use Node.js - uses: actions/setup-node@v2 + uses: actions/setup-node@v3 with: - node-version: '12.x' + node-version: '16.x' - run: npm install - run: npm test diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 4726faf..0000000 --- a/.travis.yml +++ /dev/null @@ -1,4 +0,0 @@ -language: node_js -cache: yarn -node_js: - - stable diff --git a/index.d.ts b/index.d.ts index bf5f83b..dd30cff 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,21 +1,22 @@ export type Input = number | string | Input[] | { [key: string]: Input }; -// Return same types as input but recursively cast number as string -export type Output = T extends number ? string : { [K in keyof T]: Output } +// Return same types as input but recursively cast number as string if unit !== false +export type Output = T extends number ? O extends { unit: false } ? T : string : { [K in keyof T]: Output } export type To = 'rem' | 'em' | 'px'; export type Options = { baseline?: number; precision?: number; + unit?: boolean; } -export function convert(value: Value, to?: To, options?: Options): Output; +export function convert(value: I, to?: To, options?: O): Output; -export function rem(value: Value, options?: Options): Output; +export function rem(value: I, options?: O): Output; -export function em(value: Value, baseline: Options['baseline'], options?: Omit): Output; +export function em(value: I, baseline: Options['baseline'], options?: Omit): Output; -export function px(value: Value, options?: Options): Output; +export function px(value: I, options?: Options): Output; export default rem; diff --git a/index.js b/index.js index f37e55c..765da89 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,7 @@ const defaults = { baseline: 16, precision: 5, + unit: true, }; const rounded = (value, precision) => { @@ -9,15 +10,15 @@ const rounded = (value, precision) => { }; const convert = (value, to = 'rem', options = {}) => { - const { baseline, precision } = {...defaults, ...options}; + const { baseline, precision, unit } = {...defaults, ...options}; // Number if (typeof value === 'number') { if (to === 'px') { - return rounded(value * parseFloat(baseline), precision) + to; + return rounded(value * parseFloat(baseline), precision) + (unit ? to : 0); } - return rounded(value / parseFloat(baseline), precision) + to; + return rounded(value / parseFloat(baseline), precision) + (unit ? to : 0); } // Array diff --git a/index.test.js b/index.test.js index e0b5dee..c584e6f 100644 --- a/index.test.js +++ b/index.test.js @@ -42,13 +42,21 @@ it('Variable', () => run( )); it('Array', () => run( - [24, '24px', '5px -10px 1.5rem', {a: 24}], - ['1.5rem', '1.5rem', '0.3125rem -0.625rem 1.5rem', {a: '1.5rem'}] + [24, '24px', '5px -10px 1.5rem'], + ['1.5rem', '1.5rem', '0.3125rem -0.625rem 1.5rem'] )); it('Object', () => run( - {fontSize: 24, margin: '24px', padding: '5px -10px 1.5rem', a: { b: [24] }}, - {fontSize: '1.5rem', margin: '1.5rem', padding: '0.3125rem -0.625rem 1.5rem', a: { b: ['1.5rem']}} + {fontSize: 24, margin: '24px', padding: '5px -10px 1.5rem'}, + {fontSize: '1.5rem', margin: '1.5rem', padding: '0.3125rem -0.625rem 1.5rem'} +)); + +it('Complex object with unitless output', () => run( + {a: 16, b: [24, 32], c: { d: [48, { e: 64 }] }}, + {a: 1, b: [1.5, 2], c: { d: [3, { e: 4 }] }}, + { + unit: false + } )); it('Changing baseline', () => run( @@ -75,13 +83,20 @@ it('Converting to pixels', () => run( px )); -it('Converting to pixels (unitless)', () => run( +it('Converting to pixels (unitless input)', () => run( -1.5, '-24px', {}, px )); +it('Converting to pixels (unitless input and output)', () => run( + -1.5, + -24, + { unit: false }, + px +)); + it('Converting to em', () => run( '24px', '2em', diff --git a/package.json b/package.json index 88c6af9..a9663ff 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "startijenn-rem", - "version": "1.1.0", + "version": "1.1.1", "description": "JavaScript function to convert CSS rem units.", "keywords": [ "javascript", @@ -26,7 +26,7 @@ }, "homepage": "https://github.com/pierreburel/startijenn-rem", "devDependencies": { - "jest": "^28.1.0", - "rollup": "^2.75.5" + "jest": "^29.5.0", + "rollup": "^3.20.2" } }