@oscarpalmer/abydon 0.1.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.bun.ts ADDED
@@ -0,0 +1,32 @@
1
+ import * as fs from 'node:fs/promises';
2
+
3
+ const directory = String(__dirname).replace(/\\/g, '/');
4
+ const isMjs = process.argv.includes('--mjs');
5
+
6
+ async function getFiles(path: string): Promise<string[]> {
7
+ const entries = await fs.readdir(path, {withFileTypes: true});
8
+
9
+ const files = entries
10
+ .filter(entry => entry.isFile() && entry.name.endsWith('.ts'))
11
+ .map(file => `${path}/${file.name}`);
12
+
13
+ const folders = entries.filter(entry => entry.isDirectory());
14
+
15
+ for (const folder of folders) {
16
+ files.push(...(await getFiles(`${path}/${folder.name}`)));
17
+ }
18
+
19
+ return files;
20
+ }
21
+
22
+ getFiles('./src').then(async files => {
23
+ for (const file of files) {
24
+ await Bun.build({
25
+ entrypoints: [`${directory}/${file}`],
26
+ external: isMjs ? ['*'] : [],
27
+ naming: isMjs ? '[dir]/[name].mjs' : undefined,
28
+ outdir: './dist',
29
+ root: './src',
30
+ });
31
+ }
32
+ });
package/.gitattributes ADDED
@@ -0,0 +1 @@
1
+ * text=auto eol=lf
package/biome.json ADDED
@@ -0,0 +1,22 @@
1
+ {
2
+ "$schema": "https://biomejs.dev/schemas/1.4.1/schema.json",
3
+ "files": {
4
+ "ignore": ["./dist/*"]
5
+ },
6
+ "javascript": {
7
+ "formatter": {
8
+ "arrowParentheses": "asNeeded",
9
+ "bracketSpacing": false,
10
+ "quoteStyle": "single"
11
+ }
12
+ },
13
+ "linter": {
14
+ "enabled": true,
15
+ "rules": {
16
+ "recommended": true
17
+ }
18
+ },
19
+ "organizeImports": {
20
+ "enabled": true
21
+ }
22
+ }
package/bun.lockb ADDED
Binary file
package/dist/index.js ADDED
@@ -0,0 +1,55 @@
1
+ // node_modules/@oscarpalmer/atoms/dist/js/string/index.mjs
2
+ var getString = function(value) {
3
+ if (typeof value === "string") {
4
+ return value;
5
+ }
6
+ if (typeof value !== "object" || value == null) {
7
+ return String(value);
8
+ }
9
+ const valueOff = value.valueOf?.() ?? value;
10
+ const asString = valueOff?.toString?.() ?? String(valueOff);
11
+ return asString.startsWith("[object ") ? JSON.stringify(value) : asString;
12
+ };
13
+ // node_modules/@oscarpalmer/atoms/dist/js/is.mjs
14
+ var isNullableOrWhitespace = function(value) {
15
+ return value == null || /^\s*$/.test(getString(value));
16
+ };
17
+
18
+ // src/helpers/dom.ts
19
+ function createTemplate(html) {
20
+ const template = document.createElement("template");
21
+ template.innerHTML = html;
22
+ const cloned = template.content.cloneNode(true);
23
+ const scripts = [
24
+ ...cloned instanceof Element ? cloned.querySelectorAll("script") : []
25
+ ];
26
+ const { length } = scripts;
27
+ for (let index = 0;index < length; index += 1) {
28
+ scripts[index].remove();
29
+ }
30
+ cloned.normalize();
31
+ return cloned;
32
+ }
33
+ function getNodes(node) {
34
+ return /^documentfragment$/i.test(node.constructor.name) ? [...node.childNodes] : [node];
35
+ }
36
+
37
+ // src/html.ts
38
+ function html(strings, ...values) {
39
+ const parsed = parse(strings, values);
40
+ const templated = createTemplate(parsed);
41
+ return getNodes(templated);
42
+ }
43
+ var parse = function(parts, values) {
44
+ const { length } = parts;
45
+ let template = "";
46
+ for (let index = 0;index < length; index += 1) {
47
+ const part = parts[index];
48
+ const value = values[index];
49
+ template += isNullableOrWhitespace(value) ? part : `${part}${value}`;
50
+ }
51
+ return template;
52
+ };
53
+ export {
54
+ html
55
+ };
package/package.json CHANGED
@@ -3,11 +3,37 @@
3
3
  "name": "Oscar Palmér",
4
4
  "url": "https://oscarpalmer.se"
5
5
  },
