Skip to content

Commit

Permalink
Refactor to move implementation to lib/
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jan 23, 2023
1 parent 667beb8 commit fac66a0
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 57 deletions.
60 changes: 3 additions & 57 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,6 @@
/**
* @typedef {import('unist').Node} Node
* @typedef {import('unist').Parent} Parent
* @typedef {import('unist').Literal} Literal
* @typedef {Record<string, unknown>} Props
* @typedef {Array<Node>|string} ChildrenOrValue
*
* @typedef {(<T extends string, P extends Record<string, unknown>, C extends Array<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 Array<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
*/

/**
* 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 = /**
* @type {BuildVoid & BuildVoidWithProps & BuildLiteral & BuildLiteralWithProps & BuildParent & BuildParentWithProps}
*/ (
/**
* @param {string} type
* @param {Props|ChildrenOrValue} [props]
* @param {ChildrenOrValue} [value]
* @returns {Node}
*/
function (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 (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
}
)
export {u} from './lib/index.js'
60 changes: 60 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* @typedef {import('unist').Node} Node
* @typedef {import('unist').Parent} Parent
* @typedef {import('unist').Literal} Literal
* @typedef {Record<string, unknown>} Props
* @typedef {Array<Node>|string} ChildrenOrValue
*
* @typedef {(<T extends string, P extends Record<string, unknown>, C extends Array<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 Array<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
*/

/**
* 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 = /**
* @type {BuildVoid & BuildVoidWithProps & BuildLiteral & BuildLiteralWithProps & BuildParent & BuildParentWithProps}
*/ (
/**
* @param {string} type
* @param {Props|ChildrenOrValue} [props]
* @param {ChildrenOrValue} [value]
* @returns {Node}
*/
function (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 (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
}
)
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"main": "index.js",
"types": "index.d.ts",
"files": [
"lib/",
"index.d.ts",
"index.js"
],
Expand Down

0 comments on commit fac66a0

Please sign in to comment.