# ensure-string

> Ensure that we receive a text even if an ArrayBuffer is sent

Latest version **2.0.0** (published 2026-01-26) · MIT license · 0 weekly downloads

## Install

```sh
npm install ensure-string
pnpm add ensure-string
yarn add ensure-string
bun add ensure-string
```

## Health

**Score 50/100 (C)** — status: stable.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2026-01-26 |
| First published | 2021-04-27 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM |
| Dependencies | 1 |
| Unpacked size | 9.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Luc Patiny |
| Maintainers | cheminfo-bot |

## Links

- npm: https://www.npmjs.com/package/ensure-string
- Repository: https://github.com/cheminfo/ensure-string
- Homepage: https://github.com/cheminfo/ensure-string#readme
- Issues: https://github.com/cheminfo/ensure-string/issues
- npm.io page: https://npm.io/package/ensure-string

## Dependencies (1)

- [cheminfo-types](https://npm.io/package/cheminfo-types.md) ^1.10.0

## Recent versions

- 2.0.0 (latest) — 2026-01-26
- 1.2.0 — 2022-04-15
- 1.1.0 — 2021-09-15
- 1.0.0 — 2021-07-29
- 0.1.1 — 2021-04-28
- 0.1.0 — 2021-04-27

## README

# ensure-string

[![NPM version][npm-image]][npm-url]
[![build status][ci-image]][ci-url]
[![Test coverage][codecov-image]][codecov-url]
[![npm download][download-image]][download-url]
[![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.5145388.svg)](https://doi.org/10.5281/zenodo.5145388)

If required, convert the input value to a string.

The input value may be a Buffer, ArrayBuffer, Int8Array.

By default the conversion will consider that the text encoding is `utf-8`.
If the file contains an UTF-16 byte-order mark (BOM), this will be detected and default to `utf-16le` or `utf-16be` accordingly.

If the input value is a text, no change is done. The method returns the value without further processing.

## Installation

`$ npm i ensure-string`

## Usage

We have a `text.txt` file containing the string `ABC`.

```js
import { ensureString } from '..';
import { readFileSync } from 'fs';
import { join } from 'path';
const blob = readFileSync(join(__dirname, 'test.txt')); // read the file as an ArrayBuffer
const text = ensureString(blob);
console.log(text); // the text is a string containing ÀBC`
```

## [API Documentation](https://cheminfo.github.io/ensure-string/)

## License

[MIT](./LICENSE)

[npm-image]: https://img.shields.io/npm/v/ensure-string.svg
[npm-url]: https://www.npmjs.com/package/ensure-string
[ci-image]: https://github.com/cheminfo/ensure-string/workflows/Node.js%20CI/badge.svg?branch=main
[ci-url]: https://github.com/cheminfo/ensure-string/actions?query=workflow%3A%22Node.js+CI%22
[codecov-image]: https://img.shields.io/codecov/c/github/cheminfo/ensure-string.svg
[codecov-url]: https://codecov.io/gh/cheminfo/ensure-string
[download-image]: https://img.shields.io/npm/dm/ensure-string.svg
[download-url]: https://www.npmjs.com/package/ensure-string

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