Skip to content

Commit

Permalink
v0.0.0.99
Browse files Browse the repository at this point in the history
  • Loading branch information
mimiMonads committed Feb 2, 2023
1 parent b7248bf commit ee4d40b
Show file tree
Hide file tree
Showing 17 changed files with 741 additions and 216 deletions.
119 changes: 119 additions & 0 deletions builder/atlas/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import {
ArrayFiler,
funRouterOptions,
ParamsMethod,
RequestFunction,
} from "../../types.ts";
import badMethod from "../../components/util/badMethod.ts";
import notFound from "../../components/util/notFound.ts";

type InnerObj = [string, RequestFunction] | [];
type InnerObjEmpty = [string, RequestFunction];
type Map = Record<number, Record<number, InnerObj[]>>;

export type Atlas = [
ParamsMethod[],
number[][],
string[][][],
RequestFunction[],
ArrayFiler[1],
];
export default (_o?: funRouterOptions) => (a: ArrayFiler): Atlas =>
(
(am) =>
(
(ob) =>
(
(il) =>
(
(al) =>
(
(ul) => [am, il, al, ul, a[1]]
)(
il.map(
(x, i) =>
x.map(
(y) =>
ob[i][y].map(
(z) => [z[1]],
),
) as [RequestFunction][][],
)
.flat(3)
.concat(a[1].map(([_, _a, x]) => x))
.concat(notFound)
.concat(badMethod),
)
)(
il.map(
(x, i) =>
x.map(
(y) => ob[i][y].map((x) => x[0]),
),
) as string[][][],
)
)(
Object.keys(ob)
.map((x) => Number(x))
.map(
(x) =>
Object
.keys(ob[Number(x)])
.map((y) => Number(y)),
) as [number[]],
)
)(
Object.fromEntries(
am.map((x) => a[0].filter((y) => x === y[2]))
.map(
(x) =>
((p) =>
Object.fromEntries(
p.map(
(y) => [
y,
(
(za) =>
za[0][0] === ""
? za
.slice(1)
.reduceRight(
(acc, z) => acc.concat([z]),
[za[0]],
)
.reverse()
: za
)(
x
.reduce(
(acc, z) =>
z[0] === y
? [...acc, [z[1], z[3]]] as InnerObjEmpty[]
: acc,
[] as InnerObjEmpty[],
),
),
],
),
))(
x.map((y) => y[0])
.reduce(
(acc, y) => acc.includes(y) ? acc : acc.concat([y]),
[] as number[],
) as number[],
),
)
.map(
(x, i) => [i, x],
),
) as Map,
)
)(
a[0]
.reduce((acc: ParamsMethod[], am) =>
acc
.includes(am[2])
? acc
: [...acc, am[2]], []) as ParamsMethod[],
);

23 changes: 23 additions & 0 deletions builder/atlas/split.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {
ArrayFiler,
funRouterOptions,
RouteTypes,
} from "../../types.ts";

export default (o?: funRouterOptions) => (a: RouteTypes[]): ArrayFiler =>
(
(fl) =>
(
(sp) => [
fl.map (
x => [x[1].split("/").length - 1,x[1],x[0],x[2]]
) ,
sp,
]
)(
a.filter((x) => typeof x[3] === "string") as RouteTypes[],
)
)(
a.filter((x) => x[3] === false),
);

32 changes: 32 additions & 0 deletions builder/composer/methods.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { funRouterOptions } from "../../types.ts";
import { Atlas } from "../atlas.ts"
import parser from "./parser.ts"
import position from "../position.ts"


export default (o?:funRouterOptions) => (atlas:Atlas) =>
(
position =>
atlas[0]
.map(
(_,i) =>
o && "hasName" in o && typeof o.hasName === "string"
? (p => (s:string) => p(s.slice(o!.hasName!.length -1)))(parser(o)(atlas[2][i])(position[i])(atlas[1][i])(atlas[3].length - 2))
: ( p =>
(
n =>
(s:string)=> n !== -1 ? p(s.slice(n)) : p(s.slice(
n = s
.split("/")
.filter((x) => x !== "")
.reduce((acc, x, u) => u <= 1 ? acc + x.length : acc, 3) - 1) )
)(
-1
)
)(
parser(o)(atlas[2][i])(position[i])(atlas[1][i])(atlas[3].length - 2)
)
) as [(s:string) => number]
)(
position(o) (atlas[1])(atlas[2])
)
2 changes: 1 addition & 1 deletion builder/composer/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default (o?: funRouterOptions) => (sa:string[][]) =>(position: number[])
],
).reverse().reduce((acc, v) => v[0] + acc + v[1], "")
}
)`)()
)`)() as (s:string) => number
)(
an.reduce((acc, y) => y > acc ? y : acc, 0)
)
Expand Down
47 changes: 47 additions & 0 deletions builder/composer/specialString.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@

