@burger-editor/frozen-patty 0.10.0 → 0.11.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.
Files changed (49) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +11 -12
  3. package/{lib → dist}/frozen-patty.d.ts +4 -16
  4. package/dist/frozen-patty.js +43 -0
  5. package/dist/get-value.d.ts +10 -0
  6. package/dist/get-value.js +15 -0
  7. package/dist/get-values.d.ts +10 -0
  8. package/{lib/getValue.js → dist/get-values.js} +30 -32
  9. package/dist/get.d.ts +2 -0
  10. package/dist/get.js +12 -0
  11. package/dist/index.d.ts +9 -0
  12. package/dist/index.js +10 -0
  13. package/dist/set-value.d.ts +14 -0
  14. package/{lib/setValue.js → dist/set-value.js} +96 -103
  15. package/dist/set.d.ts +2 -0
  16. package/dist/set.js +52 -0
  17. package/dist/types.d.ts +6 -0
  18. package/dist/types.js +1 -0
  19. package/{lib/util.d.ts → dist/utils.d.ts} +1 -5
  20. package/dist/utils.js +21 -0
  21. package/package.json +34 -39
  22. package/lib/frozen-patty.d.ts.map +0 -1
  23. package/lib/frozen-patty.js +0 -47
  24. package/lib/frozen-patty.js.map +0 -1
  25. package/lib/get.d.ts +0 -4
  26. package/lib/get.d.ts.map +0 -1
  27. package/lib/get.js +0 -20
  28. package/lib/get.js.map +0 -1
  29. package/lib/getValue.d.ts +0 -10
  30. package/lib/getValue.d.ts.map +0 -1
  31. package/lib/getValue.js.map +0 -1
  32. package/lib/index.d.ts +0 -35
  33. package/lib/index.d.ts.map +0 -1
  34. package/lib/index.js +0 -55
  35. package/lib/index.js.map +0 -1
  36. package/lib/polyfill.d.ts +0 -1
  37. package/lib/polyfill.d.ts.map +0 -1
  38. package/lib/polyfill.js +0 -20
  39. package/lib/polyfill.js.map +0 -1
  40. package/lib/set.d.ts +0 -4
  41. package/lib/set.d.ts.map +0 -1
  42. package/lib/set.js +0 -56
  43. package/lib/set.js.map +0 -1
  44. package/lib/setValue.d.ts +0 -15
  45. package/lib/setValue.d.ts.map +0 -1
  46. package/lib/setValue.js.map +0 -1
  47. package/lib/util.d.ts.map +0 -1
  48. package/lib/util.js +0 -30
  49. package/lib/util.js.map +0 -1
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2017 Yusuke Hirao
3
+ Copyright (c) 2017-2024 D-ZERO Co., Ltd.
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -27,10 +27,9 @@ FrozenPatty('<div data-bge="text">value</div>', { attr: 'bge' }).toJSON(); // =>
27
27
  ### Data merge
28
28
 
29
29
  ```js
30
- FrozenPatty('<div data-field="text">value</div>').merge({ text: 'merged' }).toHTML() // => "<div data-field="text">merged</div>";
30
+ FrozenPatty('<div data-field="text">value</div>').merge({ text: 'merged' }).toHTML(); // => "<div data-field="text">merged</div>";
31
31
  ```
32
32
 
33
-
34
33
  ## API
35
34
 
36
35
  ### FrozenPatty (html[, options])
@@ -39,24 +38,24 @@ Extraction data from HTML.
39
38
 
40
39
  #### arguments
41
40
 
42
- args|type|required|descriptions
43
- ---|---|---|---
44
- html|`string`|required|Original HTML
45
- options|`Object`|optional|†
41
+ | args | type | required | descriptions |
42
+ | ------- | -------- | -------- | ------------- |
43
+ | html | `string` | required | Original HTML |
44
+ | options | `Object` | optional | † |
46
45
 
47
46
  #### options†
48
47
 
49
- options|type|default|descriptions
50
- ---|---|---|---
51
- attr|string|`"field"`|**Data attribute** name for specifying the node that FrozenPatty treats as a _field_
48
+ | options | type | default | descriptions |
49
+ | ------- | ------ | --------- | ------------------------------------------------------------------------------------ |
50
+ | attr | string | `"field"` | **Data attribute** name for specifying the node that FrozenPatty treats as a _field_ |
52
51
 
53
52
  ### merge (data)
54
53
 
55
54
  Data merge
56
55
 
