diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index daa8c21..735450d 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -10,20 +10,17 @@ jobs: steps: - uses: actions/checkout@v3 - - name: Setup Node - uses: actions/setup-node@v4 - with: - node-version-file: ".node-version" - cache: "npm" + - name: Setup Bun + uses: oven-sh/setup-bun@v1 - name: Install dependencies - run: npm install + run: bun install - name: Build - run: npm run build + run: bun run build - name: Lint - run: npm run lint + run: bun run lint - name: Run Tests - run: npm run test || npm run test + run: bun run test || bun run test diff --git a/bun.lockb b/bun.lockb index 6461f4a..acb5801 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/dist/morphlex.d.ts b/dist/morphlex.d.ts index 8cd6f4d..e9d8d52 100644 --- a/dist/morphlex.d.ts +++ b/dist/morphlex.d.ts @@ -12,8 +12,6 @@ interface Options { beforePropertyUpdated?: (node: Node, propertyName: PropertyKey, newValue: unknown) => boolean; afterPropertyUpdated?: (node: Node, propertyName: PropertyKey, previousValue: unknown) => void; } -export declare function morph(node: ChildNode, reference: ChildNode, options?: Options): void; -export declare function morphInner(element: Element, reference: Element, options?: Options): void; -export declare function morphFromString(node: ChildNode, reference: string, options?: Options): void; -export declare function morphInnerFromString(element: Element, reference: string, options?: Options): void; +export declare function morph(node: ChildNode, reference: ChildNode | string, options?: Options): void; +export declare function morphInner(element: Element, reference: Element | string, options?: Options): void; export {}; diff --git a/dist/morphlex.js b/dist/morphlex.js index 79dcfd8..069ad76 100644 --- a/dist/morphlex.js +++ b/dist/morphlex.js @@ -1,15 +1,11 @@ export function morph(node, reference, options = {}) { + if (typeof reference === "string") reference = parseChildNodeFromString(reference); new Morph(options).morph([node, reference]); } export function morphInner(element, reference, options = {}) { + if (typeof reference === "string") reference = parseElementFromString(reference); new Morph(options).morphInner([element, reference]); } -export function morphFromString(node, reference, options = {}) { - morph(node, parseChildNodeFromString(reference), options); -} -export function morphInnerFromString(element, reference, options = {}) { - morphInner(element, parseElementFromString(reference), options); -} function parseElementFromString(string) { const node = parseChildNodeFromString(string); if (isElement(node)) return node; @@ -18,8 +14,7 @@ function parseElementFromString(string) { function parseChildNodeFromString(string) { const parser = new DOMParser(); const doc = parser.parseFromString(string, "text/html"); - const firstChild = doc.body.firstChild; - if (doc.childNodes.length === 1) return firstChild; + if (doc.childNodes.length === 1) return doc.body.firstChild; else throw new Error("[Morphlex] The string was not a valid HTML node."); } class Morph { diff --git a/package.json b/package.json index 6be5813..ee34f98 100644 --- a/package.json +++ b/package.json @@ -16,17 +16,16 @@ "url": "https://github.com/sponsors/joeldrapper" }, "scripts": { - "test": "web-test-runner test/**/*.test.js --node-resolve", - "t": "web-test-runner --node-resolve", - "build": "npx tsc && prettier --write ./src ./dist", - "watch": "npx tsc -w", - "test:watch": "npm run test -- --watch", - "lint": "prettier --check ./src ./dist ./test", - "minify": "terser dist/morphlex.js -o dist/morphlex.min.js --config-file terser-config.json", - "prepare": "npm run build && npm run minify", - "ship": "npm run prepare && npm run test && npm run lint && npm publish", - "format": "prettier --write ./src ./dist ./test", - "size": "npm run prepare && gzip-size ./dist/morphlex.min.js --raw --include-original" + "test": "bun run web-test-runner test/**/*.test.js --node-resolve", + "build": "bun run tsc && bun run prettier --write ./src ./dist", + "watch": "bun run tsc -w", + "test:watch": "bun run test -- --watch", + "lint": "bun run prettier --check ./src ./dist ./test", + "minify": "bun run terser dist/morphlex.js -o dist/morphlex.min.js --config-file terser-config.json", + "prepare": "bun run build && bun run minify", + "ship": "bun run prepare && bun run test && bun run lint && bun run publish", + "format": "bun run prettier --write ./src ./dist ./test", + "size": "bun run prepare && bun run gzip-size ./dist/morphlex.min.js --raw --include-original" }, "devDependencies": { "@open-wc/testing": "^3.0.0-next.5", diff --git a/src/morphlex.ts b/src/morphlex.ts index 6c23836..611396c 100644 --- a/src/morphlex.ts +++ b/src/morphlex.ts @@ -49,22 +49,16 @@ interface Options { afterPropertyUpdated?: (node: Node, propertyName: PropertyKey, previousValue: unknown) => void; } -export function morph(node: ChildNode, reference: ChildNode, options: Options = {}): void { +export function morph(node: ChildNode, reference: ChildNode | string, options: Options = {}): void { + if (typeof reference === "string") reference = parseChildNodeFromString(reference); new Morph(options).morph([node, reference]); } -export function morphInner(element: Element, reference: Element, options: Options = {}): void { +export function morphInner(element: Element, reference: Element | string, options: Options = {}): void { + if (typeof reference === "string") reference = parseElementFromString(reference); new Morph(options).morphInner([element, reference]); } -export function morphFromString(node: ChildNode, reference: string, options: Options = {}): void { - morph(node, parseChildNodeFromString(reference), options); -} - -export function morphInnerFromString(element: Element, reference: string, options: Options = {}): void { - morphInner(element, parseElementFromString(reference), options); -} - function parseElementFromString(string: string): Element { const node = parseChildNodeFromString(string); @@ -75,9 +69,8 @@ function parseElementFromString(string: string): Element { function parseChildNodeFromString(string: string): ChildNode { const parser = new DOMParser(); const doc = parser.parseFromString(string, "text/html"); - const firstChild = doc.body.firstChild; - if (doc.childNodes.length === 1) return firstChild as ChildNode; + if (doc.childNodes.length === 1) return doc.body.firstChild as ChildNode; else throw new Error("[Morphlex] The string was not a valid HTML node."); }