import { funRouterOptions, RouteTypes } from "../../types.ts";

export default (o?: funRouterOptions) => (max: number) => (ar: RouteTypes[]) =>
(
(nar) =>
(
p =>
(
(f) => (
fa =>(s:string) => fa(p(s))
)(
f(nar)
)
)(
(ar: string[]) =>
(new Function(`return s=>${
ar.reduceRight(
(acc, v, i) => `s.indexOf("${v}")===0?${i + max - 2}:` + acc,
"-1",
)
}`))() as (s: string) => number,
)
)(
o && "hasName" in o && typeof o.hasName === "string"
? (s:string) => s.slice(o!.hasName!.length -1)
:
(
n =>
(s:string)=> n !== -1 ? s.slice(n) : s.slice(
n = s
.split("/")
.filter((x) => x !== "")
.reduce((acc, x, u) => u <= 1 ? acc + x.length : acc, 3) - 1)
)(
-1
)

)
)(
ar.map(([_, a]) => a)
.sort((a, b) => b.length - a.length),
);




39 changes: 39 additions & 0 deletions builder/solver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { funRouterOptions, Atlas, RouteTypes} from "../types.ts";
import solver from "./composer/methods.ts"
import specialString from "./composer/specialString.ts";

export default
(o?:funRouterOptions) =>
(atlas:Atlas) => (
me => (
solve =>
atlas[4].length === 0
? (r:Request) =>
me(r.method) !== -1
? solve[me(r.method)](r.url)
: atlas[3].length - 1
: (
(sc) =>
(r: Request) =>
(
(w) =>
w === -1
?
me(r.method) !== -1
? solve[me(r.method)](r.url)
: atlas[3].length - 1
: w
)(
sc(r.url),
)
)(
specialString({})(atlas[3].length - 1)(
atlas[4].map((x) => [x[0], x[3], x[2], x[1]] as RouteTypes),
),
)
)(
solver (o) (atlas)
)
)(
((a: string[]) => ((o) => (s: string) => o.indexOf(s[0]))(a.map((x) => x[0])))(atlas[0])
)
8 changes: 5 additions & 3 deletions fun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@ import { ObjectRawResponse } from "./optimizer/types.ts";
import optimizer from "./optimizer/optimize.ts";
import atlas from "./builder/atlas.ts";
import arraySwap from "./builder/arraySwap.ts";
import resolver from "./builder/resolver.ts";

import solver from "./builder/solver.ts";
import split from "./builder/atlas/split.ts";

export default (o?: funRouterOptions) => (routes: ObjectRawResponse[]) =>
((re) =>
((s) => (r: Request) => re[3][s(r)](r)
)(
resolver(o)(re),
solver(o)(re),
))(
atlas(o)(
arraySwap(o)(
split(o)(
optimizer(o)(routes),
),
),
Expand Down
7 changes: 5 additions & 2 deletions optimizer/staticFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,20 @@ import resolver from "../builder/resolver.ts";
import staticPaths from "./staticPaths.ts";
import mime from "../components/util/mime.ts";

import solver from "../builder/solver.ts";
import split from "../builder/atlas/split.ts";

export default (o?: funRouterOptions) =>
(f: ObjectRawResponseStatic): (r: Request) => Response | Promise<Response> =>
((p) =>
((re) =>
(
(s) => (r: Request) => re[3][s(r)](r)
)(
resolver(o)(re),
solver(o)(re),
))(
atlas(o)(
arraySwap(o)(
split(o)(
staticPaths( "mime" in f && f.mime === false ? [] : "extra" in f ? mime.concat(f.extra): mime )(p)(f.name),
),
),
Expand Down
57 changes: 57 additions & 0 deletions test/builder/atlas/main.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { assertEquals } from "https://deno.land/std@0.160.0/testing/asserts.ts";
import atlas from "../../../builder/atlas.ts";
import optimize from "../../../optimizer/optimize.ts";
import paths from "../../util/paths.ts";
import split from "../../../builder/atlas/split.ts"

Deno.test(
"Atlas",
(_) =>
assertEquals(
((r) => [r[0], r[1], r[2]])(
atlas()(split()(optimize()(paths))),
),
[
[
"GET",
"POST",
"HEAD",
"DELETE",
],
[
[
1,
2,
4,
],
[
1,
],
[
1,
],
[
1,
],
],
[
[
["/", "/test"],
["/test/"],
["/test/:id/:name/"],
],
[
["/"],
],
[
["/"],
],
[
["/"],
],
],
],
),
);


Loading

0 comments on commit ee4d40b

Please sign in to comment.