@newy/define
v0.3.0
Published
```ts export class VNode { public children?: any[] public tag?: string constructor( public template: string | TemplateStringsArray, public propList: any[] ) {} }
Downloads
22
Readme
@newy/define
export class VNode {
public children?: any[]
public tag?: string
constructor(
public template: string | TemplateStringsArray,
public propList: any[]
) {}
}
export function h(template: TemplateStringsArray | string, ...propList: any[]) {
if (!isTemplateStringsArray(template) && !is(template, 'string'))
throw new Error('template should be a string or a TemplateStringsArray')
const vnode = new VNode(template, propList)
const res = Object.setPrototypeOf(
Object.assign(
(...children: any[]) => {
if (children.length) vnode.children = children
if (res.tag) vnode.tag = res.tag
return vnode
},
vnode,
{ [Symbol.toStringTag]: VNode.name }
),
VNode.prototype
) as ((...children: any[]) => VNode) & VNode
return res
}
export function define<T extends (...args: any[]) => any>(tag: string, fn: T, ...options: any[]) {
return (template: TemplateStringsArray | string, ...propList: Parameters<T>) => {
const res = h(template, ...propList)
res.tag = tag
return res
}
}