elm-spa
Version:
single page apps made easy
67 lines (56 loc) • 1.75 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const config_1 = __importDefault(require("../config"));
const utils_1 = require("./utils");
const routeParserOrder = (pages) => [...pages].sort(sorter);
const isHomepage = (list) => list.join('.') === config_1.default.reserved.homepage;
const isDynamic = (piece) => piece.endsWith('_');
const alphaSorter = (a, b) => a < b ? -1 : b < a ? 1 : 0;
const sorter = (a, b) => {
if (isHomepage(a))
return -1;
if (isHomepage(b))
return 1;
if (a.length < b.length)
return -1;
if (a.length > b.length)
return 1;
for (let i in a) {
const [isA, isB] = [isDynamic(a[i]), isDynamic(b[i])];
if (isA && isB)
return alphaSorter(a[i], b[i]);
if (isA)
return 1;
if (isB)
return -1;
}
return 0;
};
exports.default = (pages, _options) => `
module Gen.Route exposing
( Route(..)
, fromUrl
, toHref
)
${utils_1.paramsImports(pages)}
import Url exposing (Url)
import Url.Parser as Parser exposing ((</>), Parser)
${utils_1.routeTypeDefinition(pages)}
fromUrl : Url -> Route
fromUrl =
Parser.parse (Parser.oneOf routes) >> Maybe.withDefault NotFound
routes : List (Parser (Route -> a) a)
routes =
${utils_1.indent(utils_1.routeParserList(routeParserOrder(pages)), 1)}
toHref : Route -> String
toHref route =
let
joinAsHref : List String -> String
joinAsHref segments =
"/" ++ String.join "/" segments
in
${utils_1.indent(utils_1.routeToHref(pages), 1)}
`.trimLeft();