unist-builder
Advanced tools
Comparing version 3.0.1 to 4.0.0
export {u} from './lib/index.js' | ||
export type ChildrenOrValue = import('./lib/index.js').ChildrenOrValue | ||
export type Props = import('./lib/index.js').Props | ||
export type ChildrenOrValue = import('./lib/index.js').ChildrenOrValue |
/** | ||
* @typedef {import('./lib/index.js').ChildrenOrValue} ChildrenOrValue | ||
* @typedef {import('./lib/index.js').Props} Props | ||
* @typedef {import('./lib/index.js').ChildrenOrValue} ChildrenOrValue | ||
*/ | ||
export {u} from './lib/index.js' |
@@ -1,68 +0,66 @@ | ||
/** | ||
* @typedef {import('unist').Node} Node | ||
*/ | ||
/** | ||
* @typedef {Array<Node> | string} ChildrenOrValue | ||
* List to use as `children` or value to use as `value`. | ||
* | ||
* @typedef {Record<string, unknown>} Props | ||
* Other fields to add to the node. | ||
*/ | ||
/** | ||
* Build a node. | ||
* | ||
* @param type | ||
* Node type. | ||
* @param props | ||
* Fields assigned to node. | ||
* @param value | ||
* Children of node or value of `node` (cast to string). | ||
* @returns | ||
* Built node. | ||
*/ | ||
export const u: (<T extends string>( | ||
export function u< | ||
T extends string, | ||
P extends Record<string, unknown>, | ||
C extends import('unist').Node[] | ||
>( | ||
type: T | ||
) => { | ||
): { | ||
type: T | ||
}) & | ||
(<T_1 extends string, P extends Props>( | ||
type: T_1, | ||
props: P | ||
) => { | ||
type: T_1 | ||
} & P) & | ||
(<T_2 extends string>( | ||
type: T_2, | ||
value: string | ||
) => { | ||
type: T_2 | ||
value: string | ||
}) & | ||
(<T_3 extends string, P_1 extends Props>( | ||
type: T_3, | ||
props: P_1, | ||
value: string | ||
) => { | ||
type: T_3 | ||
value: string | ||
} & P_1) & | ||
(<T_4 extends string, C extends import('unist').Node<import('unist').Data>[]>( | ||
type: T_4, | ||
children: C | ||
) => { | ||
type: T_4 | ||
children: C | ||
}) & | ||
(< | ||
T_5 extends string, | ||
P_2 extends Props, | ||
C_1 extends import('unist').Node<import('unist').Data>[] | ||
>( | ||
type: T_5, | ||
props: P_2, | ||
children: C_1 | ||
) => { | ||
type: T_5 | ||
children: C_1 | ||
} & P_2) | ||
} | ||
export function u< | ||
T extends string, | ||
P extends Record<string, unknown>, | ||
C extends import('unist').Node[] | ||
>( | ||
type: T, | ||
props: P | ||
): { | ||
type: T | ||
} & P | ||
export function u< | ||
T extends string, | ||
P extends Record<string, unknown>, | ||
C extends import('unist').Node[] | ||
>( | ||
type: T, | ||
value: string | ||
): { | ||
type: T | ||
value: string | ||
} | ||
export function u< | ||
T extends string, | ||
P extends Record<string, unknown>, | ||
C extends import('unist').Node[] | ||
>( | ||
type: T, | ||
props: P, | ||
value: string | ||
): { | ||
type: T | ||
value: string | ||
} & P | ||
export function u< | ||
T extends string, | ||
P extends Record<string, unknown>, | ||
C extends import('unist').Node[] | ||
>( | ||
type: T, | ||
children: C | ||
): { | ||
type: T | ||
children: C | ||
} | ||
export function u< | ||
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 Node = import('unist').Node | ||
@@ -69,0 +67,0 @@ /** |
104
lib/index.js
@@ -16,52 +16,68 @@ /** | ||
* | ||
* @param type | ||
* @template {string} T | ||
* @template {Props} P | ||
* @template {Array<Node>} C | ||
* | ||
* @overload | ||
* @param {T} type | ||
* @returns {{type: T}} | ||
* | ||
* @overload | ||
* @param {T} type | ||
* @param {P} props | ||
* @returns {{type: T} & P} | ||
* | ||
* @overload | ||
* @param {T} type | ||
* @param {string} value | ||
* @returns {{type: T, value: string}} | ||
* | ||
* @overload | ||
* @param {T} type | ||
* @param {P} props | ||
* @param {string} value | ||
* @returns {{type: T, value: string} & P} | ||
* | ||
* @overload | ||
* @param {T} type | ||
* @param {C} children | ||
* @returns {{type: T, children: C}} | ||
* | ||
* @overload | ||
* @param {T} type | ||
* @param {P} props | ||
* @param {C} children | ||
* @returns {{type: T, children: C} & P} | ||
* | ||
* @param {string} type | ||
* Node type. | ||
* @param props | ||
* Fields assigned to node. | ||
* @param value | ||
* @param {ChildrenOrValue | Props | null | undefined} [props] | ||
* Fields assigned to node (default: `undefined`). | ||
* @param {ChildrenOrValue | null | undefined} [value] | ||
* Children of node or value of `node` (cast to string). | ||
* @returns | ||
* @returns {Node} | ||
* Built node. | ||
*/ | ||
export const u = | ||
/** | ||
* @type {( | ||
* (<T extends string>(type: T) => {type: T}) & | ||
* (<T extends string, P extends Props>(type: T, props: P) => {type: T} & P) & | ||
* (<T extends string>(type: T, value: string) => {type: T, value: string}) & | ||
* (<T extends string, P extends Props>(type: T, props: P, value: string) => {type: T, value: string} & P) & | ||
* (<T extends string, C extends Array<Node>>(type: T, children: C) => {type: T, children: C}) & | ||
* (<T extends string, P extends Props, C extends Array<Node>>(type: T, props: P, children: C) => {type: T, children: C} & P) | ||
* )} | ||
*/ | ||
( | ||
/** | ||
* @param {string} type | ||
* @param {Props | ChildrenOrValue | null | undefined} [props] | ||
* @param {ChildrenOrValue | null | undefined} [value] | ||
* @returns {Node} | ||
*/ | ||
function (type, props, value) { | ||
/** @type {Node} */ | ||
const node = {type: String(type)} | ||
export function u(type, props, value) { | ||
/** @type {Node} */ | ||
const node = {type: String(type)} | ||
if ( | ||
(value === undefined || value === null) && | ||
(typeof props === 'string' || Array.isArray(props)) | ||
) { | ||
value = props | ||
} else { | ||
Object.assign(node, props) | ||
} | ||
if ( | ||
(value === undefined || value === null) && | ||
(typeof props === 'string' || Array.isArray(props)) | ||
) { | ||
value = props | ||
} else { | ||
Object.assign(node, props) | ||
} | ||
if (Array.isArray(value)) { | ||
// @ts-expect-error: create a parent. | ||
node.children = value | ||
} else if (value !== undefined && value !== null) { | ||
// @ts-expect-error: create a literal. | ||
node.value = String(value) | ||
} | ||
if (Array.isArray(value)) { | ||
// @ts-expect-error: create a parent. | ||
node.children = value | ||
} else if (value !== undefined && value !== null) { | ||
// @ts-expect-error: create a literal. | ||
node.value = String(value) | ||
} | ||
return node | ||
} | ||
) | ||
return node | ||
} |
{ | ||
"name": "unist-builder", | ||
"version": "3.0.1", | ||
"version": "4.0.0", | ||
"description": "unist utility to create a new trees with a nice syntax", | ||
@@ -35,4 +35,3 @@ "license": "MIT", | ||
"type": "module", | ||
"main": "index.js", | ||
"types": "index.d.ts", | ||
"exports": "./index.js", | ||
"files": [ | ||
@@ -44,15 +43,15 @@ "lib/", | ||
"dependencies": { | ||
"@types/unist": "^2.0.0" | ||
"@types/unist": "^3.0.0" | ||
}, | ||
"devDependencies": { | ||
"@types/mdast": "^3.0.0", | ||
"@types/node": "^18.0.0", | ||
"c8": "^7.0.0", | ||
"@types/mdast": "^4.0.0", | ||
"@types/node": "^20.0.0", | ||
"c8": "^8.0.0", | ||
"prettier": "^2.0.0", | ||
"remark-cli": "^11.0.0", | ||
"remark-preset-wooorm": "^9.0.0", | ||
"tsd": "^0.25.0", | ||
"tsd": "^0.28.0", | ||
"type-coverage": "^2.0.0", | ||
"typescript": "^4.0.0", | ||
"xo": "^0.53.0" | ||
"typescript": "^5.0.0", | ||
"xo": "^0.54.0" | ||
}, | ||
@@ -64,19 +63,16 @@ "scripts": { | ||
"test-api": "node --conditions development test.js", | ||
"test-coverage": "c8 --check-coverage --100 --reporter lcov npm run test-api", | ||
"test-coverage": "c8 --100 --reporter lcov npm run test-api", | ||
"test": "npm run build && npm run format && npm run test-coverage" | ||
}, | ||
"prettier": { | ||
"tabWidth": 2, | ||
"useTabs": false, | ||
"singleQuote": true, | ||
"bracketSpacing": false, | ||
"semi": false, | ||
"trailingComma": "none" | ||
"singleQuote": true, | ||
"tabWidth": 2, | ||
"trailingComma": "none", | ||
"useTabs": false | ||
}, | ||
"xo": { | ||
"prettier": true | ||
}, | ||
"remarkConfig": { | ||
"plugins": [ | ||
"preset-wooorm" | ||
"remark-preset-wooorm" | ||
] | ||
@@ -87,4 +83,8 @@ }, | ||
"detail": true, | ||
"ignoreCatch": true, | ||
"strict": true | ||
}, | ||
"xo": { | ||
"prettier": true | ||
} | ||
} |
@@ -47,3 +47,3 @@ # unist-builder | ||
This package is [ESM only][esm]. | ||
In Node.js (version 14.14+ and 16.0+), install with [npm][]: | ||
In Node.js (version 16+), install with [npm][]: | ||
@@ -57,3 +57,3 @@ ```sh | ||
```js | ||
import {u} from 'https://esm.sh/unist-builder@3' | ||
import {u} from 'https://esm.sh/unist-builder@4' | ||
``` | ||
@@ -65,3 +65,3 @@ | ||
<script type="module"> | ||
import {u} from 'https://esm.sh/unist-builder@3?bundle' | ||
import {u} from 'https://esm.sh/unist-builder@4?bundle' | ||
</script> | ||
@@ -84,6 +84,6 @@ ``` | ||
console.dir(tree, {depth: null}) | ||
console.dir(tree, {depth: undefined}) | ||
``` | ||
results in the following tree: | ||
…yields: | ||
@@ -172,7 +172,10 @@ ```js | ||
Projects maintained by the unified collective are compatible with all maintained | ||
Projects maintained by the unified collective are compatible with 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. | ||
When we cut a new major release, we drop support for unmaintained versions of | ||
Node. | ||
This means we try to keep the current release line, `unist-builder@^4`, | ||
compatible with Node.js 16. | ||
## Related | ||
@@ -213,5 +216,5 @@ | ||
[size-badge]: https://img.shields.io/bundlephobia/minzip/unist-builder.svg | ||
[size-badge]: https://img.shields.io/badge/dynamic/json?label=minzipped%20size&query=$.size.compressedSize&url=https://deno.bundlejs.com/?q=unist-builder | ||
[size]: https://bundlephobia.com/result?p=unist-builder | ||
[size]: https://bundlejs.com/?q=unist-builder | ||
@@ -218,0 +221,0 @@ [sponsors-badge]: https://opencollective.com/unified/sponsors/badge.svg |
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
159
264
12791
+ Added@types/unist@3.0.3(transitive)
- Removed@types/unist@2.0.11(transitive)
Updated@types/unist@^3.0.0