# truncate-x

> Truncate a string to a maximum specified length.

Latest version **4.1.2** (published 2019-08-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install truncate-x
pnpm add truncate-x
yarn add truncate-x
bun add truncate-x
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 4.1.2 |
| Published | 2019-08-28 |
| First published | 2017-06-20 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Node | >=8.11.4 |
| Dependencies | 9 |
| Unpacked size | 407.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Graham Fairweather |
| Maintainers | xotic750 |
| Keywords | truncate, string, module, javascript, nodejs, browser |

## Links

- npm: https://www.npmjs.com/package/truncate-x
- Repository: https://github.com/Xotic750/truncate-x
- Issues: https://github.com/Xotic750/truncate-x/issues
- npm.io page: https://npm.io/package/truncate-x

## Dependencies (9)

- [is-nil-x](https://npm.io/package/is-nil-x.md) ^2.1.1
- [is-regexp-x](https://npm.io/package/is-regexp-x.md) ^3.1.2
- [to-length-x](https://npm.io/package/to-length-x.md) ^4.2.2
- [to-boolean-x](https://npm.io/package/to-boolean-x.md) ^2.1.1
- [array-slice-x](https://npm.io/package/array-slice-x.md) ^4.2.2
- [is-object-like-x](https://npm.io/package/is-object-like-x.md) ^2.1.2
- [has-own-property-x](https://npm.io/package/has-own-property-x.md) ^4.1.2
- [simple-methodize-x](https://npm.io/package/simple-methodize-x.md) ^1.0.4
- [to-string-symbols-supported-x](https://npm.io/package/to-string-symbols-supported-x.md) ^2.1.2

## Alternatives

- [@mce/gif](https://npm.io/package/@mce/gif.md) — 2.6K weekly downloads
- [cleanse](https://npm.io/package/cleanse.md) — 173 weekly downloads
- [str](https://npm.io/package/str.md) — 127 weekly downloads
- [naming](https://npm.io/package/naming.md) — 95 weekly downloads
- [tap-telco-api](https://npm.io/package/tap-telco-api.md) — 19 weekly downloads

## Recent versions

- 4.1.2 (latest) — 2019-08-28
- 4.1.1 — 2019-08-20
- 4.1.0 — 2019-08-14
- 4.0.12 — 2019-08-05
- 4.0.11 — 2019-08-05
- 4.0.10 — 2019-07-31
- 4.0.9 — 2019-07-27
- 4.0.8 — 2019-07-26
- 4.0.7 — 2019-07-25
- 4.0.6 — 2019-07-24
- 4.0.5 — 2019-07-24
- 4.0.4 — 2019-07-22
- 4.0.3 — 2019-07-21
- 4.0.2 — 2019-07-19
- 4.0.1 — 2019-07-18
- … 10 more at https://npm.io/package/truncate-x/versions

## README

<a
  href="https://travis-ci.org/Xotic750/truncate-x"
  title="Travis status">
<img
  src="https://travis-ci.org/Xotic750/truncate-x.svg?branch=master"
  alt="Travis status" height="18">
</a>
<a
  href="https://david-dm.org/Xotic750/truncate-x"
  title="Dependency status">
<img src="https://david-dm.org/Xotic750/truncate-x/status.svg"
  alt="Dependency status" height="18"/>
</a>
<a
  href="https://david-dm.org/Xotic750/truncate-x?type=dev"
  title="devDependency status">
<img src="https://david-dm.org/Xotic750/truncate-x/dev-status.svg"
  alt="devDependency status" height="18"/>
</a>
<a
  href="https://badge.fury.io/js/truncate-x"
  title="npm version">
<img src="https://badge.fury.io/js/truncate-x.svg"
  alt="npm version" height="18">
</a>
<a
  href="https://www.jsdelivr.com/package/npm/truncate-x"
  title="jsDelivr hits">
<img src="https://data.jsdelivr.com/v1/package/npm/truncate-x/badge?style=rounded"
  alt="jsDelivr hits" height="18">
</a>
<a
  href="https://bettercodehub.com/results/Xotic750/truncate-x"
  title="bettercodehub score">
<img src="https://bettercodehub.com/edge/badge/Xotic750/truncate-x?branch=master"
  alt="bettercodehub score" height="18">
</a>
<a
  href="https://coveralls.io/github/Xotic750/truncate-x?branch=master"
  title="Coverage Status">
<img src="https://coveralls.io/repos/github/Xotic750/truncate-x/badge.svg?branch=master"
  alt="Coverage Status" height="18">
</a>

<a name="module_truncate-x"></a>

## truncate-x

Truncate a string to a maximum specified length.

<a name="exp_module_truncate-x--module.exports"></a>

### `module.exports(string, [options])` ⇒ <code>string</code> ⏏

Truncates `string` if it's longer than the given maximum string length.
The last characters of the truncated string are replaced with the omission
string which defaults to "...".

**Kind**: Exported function  
**Returns**: <code>string</code> - Returns the truncated string.

| Param               | Type                                       | Default                                  | Description                             |
| ------------------- | ------------------------------------------ | ---------------------------------------- | --------------------------------------- |
| string              | <code>string</code>                        |                                          | The string to truncate.                 |
| [options]           | <code>Object</code>                        |                                          | The options object.                     |
| [options.length]    | <code>number</code>                        | <code>30</code>                          | The maximum string length.              |
| [options.omission]  | <code>string</code>                        | <code>&quot;&#x27;...&#x27;&quot;</code> | The string to indicate text is omitted. |
| [options.separator] | <code>RegExp</code> \| <code>string</code> |                                          | The separator pattern to truncate to.   |

**Example**

```js
import truncate from 'truncate-x';

truncate('hi-diddly-ho there, neighborino');
// 'hi-diddly-ho there, neighbo...'

truncate('hi-diddly-ho there, neighborino', {
  length: 24,
  separator: ' ',
});
// 'hi-diddly-ho there,...'

truncate('hi-diddly-ho there, neighborino', {
  length: 24,
  separator: /,? +/,
});
// 'hi-diddly-ho there...'

truncate('hi-diddly-ho there, neighborino', {
  omission: ' [...]',
});
// 'hi-diddly-ho there, neig [...]'
```

---
_Source: https://npm.io/package/truncate-x · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