57
- args|type|required|descriptions
58
- ---|---|---|---
59
- data|`Object`|required|New data
56
+ | args | type | required | descriptions |
57
+ | ---- | -------- | -------- | ------------ |
58
+ | data | `Object` | required | New data |
60
59
 
61
60
  ### toJSON
62
61
 
@@ -1,11 +1,6 @@
1
+ import type { Filter, FrozenPattyData } from './types.js';
1
2
  export default class FrozenPatty {
2
- private _dom;
3
- private _attr;
4
- private _typeConvert;
5
- /**
6
- * Value filter
7
- */
8
- private _filter?;
3
+ #private;
9
4
  /**
10
5
  *
11
6
  * @param html Original HTML
@@ -13,9 +8,9 @@ export default class FrozenPatty {
13
8
  */
14
9
  constructor(html: string, options?: FrozenPattyOptions);
15
10
  merge(data: FrozenPattyData): this;
16
- toJSON(filtering?: boolean): FrozenPattyData;
17
- toHTML(): string;
18
11
  toDOM(): Element;
12
+ toHTML(): string;
13
+ toJSON(filtering?: boolean): FrozenPattyData;
19
14
  }
20
15
  export interface FrozenPattyOptions {
21
16
  /**
@@ -42,10 +37,3 @@ export interface FrozenPattyOptions {
42
37
  */
43
38
  valueFilter?: Filter;
44
39
  }
