Skip to content

Commit

Permalink
Merge pull request #1 from jmoses89/feature/text-utils
Browse files Browse the repository at this point in the history
feature: add TextHelpers singleton class
  • Loading branch information
André Neves authored May 3, 2017
2 parents d31a6be + af69a63 commit b3509f3
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 1 deletion.
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,33 @@ react-native link react-native-orientation
| lockPortrait | `RNUtils.lockPortrait()` | Locks the device's viewport to the Portrait mode. Disables landscape. |
| unlockOrientations | `RNUtils.unlockOrientations()` | Unlocks any previous locking behavior applied by `lockPortrait` or `lockLandscape`. |

### Text Helpers

| Function | Usage | Utility |
|-----------|-------|---------|
| getFontSize | `textHelpers.getFontSize(size)` | Returns a font size based on size argument |
| getLineHeight | `textHelpers.getLineHeight(size)` | Returns a line height based on size argument |
| getFontStyles | `textHelpers.getFontStyles(size)` | Returns an object with a font size and line height based on size argument |

**Example Usage**

```
import TextHelpers from 'rn-utils'
const fontSizes = {small: 8, medium: 12};
const lineHeights = {small: 12, medium: 16};
const textHelpers = new TextHelpers(fontSizes, lineHeights);
const fontStyles = {
...textHelpers.getFontStyles('small');
}
```

## Roadmap
The aim for this package is to introduce an ever growing list of React Native utilities that can be used when developing real-world scalable applications. With that in mind, below is the list of items I'd like to tackle next. These could be full rewrites of utilities or extracting it from current React Native projects.

- [ ] Font Sizes / Line Heights
- [x] Font Sizes / Line Heights
- [ ] Legacy Operating Systems
- [ ] Themes (?)
- [ ] Animations (?)
Expand Down
4 changes: 4 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
lockPortrait,
unlockOrientations,
} from './lib/orientation';
import TextHelpers from './text';

export {
// Orientation
Expand All @@ -20,4 +21,7 @@ export {
// Dimensions
getDeviceHeight,
getDeviceWidth,

// Text
TextHelpers
};
83 changes: 83 additions & 0 deletions src/lib/text.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@

/**
* @name TextHelpers
* @summary - Singleton class for handling any text related helpers
* @return {undefined}
*/
export default class TextHelpers {
/**
* @name constructor
* @summary - Method called on initialization of TextHelpers instance
* @param {object} fontSizes - object containing defined font sizes
* @param {object} lineHeights - object containing defined line heights
* @return {undefined}
*/
constructor(fontSizes, lineHeights) {
this._fontSizes = fontSizes;
this._lineHeights = lineHeights;
}

/**
* @name handleError
* @summary - Generic method for throwing errors
* @param {string} message - the error message to be thrown
* @return {undefined}
*/
handleError(message) {
throw new Error(message);
}

/**
* @name getLineHeight
* @summary - Method for returning a line height dependent on passed size argument
* @param {string} size - desired size of the font and line height
* @return {object}
*/
getLineHeight(size) {
if (typeof size !== 'string') {
this.handleError('Passed size argument must be a string');
}

const lineHeight = this._lineHeights[size];

if (!lineHeight) {
this.handleError(`Size ${size} is not a supported line height`);
}

return lineHeight;
}

/**
* @name getFontSize
* @summary - Method for returning a font size dependent on passed size argument
* @param {string} size - desired size of the font and line height
* @return {object}
*/
getFontSize(size) {
if (typeof size !== 'string') {
this.handleError('Passed size argument must be a string');
}

const fontSize = this._fontSizes[size];

if (!fontSize) {
this.handleError(`Size ${size} is not a supported font size`);
}

return fontSize;
}

/**
* @name getFontStyles
* @summary - Method for returning object containing a font size and
* line height dependent on passed size argument
* @param {string} size - desired size of the font and line height
* @return {object}
*/
getFontStyles(size) {
return {
fontSize: this.getFontSize(size),
lineHeight: this.getLineHeight(size)
}
}
}

0 comments on commit b3509f3

Please sign in to comment.