@sudoo/marked
Version:
JavaScript & TypeScript code runner in JavaScript, safe with marked territory, asynchronous
77 lines (76 loc) • 3.95 kB
JavaScript
;
/**
* @author WMXPY
* @namespace Operation_MemberExpression
* @description Sand List
*/
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.memberExpressionSandList = exports.GET_ARRAY_MEMBER_NOT_FOUND_SYMBOL = void 0;
const error_code_1 = require("../../declare/error-code");
const error_1 = require("../../util/error/error");
const wrap_member_function_1 = require("../../util/wrap-member-function");
const sand_function_1 = require("../../variable/sand-function/sand-function");
exports.GET_ARRAY_MEMBER_NOT_FOUND_SYMBOL = Symbol("GET_ARRAY_MEMBER_NOT_FOUND");
const memberExpressionSandList = (sandbox, list, key) => {
switch (key) {
case "filter": {
return (0, wrap_member_function_1.wrapMemberFunction)(sandbox, (func) => __awaiter(void 0, void 0, void 0, function* () {
if (!(func instanceof sand_function_1.SandFunction)
&& typeof func !== "function") {
throw (0, error_1.error)(error_code_1.ERROR_CODE.LIST_FILTER_ARGUMENT_SHOULD_BE_A_FUNCTION);
}
const sandFunction = sand_function_1.SandFunction.wrapFunction(func);
const result = [];
for (let i = 0; i < list.length; i++) {
const currentResult = yield Promise.resolve(sandFunction.execute(list.get(i), i));
if (currentResult) {
result.push(list.get(i));
}
}
return result;
}));
}
case "forEach": {
return (0, wrap_member_function_1.wrapMemberFunction)(sandbox, (func) => __awaiter(void 0, void 0, void 0, function* () {
if (!(func instanceof sand_function_1.SandFunction)
&& typeof func !== "function") {
throw (0, error_1.error)(error_code_1.ERROR_CODE.LIST_FOR_EACH_ARGUMENT_SHOULD_BE_A_FUNCTION);
}
const sandFunction = sand_function_1.SandFunction.wrapFunction(func);
for (let i = 0; i < list.length; i++) {
yield Promise.resolve(sandFunction.execute(list.get(i), i));
}
return;
}));
}
case "length": {
return list.length;
}
case "map": {
return (0, wrap_member_function_1.wrapMemberFunction)(sandbox, (func) => __awaiter(void 0, void 0, void 0, function* () {
if (!(func instanceof sand_function_1.SandFunction)
&& typeof func !== "function") {
throw (0, error_1.error)(error_code_1.ERROR_CODE.LIST_MAP_ARGUMENT_SHOULD_BE_A_FUNCTION);
}
const sandFunction = sand_function_1.SandFunction.wrapFunction(func);
const result = [];
for (let i = 0; i < list.length; i++) {
const currentResult = yield Promise.resolve(sandFunction.execute(list.get(i), i));
result.push(currentResult);
}
return result;
}));
}
}
return exports.GET_ARRAY_MEMBER_NOT_FOUND_SYMBOL;
};
exports.memberExpressionSandList = memberExpressionSandList;