@htmlplus/element 0.5.6 → 0.5.8

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 (50) hide show
  1. package/bundlers/rollup.js +2 -2
  2. package/bundlers/vite.js +2 -2
  3. package/client/decorators/element.js +12 -5
  4. package/client/decorators/listen.d.ts +3 -3
  5. package/client/decorators/listen.js +3 -3
  6. package/client/decorators/property.d.ts +4 -0
  7. package/client/decorators/property.js +3 -2
  8. package/client/decorators/watch.js +3 -3
  9. package/client/utils/addMember.d.ts +2 -0
  10. package/client/utils/addMember.js +6 -0
  11. package/client/utils/appendToMethod.js +12 -4
  12. package/client/utils/fromAttribute.d.ts +1 -0
  13. package/client/utils/fromAttribute.js +53 -0
  14. package/client/utils/getMembers.js +1 -1
  15. package/client/utils/index.d.ts +2 -1
  16. package/client/utils/index.js +2 -1
  17. package/client/utils/request.js +2 -2
  18. package/client/utils/task.js +5 -5
  19. package/compiler/compiler.d.ts +1 -1
  20. package/compiler/compiler.js +18 -23
  21. package/compiler/plugins/assets.d.ts +2 -5
  22. package/compiler/plugins/assets.js +2 -2
  23. package/compiler/plugins/copy.d.ts +3 -7
  24. package/compiler/plugins/copy.js +3 -3
  25. package/compiler/plugins/customElement.d.ts +2 -5
  26. package/compiler/plugins/customElement.js +99 -36
  27. package/compiler/plugins/customElementReact/customElementReact.d.ts +2 -5
  28. package/compiler/plugins/document.d.ts +2 -5
  29. package/compiler/plugins/extract.d.ts +2 -5
  30. package/compiler/plugins/extract.js +2 -2
  31. package/compiler/plugins/parse.d.ts +2 -5
  32. package/compiler/plugins/parse.js +2 -2
  33. package/compiler/plugins/read.d.ts +2 -5
  34. package/compiler/plugins/read.js +2 -2
  35. package/compiler/plugins/readme.d.ts +2 -5
  36. package/compiler/plugins/style.d.ts +2 -5
  37. package/compiler/plugins/style.js +2 -2
  38. package/compiler/plugins/validate.d.ts +2 -5
  39. package/compiler/plugins/validate.js +2 -2
  40. package/compiler/plugins/visualStudioCode.d.ts +2 -5
  41. package/compiler/plugins/webTypes.d.ts +2 -5
  42. package/compiler/utils/getType.d.ts +2 -2
  43. package/compiler/utils/getType.js +18 -12
  44. package/constants/index.d.ts +11 -6
  45. package/constants/index.js +11 -6
  46. package/package.json +1 -1
  47. package/types/context.d.ts +0 -5
  48. package/types/plugin.d.ts +4 -4
  49. package/client/utils/parseValue.d.ts +0 -1
  50. package/client/utils/parseValue.js +0 -15
@@ -1,6 +1,6 @@
1
1
  import { compiler } from '../compiler/index.js';
