@burger-editor/frozen-patty 0.9.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.
- package/LICENSE +1 -1
- package/README.md +11 -12
- package/{lib → dist}/frozen-patty.d.ts +4 -16
- package/dist/frozen-patty.js +43 -0
- package/dist/get-value.d.ts +10 -0
- package/dist/get-value.js +15 -0
- package/dist/get-values.d.ts +10 -0
- package/{lib/getValue.js → dist/get-values.js} +47 -49
- package/dist/get.d.ts +2 -0
- package/dist/get.js +12 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +10 -0
- package/dist/set-value.d.ts +14 -0
- package/{lib/setValue.js → dist/set-value.js} +158 -157
- package/dist/set.d.ts +2 -0
- package/dist/set.js +52 -0
- package/dist/types.d.ts +6 -0
- package/dist/types.js +1 -0
- package/dist/utils.d.ts +4 -0
- package/dist/utils.js +21 -0
- package/package.json +35 -29
- package/.DS_Store +0 -0
- package/.coveralls.yml +0 -1
- package/.eslintrc +0 -13
- package/.prettierrc +0 -8
- package/lib/.DS_Store +0 -0
- package/lib/frozen-patty.js +0 -47
- package/lib/frozen-patty.js.map +0 -1
- package/lib/get.d.ts +0 -4
- package/lib/get.js +0 -20
- package/lib/get.js.map +0 -1
- package/lib/getValue.d.ts +0 -10
- package/lib/getValue.js.map +0 -1
- package/lib/index.d.ts +0 -34
- package/lib/index.js +0 -54
- package/lib/index.js.map +0 -1
- package/lib/polyfill.d.ts +0 -1
- package/lib/polyfill.js +0 -20
- package/lib/polyfill.js.map +0 -1
- package/lib/set.d.ts +0 -4
- package/lib/set.js +0 -56
- package/lib/set.js.map +0 -1
- package/lib/setValue.d.ts +0 -15
- package/lib/setValue.js.map +0 -1
- package/lib/util.d.ts +0 -8
- package/lib/util.js +0 -29
- package/lib/util.js.map +0 -1
package/LICENSE
CHANGED
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
|
|
45
|
-
options
|
|
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
|
|
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
|
|
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
|
|
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
|
|
7
|
-
* @param
|
|
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
|
|
9
|
+
export function getValues(el, typeConvert = false, attr = 'field', filter) {
|
|
11
10
|
/**
|
|
12
11
|
* [key, value, forceArray]
|
|
13
12
|
*/
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
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(
|
|
18
|
+
throw new Error(`data-${attr} attriblute is empty.`);
|
|
20
19
|
}
|
|
21
|
-
|
|
20
|
+
const fieldList = `${rawValue}`.split(/\s*,\s*/);
|
|
22
21
|
// console.log({fieldList, el: el.innerHTML});
|
|
23
|
-
for (
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
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 (/^[
|
|
27
|
+
if (/^[_a-z-][\w-]*:[_a-z-][\w-]*(?:\([a-z-]+\))?/i.test(fieldName)) {
|
|
30
28
|
splitKey = fieldName.split(':');
|
|
31
|
-
fieldName = splitKey[0]
|
|
32
|
-
keyAttr = splitKey[1]
|
|
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
|
-
|
|
40
|
-
|
|
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,12 +54,12 @@ function default_1(el, attr, typeConvert, filter) {
|
|
|
56
54
|
if (el instanceof HTMLInputElement ||
|
|
57
55
|
el instanceof HTMLSelectElement ||
|
|
58
56
|
el instanceof HTMLTextAreaElement) {
|
|
59
|
-
|
|
57
|
+
const val = el.value;
|
|
60
58
|
if (Array.isArray(val)) {
|
|
61
|
-
value = val[0];
|
|
59
|
+
value = cast(val[0]);
|
|
62
60
|
}
|
|
63
61
|
else {
|
|
64
|
-
value = val
|
|
62
|
+
value = cast(val);
|
|
65
63
|
}
|
|
66
64
|
}
|
|
67
65
|
else {
|
|
@@ -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
|
|
85
|
+
return el.getAttribute(keyAttr) === '' || el.getAttribute(keyAttr) === 'true';
|
|
89
86
|
}
|
|
90
87
|
}
|
|
91
88
|
case 'checked': {
|
|
@@ -104,39 +101,40 @@ function getAttribute(el, keyAttr, typeConvert) {
|
|
|
104
101
|
return el.getAttribute(keyAttr) || ''; // return plain string
|
|
105
102
|
}
|
|
106
103
|
default: {
|
|
107
|
-
if (
|
|
108
|
-
|
|
104
|
+
if (keyAttr.startsWith('data-')) {
|
|
105
|
+
const value = el.getAttribute(keyAttr) || '';
|
|
109
106
|
if (typeConvert) {
|
|
110
|
-
|
|
111
|
-
case 'true':
|
|
112
|
-
return true;
|
|
113
|
-
case 'false':
|
|
114
|
-
return false;
|
|
115
|
-
}
|
|
116
|
-
var numeric = parseFloat(value);
|
|
117
|
-
if (isFinite(numeric)) {
|
|
118
|
-
return numeric;
|
|
119
|
-
}
|
|
120
|
-
else {
|
|
121
|
-
return value;
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
else {
|
|
125
|
-
return value;
|
|
107
|
+
return cast(value);
|
|
126
108
|
}
|
|
109
|
+
return value;
|
|
127
110
|
}
|
|
128
|
-
|
|
129
|
-
return el.getAttribute(keyAttr) || '';
|
|
130
|
-
}
|
|
111
|
+
return el.getAttribute(keyAttr) || '';
|
|
131
112
|
}
|
|
132
113
|
}
|
|
133
114
|
}
|
|
115
|
+
function cast(value) {
|
|
116
|
+
if (value == null || value === '') {
|
|
117
|
+
return '';
|
|
118
|
+
}
|
|
119
|
+
switch (value) {
|
|
120
|
+
case 'true': {
|
|
121
|
+
return true;
|
|
122
|
+
}
|
|
123
|
+
case 'false': {
|
|
124
|
+
return false;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
const numeric = Number(value);
|
|
128
|
+
if (Number.isFinite(numeric)) {
|
|
129
|
+
return numeric;
|
|
130
|
+
}
|
|
131
|
+
return value;
|
|
132
|
+
}
|
|
134
133
|
/**
|
|
135
134
|
* Get path from value of "background-image"
|
|
136
135
|
*
|
|
137
136
|
*/
|
|
138
137
|
function getBackgroundImagePath(value) {
|
|
139
|
-
|
|
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
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
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -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;
|