sygnal 2.3.0 → 2.6.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.
@@ -0,0 +1,231 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var _extend = require('extend');
6
+
7
+ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
8
+
9
+ var _extend__default = /*#__PURE__*/_interopDefaultLegacy(_extend);
10
+
11
+ const undefinedv = (v) => v === undefined;
12
+
13
+ const number = (v) => typeof v === 'number';
14
+
15
+ const string = (v) => typeof v === 'string';
16
+
17
+ const text = (v) => string(v) || number(v);
18
+
19
+ const array = (v) => Array.isArray(v);
20
+
21
+ const object = (v) => typeof v === 'object' && v !== null;
22
+
23
+ const fun = (v) => typeof v === 'function';
24
+
25
+ const vnode$1 = (v) => object(v) && 'sel' in v && 'data' in v && 'children' in v && 'text' in v;
26
+
27
+ const svgPropsMap = { svg: 1, circle: 1, ellipse: 1, line: 1, polygon: 1,
28
+ polyline: 1, rect: 1, g: 1, path: 1, text: 1 };
29
+
30
+ const svg = (v) => v.sel in svgPropsMap;
31
+
32
+ // TODO: stop using extend here
33
+
34
+ const extend = (...objs) => _extend__default["default"](true, ...objs);
35
+
36
+ const assign = (...objs) => _extend__default["default"](false, ...objs);
37
+
38
+ const reduceDeep = (arr, fn, initial) => {
39
+ let result = initial;
40
+ for (let i = 0; i < arr.length; i++) {
41
+ const value = arr[i];
42
+ if (array(value)) {
43
+ result = reduceDeep(value, fn, result);
44
+ } else {
45
+ result = fn(result, value);
46
+ }
47
+ }
48
+ return result
49
+ };
50
+
51
+ const mapObject = (obj, fn) => Object.keys(obj).map(
52
+ (key) => fn(key, obj[key])
53
+ ).reduce(
54
+ (acc, curr) => extend(acc, curr),
55
+ {}
56
+ );
57
+
58
+ const deepifyKeys = (obj, modules) => mapObject(obj,
59
+ (key, val) => {
60
+ const dashIndex = key.indexOf('-');
61
+ if (dashIndex > -1 && modules[key.slice(0, dashIndex)] !== undefined) {
62
+ const moduleData = {
63
+ [key.slice(dashIndex + 1)]: val
64
+ };
65
+ return {
66
+ [key.slice(0, dashIndex)]: moduleData
67
+ }
68
+ }
69
+ return { [key]: val }
70
+ }
71
+ );
72
+
73
+ const omit = (key, obj) => mapObject(obj,
74
+ (mod, data) => mod !== key ? ({ [mod]: data }) : {}
75
+ );
76
+
77
+ // Const fnName = (...params) => guard ? default : ...
78
+
79
+ const createTextElement = (text$1) => !text(text$1) ? undefined : {
80
+ text: text$1,
81
+ sel: undefined,
82
+ data: undefined,
83
+ children: undefined,
84
+ elm: undefined,
85
+ key: undefined
86
+ };
87
+
88
+ const considerSvg = (vnode) => !svg(vnode) ? vnode :
89
+ assign(vnode,
90
+ { data: omit('props', extend(vnode.data,
91
+ { ns: 'http://www.w3.org/2000/svg', attrs: omit('className', extend(vnode.data.props,
92
+ { class: vnode.data.props ? vnode.data.props.className : undefined }
93
+ )) }
94
+ )) },
95
+ { children: undefinedv(vnode.children) ? undefined :
96
+ vnode.children.map((child) => considerSvg(child))
97
+ }
98
+ );
99
+
100
+ const rewrites = {
101
+ for: 'attrs',
102
+ role: 'attrs',
103
+ tabindex: 'attrs',
104
+ 'aria-*': 'attrs',
105
+ key: null
106
+ };
107
+
108
+ const rewriteModules = (data, modules) => mapObject(data, (key, val) => {
109
+ const inner = { [key]: val };
110
+ if (rewrites[key] && modules[rewrites[key]] !== undefined) {
111
+ return { [rewrites[key]]: inner }
112
+ }
113
+ if (rewrites[key] === null) {
114
+ return {}
115
+ }
116
+ const keys = Object.keys(rewrites);
117
+ for (let i = 0; i < keys.length; i++) {
118
+ const k = keys[i];
119
+ if (k.charAt(k.length - 1) === '*' && key.indexOf(k.slice(0, -1)) === 0 && modules[rewrites[k]] !== undefined) {
120
+ return { [rewrites[k]]: inner }
121
+ }
122
+ }
123
+ if (modules[key] !== undefined) {
124
+ return { [modules[key] ? modules[key] : key]: val }
125
+ }
126
+ if (modules.props !== undefined) {
127
+ return { props: inner }
128
+ }
129
+ return inner
130
+ });
131
+
132
+ const sanitizeData = (data, modules) => considerSvg(rewriteModules(deepifyKeys(data, modules), modules));
133
+
134
+ const sanitizeText = (children) => children.length > 1 || !text(children[0]) ? undefined : children[0].toString();
135
+
136
+ const sanitizeChildren = (children) => reduceDeep(children, (acc, child) => {
137
+ const vnode = vnode$1(child) ? child : createTextElement(child);
138
+ acc.push(vnode);
139
+ return acc
140
+ }
141
+ , []);
142
+
143
+ const defaultModules = {
144
+ attrs: '',
145
+ props: '',
146
+ class: '',
147
+ data: 'dataset',
148
+ style: '',
149
+ hook: '',
150
+ on: ''
151
+ };
152
+
153
+ const createElementWithModules = (modules) => {
154
+ return (sel, data, ...children) => {
155
+ if (typeof sel === 'undefined') {
156
+ sel = 'UNDEFINED';
157
+ cnosole.error('JSX Error: Capitalized HTML element without corresponding Sygnal factory. Components with names where the first letter is capital MUST be defined or included at the parent component\'s file scope.');
158
+ }
159
+ if (fun(sel)) {
160
+ if (sel.name === 'Fragment') {
161
+ return sel(data || {}, children)
162
+ }
163
+ const factory = sel;
164
+ sel = sel.name || 'sygnal-factory';
165
+ data ||= {};
166
+ data.sygnalFactory = factory;
167
+ }
168
+ const text = sanitizeText(children);
169
+ return considerSvg({
170
+ sel,
171
+ data: data ? sanitizeData(data, modules) : {},
172
+ children: typeof text !== 'undefined' ? undefined : sanitizeChildren(children),
173
+ text,
174
+ elm: undefined,
175
+ key: data ? data.key : undefined
176
+ })
177
+ }
178
+ };
179
+
180
+ const createElement = createElementWithModules(defaultModules);
181
+
182
+ function vnode(sel, data, children, text, elm) {
183
+ const key = data === undefined ? undefined : data.key;
184
+ return { sel, data, children, text, elm, key };
185
+ }
186
+
187
+ vnode("", {}, [], undefined, undefined);
188
+
189
+ // Bindig `requestAnimationFrame` like this fixes a bug in IE/Edge. See #360 and #409.
190
+ (typeof window !== "undefined" &&
191
+ window.requestAnimationFrame.bind(window)) ||
192
+ setTimeout;
193
+
194
+ /* eslint-disable @typescript-eslint/no-namespace, import/export */
195
+ function Fragment(data, ...children) {
196
+ const flatChildren = flattenAndFilter(children, []);
197
+ if (flatChildren.length === 1 &&
198
+ !flatChildren[0].sel &&
199
+ flatChildren[0].text) {
200
+ // only child is a simple text node, pass as text for a simpler vtree
201
+ return vnode(undefined, undefined, undefined, flatChildren[0].text, undefined);
202
+ }
203
+ else {
204
+ return vnode(undefined, data !== null && data !== void 0 ? data : {}, flatChildren, undefined, undefined);
205
+ }
206
+ }
207
+ function flattenAndFilter(children, flattened) {
208
+ for (const child of children) {
209
+ // filter out falsey children, except 0 since zero can be a valid value e.g inside a chart
210
+ if (child !== undefined &&
211
+ child !== null &&
212
+ child !== false &&
213
+ child !== "") {
214
+ if (Array.isArray(child)) {
215
+ flattenAndFilter(child, flattened);
216
+ }
217
+ else if (typeof child === "string" ||
218
+ typeof child === "number" ||
219
+ typeof child === "boolean") {
220
+ flattened.push(vnode(undefined, undefined, undefined, String(child), undefined));
221
+ }
222
+ else {
223
+ flattened.push(child);
224
+ }
225
+ }
226
+ }
227
+ return flattened;
228
+ }
229
+
230
+ exports.Fragment = Fragment;
231
+ exports.jsx = createElement;
@@ -0,0 +1,222 @@
1
+ import _extend from 'extend';
2
+
3
+ const undefinedv = (v) => v === undefined;
4
+
5
+ const number = (v) => typeof v === 'number';
6
+
7
+ const string = (v) => typeof v === 'string';
8
+
9
+ const text = (v) => string(v) || number(v);
10
+
11
+ const array = (v) => Array.isArray(v);
12
+
13
+ const object = (v) => typeof v === 'object' && v !== null;
14
+
15
+ const fun = (v) => typeof v === 'function';
16
+
17
+ const vnode$1 = (v) => object(v) && 'sel' in v && 'data' in v && 'children' in v && 'text' in v;
18
+
19
+ const svgPropsMap = { svg: 1, circle: 1, ellipse: 1, line: 1, polygon: 1,
20
+ polyline: 1, rect: 1, g: 1, path: 1, text: 1 };
21
+
22
+ const svg = (v) => v.sel in svgPropsMap;
23
+
24
+ // TODO: stop using extend here
25
+
26
+ const extend = (...objs) => _extend(true, ...objs);
27
+
28
+ const assign = (...objs) => _extend(false, ...objs);
29
+
30
+ const reduceDeep = (arr, fn, initial) => {
31
+ let result = initial;
32
+ for (let i = 0; i < arr.length; i++) {
33
+ const value = arr[i];
34
+ if (array(value)) {
35
+ result = reduceDeep(value, fn, result);
36
+ } else {
37
+ result = fn(result, value);
38
+ }
39
+ }
40
+ return result
41
+ };
42
+
43
+ const mapObject = (obj, fn) => Object.keys(obj).map(
44
+ (key) => fn(key, obj[key])
45
+ ).reduce(
46
+ (acc, curr) => extend(acc, curr),
47
+ {}
48
+ );
49
+
50
+ const deepifyKeys = (obj, modules) => mapObject(obj,
51
+ (key, val) => {
52
+ const dashIndex = key.indexOf('-');
53
+ if (dashIndex > -1 && modules[key.slice(0, dashIndex)] !== undefined) {
54
+ const moduleData = {
55
+ [key.slice(dashIndex + 1)]: val
56
+ };
57
+ return {
58
+ [key.slice(0, dashIndex)]: moduleData
59
+ }
60
+ }
61
+ return { [key]: val }
62
+ }
63
+ );
64
+
65
+ const omit = (key, obj) => mapObject(obj,
66
+ (mod, data) => mod !== key ? ({ [mod]: data }) : {}
67
+ );
68
+
69
+ // Const fnName = (...params) => guard ? default : ...
70
+
71
+ const createTextElement = (text$1) => !text(text$1) ? undefined : {
72
+ text: text$1,
73
+ sel: undefined,
74
+ data: undefined,
75
+ children: undefined,
76
+ elm: undefined,
77
+ key: undefined
78
+ };
79
+
80
+ const considerSvg = (vnode) => !svg(vnode) ? vnode :
81
+ assign(vnode,
82
+ { data: omit('props', extend(vnode.data,
83
+ { ns: 'http://www.w3.org/2000/svg', attrs: omit('className', extend(vnode.data.props,
84
+ { class: vnode.data.props ? vnode.data.props.className : undefined }
85
+ )) }
86
+ )) },
87
+ { children: undefinedv(vnode.children) ? undefined :
88
+ vnode.children.map((child) => considerSvg(child))
89
+ }
90
+ );
91
+
92
+ const rewrites = {
93
+ for: 'attrs',
94
+ role: 'attrs',
95
+ tabindex: 'attrs',
96
+ 'aria-*': 'attrs',
97
+ key: null
98
+ };
99
+
100
+ const rewriteModules = (data, modules) => mapObject(data, (key, val) => {
101
+ const inner = { [key]: val };
102
+ if (rewrites[key] && modules[rewrites[key]] !== undefined) {
103
+ return { [rewrites[key]]: inner }
104
+ }
105
+ if (rewrites[key] === null) {
106
+ return {}
107
+ }
108
+ const keys = Object.keys(rewrites);
109
+ for (let i = 0; i < keys.length; i++) {
110
+ const k = keys[i];
111
+ if (k.charAt(k.length - 1) === '*' && key.indexOf(k.slice(0, -1)) === 0 && modules[rewrites[k]] !== undefined) {
112
+ return { [rewrites[k]]: inner }
113
+ }
114
+ }
115
+ if (modules[key] !== undefined) {
116
+ return { [modules[key] ? modules[key] : key]: val }
117
+ }
118
+ if (modules.props !== undefined) {
119
+ return { props: inner }
120
+ }
121
+ return inner
122
+ });
123
+
124
+ const sanitizeData = (data, modules) => considerSvg(rewriteModules(deepifyKeys(data, modules), modules));
125
+
126
+ const sanitizeText = (children) => children.length > 1 || !text(children[0]) ? undefined : children[0].toString();
127
+
128
+ const sanitizeChildren = (children) => reduceDeep(children, (acc, child) => {
129
+ const vnode = vnode$1(child) ? child : createTextElement(child);
130
+ acc.push(vnode);
131
+ return acc
132
+ }
133
+ , []);
134
+
135
+ const defaultModules = {
136
+ attrs: '',
137
+ props: '',
138
+ class: '',
139
+ data: 'dataset',
140
+ style: '',
141
+ hook: '',
142
+ on: ''
143
+ };
144
+
145
+ const createElementWithModules = (modules) => {
146
+ return (sel, data, ...children) => {
147
+ if (typeof sel === 'undefined') {
148
+ sel = 'UNDEFINED';
149
+ cnosole.error('JSX Error: Capitalized HTML element without corresponding Sygnal factory. Components with names where the first letter is capital MUST be defined or included at the parent component\'s file scope.');
150
+ }
151
+ if (fun(sel)) {
152
+ if (sel.name === 'Fragment') {
153
+ return sel(data || {}, children)
154
+ }
155
+ const factory = sel;
156
+ sel = sel.name || 'sygnal-factory';
157
+ data ||= {};
158
+ data.sygnalFactory = factory;
159
+ }
160
+ const text = sanitizeText(children);
161
+ return considerSvg({
162
+ sel,
163
+ data: data ? sanitizeData(data, modules) : {},
164
+ children: typeof text !== 'undefined' ? undefined : sanitizeChildren(children),
165
+ text,
166
+ elm: undefined,
167
+ key: data ? data.key : undefined
168
+ })
169
+ }
170
+ };
171
+
172
+ const createElement = createElementWithModules(defaultModules);
173
+
174
+ function vnode(sel, data, children, text, elm) {
175
+ const key = data === undefined ? undefined : data.key;
176
+ return { sel, data, children, text, elm, key };
177
+ }
178
+
179
+ vnode("", {}, [], undefined, undefined);
180
+
181
+ // Bindig `requestAnimationFrame` like this fixes a bug in IE/Edge. See #360 and #409.
182
+ (typeof window !== "undefined" &&
183
+ window.requestAnimationFrame.bind(window)) ||
184
+ setTimeout;
185
+
186
+ /* eslint-disable @typescript-eslint/no-namespace, import/export */
187
+ function Fragment(data, ...children) {
188
+ const flatChildren = flattenAndFilter(children, []);
189
+ if (flatChildren.length === 1 &&
190
+ !flatChildren[0].sel &&
191
+ flatChildren[0].text) {
192
+ // only child is a simple text node, pass as text for a simpler vtree
193
+ return vnode(undefined, undefined, undefined, flatChildren[0].text, undefined);
194
+ }
195
+ else {
196
+ return vnode(undefined, data !== null && data !== void 0 ? data : {}, flatChildren, undefined, undefined);
197
+ }
198
+ }
199
+ function flattenAndFilter(children, flattened) {
200
+ for (const child of children) {
201
+ // filter out falsey children, except 0 since zero can be a valid value e.g inside a chart
202
+ if (child !== undefined &&
203
+ child !== null &&
204
+ child !== false &&
205
+ child !== "") {
206
+ if (Array.isArray(child)) {
207
+ flattenAndFilter(child, flattened);
208
+ }
209
+ else if (typeof child === "string" ||
210
+ typeof child === "number" ||
211
+ typeof child === "boolean") {
212
+ flattened.push(vnode(undefined, undefined, undefined, String(child), undefined));
213
+ }
214
+ else {
215
+ flattened.push(child);
216
+ }
217
+ }
218
+ }
219
+ return flattened;
220
+ }
221
+
222
+ export { Fragment, createElement as jsx };