UNPKG

js-slang

Version:

Javascript-based implementations of Source, written in Typescript

49 lines 1.56 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.timeoutPromise = exports.PromiseTimeoutError = void 0; exports.mapAndFilter = mapAndFilter; exports.objectKeys = objectKeys; exports.getChapterName = getChapterName; const runtimeSourceError_1 = require("../errors/runtimeSourceError"); const langs_1 = require("../langs"); class PromiseTimeoutError extends runtimeSourceError_1.RuntimeSourceError { } exports.PromiseTimeoutError = PromiseTimeoutError; const timeoutPromise = (promise, timeout) => new Promise((resolve, reject) => { const timeoutid = setTimeout(() => reject(new PromiseTimeoutError()), timeout); promise .then(res => { clearTimeout(timeoutid); resolve(res); }) .catch(e => { clearTimeout(timeoutid); reject(e); }); }); exports.timeoutPromise = timeoutPromise; /** * Run the mapping function over the items, filtering out any items that * that the mapping function returned `undefined` for */ function mapAndFilter(items, mapper) { return items.reduce((res, item) => { const newItem = mapper(item); if (newItem !== undefined) return [...res, newItem]; return res; }, []); } /** * Type safe `Object.keys` */ function objectKeys(obj) { return Object.keys(obj); } /** * Given the chapter value, return the string name of that chapter */ function getChapterName(chapter) { return objectKeys(langs_1.Chapter).find(name => langs_1.Chapter[name] === chapter); } //# sourceMappingURL=misc.js.map