unist-builder
Advanced tools
Comparing version 3.0.0 to 3.0.1
@@ -1,78 +0,3 @@ | ||
/** | ||
* @typedef {import('unist').Node} Node | ||
* @typedef {import('unist').Parent} Parent | ||
* @typedef {import('unist').Literal} Literal | ||
* @typedef {Object.<string, unknown>} Props | ||
* @typedef {Array.<Node>|string} ChildrenOrValue | ||
* | ||
* @typedef {(<T extends string, P extends Record<string, unknown>, C extends Node[]>(type: T, props: P, children: C) => {type: T, children: C} & P)} BuildParentWithProps | ||
* @typedef {(<T extends string, P extends Record<string, unknown>>(type: T, props: P, value: string) => {type: T, value: string} & P)} BuildLiteralWithProps | ||
* @typedef {(<T extends string, P extends Record<string, unknown>>(type: T, props: P) => {type: T} & P)} BuildVoidWithProps | ||
* @typedef {(<T extends string, C extends Node[]>(type: T, children: C) => {type: T, children: C})} BuildParent | ||
* @typedef {(<T extends string>(type: T, value: string) => {type: T, value: string})} BuildLiteral | ||
* @typedef {(<T extends string>(type: T) => {type: T})} BuildVoid | ||
*/ | ||
export var u: BuildVoid & | ||
BuildVoidWithProps & | ||
BuildLiteral & | ||
BuildLiteralWithProps & | ||
BuildParent & | ||
BuildParentWithProps | ||
export type Node = import('unist').Node | ||
export type Parent = import('unist').Parent | ||
export type Literal = import('unist').Literal | ||
export type Props = { | ||
[x: string]: unknown | ||
} | ||
export type ChildrenOrValue = Array<Node> | string | ||
export type BuildParentWithProps = < | ||
T extends string, | ||
P extends Record<string, unknown>, | ||
C extends import('unist').Node[] | ||
>( | ||
type: T, | ||
props: P, | ||
children: C | ||
) => { | ||
type: T | ||
children: C | ||
} & P | ||
export type BuildLiteralWithProps = < | ||
T extends string, | ||
P extends Record<string, unknown> | ||
>( | ||
type: T, | ||
props: P, | ||
value: string | ||
) => { | ||
type: T | ||
value: string | ||
} & P | ||
export type BuildVoidWithProps = < | ||
T extends string, | ||
P extends Record<string, unknown> | ||
>( | ||
type: T, | ||
props: P | ||
) => { | ||
type: T | ||
} & P | ||
export type BuildParent = <T extends string, C extends import('unist').Node[]>( | ||
type: T, | ||
children: C | ||
) => { | ||
type: T | ||
children: C | ||
} | ||
export type BuildLiteral = <T extends string>( | ||
type: T, | ||
value: string | ||
) => { | ||
type: T | ||
value: string | ||
} | ||
export type BuildVoid = <T extends string>( | ||
type: T | ||
) => { | ||
type: T | ||
} | ||
export {u} from './lib/index.js' | ||
export type Props = import('./lib/index.js').Props | ||
export type ChildrenOrValue = import('./lib/index.js').ChildrenOrValue |
46
index.js
/** | ||
* @typedef {import('unist').Node} Node | ||
* @typedef {import('unist').Parent} Parent | ||
* @typedef {import('unist').Literal} Literal | ||
* @typedef {Object.<string, unknown>} Props | ||
* @typedef {Array.<Node>|string} ChildrenOrValue | ||
* | ||
* @typedef {(<T extends string, P extends Record<string, unknown>, C extends Node[]>(type: T, props: P, children: C) => {type: T, children: C} & P)} BuildParentWithProps | ||
* @typedef {(<T extends string, P extends Record<string, unknown>>(type: T, props: P, value: string) => {type: T, value: string} & P)} BuildLiteralWithProps | ||
* @typedef {(<T extends string, P extends Record<string, unknown>>(type: T, props: P) => {type: T} & P)} BuildVoidWithProps | ||
* @typedef {(<T extends string, C extends Node[]>(type: T, children: C) => {type: T, children: C})} BuildParent | ||
* @typedef {(<T extends string>(type: T, value: string) => {type: T, value: string})} BuildLiteral | ||
* @typedef {(<T extends string>(type: T) => {type: T})} BuildVoid | ||
* @typedef {import('./lib/index.js').Props} Props | ||
* @typedef {import('./lib/index.js').ChildrenOrValue} ChildrenOrValue | ||
*/ | ||
export var u = /** | ||
* @type {BuildVoid & BuildVoidWithProps & BuildLiteral & BuildLiteralWithProps & BuildParent & BuildParentWithProps} | ||
*/ ( | ||
/** | ||
* @param {string} type Type of node | ||
* @param {Props|ChildrenOrValue} [props] Additional properties for node (or `children` or `value`) | ||
* @param {ChildrenOrValue} [value] `children` or `value` of node | ||
* @returns {Node} | ||
*/ | ||
function (type, props, value) { | ||
/** @type {Node} */ | ||
var node = {type: String(type)} | ||
if ( | ||
(value === undefined || value === null) && | ||
(typeof props === 'string' || Array.isArray(props)) | ||
) { | ||
value = props | ||
} else { | ||
Object.assign(node, props) | ||
} | ||
if (Array.isArray(value)) { | ||
node.children = value | ||
} else if (value !== undefined && value !== null) { | ||
node.value = String(value) | ||
} | ||
return node | ||
} | ||
) | ||
export {u} from './lib/index.js' |
{ | ||
"name": "unist-builder", | ||
"version": "3.0.0", | ||
"version": "3.0.1", | ||
"description": "unist utility to create a new trees with a nice syntax", | ||
@@ -38,2 +38,3 @@ "license": "MIT", | ||
"files": [ | ||
"lib/", | ||
"index.d.ts", | ||
@@ -47,20 +48,18 @@ "index.js" | ||
"@types/mdast": "^3.0.0", | ||
"@types/tape": "^4.0.0", | ||
"@types/node": "^18.0.0", | ||
"c8": "^7.0.0", | ||
"prettier": "^2.0.0", | ||
"remark-cli": "^9.0.0", | ||
"remark-preset-wooorm": "^8.0.0", | ||
"rimraf": "^3.0.0", | ||
"tape": "^5.0.0", | ||
"tsd": "^0.14.0", | ||
"remark-cli": "^11.0.0", | ||
"remark-preset-wooorm": "^9.0.0", | ||
"tsd": "^0.25.0", | ||
"type-coverage": "^2.0.0", | ||
"typescript": "^4.0.0", | ||
"xo": "^0.38.0" | ||
"xo": "^0.53.0" | ||
}, | ||
"scripts": { | ||
"prepack": "npm run build && npm run format", | ||
"build": "rimraf \"*.d.ts\" && tsc && tsd && type-coverage", | ||
"build": "tsc --build --clean && tsc --build && tsd && 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" | ||
@@ -77,9 +76,3 @@ }, | ||
"xo": { | ||
"prettier": true, | ||
"rules": { | ||
"capitalized-comments": "off", | ||
"import/no-mutable-exports": "off", | ||
"no-var": "off", | ||
"prefer-arrow-callback": "off" | ||
} | ||
"prettier": true | ||
}, | ||
@@ -86,0 +79,0 @@ "remarkConfig": { |
157
readme.md
@@ -11,16 +11,58 @@ # unist-builder | ||
[**unist**][unist] utility to create a new [tree][]s with [hyperscript][]-like | ||
syntax. | ||
[unist][] utility to create trees with ease. | ||
## Contents | ||
* [What is this?](#what-is-this) | ||
* [When should I use this?](#when-should-i-use-this) | ||
* [Install](#install) | ||
* [Use](#use) | ||
* [API](#api) | ||
* [`u(type[, props][, children|value])`](#utype-props-childrenvalue) | ||
* [`ChildrenOrValue`](#childrenorvalue) | ||
* [`Props`](#props) | ||
* [Types](#types) | ||
* [Compatibility](#compatibility) | ||
* [Related](#related) | ||
* [Contribute](#contribute) | ||
* [License](#license) | ||
## What is this? | ||
This package is a hyperscript interface (like `createElement` from React and | ||
`h` from Vue and such) to help with creating unist trees. | ||
## When should I use this? | ||
You can use this utility in your project when you generate syntax trees with | ||
code. | ||
It helps because it replaces most of the repetition otherwise needed in a syntax | ||
tree with function calls. | ||
You can instead use [`hastscript`][hastscript] or [`xastscript`][xastscript] | ||
when creating hast (HTML) or xast (XML) nodes. | ||
## 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][]: | ||
```bash | ||
```sh | ||
npm install unist-builder | ||
``` | ||
In Deno with [`esm.sh`][esmsh]: | ||
```js | ||
import {u} from 'https://esm.sh/unist-builder@3' | ||
``` | ||
In browsers with [`esm.sh`][esmsh]: | ||
```html | ||
<script type="module"> | ||
import {u} from 'https://esm.sh/unist-builder@3?bundle' | ||
</script> | ||
``` | ||
## Use | ||
@@ -31,3 +73,3 @@ | ||
var tree = u('root', [ | ||
const tree = u('root', [ | ||
u('subtree', {id: 1}), | ||
@@ -72,3 +114,3 @@ u('subtree', {id: 2}, [ | ||
This package exports the following identifiers: `u`. | ||
This package exports the identifier [`u`][u]. | ||
There is no default export. | ||
@@ -78,34 +120,69 @@ | ||
Creates a node from `props`, `children`, and optionally `value`. | ||
Build a node. | ||
###### Signatures | ||
* `u(type[, props], children)` — create a [parent][] | ||
* `u(type[, props], value)` — create a [literal][] | ||
* `u(type[, props])` — create a void node | ||
* `u(type[, props], children)` — create a parent ([`Parent`][parent]) | ||
* `u(type[, props], value)` — create a literal ([`Literal`][literal]) | ||
* `u(type[, props])` — create a void node (neither parent not literal) | ||
###### Parameters | ||
* `type` (`string`) — node [type][] | ||
* `props` (`Object`) — other values assigned to `node` | ||
* `children` ([`Array.<Node>`][node]) — children of `node` | ||
* `value` (`*`) — value of `node` (cast to string) | ||
* `type` (`string`) | ||
— node type | ||
* `props` (`Record<string, unknown>`) | ||
— fields assigned to node | ||
* `children` ([`Array<Node>`][node]) | ||
— children of node | ||
* `value` (`*`) | ||
— value of `node` (cast to string) | ||
###### Returns | ||
[`Node`][node]. | ||
Built node ([`Node`][node]). | ||
### `ChildrenOrValue` | ||
List to use as `children` or value to use as `value` (TypeScript type). | ||
###### Type | ||
```ts | ||
type ChildrenOrValue = Array<Node> | string | ||
``` | ||
### `Props` | ||
Other fields to add to the node (TypeScript type). | ||
###### Type | ||
```ts | ||
export type Props = Record<string, unknown> | ||
``` | ||
## Types | ||
This package is fully typed with [TypeScript][]. | ||
It exports the additional types [`ChildrenOrValue`][childrenorvalue] and | ||
[`Props`][props]. | ||
## 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. | ||
## Related | ||
* [`unist-builder-blueprint`](https://github.com/syntax-tree/unist-builder-blueprint) | ||
— Convert unist trees to `unist-builder` notation | ||
* [`hastscript`](https://github.com/syntax-tree/hastscript) | ||
— Create [hast][] elements | ||
— create [hast][] trees | ||
* [`xastscript`](https://github.com/syntax-tree/xastscript) | ||
— Create [xast][] elements | ||
— create [xast][] trees | ||
## 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. | ||
@@ -151,12 +228,26 @@ | ||
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c | ||
[esmsh]: https://esm.sh | ||
[typescript]: https://www.typescriptlang.org | ||
[license]: license | ||
[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 | ||
[unist]: https://github.com/syntax-tree/unist | ||
[node]: https://github.com/syntax-tree/unist#node | ||
[parent]: https://github.com/syntax-tree/unist#parent | ||
[literal]: https://github.com/syntax-tree/unist#literal | ||
[hast]: https://github.com/syntax-tree/hast | ||
@@ -166,12 +257,10 @@ | ||
[hyperscript]: https://github.com/dominictarr/hyperscript | ||
[hastscript]: https://github.com/syntax-tree/hastscript | ||
[node]: https://github.com/syntax-tree/unist#node | ||
[xastscript]: https://github.com/syntax-tree/xastscript | ||
[tree]: https://github.com/syntax-tree/unist#tree | ||
[u]: #utype-props-childrenvalue | ||
[parent]: https://github.com/syntax-tree/unist#parent | ||
[props]: #props | ||
[literal]: https://github.com/syntax-tree/unist#literal | ||
[type]: https://github.com/syntax-tree/unist#type | ||
[childrenorvalue]: #childrenorvalue |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
13044
10
7
145
261