release: WRNexusJS 0.2.41
This commit is contained in:
@@ -816,10 +816,22 @@ export const REACTIVE_RUNTIME = String.raw`
|
||||
expression || "",
|
||||
).trim();
|
||||
|
||||
// Remove an optional trailing semicolon.
|
||||
if (
|
||||
source.charAt(
|
||||
source.length - 1,
|
||||
) === ";"
|
||||
) {
|
||||
source = source
|
||||
.slice(0, -1)
|
||||
.trim();
|
||||
}
|
||||
|
||||
/*
|
||||
* Array spread:
|
||||
* Array literals containing spreads:
|
||||
*
|
||||
* [...openIndexes, index]
|
||||
* [first, ...otherItems]
|
||||
*/
|
||||
if (
|
||||
source.charAt(0) === "[" &&
|
||||
@@ -828,42 +840,57 @@ export const REACTIVE_RUNTIME = String.raw`
|
||||
) === "]" &&
|
||||
source.indexOf("...") !== -1
|
||||
) {
|
||||
var inner = source.slice(1, -1);
|
||||
var arraySource = source.slice(
|
||||
1,
|
||||
-1,
|
||||
);
|
||||
|
||||
var parts = splitTopLevel(
|
||||
inner,
|
||||
var arrayParts = splitTopLevel(
|
||||
arraySource,
|
||||
",",
|
||||
);
|
||||
|
||||
var values = [];
|
||||
var arrayResult = [];
|
||||
|
||||
parts.forEach(function (part) {
|
||||
var item = part.trim();
|
||||
arrayParts.forEach(function (
|
||||
part,
|
||||
) {
|
||||
var partSource =
|
||||
part.trim();
|
||||
|
||||
if (!partSource) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (
|
||||
item.slice(0, 3) === "..."
|
||||
partSource.slice(0, 3) ===
|
||||
"..."
|
||||
) {
|
||||
var spreadValue =
|
||||
var spreadResult =
|
||||
evaluateExpression(
|
||||
item.slice(3),
|
||||
partSource
|
||||
.slice(3)
|
||||
.trim(),
|
||||
read,
|
||||
);
|
||||
|
||||
if (
|
||||
Array.isArray(spreadValue)
|
||||
Array.isArray(
|
||||
spreadResult,
|
||||
)
|
||||
) {
|
||||
Array.prototype.push.apply(
|
||||
values,
|
||||
spreadValue,
|
||||
arrayResult,
|
||||
spreadResult,
|
||||
);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
values.push(
|
||||
arrayResult.push(
|
||||
evaluateExpression(
|
||||
item,
|
||||
partSource,
|
||||
read,
|
||||
),
|
||||
);
|
||||
@@ -871,98 +898,151 @@ export const REACTIVE_RUNTIME = String.raw`
|
||||
|
||||
return {
|
||||
matched: true,
|
||||
value: values,
|
||||
value: arrayResult,
|
||||
};
|
||||
}
|
||||
|
||||
/*
|
||||
* Array methods with expression callbacks:
|
||||
*
|
||||
* list.filter((item) => item !== value)
|
||||
* list.map((item, index) => item.title)
|
||||
* First recognize the array method call without
|
||||
* trying to parse the callback in the same regex.
|
||||
*/
|
||||
var callbackMatch =
|
||||
/^([\s\S]+)\.(filter|map|some|every|find|findIndex)\(\s*(?:\(\s*([A-Za-z_$][A-Za-z0-9_$]*)\s*(?:,\s*([A-Za-z_$][A-Za-z0-9_$]*))?\s*\)|([A-Za-z_$][A-Za-z0-9_$]*))\s*=>\s*([\s\S]+)\)$/.exec(
|
||||
var methodMatch =
|
||||
/^([\s\S]+)\.(filter|map|some|every|find|findIndex)\(\s*([\s\S]*)\s*\)$/.exec(
|
||||
source,
|
||||
);
|
||||
|
||||
if (callbackMatch) {
|
||||
var collection =
|
||||
evaluateExpression(
|
||||
callbackMatch[1].trim(),
|
||||
read,
|
||||
);
|
||||
|
||||
var method = callbackMatch[2];
|
||||
|
||||
var itemName =
|
||||
callbackMatch[3] ||
|
||||
callbackMatch[5];
|
||||
|
||||
var indexName =
|
||||
callbackMatch[4];
|
||||
|
||||
var callbackBody =
|
||||
callbackMatch[6].trim();
|
||||
|
||||
if (!Array.isArray(collection)) {
|
||||
collection = [];
|
||||
}
|
||||
|
||||
var callback = function (
|
||||
itemValue,
|
||||
itemIndex,
|
||||
) {
|
||||
return evaluateExpression(
|
||||
callbackBody,
|
||||
function (name) {
|
||||
if (name === itemName) {
|
||||
return itemValue;
|
||||
}
|
||||
|
||||
if (
|
||||
indexName &&
|
||||
name === indexName
|
||||
) {
|
||||
return itemIndex;
|
||||
}
|
||||
|
||||
return read(name);
|
||||
},
|
||||
);
|
||||
};
|
||||
|
||||
var result;
|
||||
|
||||
if (method === "filter") {
|
||||
result =
|
||||
collection.filter(callback);
|
||||
} else if (method === "map") {
|
||||
result =
|
||||
collection.map(callback);
|
||||
} else if (method === "some") {
|
||||
result =
|
||||
collection.some(callback);
|
||||
} else if (method === "every") {
|
||||
result =
|
||||
collection.every(callback);
|
||||
} else if (method === "find") {
|
||||
result =
|
||||
collection.find(callback);
|
||||
} else {
|
||||
result =
|
||||
collection.findIndex(callback);
|
||||
}
|
||||
|
||||
if (!methodMatch) {
|
||||
return {
|
||||
matched: true,
|
||||
value: result,
|
||||
matched: false,
|
||||
value: undefined,
|
||||
};
|
||||
}
|
||||
|
||||
var collectionSource =
|
||||
methodMatch[1].trim();
|
||||
|
||||
var method =
|
||||
methodMatch[2];
|
||||
|
||||
var callbackSource =
|
||||
methodMatch[3].trim();
|
||||
|
||||
/*
|
||||
* Supported callbacks:
|
||||
*
|
||||
* item => item.active
|
||||
* (item) => item.active
|
||||
* (item, index) => index > 0
|
||||
*/
|
||||
var callbackMatch =
|
||||
/^(?:\(\s*([A-Za-z_$][A-Za-z0-9_$]*)\s*(?:,\s*([A-Za-z_$][A-Za-z0-9_$]*)\s*)?\)|([A-Za-z_$][A-Za-z0-9_$]*))\s*=>\s*([\s\S]+)$/.exec(
|
||||
callbackSource,
|
||||
);
|
||||
|
||||
if (!callbackMatch) {
|
||||
return {
|
||||
matched: false,
|
||||
value: undefined,
|
||||
};
|
||||
}
|
||||
|
||||
var itemName =
|
||||
callbackMatch[1] ||
|
||||
callbackMatch[3];
|
||||
|
||||
var indexName =
|
||||
callbackMatch[2];
|
||||
|
||||
var callbackBody =
|
||||
callbackMatch[4].trim();
|
||||
|
||||
/*
|
||||
* Support expression bodies wrapped in parentheses:
|
||||
*
|
||||
* item => (
|
||||
* item.active
|
||||
* )
|
||||
*/
|
||||
if (
|
||||
callbackBody.charAt(0) ===
|
||||
"(" &&
|
||||
callbackBody.charAt(
|
||||
callbackBody.length - 1,
|
||||
) === ")"
|
||||
) {
|
||||
callbackBody = callbackBody
|
||||
.slice(1, -1)
|
||||
.trim();
|
||||
}
|
||||
|
||||
var collection =
|
||||
evaluateExpression(
|
||||
collectionSource,
|
||||
read,
|
||||
);
|
||||
|
||||
if (!Array.isArray(collection)) {
|
||||
collection = [];
|
||||
}
|
||||
|
||||
var callback = function (
|
||||
itemValue,
|
||||
itemIndex,
|
||||
) {
|
||||
return evaluateExpression(
|
||||
callbackBody,
|
||||
function (name) {
|
||||
if (name === itemName) {
|
||||
return itemValue;
|
||||
}
|
||||
|
||||
if (
|
||||
indexName &&
|
||||
name === indexName
|
||||
) {
|
||||
return itemIndex;
|
||||
}
|
||||
|
||||
return read(name);
|
||||
},
|
||||
);
|
||||
};
|
||||
|
||||
var result;
|
||||
|
||||
if (method === "filter") {
|
||||
result =
|
||||
collection.filter(callback);
|
||||
} else if (
|
||||
method === "map"
|
||||
) {
|
||||
result =
|
||||
collection.map(callback);
|
||||
} else if (
|
||||
method === "some"
|
||||
) {
|
||||
result =
|
||||
collection.some(callback);
|
||||
} else if (
|
||||
method === "every"
|
||||
) {
|
||||
result =
|
||||
collection.every(callback);
|
||||
} else if (
|
||||
method === "find"
|
||||
) {
|
||||
result =
|
||||
collection.find(callback);
|
||||
} else {
|
||||
result =
|
||||
collection.findIndex(
|
||||
callback,
|
||||
);
|
||||
}
|
||||
|
||||
return {
|
||||
matched: false,
|
||||
value: undefined,
|
||||
matched: true,
|
||||
value: result,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -974,7 +1054,9 @@ export const REACTIVE_RUNTIME = String.raw`
|
||||
expr,
|
||||
read,
|
||||
) {
|
||||
expr = String(expr || "").trim();
|
||||
expr = String(
|
||||
expr || "",
|
||||
).trim();
|
||||
|
||||
var special =
|
||||
evaluateSpecialExpression(
|
||||
@@ -988,6 +1070,7 @@ export const REACTIVE_RUNTIME = String.raw`
|
||||
|
||||
var tokens =
|
||||
tokenizeExpression(expr);
|
||||
|
||||
var index = 0;
|
||||
function peek() { return tokens[index]; }
|
||||
function next() { return tokens[index++]; }
|
||||
|
||||
Reference in New Issue
Block a user