@oscarpalmer/abydon 0.15.0 → 0.16.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/src/parse.ts CHANGED
@@ -1,11 +1,8 @@
1
1
  import {isNullableOrWhitespace} from '@oscarpalmer/atoms/is';
2
+ import {EXPRESSION_ABYDON_ATTRIBUTE_FULL} from './constants';
2
3
  import type {FragmentData} from './models';
3
4
 
4
- function handleExpression(
5
- data: FragmentData,
6
- prefix: string,
7
- expression: unknown,
8
- ): string {
5
+ function handleExpression(data: FragmentData, prefix: string, expression: unknown): string {
9
6
  if (Array.isArray(expression)) {
10
7
  const {length} = expression;
11
8
 
@@ -18,13 +15,8 @@ function handleExpression(
18
15
  return `${prefix}${expressions}`;
19
16
  }
20
17
 
21
- if (
22
- typeof expression === 'function' ||
23
- (typeof expression === 'object' && expression != null)
24
- ) {
25
- const index = data.values.push(expression) - 1;
26
-
27
- return `${prefix}<!--abydon.${index}-->`;
18
+ if (typeof expression === 'function' || (typeof expression === 'object' && expression != null)) {
19
+ return transformExpression(prefix, data.values.push(expression) - 1);
28
20
  }
29
21
 
30
22
  return isNullableOrWhitespace(expression) ? prefix : `${prefix}${expression}`;
@@ -40,15 +32,21 @@ export function parse(data: FragmentData): string {
40
32
  data.template = '';
41
33
 
42
34
  for (let index = 0; index < length; index += 1) {
43
- data.template += handleExpression(
44
- data,
45
- data.strings[index],
46
- data.expressions[index],
47
- );
35
+ data.template += handleExpression(data, data.strings[index], data.expressions[index]);
48
36
  }
49
37
 
38
+ data.template = data.template.replaceAll(EXPRESSION_ABYDON_ATTRIBUTE_FULL, transformAttribute);
39
+
50
40
  data.expressions = [];
51
41
  data.strings = [] as never;
52
42
 
53
43
  return data.template;
54
44
  }
45
+
46
+ function transformAttribute(_: string, name: string, index: string): string {
47
+ return `_${name}="@abydon.${index}@"`;
48
+ }
49
+
50
+ function transformExpression(prefix: string, index: number): string {
51
+ return `${prefix}<!--abydon.${index}-->`;
52
+ }
@@ -1,9 +1,17 @@
1
- export declare const ABORT_CONTROLLERS: WeakMap<HTMLOrSVGElement, AbortController>;
2
1
  export declare const ATTRIBUTE_CLASS_PREFIX_LENGTH = 6;
2
+ export declare const EVENT_DEFAULTS: Record<string, string>;
3
+ export declare const EXPRESSION_ABYDON_ATTRIBUTE_FULL: RegExp;
4
+ export declare const EXPRESSION_ABYDON_ATTRIBUTE_PREFIX: RegExp;
5
+ export declare const EXPRESSION_ABYDON_CONTENT: RegExp;
3
6
  export declare const EXPRESSION_ATTRIBUTE_CLASS: RegExp;
4
7
  export declare const EXPRESSION_ATTRIBUTE_STYLE_FULL: RegExp;
5
8
  export declare const EXPRESSION_ATTRIBUTE_STYLE_PREFIX: RegExp;
6
- export declare const EXPRESSION_COMMENT_FULL: RegExp;
7
- export declare const EXPRESSION_COMMENT_CONTENT: RegExp;
9
+ export declare const EXPRESSION_EVENT_CHANGE_TYPES: RegExp;
8
10
  export declare const EXPRESSION_EVENT_NAME: RegExp;
9
- export declare const REASON_EVENT_REMOVED = "Event removed as element was removed from document by Abydon";
11
+ export declare const EXPRESSION_EVENT_OPTIONS_ACTIVE: RegExp;
12
+ export declare const EXPRESSION_EVENT_OPTIONS_CAPTURE: RegExp;
13
+ export declare const EXPRESSION_EVENT_OPTIONS_ONCE: RegExp;
14
+ export declare const EXPRESSION_EVENT_PREFIX: RegExp;
15
+ export declare const EXPRESSION_PERIOD: RegExp;
16
+ export declare const NAME_FRAGMENT = "$fragment";
17
+ export declare const NAME_FRAGMENTS = "$fragments";
package/types/index.d.ts CHANGED
@@ -2,8 +2,19 @@ import '@oscarpalmer/mora';
2
2
  import type { ReactiveArray } from '@oscarpalmer/mora';
3
3
  import { Fragment } from './fragment';
4
4
  import { Fragments } from './fragments';
5
+ /**
6
+ * Create Fragments from a reactive array
7
+ * @param array Reactive array
8
+ * @param identify Function to identify item
9
+ * @param fragment Function to create fragment from item
10
+ * @returns Fragments
11
+ */
5
12
  export declare function fragments<Item>(array: ReactiveArray<Item>, identify: (item: Item) => unknown, fragment: (item: Item) => Fragment): Fragments;
6
- export declare function html(strings: TemplateStringsArray, ...values: unknown[]): unknown;
13
+ /**
14
+ * Create Fragment from a template
15
+ * @returns Fragment
16
+ */
17
+ export declare function html(template: TemplateStringsArray, ...values: unknown[]): Fragment;
7
18
  export * from '@oscarpalmer/mora';
8
19
  export type { Fragment } from './fragment';
9
20
  export type { Fragments } from './fragments';
package/types/models.d.ts CHANGED
@@ -1,8 +1,17 @@
1
1
  import type { Reactive, ReactiveArray, Unsubscribe } from '@oscarpalmer/mora';
2
2
  import type { Fragment } from './fragment';
3
3
  export type FragmentConfiguration = {
4
+ /**
5
+ * Should the template be cached? _(defaults to `true`)_
6
+ */
7
+ cache?: boolean;
8
+ /**
9
+ * Identifier for the fragment
10
+ *
11
+ * _(An identifier can be used to uniquely identify a fragment,
12
+ * which helps prevent re-rendering in certain scenarios)_
13
+ */
4
14
  identifier?: unknown;
5
- ignoreCache?: boolean;
6
15
  };
7
16
  export type FragmentData = {
8
17
  expressions: unknown[];
@@ -1,3 +1,2 @@
1
- import type { HTMLOrSVGElement } from '@oscarpalmer/toretto/models';
2
1
  import type { FragmentData } from '../../models';
3
- export declare function mapAttributes(data: FragmentData, element: HTMLOrSVGElement): void;
2
+ export declare function mapAttributes(data: FragmentData, element: HTMLElement | SVGElement): void;
@@ -1,3 +1,2 @@
1
- import type { HTMLOrSVGElement } from '@oscarpalmer/toretto/models';
2
1
  import type { FragmentData } from '../../models';
3
- export declare function setAttribute(data: FragmentData, element: HTMLOrSVGElement, name: string, value: unknown): void;
2
+ export declare function setAttribute(data: FragmentData, element: HTMLElement | SVGElement, name: string, value: unknown): void;
@@ -1,3 +1 @@
1
- import type { HTMLOrSVGElement } from '@oscarpalmer/toretto/models';
2
- export declare function mapEvent(element: HTMLOrSVGElement, name: string, value: unknown): void;
3
- export declare function removeEvents(element: HTMLOrSVGElement): void;
1
+ export declare function mapEvent(element: HTMLElement | SVGElement, name: string, value: unknown): void;