Skip to content

Commit

Permalink
feat(pixel-to-rem): adding pxToRem helper (#190)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Amaral <[email protected]>
  • Loading branch information
ammichael and Michael Amaral authored Sep 18, 2020
1 parent e213fd9 commit 05e242a
Show file tree
Hide file tree
Showing 7 changed files with 404 additions and 202 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import { isIOS } from '@platformbuilders/helpers/native'
| [`removeWhiteSpaces`](./docs/removeWhiteSpaces.md) | string | (value) |
| [`switchStyle`](./docs/switchStyle.md) | string | (name)(stylesObj)(props) |
| [`toOnlyNumbers`](./docs/toOnlyNumbers.md) | string | (value) |
| [`pxToRem`](./docs/pxToRem.md) | string | (pixel, baseline?) |

## React Native

Expand Down
22 changes: 22 additions & 0 deletions docs/pxToRem.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# `pxToRem`

Convert `px` metric to `rem`, using an optional baseline as argument.

## Arguments

- `pixel: number`: value to be converted
- `base?: number`: optional baseline, normally 16

## Returns

- `rem: string`: The result between the pixel and base calculation

## Usage

```jsx
import { pxToRem } from '@platformbuilders/helpers';

pxToRem(20); // return '1.25rem'

pxToRem(20, 20); // return '1rem'
```
46 changes: 23 additions & 23 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@platformbuilders/helpers",
"version": "0.3.1",
"version": "0.3.2",
"description": "Platfom builders helpers library",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down Expand Up @@ -49,62 +49,62 @@
},
"dependencies": {
"lodash": "^4.17.20",
"moment": "^2.27.0",
"moment": "^2.28.0",
"numeral": "^2.0.6",
"react": "^16.13.1",
"react-dom": "^16.13.1"
},
"devDependencies": {
"@babel/core": "^7.11.4",
"@babel/core": "^7.11.6",
"@babel/plugin-proposal-class-properties": "^7.10.4",
"@babel/plugin-transform-flow-strip-types": "^7.10.4",
"@babel/plugin-transform-object-assign": "^7.10.4",
"@babel/preset-env": "^7.11.0",
"@babel/preset-env": "^7.11.5",
"@babel/preset-react": "^7.10.4",
"@babel/runtime": "^7.11.2",
"@types/enzyme": "^3.10.5",
"@types/enzyme": "^3.10.6",
"@types/enzyme-adapter-react-16": "^1.0.6",
"@types/jest": "^26.0.10",
"@types/lodash": "^4.14.159",
"@types/jest": "^26.0.14",
"@types/lodash": "^4.14.161",
"@types/moment": "^2.13.0",
"@types/numeral": "^0.0.28",
"@types/react": "^16.9.46",
"@types/react": "^16.9.49",
"@types/react-dom": "^16.9.8",
"@types/react-native": "^0.63.8",
"@typescript-eslint/eslint-plugin": "^3.9.1",
"@typescript-eslint/parser": "^3.9.1",
"@types/react-native": "^0.63.19",
"@typescript-eslint/eslint-plugin": "^4.1.1",
"@typescript-eslint/parser": "^4.1.1",
"babel-jest": "^26.3.0",
"babel-plugin-module-resolver": "^4.0.0",
"babel-plugin-transform-es2015-modules-commonjs": "^6.26.2",
"enzyme": "^3.11.0",
"enzyme-adapter-react-16": "^1.15.3",
"eslint": "^7.7.0",
"enzyme-adapter-react-16": "^1.15.4",
"eslint": "^7.9.0",
"eslint-config-airbnb": "^18.2.0",
"eslint-config-airbnb-typescript": "^9.0.0",
"eslint-config-airbnb-typescript": "^10.0.0",
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-import": "^2.22.0",
"eslint-plugin-jest": "^23.20.0",
"eslint-plugin-jest": "^24.0.1",
"eslint-plugin-json": "^2.1.2",
"eslint-plugin-jsx-a11y": "^6.3.1",
"eslint-plugin-prettier": "^3.1.4",
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-react": "^7.20.6",
"eslint-plugin-react-hooks": "^4.1.0",
"eslint-plugin-react-hooks": "^4.1.2",
"eslint-plugin-sonarjs": "^0.5.0",
"husky": "^4.2.5",
"jest": "^26.4.1",
"husky": "^4.3.0",
"jest": "^26.4.2",
"jest-environment-node": "^26.3.0",
"jest-enzyme": "^7.1.2",
"lint-staged": "^10.2.11",
"lint-staged": "^10.4.0",
"mz": "^2.7.0",
"prettier": "^2.0.5",
"prettier": "^2.1.2",
"react-native": "^0.63.2",
"react-native-haptic": "^1.0.1",
"react-native-size-matters": "^0.3.0",
"react-native-size-matters": "^0.3.1",
"rmfr": "^2.0.0",
"rollup": "^2.26.4",
"rollup": "^2.27.1",
"rollup-plugin-typescript2": "^0.27.2",
"ts-jest": "^26.2.0",
"ts-jest": "^26.3.0",
"typescript": "^4.0.2"
}
}
24 changes: 24 additions & 0 deletions src/__tests__/pixelToRem.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { pxToRem } from '../pixelToRem';

describe('pixelToRem helpers tests', () => {
it('should return a formatted rem', () => {
// given
const pixelValue = 20;
const expected = '1.25rem';
// when
const formatted = pxToRem(pixelValue);
// then
expect(formatted).toBe(expected);
});

it('should return a formatted rem using baseValue', () => {
// given
const pixelValue = 20;
const baseValue = 20;
const expected = '1rem';
// when
const formatted = pxToRem(pixelValue, baseValue);
// then
expect(formatted).toBe(expected);
});
});
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export * from './getTheme';
export * from './ifStyle';
export * from './switchStyle';
export * from './base64';
export * from './pixelToRem';
3 changes: 3 additions & 0 deletions src/pixelToRem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const pxToRem = (pixels: number, baseline = 16): string => {
return `${pixels / baseline}rem`;
};
Loading

0 comments on commit 05e242a

Please sign in to comment.