2
2
  export const rollup = (...plugins) => {
3
- const { start, next, finish } = compiler(...plugins);
3
+ const { start, run, finish } = compiler(...plugins);
4
4
  return {
5
5
  name: 'htmlplus',
6
6
  async buildStart() {
@@ -9,7 +9,7 @@ export const rollup = (...plugins) => {
9
9
  async load(id) {
10
10
  if (!id.endsWith('.tsx'))
11
11
  return;
12
- const { isInvalid, script } = await next(id);
12
+ const { isInvalid, script } = await run(id);
13
13
  if (isInvalid)
14
14
  return;
15
15
  return script;
package/bundlers/vite.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import path from 'path';
2
2
  import { compiler } from '../compiler/index.js';
3
3
  export const vite = (...plugins) => {
4
- const { start, next, finish } = compiler(...plugins);
4
+ const { start, run, finish } = compiler(...plugins);
5
5
  return {
6
6
  name: 'htmlplus',
7
7
  async buildStart() {
@@ -10,7 +10,7 @@ export const vite = (...plugins) => {
10
10
  async load(id) {
11
11
  if (!id.endsWith('.tsx'))
12
12
  return;
13
- let { isInvalid, script, stylePath } = await next(id);
13
+ let { isInvalid, script, stylePath } = await run(id);
14
14
  if (isInvalid)
15
15
  return;
16
16
  if (script && stylePath) {
@@ -1,22 +1,29 @@
1
1
  import { camelCase, paramCase } from 'change-case';
2
2
  import * as CONSTANTS from '../../constants/index.js';
3
- import { call, getMembers, isServer, parseValue, request } from '../utils/index.js';
3
+ import { call, fromAttribute, getMembers, isServer, request } from '../utils/index.js';
4
4
  export function Element(tag) {
5
5
  return function (constructor) {
6
6
  if (isServer())
7
7
  return;
8
8
  if (customElements.get(tag))
9
9
  return;
10
+ const members = getMembers(constructor);
10
11
  class Plus extends HTMLElement {
11
12
  constructor() {
12
13
  super();
13
14
  this.attachShadow({ mode: 'open' });
14
15
  this[CONSTANTS.API_INSTANCE] = new constructor();
16
+ Object.keys(members)
17
+ .filter((key) => members[key].type != CONSTANTS.TYPE_FUNCTION)
18
+ .forEach((key) => {
19
+ members[key].default = this[CONSTANTS.API_INSTANCE][key];
20
+ });
15
21
  this[CONSTANTS.API_INSTANCE][CONSTANTS.API_HOST] = () => this;
16
22
  }
17
- // TODO: ignore functions
18
23
  static get observedAttributes() {
19
- return Object.keys(getMembers(constructor)).map((key) => paramCase(key));
24
+ return Object.keys(members)
25
+ .filter((key) => members[key].type != CONSTANTS.TYPE_FUNCTION)
26
+ .map((key) => paramCase(key));
20
27
  }
21
28
  adoptedCallback() {
22
29
  call(this[CONSTANTS.API_INSTANCE], CONSTANTS.LIFECYCLE_ADOPTED);
@@ -27,8 +34,8 @@ export function Element(tag) {
27
34
  if (instance[CONSTANTS.API_LOCKED])
28
35
  return;
29
36
  const name = camelCase(attribute);
30
- const type = (_a = getMembers(instance)[name]) === null || _a === void 0 ? void 0 : _a.type;
31
- const value = parseValue(next, type);
37
+ const type = (_a = members[name]) === null || _a === void 0 ? void 0 : _a.type;
38
+ const value = fromAttribute(next, type);
32
39
  if (instance[name] === value)
33
40
  return;
34
41
  instance[name] = value;
@@ -12,9 +12,9 @@ export interface ListenOptions {
12
12
  export declare const ListenOptionsDefault: ListenOptions;
13
13
  /**
14
14
  * Will be called whenever the specified event is delivered to the target.
15
- * [More](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener).
16
- * @param type TODO
17
- * @param options TODO
15
+ * [More](https://mdn.io/addEventListener).
16
+ * @param type A case-sensitive string representing the [event type](https://mdn.io/events) to listen for.
17
+ * @param options An object that specifies characteristics about the event listener.
18
18
  */
19
19
  export declare function Listen(type: string, options?: ListenOptions): (target: PlusElement, propertyKey: PropertyKey, descriptor: PropertyDescriptor) => {
20
20
  configurable: boolean;
@@ -9,9 +9,9 @@ export const ListenOptionsDefault = {
9
9
  };
10
10
  /**
11
11
  * Will be called whenever the specified event is delivered to the target.
12
- * [More](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener).
13
- * @param type TODO
14
- * @param options TODO
12
+ * [More](https://mdn.io/addEventListener).
13
+ * @param type A case-sensitive string representing the [event type](https://mdn.io/events) to listen for.
14
+ * @param options An object that specifies characteristics about the event listener.
15
15
  */
16
16
  export function Listen(type, options) {
17
17
  return function (target, propertyKey, descriptor) {
@@ -4,5 +4,9 @@ export interface PropertyOptions {
4
4
  * Whether property value is reflected back to the associated attribute. default is `false`.
5
5
  */
6
6
  reflect?: boolean;
7
+ /**
8
+ * TODO
9
+ */
10
+ type?: number;
7
11
  }
8
12
  export declare function Property(options?: PropertyOptions): (target: PlusElement, propertyKey: PropertyKey) => void;
@@ -1,9 +1,10 @@
1
1
  import * as CONSTANTS from '../../constants/index.js';
2
- import { defineProperty, host, request, appendToMethod, updateAttribute, getConfig, getMembers, getTag } from '../utils/index.js';
2
+ import { addMember, appendToMethod, defineProperty, getConfig, getMembers, getTag, host, request, updateAttribute } from '../utils/index.js';
3
3
  export function Property(options) {
4
4
  return function (target, propertyKey) {
5
5
  const name = String(propertyKey);
6
6
  const symbol = Symbol();
7
+ addMember(target.constructor, name, options);
7
8
  function get() {
8
9
  return this[symbol];
9
10
  }
@@ -25,7 +26,7 @@ export function Property(options) {
25
26
  var _a;
26
27
  const element = host(this);
27
28
  // TODO: experimental for global config
28
- if (((_a = getMembers(this)[name]) === null || _a === void 0 ? void 0 : _a.default) === this[name]) {
29
+ if (((_a = getMembers(this.constructor)[name]) === null || _a === void 0 ? void 0 : _a.default) === this[name]) {
29
30
  const config = getConfig('component', getTag(target), 'property', name);
30
31
  if (typeof config != 'undefined')
31
32
  this[name] = config;
@@ -15,15 +15,15 @@ export function Watch(keys, immediate) {
15
15
  // Registers a lifecycle to detect changes.
16
16
  appendToMethod(target, CONSTANTS.LIFECYCLE_UPDATED, function (states) {
17
17
  // Skips the logic if 'immediate' wasn't passed.
18
- if (!immediate && !this[CONSTANTS.API_LOADED])
18
+ if (!immediate && !this[CONSTANTS.API_RENDER_COMPLETED])
19
19
  return;
20
20
  // Loops the keys.
21
- states.forEach((prev, key) => {
21
+ states.forEach((previous, key) => {
22
22
  // Skips the current key.
23
23
  if (all.length && !all.includes(key))
24
24
  return;
25
25
  // Invokes the method with parameters.
26
- this[propertyKey](this[key], prev, key);
26
+ this[propertyKey](this[key], previous, key);
27
27
  });
28
28
  });
29
29
  };
@@ -0,0 +1,2 @@
1
+ import { PlusElement } from '../../types';
2
+ export declare const addMember: (target: PlusElement, key: string, data: any) => void;
@@ -0,0 +1,6 @@
1
+ import * as CONSTANTS from '../../constants/index.js';
2
+ export const addMember = (target, key, data) => {
3
+ var _a;
4
+ target[_a = CONSTANTS.STATIC_MEMBERS] || (target[_a] = {});
5
+ target[CONSTANTS.STATIC_MEMBERS][key] = data;
6
+ };
@@ -1,7 +1,15 @@
1
1
  export const appendToMethod = (target, propertyKey, handler) => {
2
- const callback = target[propertyKey];
3
- target[propertyKey] = function (...parameters) {
2
+ // Gets the previous function
3
+ const previous = target[propertyKey];
4
+ // Creates new function
5
+ function next(...parameters) {
6
+ // Calls the previous
7
+ const result = previous === null || previous === void 0 ? void 0 : previous.bind(this)(...parameters);
8
+ // Calls the appended
4
9
  handler.bind(this)(...parameters);
5
- return callback === null || callback === void 0 ? void 0 : callback.bind(this)(...parameters);
6
- };
10
+ // Returns the result
11
+ return result;
12
+ }
13
+ // Replaces the next with the previous one
14
+ target[propertyKey] = next;
7
15
  };
@@ -0,0 +1 @@
1
+ export declare const fromAttribute: (input: any, type: any) => any;
@@ -0,0 +1,53 @@
1
+ import * as CONSTANTS from '../../constants/index.js';
2
+ import { typeOf } from './typeOf.js';
3
+ export const fromAttribute = (input, type) => {
4
+ const string = `${input}`;
5
+ if (CONSTANTS.TYPE_BOOLEAN & type) {
6
+ if (string === '')
7
+ return true;
8
+ if (string === 'false')
9
+ return false;
10
+ if (string === 'true')
11
+ return true;
12
+ }
13
+ if (CONSTANTS.TYPE_NUMBER & type) {
14
+ if (string != '' && !isNaN(input)) {
15
+ return parseFloat(input);
16
+ }
17
+ }
18
+ if (CONSTANTS.TYPE_NULL & type) {
19
+ if (string === 'null') {
20
+ return null;
21
+ }
22
+ }
23
+ if (CONSTANTS.TYPE_DATE & type) {
24
+ const value = new Date(input);
25
+ if (value.toString() != 'Invalid Date') {
26
+ return value;
27
+ }
28
+ }
29
+ if (CONSTANTS.TYPE_ARRAY & type) {
30
+ try {
31
+ const value = JSON.parse(input);
32
+ if (typeOf(value) == 'array') {
33
+ return value;
34
+ }
35
+ }
36
+ catch (_a) { }
37
+ }
38
+ if (CONSTANTS.TYPE_OBJECT & type) {
39
+ try {
40
+ const value = JSON.parse(input);
41
+ if (typeOf(value) == 'object') {
42
+ return value;
43
+ }
44
+ }
45
+ catch (_b) { }
46
+ }
47
+ if (CONSTANTS.TYPE_UNDEFINED & type) {
48
+ if (string === 'undefined') {
49
+ return undefined;
50
+ }
51
+ }
52
+ return input;
53
+ };
@@ -1,5 +1,5 @@
1
1
  import * as CONSTANTS from '../../constants/index.js';
2
2
  // TODO
3
3
  export const getMembers = (target) => {
4
- return target.constructor[CONSTANTS.STATIC_MEMBERS] || target[CONSTANTS.STATIC_MEMBERS] || {};
4
+ return target[CONSTANTS.STATIC_MEMBERS] || {};
5
5
  };
@@ -1,7 +1,9 @@
1
+ export * from './addMember.js';
1
2
  export * from './appendToMethod.js';
2
3
  export * from './call.js';
3
4
  export * from './config.js';
4
5
  export * from './defineProperty.js';
6
+ export * from './fromAttribute.js';
5
7
  export * from './getFramework.js';
6
8
  export * from './getMembers.js';
7
9
  export * from './getStyles.js';
@@ -11,7 +13,6 @@ export * from './isEvent.js';
11
13
  export * from './isServer.js';
12
14
  export * from './off.js';
13
15
  export * from './on.js';
14
- export * from './parseValue.js';
15
16
  export * from './request.js';
16
17
  export * from './shadowRoot.js';
17
18
  export * from './syncAttributes.js';
@@ -1,7 +1,9 @@
1
+ export * from './addMember.js';
1
2
  export * from './appendToMethod.js';
2
3
  export * from './call.js';
3
4
  export * from './config.js';
4
5
  export * from './defineProperty.js';
6
+ export * from './fromAttribute.js';
5
7
  export * from './getFramework.js';
6
8
  export * from './getMembers.js';
7
9
  export * from './getStyles.js';
@@ -11,7 +13,6 @@ export * from './isEvent.js';
11
13
  export * from './isServer.js';
12
14
  export * from './off.js';
13
15
  export * from './on.js';
14
- export * from './parseValue.js';
15
16
  export * from './request.js';
16
17
  export * from './shadowRoot.js';
17
18
  export * from './syncAttributes.js';
@@ -55,10 +55,10 @@ export const request = (target, name, previous, callback) => {
55
55
  });
56
56
  // Calls the lifecycle's callback after the rendering phase.
57
57
  call(target, CONSTANTS.LIFECYCLE_UPDATED, states);
58
- // TODO: releated to the @Watch decorator.
59
- target[CONSTANTS.API_LOADED] = true;
60
58
  // Clears stacks.
61
59
  stacks.clear();
60
+ // TODO: releated to the @Watch decorator.
61
+ target[CONSTANTS.API_RENDER_COMPLETED] = true;
62
62
  }
63
63
  }));
64
64
  // Calls the micro task.
@@ -1,23 +1,23 @@
1
1
  export const task = (options) => {
2
- let isPending, updatePromise;
2
+ let isPending, promise;
3
3
  const run = () => {
4
4
  if (options.canStart && !options.canStart())
5
5
  return Promise.resolve(false);
6
6
  if (!isPending)
7
- updatePromise = enqueue();
8
- return updatePromise;
7
+ promise = enqueue();
8
+ return promise;
9
9
  };
10
10
  const enqueue = async () => {
11
11
  isPending = true;
12
12
  try {
13
- await updatePromise;
13
+ await promise;
14
14
  }
15
15
  catch (error) {
16
16
  Promise.reject(error);
17
17
  }
18
18
  // TODO: maybe is optional
19
19
  if (!isPending)
20
- return updatePromise;
20
+ return promise;
21
21
  try {
22
22
  if (options.canRun && !options.canRun())
23
23
  return (isPending = false);
@@ -1,6 +1,6 @@
1
1
  import { Context, Plugin } from '../types';
2
2
  export declare const compiler: (...plugins: Array<Plugin>) => {
3
3
  start: () => Promise<void>;
4
- next: (filePath: string) => Promise<Context>;
4
+ run: (filePath: string) => Promise<Context>;
5
5
  finish: () => Promise<void>;
6
6
  };
@@ -19,38 +19,33 @@ export const compiler = (...plugins) => {
19
19
  if (!plugin.start)
20
20
  continue;
21
21
  log(`Plugin '${plugin.name}' is starting...`);
22
- await plugin.start(global);
22
+ global = (await plugin.start(global)) || global;
23
23
  log(`Plugin '${plugin.name}' started successfully.`);
24
24
  }
25
25
  log(`Plugins started successfully.`, true);
26
26
  };
27
- const next = async (filePath) => {
28
- var _a;
27
+ const run = async (filePath) => {
29
28
  const key = filePath.split(/[\/|\\]/g).pop();
30
29
  let context = {
31
30
  filePath
32
31
  };
32
+ const parsed = path.parse(filePath);
33
33
  for (const plugin of plugins) {
34
- if (!plugin.next)
34
+ if (!plugin.run)
35
35
  continue;
36
- log(`Plugin '${plugin.name}' is executing on '${path.basename(filePath)}' file.`);
37
- const output = await plugin.next(context, global);
38
- // TODO
39
- if (output) {
40
- context.outputs = ((_a = context.outputs) !== null && _a !== void 0 ? _a : [])
41
- .filter((output) => {
42
- if (plugin.name != output.name)
43
- return true;
44
- if (plugin.options && plugin.options != output.options)
45
- return true;
46
- })
47
- .concat({
48
- name: plugin.name,
49
- options: plugin.options,
50
- output
51
- });
36
+ log(`Plugin '${plugin.name}' is executing on '${path.basename(parsed.dir)}/${parsed.base}' file.`);
37
+ try {
38
+ context = (await plugin.run(context, global)) || context;
52
39
  }
53
- global.contexts = global.contexts.filter((current) => current.filePath != context.filePath).concat(context);
40
+ catch (error) {
41
+ log(`Error in '${plugin.name}' plugin on '${path.basename(parsed.dir)}/${parsed.base}' file.`, true);
42
+ throw error;
43
+ }
44
+ global.contexts = global.contexts
45
+ .filter((current) => {
46
+ return current.filePath != context.filePath;
47
+ })
48
+ .concat(context);
54
49
  log(`Plugin '${plugin.name}' executed successfully on '${path.basename(filePath)}' file.`);
55
50
  if (context.isInvalid)
56
51
  break;
@@ -66,11 +61,11 @@ export const compiler = (...plugins) => {
66
61
  if (!plugin.finish)
67
62
  continue;
68
63
  log(`Plugin '${plugin.name}' is finishing...`);
69
- await plugin.finish(global);
64
+ global = (await plugin.finish(global)) || global;
70
65
  log(`Plugin '${plugin.name}' finished successfully.`);
71
66
  }
72
67
  log(`Plugins finished successfully.`, true);
73
68
  log(`Finished.`, true);
74
69
  };
75
- return { start, next, finish };
70
+ return { start, run, finish };
76
71
  };
@@ -1,11 +1,8 @@
1
- import { Context } from '../../types';
1
+ import { Context, Plugin } from '../../types';
2
2
  export declare const ASSETS_OPTIONS: Partial<AssetsOptions>;
3
3
  export type AssetsOptions = {
4
4
  once?: boolean;
5
5
  destination: (context: Context) => string;
6
6
  source?: (context: Context) => string;
7
7
  };
8
- export declare const assets: (options: AssetsOptions) => {
9
- name: string;
10
- next: (context: Context) => void;
11
- };
8
+ export declare const assets: (options: AssetsOptions) => Plugin;
@@ -13,7 +13,7 @@ export const assets = (options) => {
13
13
  const name = 'assets';
14
14
  options = Object.assign({}, ASSETS_OPTIONS, options);
15
15
  const sources = new Set();
16
- const next = (context) => {
16
+ const run = (context) => {
17
17
  var _a, _b;
18
18
  const source = (_a = options.source) === null || _a === void 0 ? void 0 : _a.call(options, context);
19
19
  if (!source)
@@ -29,5 +29,5 @@ export const assets = (options) => {
29
29
  fs.copySync(source, destination);
30
30
  context.assets = source;
31
31
  };
32
- return { name, next };
32
+ return { name, run };
33
33
  };
@@ -1,13 +1,9 @@
1
+ import { Plugin } from '../../types';
1
2
  export declare const COPY_OPTIONS: Partial<CopyOptions>;
2
3
  export interface CopyOptions {
3
- at?: 'start' | 'next' | 'finish';
4
+ at?: 'start' | 'run' | 'finish';
4
5
  destination: string;
5
6
  source: string;
6
7
  transformer?: (content: string) => string;
7
8
  }
8
- export declare const copy: (options: CopyOptions) => {
9
- name: string;
10
- start: () => void;
11
- next: () => void;
12
- finish: () => void;
13
- };
9
+ export declare const copy: (options: CopyOptions) => Plugin;
@@ -19,11 +19,11 @@ export const copy = (options) => {
19
19
  const start = () => {
20
20
  copy('start');
21
21
  };
22
- const next = () => {
23
- copy('next');
22
+ const run = () => {
23
+ copy('run');
24
24
  };
25
25
  const finish = () => {
26
26
  copy('finish');
27
27
  };
28
- return { name, start, next, finish };
28
+ return { name, start, run, finish };
29
29
  };
@@ -1,9 +1,6 @@
1
- import { Context } from '../../types';
1
+ import { Plugin } from '../../types';
2
2
  export declare const CUSTOM_ELEMENT_OPTIONS: Partial<CustomElementOptions>;
3
3
  export interface CustomElementOptions {
4
4
  typings?: boolean;
5
5
  }
6
- export declare const customElement: (options?: CustomElementOptions) => {
7
- name: string;
8
- next: (context: Context) => void;
9
- };
6
+ export declare const customElement: (options?: CustomElementOptions) => Plugin;
@@ -1,7 +1,7 @@
1
1
  import t from '@babel/types';
2
2
  import { pascalCase } from 'change-case';
3
3
  import * as CONSTANTS from '../../constants/index.js';
4
- import { addDependency, print, visitor } from '../utils/index.js';
4
+ import { addDependency, getType, print, visitor } from '../utils/index.js';
5
5
  export const CUSTOM_ELEMENT_OPTIONS = {
6
6
  // prefix: undefined,
7
7
  typings: true
@@ -10,7 +10,7 @@ export const CUSTOM_ELEMENT_OPTIONS = {
10
10
  export const customElement = (options) => {
11
11
  const name = 'customElement';
12
12
  options = Object.assign({}, CUSTOM_ELEMENT_OPTIONS, options);
13
- const next = (context) => {
13
+ const run = (context) => {
14
14
  const ast = t.cloneNode(context.fileAST, true);
15
15
  // attaches name
16
16
  visitor(ast, {
@@ -98,49 +98,112 @@ export const customElement = (options) => {
98
98
  t.addComment(node, 'leading', CONSTANTS.COMMENT_AUTO_ADDED_DEPENDENCY, true);
99
99
  }
100
100
  });
101
+ // adds type to properties
102
+ visitor(ast, {
103
+ Decorator(path) {
104
+ var _a, _b;
105
+ const { expression } = path.node;
106
+ if (((_a = expression.callee) === null || _a === void 0 ? void 0 : _a.name) != CONSTANTS.DECORATOR_PROPERTY)
107
+ return;
108
+ let type = 0;
109
+ const extract = (input) => {
110
+ switch (input === null || input === void 0 ? void 0 : input.type) {
111
+ case 'BooleanLiteral':
112
+ type |= CONSTANTS.TYPE_BOOLEAN;
113
+ break;
114
+ case 'NumericLiteral':
115
+ type |= CONSTANTS.TYPE_NUMBER;
116
+ break;
117
+ case 'StringLiteral':
118
+ type |= CONSTANTS.TYPE_ENUM;
119
+ break;
120
+ case 'TSArrayType':
121
+ type |= CONSTANTS.TYPE_ARRAY;
122
+ break;
123
+ case 'TSBooleanKeyword':
124
+ type |= CONSTANTS.TYPE_BOOLEAN;
125
+ break;
126
+ case 'TSLiteralType':
127
+ extract(input.literal);
128
+ break;
129
+ case 'TSNullKeyword':
130
+ type |= CONSTANTS.TYPE_NULL;
131
+ break;
132
+ case 'TSNumberKeyword':
133
+ type |= CONSTANTS.TYPE_NUMBER;
134
+ break;
135
+ case 'TSObjectKeyword':
136
+ type |= CONSTANTS.TYPE_OBJECT;
137
+ break;
138
+ case 'TSStringKeyword':
139
+ type |= CONSTANTS.TYPE_STRING;
140
+ break;
141
+ case 'TSTypeLiteral':
142
+ type |= CONSTANTS.TYPE_OBJECT;
143
+ break;
144
+ case 'TSTypeReference':
145
+ if (!input.typeName)
146
+ break;
147
+ switch (input.typeName.name) {
148
+ case 'Array':
149
+ type |= CONSTANTS.TYPE_ARRAY;
150
+ break;
151
+ case 'Boolean':
152
+ type |= CONSTANTS.TYPE_BOOLEAN;
153
+ break;
154
+ case 'bool':
155
+ type |= CONSTANTS.TYPE_BOOLEAN;
156
+ break;
157
+ case 'Date':
158
+ type |= CONSTANTS.TYPE_DATE;
159
+ break;
160
+ case 'Number':
161
+ type |= CONSTANTS.TYPE_NUMBER;
162
+ break;
163
+ case 'Object':
164
+ type |= CONSTANTS.TYPE_OBJECT;
165
+ break;
166
+ }
167
+ break;
168
+ case 'TSUnionType':
169
+ input.types.forEach(extract);
170
+ break;
171
+ }
172
+ };
173
+ extract(getType(ast, (_b = path.parentPath.node.typeAnnotation) === null || _b === void 0 ? void 0 : _b.typeAnnotation, { directory: context.directoryPath }));
174
+ if (!expression.arguments.length) {
175
+ expression.arguments.push(t.objectExpression([]));
176
+ }
177
+ const [argument] = expression.arguments;
178
+ argument.properties = argument.properties.filter((property) => {
179
+ return property.key.name != CONSTANTS.STATIC_MEMBERS_TYPE;
180
+ });
181
+ argument.properties.push(t.objectProperty(t.identifier(CONSTANTS.STATIC_MEMBERS_TYPE), t.numericLiteral(type)));
182
+ }
183
+ });
101
184
  // attaches members
102
185
  visitor(ast, {
103
186
  ClassDeclaration(path) {
104
187
  const { body, id } = path.node;
105
188
  if (id.name != context.className)
106
189
  return;
107
- const node = t.classProperty(t.identifier(CONSTANTS.STATIC_MEMBERS), t.objectExpression([
108
- ...context.classProperties.map((property) => {
109
- var _a, _b, _c, _d, _e, _f;
110
- const properties = [];
111
- if (property.value) {
112
- properties.push(t.objectProperty(t.identifier(CONSTANTS.STATIC_MEMBERS_INITIALIZER), property.value));
113
- }
114
- const type = (() => {
115
- var _a, _b;
116
- switch ((_b = (_a = property.typeAnnotation) === null || _a === void 0 ? void 0 : _a.typeAnnotation) === null || _b === void 0 ? void 0 : _b.type) {
117
- case 'TSBooleanKeyword':
118
- return t.stringLiteral(CONSTANTS.TYPE_BOOLEAN);
119
- case 'TSStringKeyword':
120
- return t.stringLiteral(CONSTANTS.TYPE_STRING);
121
- case 'TSNumberKeyword':
122
- return t.stringLiteral(CONSTANTS.TYPE_NUMBER);
123
- }
124
- })();
125
- if (type) {
126
- properties.push(t.objectProperty(t.identifier(CONSTANTS.STATIC_MEMBERS_TYPE), type));
127
- }
128
- // TODO
129
- // prettier-ignore
130
- (_f = (_e = (_d = (_c = (_b = (_a = property === null || property === void 0 ? void 0 : property.decorators) === null || _a === void 0 ? void 0 : _a.find((decorator) => {
131
- var _a, _b;
132
- return ((_b = (_a = decorator.expression) === null || _a === void 0 ? void 0 : _a['callee']) === null || _b === void 0 ? void 0 : _b.name) == CONSTANTS.DECORATOR_PROPERTY;
133
- })) === null || _b === void 0 ? void 0 : _b.expression) === null || _c === void 0 ? void 0 : _c['arguments']) === null || _d === void 0 ? void 0 : _d[0]) === null || _e === void 0 ? void 0 : _e.properties) === null || _f === void 0 ? void 0 : _f.forEach((property) => properties.push(property));
134
- return t.objectProperty(t.identifier(property.key['name']), t.objectExpression(properties));
135
- }),
136
- ...context.classMethods.map((method) => t.objectProperty(t.identifier(method.key['name']), t.objectExpression([
137
- t.objectProperty(t.identifier(CONSTANTS.STATIC_MEMBERS_TYPE), t.stringLiteral(CONSTANTS.TYPE_FUNCTION))
138
- ])))
139
- ]), undefined, undefined, undefined, true);
190
+ const node = t.classProperty(t.identifier(CONSTANTS.STATIC_MEMBERS), t.objectExpression(context.classMethods.map((method) => t.objectProperty(t.identifier(method.key['name']), t.objectExpression([
191
+ t.objectProperty(t.identifier(CONSTANTS.STATIC_MEMBERS_TYPE), t.numericLiteral(CONSTANTS.TYPE_FUNCTION))
192
+ ])))), undefined, undefined, undefined, true);
140
193
  t.addComment(node, 'leading', CONSTANTS.COMMENT_AUTO_ADDED_PROPERTY, true);
141
194
  body.body.unshift(node);
142
195
  }
143
196
  });
197
+ // removes methods decorators
198
+ visitor(ast, {
199
+ Decorator(path) {
200
+ var _a;
201
+ const { expression } = path.node;
202
+ if (((_a = expression.callee) === null || _a === void 0 ? void 0 : _a.name) != CONSTANTS.DECORATOR_METHOD)
203
+ return;
204
+ path.remove();
205
+ }
206
+ });
144
207
  // attaches typings
145
208
  if (options === null || options === void 0 ? void 0 : options.typings) {
146
209
  visitor(ast, {
@@ -209,5 +272,5 @@ export const customElement = (options) => {
209
272
  // TODO
210
273
  context.script = print(ast);
211
274
  };
212
- return { name, next };
275
+ return { name, run };
213
276
  };
@@ -1,4 +1,4 @@
1
- import { Context, Global } from '../../../types';
1
+ import { Context, Plugin } from '../../../types';
2
2
  export declare const CUSTOM_ELEMENT_REACT_OPTIONS: Partial<CustomElementReactOptions>;
3
3
  export interface CustomElementReactOptions {
4
4
  compact?: boolean;
@@ -13,7 +13,4 @@ export interface CustomElementReactOptions {
13
13
  local: string;
14
14
  };
15
15
  }
16
- export declare const customElementReact: (options: CustomElementReactOptions) => {
17
- name: string;
18
- finish: (global: Global) => void;
19
- };
16
+ export declare const customElementReact: (options: CustomElementReactOptions) => Plugin;
@@ -1,10 +1,7 @@
1
- import { Context, Global } from '../../types';
1
+ import { Context, Plugin } from '../../types';
2
2
  export declare const DOCUMENT_OPTIONS: Partial<DocumentOptions>;
3
3
  export interface DocumentOptions {
4
4
  destination: string;
5
5
  transformer?: (context: Context, element: any) => any;
6
6
  }
7
- export declare const document: (options: DocumentOptions) => {
8
- name: string;
9
- finish: (global: Global) => void;
10
- };
7
+ export declare const document: (options: DocumentOptions) => Plugin;
@@ -1,9 +1,6 @@
1
- import { Context } from '../../types';
1
+ import { Plugin } from '../../types';
2
2
  export declare const EXTRACT_OPTIONS: Partial<ExtractOptions>;
3
3
  export interface ExtractOptions {
4
4
  prefix?: string;
5
5
  }
6
- export declare const extract: (options?: ExtractOptions) => {
7
- name: string;
8
- next: (context: Context) => void;
9
- };
6
+ export declare const extract: (options?: ExtractOptions) => Plugin;
@@ -7,7 +7,7 @@ export const EXTRACT_OPTIONS = {};
7
7
  export const extract = (options) => {
8
8
  const name = 'extract';
9
9
  options = Object.assign({}, EXTRACT_OPTIONS, options);
10
- const next = (context) => {
10
+ const run = (context) => {
11
11
  var _a, _b;
12
12
  visitor(context.fileAST, {
13
13
  ClassDeclaration: {
@@ -71,5 +71,5 @@ export const extract = (options) => {
71
71
  context.classHasUnmount = (context.classMembers || []).some((member) => member['key'].name == CONSTANTS.LIFECYCLE_DISCONNECTED);
72
72
  context.classRender = (context.classMembers || []).find((member) => member['key'].name == CONSTANTS.METHOD_RENDER);
73
73
  };
74
- return { name, next };
74
+ return { name, run };
75
75
  };
@@ -1,8 +1,5 @@
1
1
  import { ParserOptions } from '@babel/parser';
2
- import { Context } from '../../types';
2
+ import { Plugin } from '../../types';
3
3
  export declare const PARSE_OPTIONS: Partial<ParseOptions>;
4
4
  export type ParseOptions = ParserOptions;
5
- export declare const parse: (options?: ParseOptions) => {
6
- name: string;
7
- next: (context: Context) => void;
8
- };
5
+ export declare const parse: (options?: ParseOptions) => Plugin;
@@ -6,9 +6,9 @@ export const PARSE_OPTIONS = {
6
6
  export const parse = (options) => {
7
7
  const name = 'parse';
8
8
  options = Object.assign({}, PARSE_OPTIONS, options);
9
- const next = (context) => {
9
+ const run = (context) => {
10
10
  var _a;
11
11
  context.fileAST = (_a = context.fileAST) !== null && _a !== void 0 ? _a : parser(context.fileContent, options);
12
12
  };
13
- return { name, next };
13
+ return { name, run };
14
14
  };
@@ -1,11 +1,8 @@
1
1
  /// <reference types="node" />
2
- import { Context } from '../../types';
2
+ import { Plugin } from '../../types';
3
3
  export declare const READ_OPTIONS: Partial<ReadOptions>;
4
4
  export type ReadOptions = {
5
5
  encoding: BufferEncoding;
6
6
  flag?: string | undefined;
7
7
  };
8
- export declare const read: (options?: ReadOptions) => {
9
- name: string;
10
- next: (context: Context) => void;
11
- };
8
+ export declare const read: (options?: ReadOptions) => Plugin;
@@ -5,9 +5,9 @@ export const READ_OPTIONS = {
5
5
  export const read = (options) => {
6
6
  const name = 'read';
7
7
  options = Object.assign({}, READ_OPTIONS, options);
8
- const next = (context) => {
8
+ const run = (context) => {
9
9
  var _a;
10
10
  context.fileContent = (_a = context.fileContent) !== null && _a !== void 0 ? _a : fs.readFileSync(context.filePath, options);
11
11
  };
12
- return { name, next };
12
+ return { name, run };
13
13
  };
@@ -1,9 +1,6 @@
1
- import { Context, Global } from '../../types';
1
+ import { Context, Plugin } from '../../types';
2
2
  export declare const README_OPTIONS: Partial<ReadmeOptions>;
3
3
  export type ReadmeOptions = {
4
4
  source?: (context: Context) => string;
5
5
  };
6
- export declare const readme: (options: ReadmeOptions) => {
7
- name: string;
8
- finish: (global: Global) => void;
9
- };
6
+ export declare const readme: (options: ReadmeOptions) => Plugin;
@@ -1,9 +1,6 @@
1
- import { Context } from '../../types';
1
+ import { Context, Plugin } from '../../types';
2
2
  export declare const STYLE_OPTIONS: Partial<StyleOptions>;
3
3
  export type StyleOptions = {
4
4
  source?: (context: Context) => string | string[];
5
5
  };
6
- export declare const style: (options?: StyleOptions) => {
7
- name: string;
8
- next: (context: Context) => void;
9
- };
6
+ export declare const style: (options?: StyleOptions) => Plugin;
@@ -17,7 +17,7 @@ export const STYLE_OPTIONS = {
17
17
  export const style = (options) => {
18
18
  const name = 'style';
19
19
  options = Object.assign({}, STYLE_OPTIONS, options);
20
- const next = (context) => {
20
+ const run = (context) => {
21
21
  var _a;
22
22
  const sources = [(_a = options === null || options === void 0 ? void 0 : options.source) === null || _a === void 0 ? void 0 : _a.call(options, context)].flat();
23
23
  for (const source of sources) {
@@ -37,5 +37,5 @@ export const style = (options) => {
37
37
  t.addComment(property, 'leading', CONSTANTS.COMMENT_AUTO_ADDED_PROPERTY, true);
38
38
  context.class.body.body.unshift(property);
39
39
  };
40
- return { name, next };
40
+ return { name, run };
41
41
  };
@@ -1,5 +1,2 @@
1
- import { Context } from '../../types';
2
- export declare const validate: () => {
3
- name: string;
4
- next: (context: Context) => void;
5
- };
1
+ import { Plugin } from '../../types';
2
+ export declare const validate: () => Plugin;
@@ -2,7 +2,7 @@ import * as CONSTANTS from '../../constants/index.js';
2
2
  import { hasDecorator, visitor } from '../utils/index.js';
3
3
  export const validate = () => {
4
4
  const name = 'validate';
5
- const next = (context) => {
5
+ const run = (context) => {
6
6
  let hasValidImport;
7
7
  visitor(context.fileAST, {
8
8
  ImportDeclaration(path) {
@@ -33,5 +33,5 @@ export const validate = () => {
33
33
  });
34
34
  context.isInvalid = !hasValidImport || !hasValidExport;
35
35
  };
36
- return { name, next };
36
+ return { name, run };
37
37
  };
@@ -1,9 +1,6 @@
1
- import { Global } from '../../types';
1
+ import { Plugin } from '../../types';
2
2
  export declare const VISUAL_STUDIO_CODE_OPTIONS: Partial<VisualStudioCodeOptions>;
3
3
  export interface VisualStudioCodeOptions {
4
4
  destination: string;
5
5
  }
6
- export declare const visualStudioCode: (options: VisualStudioCodeOptions) => {
7
- name: string;
8
- finish: (global: Global) => void;
9
- };
6
+ export declare const visualStudioCode: (options: VisualStudioCodeOptions) => Plugin;
@@ -1,4 +1,4 @@
1
- import { Context, Global } from '../../types';
1
+ import { Context, Plugin } from '../../types';
2
2
  export declare const WEB_TYPES_OPTIONS: Partial<WebTypesOptions>;
3
3
  export interface WebTypesOptions {
4
4
  destination: string;
@@ -7,7 +7,4 @@ export interface WebTypesOptions {
7
7
  reference?: (context: Context) => string;
8
8
  transformer?: (context: Context, component: any) => any;
9
9
  }
10
- export declare const webTypes: (options: WebTypesOptions) => {
11
- name: string;
12
- finish: (global: Global) => void;
13
- };
10
+ export declare const webTypes: (options: WebTypesOptions) => Plugin;
@@ -1,2 +1,2 @@
1
- import { File } from '@babel/types';
2
- export declare const getType: (file: File, node: any, options: any) => any;
1
+ import { File, Node } from '@babel/types';
2
+ export declare const getType: (file: File, node: Node, options: any) => any;
@@ -1,6 +1,8 @@
1
1
  import { parse } from '@babel/parser';
2
+ import glob from 'fast-glob';
2
3
  import fs from 'fs-extra';
3
4
  import { dirname, resolve } from 'path';
5
+ import { join } from 'path';
4
6
  import { visitor } from './visitor.js';
5
7
  // TODO: return type
6
8
  export const getType = (file, node, options) => {
@@ -12,7 +14,7 @@ export const getType = (file, node, options) => {
12
14
  let result;
13
15
  visitor(file, {
14
16
  ClassDeclaration(path) {
15
- if (path.node.id.name != node.typeName.name)
17
+ if (path.node.id.name != node.typeName['name'])
16
18
  return;
17
19
  result = path.node;
18
20
  path.stop();
@@ -20,7 +22,7 @@ export const getType = (file, node, options) => {
20
22
  ImportDeclaration(path) {
21
23
  for (const specifier of path.node.specifiers) {
22
24
  const alias = specifier.local.name;
23
- if (alias != node.typeName.name)
25
+ if (alias != node.typeName['name'])
24
26
  continue;
25
27
  let key;
26
28
  switch (specifier.type) {
@@ -35,14 +37,18 @@ export const getType = (file, node, options) => {
35
37
  break;
36
38
  }
37
39
  try {
40
+ const reference = glob
41
+ .sync(['.ts*', '/index.ts*'].map((key) => {
42
+ return join(directory, path.node.source.value).replace(/\\/g, '/') + key;
43
+ }))
44
+ .find((reference) => fs.existsSync(reference));
45
+ const content = fs.readFileSync(reference, 'utf8');
38
46
  const filePath = resolve(directory, path.node.source.value + '.ts');
39
- path.$ast =
40
- path.$ast ||
41
- parse(fs.readFileSync(filePath, 'utf8'), {
42
- allowImportExportEverywhere: true,
43
- plugins: ['typescript'],
44
- ranges: false
45
- });
47
+ path.$ast || (path.$ast = parse(content, {
48
+ allowImportExportEverywhere: true,
49
+ plugins: ['typescript'],
50
+ ranges: false
51
+ }));
46
52
  result = getType(path.$ast, node, {
47
53
  directory: dirname(filePath)
48
54
  });
@@ -53,15 +59,15 @@ export const getType = (file, node, options) => {
53
59
  }
54
60
  },
55
61
  TSInterfaceDeclaration(path) {
56
- if (path.node.id.name != node.typeName.name)
62
+ if (path.node.id.name != node.typeName['name'])
57
63
  return;
58
64
  result = path.node;
59
65
  path.stop();
60
66
  },
61
67
  TSTypeAliasDeclaration(path) {
62
- if (path.node.id.name != node.typeName.name)
68
+ if (path.node.id.name != node.typeName['name'])
63
69
  return;
64
- result = path.node;
70
+ result = path.node.typeAnnotation;
65
71
  path.stop();
66
72
  }
67
73
  });
@@ -3,9 +3,9 @@ export declare const API_ATTRIBUTES_SYNCER: unique symbol;
3
3
  export declare const API_CONNECTED: unique symbol;
4
4
  export declare const API_HOST: unique symbol;
5
5
  export declare const API_INSTANCE: unique symbol;
6
- export declare const API_LOADED: unique symbol;
7
6
  export declare const API_LOCKED: unique symbol;
8
7
  export declare const API_REQUEST: unique symbol;
8
+ export declare const API_RENDER_COMPLETED: unique symbol;
9
9
  export declare const API_STACKS: unique symbol;
10
10
  export declare const COMMENT_AUTO_ADDED_DEPENDENCY = " THIS DEPENDENCY IS AUTO-ADDED, DO NOT EDIT MANUALY";
11
11
  export declare const COMMENT_AUTO_ADDED_PROPERTY = " THIS PROPERTY IS AUTO-ADDED, DO NOT EDIT MANUALY";
@@ -27,10 +27,15 @@ export declare const STATIC_MEMBERS_TYPE = "type";
27
27
  export declare const STATIC_STYLES = "STYLES";
28
28
  export declare const STATIC_TAG = "TAG";
29
29
  export declare const STYLE_IMPORTED = "STYLE_IMPORTED";
30
- export declare const TYPE_BOOLEAN = "Boolean";
31
- export declare const TYPE_DATE = "Date";
32
- export declare const TYPE_FUNCTION = "Function";
33
- export declare const TYPE_STRING = "String";
34
- export declare const TYPE_NUMBER = "Number";
30
+ export declare const TYPE_ARRAY = 1;
31
+ export declare const TYPE_BOOLEAN = 2;
32
+ export declare const TYPE_DATE = 4;
33
+ export declare const TYPE_ENUM = 8;
34
+ export declare const TYPE_FUNCTION = 16;
35
+ export declare const TYPE_NULL = 32;
36
+ export declare const TYPE_NUMBER = 64;
37
+ export declare const TYPE_OBJECT = 128;
38
+ export declare const TYPE_STRING = 256;
39
+ export declare const TYPE_UNDEFINED = 512;
35
40
  export declare const UTIL_STYLE_MAPPER = "styles";
36
41
  export declare const VENDOR_UHTML_PATH = "@htmlplus/element/client/vendors/uhtml.js";
@@ -4,9 +4,9 @@ export const API_ATTRIBUTES_SYNCER = Symbol();
4
4
  export const API_CONNECTED = Symbol();
5
5
  export const API_HOST = Symbol();
6
6
  export const API_INSTANCE = Symbol();
7
- export const API_LOADED = Symbol();
8
7
  export const API_LOCKED = Symbol();
9
8
  export const API_REQUEST = Symbol();
9
+ export const API_RENDER_COMPLETED = Symbol();
10
10
  export const API_STACKS = Symbol();
11
11
  // comments
12
12
  export const COMMENT_AUTO_ADDED_DEPENDENCY = ' THIS DEPENDENCY IS AUTO-ADDED, DO NOT EDIT MANUALY';
@@ -35,11 +35,16 @@ export const STATIC_TAG = 'TAG';
35
35
  // style
36
36
  export const STYLE_IMPORTED = 'STYLE_IMPORTED';
37
37
  // types
38
- export const TYPE_BOOLEAN = 'Boolean';
39
- export const TYPE_DATE = 'Date';
40
- export const TYPE_FUNCTION = 'Function';
41
- export const TYPE_STRING = 'String';
42
- export const TYPE_NUMBER = 'Number';
38
+ export const TYPE_ARRAY = 1;
39
+ export const TYPE_BOOLEAN = 2;
40
+ export const TYPE_DATE = 4;
41
+ export const TYPE_ENUM = 8;
42
+ export const TYPE_FUNCTION = 16;
43
+ export const TYPE_NULL = 32;
44
+ export const TYPE_NUMBER = 64;
45
+ export const TYPE_OBJECT = 128;
46
+ export const TYPE_STRING = 256;
47
+ export const TYPE_UNDEFINED = 512;
43
48
  // utils
44
49
  export const UTIL_STYLE_MAPPER = 'styles';
45
50
  // vendors
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@htmlplus/element",
3
- "version": "0.5.6",
3
+ "version": "0.5.8",
4
4
  "license": "MIT",
5
5
  "author": "Masood Abdolian <m.abdolian@gmail.com>",
6
6
  "description": "A powerful library for building scalable, reusable, fast, tastable and lightweight design system for any web technologies. Powerd by Web Component.",
@@ -3,11 +3,6 @@ export interface Context {
3
3
  customElementNames?: Array<string>;
4
4
  isInvalid?: boolean;
5
5
  script?: string;
6
- outputs?: Array<{
7
- name: string;
8
- options?: any;
9
- output?: any;
10
- }>;
11
6
  assets?: string;
12
7
  class?: ClassDeclaration;
13
8
  classEvents?: Array<ClassProperty>;
package/types/plugin.d.ts CHANGED
@@ -1,10 +1,10 @@
1
1
  import { Context } from './context';
2
2
  import { Global } from './global';
3
- export type Return<T> = void | T | Promise<T>;
3
+ export type Return<T> = void | T | Promise<void | T>;
4
4
  export type Plugin = {
5
5
  name: string;
6
6
  options?: any;
7
- start?: (global: Global) => Return<void>;
8
- next?: (context: Context, global: Global) => Return<any>;
9
- finish?: (global: Global) => Return<void>;
7
+ start?: (global: Global) => Return<Global>;
8
+ run?: (context: Context, global: Global) => Return<Context>;
9
+ finish?: (global: Global) => Return<Global>;
10
10
  };
@@ -1 +0,0 @@
1
- export declare const parseValue: (value: any, type: any) => any;
@@ -1,15 +0,0 @@
1
- import * as CONSTANTS from '../../constants/index.js';
2
- import { toBoolean } from './toBoolean.js';
3
- // TODO: input type & validate date
4
- export const parseValue = (value, type) => {
5
- switch (type) {
6
- case CONSTANTS.TYPE_BOOLEAN:
7
- return toBoolean(value);
8
- case CONSTANTS.TYPE_DATE:
9
- return new Date(value);
10
- case CONSTANTS.TYPE_NUMBER:
11
- return isNaN(value) ? value : parseFloat(value);
12
- default:
13
- return value;
14
- }
15
- };