nlcst-to-string
Advanced tools
Comparing version 3.1.0 to 3.1.1
@@ -1,17 +0,1 @@ | ||
/** | ||
* @typedef {import('nlcst').Content} Content | ||
* @typedef {import('nlcst').Root} Root | ||
*/ | ||
/** | ||
* Stringify one nlcst node or list of nodes. | ||
* | ||
* @param {Root|Content|Content[]} node | ||
* @param {string} [separator=''] | ||
* @returns {string} | ||
*/ | ||
export function toString( | ||
node: Root | Content | Content[], | ||
separator?: string | undefined | ||
): string | ||
export type Content = import('nlcst').Content | ||
export type Root = import('nlcst').Root | ||
export {toString} from './lib/index.js' |
41
index.js
@@ -1,40 +0,1 @@ | ||
/** | ||
* @typedef {import('nlcst').Content} Content | ||
* @typedef {import('nlcst').Root} Root | ||
*/ | ||
/** | ||
* Stringify one nlcst node or list of nodes. | ||
* | ||
* @param {Root|Content|Content[]} node | ||
* @param {string} [separator=''] | ||
* @returns {string} | ||
*/ | ||
export function toString(node, separator = '') { | ||
let index = -1 | ||
if (!node || (!Array.isArray(node) && !node.type)) { | ||
throw new Error('Expected node, not `' + node + '`') | ||
} | ||
// @ts-expect-error Looks like a literal. | ||
if (typeof node.value === 'string') return node.value | ||
/** @type {Array.<Content|Root>} */ | ||
// @ts-expect-error Looks like a list of nodes or parent. | ||
const children = (Array.isArray(node) ? node : node.children) || [] | ||
// Shortcut: This is pretty common, and a small performance win. | ||
if (children.length === 1 && 'value' in children[0]) { | ||
return children[0].value | ||
} | ||
/** @type {Array.<string>} */ | ||
const values = [] | ||
while (++index < children.length) { | ||
values[index] = toString(children[index], separator) | ||
} | ||
return values.join(separator) | ||
} | ||
export {toString} from './lib/index.js' |
{ | ||
"name": "nlcst-to-string", | ||
"version": "3.1.0", | ||
"version": "3.1.1", | ||
"description": "nlcst utility to transform a tree to a string", | ||
@@ -32,2 +32,3 @@ "license": "MIT", | ||
"files": [ | ||
"lib/", | ||
"index.d.ts", | ||
@@ -40,20 +41,18 @@ "index.js" | ||
"devDependencies": { | ||
"@types/tape": "^4.0.0", | ||
"@types/node": "^18.0.0", | ||
"c8": "^7.0.0", | ||
"prettier": "^2.0.0", | ||
"remark-cli": "^10.0.0", | ||
"remark-cli": "^11.0.0", | ||
"remark-preset-wooorm": "^9.0.0", | ||
"rimraf": "^3.0.0", | ||
"tape": "^5.0.0", | ||
"type-coverage": "^2.0.0", | ||
"typescript": "^4.0.0", | ||
"unist-builder": "^3.0.0", | ||
"xo": "^0.44.0" | ||
"xo": "^0.53.0" | ||
}, | ||
"scripts": { | ||
"prepack": "npm run build && npm run format", | ||
"build": "rimraf \"*.d.ts\" && tsc && type-coverage", | ||
"build": "tsc --build --clean && tsc --build && type-coverage", | ||
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix", | ||
"test-api": "node test.js", | ||
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js", | ||
"test-api": "node --conditions development test.js", | ||
"test-coverage": "c8 --check-coverage --100 --reporter lcov npm run test-api", | ||
"test": "npm run build && npm run format && npm run test-coverage" | ||
@@ -60,0 +59,0 @@ }, |
@@ -11,11 +11,31 @@ # nlcst-to-string | ||
[**nlcst**][nlcst] utility to serialize a node. | ||
[nlcst][] utility to serialize a node. | ||
## Contents | ||
* [What is this?](#what-is-this) | ||
* [When should I use this?](#when-should-i-use-this) | ||
* [Install](#install) | ||
* [Use](#use) | ||
* [API](#api) | ||
* [`toString(value[, separator])`](#tostringvalue-separator) | ||
* [Types](#types) | ||
* [Compatibility](#compatibility) | ||
* [Contribute](#contribute) | ||
* [License](#license) | ||
## What is this? | ||
This package is a utility that takes [nlcst][] nodes and gets their plain-text | ||
value. | ||
## When should I use this? | ||
This is a small utility that is useful when you’re dealing with ASTs. | ||
## Install | ||
This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c): | ||
Node 12+ is needed to use it and it must be `import`ed instead of `require`d. | ||
This package is [ESM only][esm]. | ||
In Node.js (version 14.14+ and 16.0+), install with [npm][]: | ||
[npm][]: | ||
```sh | ||
@@ -25,2 +45,16 @@ npm install nlcst-to-string | ||
In Deno with [`esm.sh`][esmsh]: | ||
```js | ||
import {toString} from 'https://esm.sh/nlcst-to-string@3' | ||
``` | ||
In browsers with [`esm.sh`][esmsh]: | ||
```html | ||
<script type="module"> | ||
import {toString} from 'https://esm.sh/nlcst-to-string@3?bundle' | ||
</script> | ||
``` | ||
## Use | ||
@@ -45,22 +79,39 @@ | ||
This package exports the following identifiers: `toString`. | ||
This package exports the identifier [`toString`][tostring]. | ||
There is no default export. | ||
### `toString(node[, separator])` | ||
### `toString(value[, separator])` | ||
Stringify the given [nlcst][] node (or list of nodes). | ||
Get the text content of a node or list of nodes. | ||
Prefers the node’s plain-text fields, otherwise serializes its children, and | ||
if the given value is an array, serialize the nodes in it. | ||
###### Parameters | ||
* `node` ([`Node`][node] or `Array.<Node>`) | ||
* `separator` (`string`, default: `''`) — Value to delimit each item | ||
* `node` ([`Node`][node] or `Array<Node>`) | ||
— node to serialize. | ||
* `separator` (`string`, default: `''`) | ||
— value to delimit each item | ||
###### Returns | ||
`string`. | ||
Result (`string`). | ||
## Types | ||
This package is fully typed with [TypeScript][]. | ||
It exports no additional types. | ||
## Compatibility | ||
Projects maintained by the unified collective are compatible with all maintained | ||
versions of Node.js. | ||
As of now, that is Node.js 14.14+ and 16.0+. | ||
Our projects sometimes work with older versions, but this is not guaranteed. | ||
## Contribute | ||
See [`contributing.md` in `syntax-tree/.github`][contributing] for ways to get | ||
started. | ||
See [`contributing.md`][contributing] in [`syntax-tree/.github`][health] for | ||
ways to get started. | ||
See [`support.md`][support] for ways to get help. | ||
@@ -106,2 +157,8 @@ | ||
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c | ||
[esmsh]: https://esm.sh | ||
[typescript]: https://www.typescriptlang.org | ||
[license]: license | ||
@@ -111,10 +168,14 @@ | ||
[contributing]: https://github.com/syntax-tree/.github/blob/HEAD/contributing.md | ||
[health]: https://github.com/syntax-tree/.github | ||
[support]: https://github.com/syntax-tree/.github/blob/HEAD/support.md | ||
[contributing]: https://github.com/syntax-tree/.github/blob/main/contributing.md | ||
[coc]: https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md | ||
[support]: https://github.com/syntax-tree/.github/blob/main/support.md | ||
[coc]: https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md | ||
[nlcst]: https://github.com/syntax-tree/nlcst | ||
[node]: https://github.com/syntax-tree/nlcst#nodes | ||
[tostring]: #tostringvalue-separator |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9545
9
7
64
177