-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes GH-6. Reviewed-by: Christian Murphy <[email protected]> Reviewed-by: Remco Haszing <[email protected]>
- Loading branch information
Showing
9 changed files
with
106 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
.DS_Store | ||
*.d.ts | ||
*.log | ||
coverage/ | ||
node_modules/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,46 @@ | ||
export function u(type, props, value) { | ||
var node = {type: String(type)} | ||
/** | ||
* @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 | ||
*/ | ||
|
||
if ( | ||
(value === undefined || value === null) && | ||
(typeof props !== 'object' || Array.isArray(props)) | ||
) { | ||
value = props | ||
} else { | ||
Object.assign(node, props) | ||
} | ||
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 (Array.isArray(value)) { | ||
node.children = value | ||
} else if (value !== undefined && value !== null) { | ||
node.value = String(value) | ||
} | ||
if ( | ||
(value === undefined || value === null) && | ||
(typeof props === 'string' || Array.isArray(props)) | ||
) { | ||
value = props | ||
} else { | ||
Object.assign(node, props) | ||
} | ||
|
||
return node | ||
} | ||
if (Array.isArray(value)) { | ||
node.children = value | ||
} else if (value !== undefined && value !== null) { | ||
node.value = String(value) | ||
} | ||
|
||
return node | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import {expectType, expectAssignable} from 'tsd' | ||
import {Text, List, ListItem, HTML} from 'mdast' | ||
import {u} from './index.js' | ||
|
||
expectType<{type: 'example'}>(u('example')) | ||
expectType<{type: 'example'} & {property: true}>(u('example', {property: true})) | ||
|
||
const node1 = u('text', 'text') | ||
|
||
expectType<{type: 'text'; value: string}>(node1) | ||
expectAssignable<Text>(node1) | ||
|
||
const node2 = u('list', [ | ||
u('listItem', [u('html', {checked: true}, '<strong>text</strong>')]) | ||
]) | ||
|
||
expectType<{ | ||
type: 'list' | ||
children: Array<{ | ||
type: 'listItem' | ||
children: Array<{type: 'html'; value: string} & {checked: boolean}> | ||
}> | ||
}>(node2) | ||
expectAssignable<List>(node2) | ||
expectAssignable<ListItem>(node2.children[0]) | ||
expectAssignable<HTML>(node2.children[0].children[0]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"include": ["*.js"], | ||
"compilerOptions": { | ||
"target": "ES2020", | ||
"lib": ["ES2020"], | ||
"module": "ES2020", | ||
"moduleResolution": "node", | ||
"allowJs": true, | ||
"checkJs": true, | ||
"declaration": true, | ||
"emitDeclarationOnly": true, | ||
"allowSyntheticDefaultImports": true, | ||
"skipLibCheck": true | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.