6
+ "dependencies": {
7
+ "@oscarpalmer/atoms": "^0.61.0"
8
+ },
9
+ "devDependencies": {
10
+ "@biomejs/biome": "^1.8.3",
11
+ "@types/bun": "^1.1.6",
12
+ "bun": "^1.1.20",
13
+ "typescript": "^5.5.4"
14
+ },
15
+ "exports": {
16
+ ".": {
17
+ "types": "./types/index.d.ts",
18
+ "bun": "./src/index.ts",
19
+ "import": "./dist/index.mjs",
20
+ "require": "./dist/index.js"
21
+ }
22
+ },
6
23
  "license": "MIT",
24
+ "main": "dist/index.js",
25
+ "module": "dist/index.mjs",
7
26
  "name": "@oscarpalmer/abydon",
8
27
  "repository": {
9
28
  "type": "git",
10
29
  "url": "git+https://github.com/oscarpalmer/abydon.git"
11
30
  },
12
- "version": "0.1.0"
31
+ "scripts": {
32
+ "build": "bun run clean && bunx bun ./.bun.ts && bunx bun ./.bun.ts --mjs && bun run types",
33
+ "clean": "rm -rf ./dist && rm -rf ./types && rm -rf ./tsconfig.tsbuildinfo",
34
+ "types": "bunx tsc -p ./tsconfig.json",
35
+ "watch": "bun build ./src/index.ts --outfile ./dist/index.js --watch"
36
+ },
37
+ "types": "src/types.d.ts",
38
+ "version": "0.3.0"
13
39
  }
@@ -0,0 +1,27 @@
1
+ export function createTemplate(html: string): Node {
2
+ const template = document.createElement('template');
3
+
4
+ template.innerHTML = html;
5
+
6
+ const cloned = template.content.cloneNode(true);
7
+
8
+ const scripts = [
9
+ ...(cloned instanceof Element ? cloned.querySelectorAll('script') : []),
10
+ ];
11
+
12
+ const {length} = scripts;
13
+
14
+ for (let index = 0; index < length; index += 1) {
15
+ scripts[index].remove();
16
+ }
17
+
18
+ cloned.normalize();
19
+
20
+ return cloned;
21
+ }
22
+
23
+ export function getNodes(node: Node): Node[] {
24
+ return /^documentfragment$/i.test(node.constructor.name)
25
+ ? [...node.childNodes]
26
+ : [node];
27
+ }
package/src/html.ts ADDED
@@ -0,0 +1,27 @@
1
+ import {isNullableOrWhitespace} from '@oscarpalmer/atoms/is';
2
+ import {createTemplate, getNodes} from './helpers/dom';
3
+
4
+ export function html(
5
+ strings: TemplateStringsArray,
6
+ ...values: unknown[]
7
+ ): Node[] {
8
+ const parsed = parse(strings, values);
9
+ const templated = createTemplate(parsed);
10
+
11
+ return getNodes(templated);
12
+ }
13
+
14
+ function parse(parts: TemplateStringsArray, values: unknown[]): string {
15
+ const {length} = parts;
16
+
17
+ let template = '';
18
+
19
+ for (let index = 0; index < length; index += 1) {
20
+ const part = parts[index];
21
+ const value = values[index];
22
+
23
+ template += isNullableOrWhitespace(value) ? part : `${part}${value}`;
24
+ }
25
+
26
+ return template;
27
+ }
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './html';
package/tsconfig.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "compilerOptions": {
3
+ "allowImportingTsExtensions": true,
4
+ "allowJs": true,
5
+ "allowSyntheticDefaultImports": true,
6
+ "composite": false,
7
+ "declaration": true,
8
+ "declarationDir": "./types",
9
+ "downlevelIteration": true,
10
+ "emitDeclarationOnly": true,
11
+ "forceConsistentCasingInFileNames": true,
12
+ "jsx": "react-jsx",
13
+ "lib": ["DOM", "DOM.Iterable", "ESNext"],
14
+ "module": "esnext",
15
+ "moduleDetection": "force",
16
+ "moduleResolution": "bundler",
17
+ "rootDir": "./src",
18
+ "skipLibCheck": true,
19
+ "strict": true,
20
+ "target": "esnext"
21
+ },
22
+ "exclude": ["dist", "node_modules", "test", "types", "www"]
23
+ }
@@ -0,0 +1,2 @@
1
+ export declare function createTemplate(html: string): Node;
2
+ export declare function getNodes(node: Node): Node[];
@@ -0,0 +1 @@
1
+ export declare function html(strings: TemplateStringsArray, ...values: unknown[]): Node[];
@@ -0,0 +1 @@
1
+ export * from './html';