UNPKG

midnight

Version:

Web framework for building modern web applications

78 lines (77 loc) 2.36 kB
"use strict"; var fs = require("fs"); module.exports = { /** * @param {string} html * @returns {string} */ escape: function (html) { return String(html) .replace(/&(?!\w+;)/g, "&amp;") .replace(/</g, "&lt;") .replace(/>/g, "&gt;") .replace(/"/g, "&quot;"); }, /** * @param {object} a * @param {object} b * @returns {object} */ merge: function (a, b) { if (a && b) { for (var property in b) { a[property] = b[property]; } } return a; }, /** * @param {string} path * @param {(err: NodeJS.ErrnoException, data: string) => void} callback */ readFile: function (path, callback) { fs.readFile(path, "utf8", function (err, data) { callback(err, data); }); }, path: { /** * @param {string|RegExp} path * @param {string[]} [keys] * @param {boolean} [sensitive] * @param {boolean} [strict] * @returns {{ re: RegExp, keys: string[] }} */ normalize: function (path, keys, sensitive, strict) { if (keys === void 0) { keys = []; } if (toString.call(path) === "[object RegExp]") { return { re: path, keys: keys }; } path = path .concat(strict ? "" : "/?") .replace(/\/\(/g, "(?:/") .replace(/(\/)?(\.)?:(\w+)(?:(\(.*?\)))?(\?)?(\*)?/g, function (_, slash, format, key, capture, optional, star) { keys && keys.push(key); slash = slash || ""; return ("" + (optional ? "" : slash) + "(?:" + (optional ? slash : "") + (format || "") + (capture || ((format && "([^/.]+?)") || "([^/]+?)")) + ")" + (optional || "") + (star ? "(/*)?" : "")); }) .replace(/([\/.])/g, "\\$1") .replace(/\*/g, "(.*)"); return { re: new RegExp("^" + path + "$", sensitive ? "" : "i"), keys: keys }; } } };