Skip to content

Commit

Permalink
chore: upgrade github actions to v3, node to 16.x, jest to 29.5 and r…
Browse files Browse the repository at this point in the history
…ollup to 3.20.2 + fix typing
  • Loading branch information
Pierre Burel committed Mar 27, 2023
1 parent 59b8935 commit 9fbba5a
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 24 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 0 additions & 4 deletions .travis.yml

This file was deleted.

13 changes: 7 additions & 6 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -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> = T extends number ? string : { [K in keyof T]: Output<T[K]> }
// Return same types as input but recursively cast number as string if unit !== false
export type Output<T, O> = T extends number ? O extends { unit: false } ? T : string : { [K in keyof T]: Output<T[K], O> }

export type To = 'rem' | 'em' | 'px';

export type Options = {
baseline?: number;
precision?: number;
unit?: boolean;
}

export function convert<Value extends Input>(value: Value, to?: To, options?: Options): Output<Value>;
export function convert<I extends Input, _, O extends Options>(value: I, to?: To, options?: O): Output<I, O>;

export function rem<Value extends Input>(value: Value, options?: Options): Output<Value>;
export function rem<I extends Input, O extends Options>(value: I, options?: O): Output<I, O>;

export function em<Value extends Input>(value: Value, baseline: Options['baseline'], options?: Omit<Options, 'baseline'>): Output<Value>;
export function em<I extends Input, _, O extends Options>(value: I, baseline: Options['baseline'], options?: Omit<Options, 'baseline'>): Output<I, O>;

export function px<Value extends Input>(value: Value, options?: Options): Output<Value>;
export function px<I extends Input, O extends Options>(value: I, options?: Options): Output<I, O>;

export default rem;
7 changes: 4 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const defaults = {
baseline: 16,
precision: 5,
unit: true,
};

const rounded = (value, precision) => {
Expand All @@ -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
Expand Down
25 changes: 20 additions & 5 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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',
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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"
}
}

0 comments on commit 9fbba5a

Please sign in to comment.