45
- export interface FrozenPattyData {
46
- [filed: string]: PrimitiveData;
47
- }
48
- export declare type PrimitiveDatum = string | number | boolean | null | undefined;
49
- export declare type PrimitiveData = PrimitiveDatum | PrimitiveDatum[];
50
- export declare type Filter = <T>(value: T) => T;
51
- //# sourceMappingURL=frozen-patty.d.ts.map
@@ -0,0 +1,43 @@
1
+ import get from './get.js';
2
+ import set from './set.js';
3
+ export default class FrozenPatty {
4
+ #attr = 'field';
5
+ #dom;
6
+ /**
7
+ * Value filter
8
+ */
9
+ #filter;
10
+ #typeConvert = false;
11
+ /**
12
+ *
13
+ * @param html Original HTML
14
+ * @param options Options
15
+ */
16
+ constructor(html, options) {
17
+ this.#dom = document.createElement('fp-placeholer');
18
+ this.#dom.innerHTML = html;
19
+ if (options) {
20
+ if (options.attr) {
21
+ this.#attr = options.attr;
22
+ }
23
+ this.#typeConvert = !!options.typeConvert;
24
+ this.#filter = options.valueFilter;
25
+ }
26
+ }
27
+ merge(data) {
28
+ const currentData = this.toJSON(false);
29
+ const newData = Object.assign(currentData, data);
30
+ this.#dom = set(this.#dom, newData, this.#attr, this.#filter);
31
+ return this;
32
+ }
33
+ toDOM() {
34
+ return this.#dom;
35
+ }
36
+ toHTML() {
37
+ return this.#dom.innerHTML;
38
+ }
39
+ toJSON(filtering = true) {
40
+ const filter = filtering ? this.#filter : undefined;
41
+ return get(this.#dom, this.#attr, this.#typeConvert, filter);
42
+ }
43
+ }
@@ -0,0 +1,10 @@
1
+ import type { Filter, PrimitiveDatum } from './types.js';
2
+ /**
3
+ * Get value from an element
4
+ *
5
+ * @param el A target element
6
+ * @param name A label name
7
+ * @param typeConvert Auto covert type of value
8
+ * @param attr Data attribute name for specifying the node that FrozenPatty treats as a field
9
+ */
10
+ export declare function getValue(el: Element, name: string, typeConvert?: boolean, attr?: string, filter?: Filter): PrimitiveDatum | PrimitiveDatum[];
@@ -0,0 +1,15 @@
1
+ import { getValues } from './get-values.js';
2
+ import { arrayToHash } from './utils.js';
3
+ /**
4
+ * Get value from an element
5
+ *
6
+ * @param el A target element
7
+ * @param name A label name
8
+ * @param typeConvert Auto covert type of value
9
+ * @param attr Data attribute name for specifying the node that FrozenPatty treats as a field
10
+ */
11
+ export function getValue(el, name, typeConvert = false, attr = 'field', filter) {
12
+ const value = getValues(el, typeConvert, attr, filter);
13
+ const data = arrayToHash(value);
14
+ return data[name];
15
+ }
@@ -0,0 +1,10 @@
1
+ import type { Filter, PrimitiveDatum } from './types.js';
2
+ /**
3
+ * Get value from an element
4
+ *
5
+ * @param el A target element
6
+ * @param name A label name
7
+ * @param typeConvert Auto covert type of value
8
+ * @param attr Data attribute name for specifying the node that FrozenPatty treats as a field
9
+ */
10
+ export declare function getValues(el: Element, typeConvert?: boolean, attr?: string, filter?: Filter): [string | number, PrimitiveDatum, boolean][];
@@ -1,43 +1,41 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
1
  /**
4
2
  * Get value from an element
5
3
  *
6
- * @param el HTMLElement
7
- * @param attr Data attribute name for specifying the node that FrozenPatty treats as a field
4
+ * @param el A target element
5
+ * @param name A label name
8
6
  * @param typeConvert Auto covert type of value
7
+ * @param attr Data attribute name for specifying the node that FrozenPatty treats as a field
9
8
  */
10
- function default_1(el, attr, typeConvert, filter) {
9
+ export function getValues(el, typeConvert = false, attr = 'field', filter) {
11
10
  /**
12
11
  * [key, value, forceArray]
13
12
  */
14
- var result = [];
15
- var rawValue = el.getAttribute("data-".concat(attr));
16
- var listRoot = el.closest("[data-".concat(attr, "-list]"));
17
- var forceArray = !!listRoot;
13
+ const result = [];
14
+ const rawValue = el.getAttribute(`data-${attr}`);
15
+ const listRoot = el.closest(`[data-${attr}-list]`);
16
+ const forceArray = !!listRoot;
18
17
  if (rawValue == null) {
19
- throw new Error("data-".concat(attr, " attriblute is empty."));
18
+ throw new Error(`data-${attr} attriblute is empty.`);
20
19
  }
21
- var fieldList = "".concat(rawValue).split(/\s*,\s*/);
20
+ const fieldList = `${rawValue}`.split(/\s*,\s*/);
22
21
  // console.log({fieldList, el: el.innerHTML});
23
- for (var _i = 0, fieldList_1 = fieldList; _i < fieldList_1.length; _i++) {
24
- var fieldName = fieldList_1[_i];
25
- var splitKey = void 0;
26
- var keyAttr = '';
27
- var value = void 0;
22
+ for (let fieldName of fieldList) {
23
+ let splitKey;
24
+ let keyAttr = '';
25
+ let value;
28
26
  fieldName = fieldName.trim();
29
- if (/^[a-z_-](?:[a-z0-9_-])*:[a-z_-](?:[a-z0-9_-])*(?:\([a-z-]+\))?/i.test(fieldName)) {
27
+ if (/^[_a-z-][\w-]*:[_a-z-][\w-]*(?:\([a-z-]+\))?/i.test(fieldName)) {
30
28
  splitKey = fieldName.split(':');
31
- fieldName = splitKey[0].trim();
32
- keyAttr = splitKey[1].trim();
29
+ fieldName = splitKey[0]?.trim() ?? '';
30
+ keyAttr = splitKey[1]?.trim() ?? '';
33
31
  }
34
32
  // console.log({fieldName, keyAttr, el: el.innerHTML});
35
33
  if (keyAttr === 'text') {
36
34
  value = el.innerHTML;
37
35
  }
38
36
  else if (/^style\([a-z-]+\)$/i.test(keyAttr)) {
39
- var css = keyAttr.replace(/^style\(([a-z-]+)\)$/i, '$1');
40
- var style = void 0;
37
+ const css = keyAttr.replace(/^style\(([a-z-]+)\)$/i, '$1');
38
+ let style;
41
39
  if (el instanceof HTMLElement) {
42
40
  style = el.style;
43
41
  }
@@ -56,7 +54,7 @@ function default_1(el, attr, typeConvert, filter) {
56
54
  if (el instanceof HTMLInputElement ||
57
55
  el instanceof HTMLSelectElement ||
58
56
  el instanceof HTMLTextAreaElement) {
59
- var val = el.value;
57
+ const val = el.value;
60
58
  if (Array.isArray(val)) {
61
59
  value = cast(val[0]);
62
60
  }
@@ -77,7 +75,6 @@ function default_1(el, attr, typeConvert, filter) {
77
75
  // console.log({result});
78
76
  return result;
79
77
  }
80
- exports.default = default_1;
81
78
  function getAttribute(el, keyAttr, typeConvert) {
82
79
  switch (keyAttr) {
83
80
  case 'contenteditable': {
@@ -85,7 +82,7 @@ function getAttribute(el, keyAttr, typeConvert) {
85
82
  return el.contentEditable === '' || el.contentEditable === 'true';
86
83
  }
87
84
  else {
88
- return (el.getAttribute(keyAttr) === '' || el.getAttribute(keyAttr) === 'true');
85
+ return el.getAttribute(keyAttr) === '' || el.getAttribute(keyAttr) === 'true';
89
86
  }
90
87
  }
91
88
  case 'checked': {
@@ -104,8 +101,8 @@ function getAttribute(el, keyAttr, typeConvert) {
104
101
  return el.getAttribute(keyAttr) || ''; // return plain string
105
102
  }
106
103
  default: {
107
- if (/^data-/.test(keyAttr)) {
108
- var value = el.getAttribute(keyAttr) || '';
104
+ if (keyAttr.startsWith('data-')) {
105
+ const value = el.getAttribute(keyAttr) || '';
109
106
  if (typeConvert) {
110
107
  return cast(value);
111
108
  }
@@ -120,13 +117,15 @@ function cast(value) {
120
117
  return '';
121
118
  }
122
119
  switch (value) {
123
- case 'true':
120
+ case 'true': {
124
121
  return true;
125
- case 'false':
122
+ }
123
+ case 'false': {
126
124
  return false;
125
+ }
127
126
  }
128
- var numeric = Number(value);
129
- if (isFinite(numeric)) {
127
+ const numeric = Number(value);
128
+ if (Number.isFinite(numeric)) {
130
129
  return numeric;
131
130
  }
132
131
  return value;
@@ -136,7 +135,6 @@ function cast(value) {
136
135
  *
137
136
  */
138
137
  function getBackgroundImagePath(value) {
139
- var origin = "".concat(location.protocol, "//").concat(location.hostname).concat(location.port ? ":".concat(location.port) : '');
138
+ const origin = `${location.protocol}//${location.hostname}${location.port ? `:${location.port}` : ''}`;
140
139
  return decodeURI(value.replace(/^url\(["']?([^"']+)["']?\)$/i, '$1').replace(origin, ''));
141
140
  }
142
- //# sourceMappingURL=getValue.js.map
package/dist/get.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ import type { Filter, FrozenPattyData } from './types.js';
2
+ export default function (el: Element, attr: string, typeConvert: boolean, filter?: Filter): FrozenPattyData;
package/dist/get.js ADDED
@@ -0,0 +1,12 @@
1
+ import { getValues } from './get-values.js';
2
+ import { arrayToHash } from './utils.js';
3
+ export default function (el, attr, typeConvert, filter) {
4
+ // eslint-disable-next-line unicorn/prefer-spread
5
+ const filedElements = Array.from(el.querySelectorAll(`[data-${attr}]`));
6
+ let values = [];
7
+ for (const _el of filedElements) {
8
+ values = [...values, ...getValues(_el, typeConvert, attr, filter)];
9
+ }
10
+ const result = arrayToHash(values);
11
+ return result;
12
+ }
@@ -0,0 +1,9 @@
1
+ import type { FrozenPattyOptions } from './frozen-patty.js';
2
+ import FrozenPatty from './frozen-patty.js';
3
+ /**
4
+ * Pure HTML to JSON converter that not use template engine.
5
+ *
6
+ * @param html Original HTML
7
+ * @param options Options
8
+ */
9
+ export default function frozenPatty(html: string, options?: FrozenPattyOptions): FrozenPatty;
package/dist/index.js ADDED
@@ -0,0 +1,10 @@
1
+ import FrozenPatty from './frozen-patty.js';
2
+ /**
3
+ * Pure HTML to JSON converter that not use template engine.
4
+ *
5
+ * @param html Original HTML
6
+ * @param options Options
7
+ */
8
+ export default function frozenPatty(html, options) {
9
+ return new FrozenPatty(html, options);
10
+ }
@@ -0,0 +1,14 @@
1
+ import type { Filter, PrimitiveDatum } from './types.js';
2
+ /**
3
+ * Set value to an element
4
+ *
5
+ * ```html
6
+ * <div [target-attribute] data-[attr]="[name]:[target-attribute]"></div>
7
+ * ```
8
+ *
9
+ * @param el A target element
10
+ * @param name A label name
11
+ * @param datum A datum of value
12
+ * @param attr Data attribute name for specifying the node that FrozenPatty treats as a field
13
+ */
14
+ export declare function setValue(el: Element, name: string, datum: PrimitiveDatum, attr?: string, filter?: Filter): void;