Skip to content

Commit abd174a

Browse files
committed
Adds first implementation of JSX parser
1 parent 0b8bea8 commit abd174a

File tree

5 files changed

+24
-5
lines changed

5 files changed

+24
-5
lines changed

dist/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export declare const printMsg: () => void;
1+
export declare function kuru(tagOrNodeFunction: any, attrs: any, ...children: any[]): any;

dist/index.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
11
"use strict";
22
Object.defineProperty(exports, "__esModule", { value: true });
3-
exports.printMsg = function () { return console.log("Message from npm package"); };
3+
function kuru(tagOrNodeFunction, attrs, ...children) {
4+
if (typeof tagOrNodeFunction === 'function')
5+
return tagOrNodeFunction(); //this allows for custom JSX functions(Components)
6+
if (Array.isArray(children[0]))
7+
children = children[0]; //this allow for jsx to be put in array and rendered still
8+
return children.reduce((element, child) => (child instanceof HTMLElement && element.appendChild(child),
9+
(typeof child === 'string' || typeof child === 'number') && (element.appendChild(document.createTextNode(String(child)))),
10+
element), //returns the mutated element
11+
Object.assign(document.createElement(tagOrNodeFunction), attrs)); //this allows attributes on elements, ie. class,id,etc..
12+
}
13+
exports.kuru = kuru;

index.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,10 @@
1-
export const printMsg = () => console.log("Message from npm package")
1+
export function kuru(tagOrNodeFunction: any, attrs: any, ...children: any[]) {
2+
if (typeof tagOrNodeFunction === 'function') return tagOrNodeFunction()//this allows for custom JSX functions(Components)
3+
if (Array.isArray(children[0])) children = children[0]//this allow for jsx to be put in array and rendered still
4+
5+
return children.reduce((element, child) => (
6+
child instanceof HTMLElement && element.appendChild(child),
7+
(typeof child === 'string' || typeof child === 'number') && (element.appendChild(document.createTextNode(String(child)))),
8+
element),//returns the mutated element
9+
Object.assign(document.createElement(tagOrNodeFunction), attrs))//this allows attributes on elements, ie. class,id,etc..
10+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "kuru",
3-
"version": "1.0.01",
3+
"version": "1.0.12",
44
"description": "JSX parser with using React",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"compilerOptions": {
3-
"target": "es5",
3+
"target": "es6",
44
"module": "commonjs",
55
"declaration": true,
66
"outDir": "./dist",

0 commit comments

Comments
 (0)