Skip to content

Commit

Permalink
Update readme and jsdoc
Browse files Browse the repository at this point in the history
  • Loading branch information
millerized committed Jan 6, 2018
1 parent fe337a9 commit 72994a8
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 19 deletions.
21 changes: 12 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
Smart Truncate [![Build Status](https://travis-ci.org/millerized/smart-truncate.svg?branch=master)](https://travis-ci.org/millerized/smart-truncate) [![Coverage Status](https://coveralls.io/repos/github/millerized/smart-truncate/badge.svg?branch=master)](https://coveralls.io/github/millerized/smart-truncate?branch=master)
=========

A small library that truncates a string. It can insert or append an ellipsis at any desired position of the truncated result.
A small library that truncates a string. It can insert or append an ellipsis (or a custom mark) at any desired position of the truncated result.

## Installation

`npm install smart-truncate`
`npm install --save smart-truncate`

## Syntax
```js
smartTruncate(string, length[, position])
smartTruncate(string, length[, options])
```
#### Paramaters
>**_string_**<br>
&nbsp;&nbsp;&nbsp;&nbsp;A string with a minimum lenght of 4 characters.
&emsp;A string with a minimum length of 4 characters.

>**_length_**<br>
&nbsp;&nbsp;&nbsp;&nbsp;The length of the truncated result.
&emsp;The length of the truncated result.

>**_position_**<br>
&nbsp;&nbsp;&nbsp;&nbsp;Optional. The index of the ellipsis (zero based). Default is at the end.
>**_options_**<br>
>**_options.position_**<br>
&emsp;Optional. The index of the ellipsis (zero based). Default is at the end.<br>
>**_options.mark_**<br>
&emsp;Optional. The character[s] indicating omission. Default is an ellipsis "…".

#### Return value
>A new string truncated with an ellipsis.
Expand All @@ -42,7 +45,7 @@ const truncated = smartTruncate(string, 15);
const string = 'To iterate is human, to recurse divine.';

// Insert an ellipsis in the middle of a smart truncated string.
const truncated = smartTruncate(string, 21, 10);
const truncated = smartTruncate(string, 21, {position: 10});
```

**Output**: `"To iterate…se divine."`
Expand All @@ -60,7 +63,7 @@ const files = [
'App Store.app'
];

const truncated = files.map((filename) => smartTruncate(filename, 21, 10));
const truncated = files.map((filename) => smartTruncate(filename, 21, {position: 10}));
```

**Output**:
Expand Down
13 changes: 8 additions & 5 deletions dist/smart-truncate.es5.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
/**
* smartTruncate - Smartly™ truncate a given string.
*
* @param {String} string A string with a minimum lenght of 4 chars.
* @param {Number} length The length of the truncated result.
* @param {Number} [position] The index of the ellipsis (zero based). Default is the end.
* @return {String} Return a truncated string w/ ellipsis.
* @param {String} string - A string with a minimum lenght of 4 chars.
* @param {Number} length - The length of the truncated result.
* @param {Object} [options]
* @param {Number} [options.position] - The index of the ellipsis (zero based).
* Default is the end.
* @param {String} [options.mark = '…'] - The character[s] indicating omission.
* @return {String} - Return a truncated string w/ ellipsis.
*
* Example: smartTruncate('Steve Miller', 8) === 'Steve M…'.
* Example: smartTruncate('Steve Miller', 9, 4) === 'Stev…ller'.
Expand All @@ -16,7 +19,7 @@ var smartTruncate = function smartTruncate(string, length) {
_ref$mark = _ref.mark,
mark = _ref$mark === undefined ? '\u2026' : _ref$mark,
_ref$position = _ref.position,
position = _ref$position === undefined ? length : _ref$position;
position = _ref$position === undefined ? length - 1 : _ref$position;

if (typeof mark !== 'string') return string;

Expand Down
13 changes: 8 additions & 5 deletions src/smart-truncate.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
/**
* smartTruncate - Smartly™ truncate a given string.
*
* @param {String} string A string with a minimum lenght of 4 chars.
* @param {Number} length The length of the truncated result.
* @param {Number} [position] The index of the ellipsis (zero based). Default is the end.
* @return {String} Return a truncated string w/ ellipsis.
* @param {String} string - A string with a minimum lenght of 4 chars.
* @param {Number} length - The length of the truncated result.
* @param {Object} [options]
* @param {Number} [options.position] - The index of the ellipsis (zero based).
* Default is the end.
* @param {String} [options.mark = '…'] - The character[s] indicating omission.
* @return {String} - Return a truncated string w/ ellipsis.
*
* Example: smartTruncate('Steve Miller', 8) === 'Steve M…'.
* Example: smartTruncate('Steve Miller', 9, 4) === 'Stev…ller'.
*/
const smartTruncate = (string, length,
{
mark = '\u2026', // ellipsis = …
position = length,
position = (length - 1),
} = {}
) => {
if (typeof mark !== 'string') return string;
Expand Down

0 comments on commit 72994a8

Please sign in to comment.