UNPKG

js-slang

Version:

Javascript-based implementations of Source, written in Typescript

56 lines 1.79 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getChapterName = exports.objectValues = exports.objectKeys = exports.mapAndFilter = exports.timeoutPromise = exports.PromiseTimeoutError = void 0; const runtimeSourceError_1 = require("../errors/runtimeSourceError"); const types_1 = require("../types"); 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; }, []); } exports.mapAndFilter = mapAndFilter; /** * Type safe `Object.keys` */ function objectKeys(obj) { return Object.keys(obj); } exports.objectKeys = objectKeys; /** * Type safe `Object.values` */ function objectValues(obj) { return Object.values(obj); } exports.objectValues = objectValues; /** * Given the chapter value, return the string name of that chapter */ function getChapterName(chapter) { return objectKeys(types_1.Chapter).find(name => types_1.Chapter[name] === chapter); } exports.getChapterName = getChapterName; //# sourceMappingURL=misc.js.map