Skip to content

Commit

Permalink
Fixed Multi.Name.Space components
Browse files Browse the repository at this point in the history
  • Loading branch information
WebReflection committed Dec 16, 2022
1 parent c74315f commit 0f41b8f
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 32 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ function B() {}
* **transformer** - the tokenizer takes less than 1ms to transform even complex templates, closing it to zero time to update the resulting structure
* **memory** - each unique template generates *one and one only* token, updating interpolations and values *only* when the same template is executed again. This procedure grants no extra memory or Garbage Collector pressure when the same template is reused multiple times, also weakly relating the template with its own tokens to keep the heap under control
* **serialization** - complex structures can still be serialized in less than 1ms and revived in even less than that
* **size** - once minified and compressed, the [es.js](./es.js) file is around 960bytes while the [json.js](./json.js) helper is around 580bytes
* **size** - once minified and compressed, the [es.js](./es.js) file is around 990bytes while the [json.js](./json.js) helper is around 590bytes

</details>

Expand Down
2 changes: 2 additions & 0 deletions cjs/constants.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';
const EMPTY = Object.freeze([]);
exports.EMPTY = EMPTY;
const target = (nmsp, key) => nmsp[key];
exports.target = target;
17 changes: 10 additions & 7 deletions cjs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/** (c) Andrea Giammarchi - ISC */

const {Token} = require('./token.js');
const {EMPTY} = require('./constants.js');
const {EMPTY, target} = require('./constants.js');

const NUL = '\x00';

Expand Down Expand Up @@ -58,10 +58,6 @@ const parse = (template, nmsp, components) => {
if (tree) addToken(token);
tree = [token, tree || token];
};
const getKey = value => {
const i = context.indexOf(value);
return NUL + (i < 0 ? (context.push(value) - 1) : i);
};
const context = [Token.prototype, EMPTY, {}];
const esx = template.join('\x00');
let tree, i = 0, updates = 0;
Expand Down Expand Up @@ -103,13 +99,20 @@ const parse = (template, nmsp, components) => {
}
}
const tagName = name || other;
const isComponent = components.has(tagName);
const splitted = tagName.split('.');
const isComponent = components.has(splitted[0]);
let value = tagName;
if (isComponent) {
const component = splitted.reduce(target, nmsp);
const i = context.indexOf(component);
value = NUL + (i < 0 ? (context.push(component) - 1) : i);
}
pushTree(node(
isComponent ? Token.COMPONENT : Token.ELEMENT,
attributes,
selfClosing ? EMPTY : [],
tagName,
isComponent ? getKey(nmsp[tagName]) : tagName
value
));
if (selfClosing)
tree = tree[1];
Expand Down
4 changes: 2 additions & 2 deletions cjs/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/** (c) Andrea Giammarchi - ISC */

const {Token} = require('./token.js');
const {EMPTY} = require('./constants.js');
const {EMPTY, target} = require('./constants.js');

function parse(esx, nmsp, ...rest) {
const ids = (this || JSON).parse(esx, ...rest);
Expand All @@ -22,7 +22,7 @@ function fromJSON(esx) {
const {type} = esx;
switch (type) {
case Token.COMPONENT:
esx.value = this.nmsp[esx.name];
esx.value = esx.name.split('.').reduce(target, this.nmsp);
case Token.ELEMENT:
case Token.FRAGMENT: {
esx.attributes = (esx.attributes || EMPTY).map(fromJSON, this);
Expand Down
2 changes: 1 addition & 1 deletion es.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions esm/constants.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export const EMPTY = Object.freeze([]);
export const target = (nmsp, key) => nmsp[key];
17 changes: 10 additions & 7 deletions esm/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/** (c) Andrea Giammarchi - ISC */

import {Token} from './token.js';
import {EMPTY} from './constants.js';
import {EMPTY, target} from './constants.js';

const NUL = '\x00';

Expand Down Expand Up @@ -57,10 +57,6 @@ const parse = (template, nmsp, components) => {
if (tree) addToken(token);
tree = [token, tree || token];
};
const getKey = value => {
const i = context.indexOf(value);
return NUL + (i < 0 ? (context.push(value) - 1) : i);
};
const context = [Token.prototype, EMPTY, {}];
const esx = template.join('\x00');
let tree, i = 0, updates = 0;
Expand Down Expand Up @@ -102,13 +98,20 @@ const parse = (template, nmsp, components) => {
}
}
const tagName = name || other;
const isComponent = components.has(tagName);
const splitted = tagName.split('.');
const isComponent = components.has(splitted[0]);
let value = tagName;
if (isComponent) {
const component = splitted.reduce(target, nmsp);
const i = context.indexOf(component);
value = NUL + (i < 0 ? (context.push(component) - 1) : i);
}
pushTree(node(
isComponent ? Token.COMPONENT : Token.ELEMENT,
attributes,
selfClosing ? EMPTY : [],
tagName,
isComponent ? getKey(nmsp[tagName]) : tagName
value
));
if (selfClosing)
tree = tree[1];
Expand Down
4 changes: 2 additions & 2 deletions esm/json.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/** (c) Andrea Giammarchi - ISC */

import {Token} from './token.js';
import {EMPTY} from './constants.js';
import {EMPTY, target} from './constants.js';

export function parse(esx, nmsp, ...rest) {
const ids = (this || JSON).parse(esx, ...rest);
Expand All @@ -19,7 +19,7 @@ function fromJSON(esx) {
const {type} = esx;
switch (type) {
case Token.COMPONENT:
esx.value = this.nmsp[esx.name];
esx.value = esx.name.split('.').reduce(target, this.nmsp);
case Token.ELEMENT:
case Token.FRAGMENT: {
esx.attributes = (esx.attributes || EMPTY).map(fromJSON, this);
Expand Down
2 changes: 1 addition & 1 deletion json.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 1 addition & 9 deletions test/benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,7 @@
const {ESX} = await import('../esm/index.js');
const {stringify, parse} = await import('../esm/json.js');

const esx = ESX({
Object,
Array,
Uint16Array,
Function,
String,
Boolean,
Number
});
const esx = ESX();

const program = (a, b, c, d, e) => esx`
<Object>
Expand Down
11 changes: 9 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
const {ESX, Token} = require('../cjs/index.js');

function Component() {}
Component.Nested = function () {};

const assert = (value, expected, message = `expected: ${expected} - received: ${value}`) => {
if (value !== expected)
throw new Error(message);
Expand Down Expand Up @@ -103,6 +106,12 @@ assertFields(outcome.children[2], {
value: ' c'
});

var outcome = esx`<Component.Nested />`;
assertFields(outcome, {
type: Token.COMPONENT,
name: 'Component.Nested',
value: Component.Nested
});

var outcome = esx`
<>
Expand All @@ -119,5 +128,3 @@ assertFields(outcome.children[1], {
type: Token.COMPONENT,
value: Component
});

function Component() {}

0 comments on commit 0f41b8f

Please sign in to comment.