ovr 3.0.0 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/app/context.d.ts +1 -0
- package/dist/app/context.d.ts.map +1 -0
- package/dist/app/context.js +46 -27
- package/dist/app/{page.d.ts → get.d.ts} +5 -6
- package/dist/app/get.d.ts.map +1 -0
- package/dist/app/{page.js → get.js} +4 -6
- package/dist/app/index.d.ts +5 -4
- package/dist/app/index.d.ts.map +1 -0
- package/dist/app/index.js +7 -7
- package/dist/app/memo/index.d.ts +1 -0
- package/dist/app/memo/index.d.ts.map +1 -0
- package/dist/app/mw/csrf.d.ts +1 -0
- package/dist/app/mw/csrf.d.ts.map +1 -0
- package/dist/app/{action.d.ts → post.d.ts} +8 -9
- package/dist/app/post.d.ts.map +1 -0
- package/dist/app/{action.js → post.js} +1 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2 -2
- package/dist/jsx/chunk/index.d.ts +14 -11
- package/dist/jsx/chunk/index.d.ts.map +1 -0
- package/dist/jsx/chunk/index.js +26 -28
- package/dist/jsx/index.d.ts +13 -9
- package/dist/jsx/index.d.ts.map +1 -0
- package/dist/jsx/index.js +57 -36
- package/dist/jsx/{elements.d.ts → intrinsic-elements.d.ts} +2 -1
- package/dist/jsx/intrinsic-elements.d.ts.map +1 -0
- package/dist/jsx/{merge-async-iterables.d.ts → merge-async-generators.d.ts} +2 -4
- package/dist/jsx/merge-async-generators.d.ts.map +1 -0
- package/dist/jsx/{merge-async-iterables.js → merge-async-generators.js} +7 -9
- package/dist/jsx-dev-runtime.d.ts +1 -0
- package/dist/jsx-dev-runtime.d.ts.map +1 -0
- package/dist/jsx-runtime.d.ts +1 -0
- package/dist/jsx-runtime.d.ts.map +1 -0
- package/dist/trie/index.d.ts +1 -0
- package/dist/trie/index.d.ts.map +1 -0
- package/dist/trie/insert-params.d.ts +1 -0
- package/dist/trie/insert-params.d.ts.map +1 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/util/hash.d.ts +10 -0
- package/dist/util/hash.d.ts.map +1 -0
- package/dist/util/hash.js +9 -2
- package/package.json +4 -2
- package/src/app/context.ts +350 -0
- package/src/app/get.ts +54 -0
- package/src/app/index.ts +302 -0
- package/src/app/memo/index.ts +40 -0
- package/src/app/mw/csrf.ts +28 -0
- package/src/app/post.ts +79 -0
- package/src/index.ts +9 -0
- package/src/jsx/chunk/index.ts +68 -0
- package/src/jsx/index.ts +247 -0
- package/src/jsx/intrinsic-elements.ts +812 -0
- package/src/jsx/merge-async-generators.ts +42 -0
- package/src/jsx-dev-runtime.ts +1 -0
- package/src/jsx-runtime.ts +1 -0
- package/src/trie/index.ts +335 -0
- package/src/trie/insert-params.ts +31 -0
- package/src/types/index.ts +23 -0
- package/src/util/hash.ts +17 -0
- /package/dist/jsx/{elements.js → intrinsic-elements.js} +0 -0
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
const next = async <T, R>(iterator: AsyncIterator<T, R>, index: number) => ({
|
|
2
|
+
index,
|
|
3
|
+
result: await iterator.next(),
|
|
4
|
+
});
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Merges `AsyncGenerator[]` into a single `AsyncGenerator`, resolving all in parallel.
|
|
8
|
+
* The return of each `AsyncGenerator` is yielded from the generator with `done: true`.
|
|
9
|
+
*
|
|
10
|
+
* Adapted from [stack overflow answers](https://stackoverflow.com/questions/50585456).
|
|
11
|
+
*
|
|
12
|
+
* @param generators Resolved in parallel.
|
|
13
|
+
* @param returnValue Passed into each `iterator.return()`
|
|
14
|
+
* @yields `IteratorResult` and `index` of the resolved generator.
|
|
15
|
+
*/
|
|
16
|
+
export async function* merge<T>(generators: AsyncGenerator<T, void>[]) {
|
|
17
|
+
const iterators = generators.map((gen) => gen[Symbol.asyncIterator]());
|
|
18
|
+
const promises = new Map<number, Promise<any>>();
|
|
19
|
+
|
|
20
|
+
iterators.forEach((iterator, index) =>
|
|
21
|
+
promises.set(index, next(iterator, index)),
|
|
22
|
+
);
|
|
23
|
+
|
|
24
|
+
let current: Awaited<ReturnType<typeof next>>;
|
|
25
|
+
|
|
26
|
+
try {
|
|
27
|
+
while (promises.size > 0) {
|
|
28
|
+
yield (current = await Promise.race(promises.values()));
|
|
29
|
+
|
|
30
|
+
if (current.result.done) {
|
|
31
|
+
promises.delete(current.index);
|
|
32
|
+
} else {
|
|
33
|
+
promises.set(
|
|
34
|
+
current.index,
|
|
35
|
+
next(iterators[current.index]!, current.index),
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
} finally {
|
|
40
|
+
for (const iter of iterators) iter.return().catch(() => {}); // could have already returned
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { jsx as jsxDEV, Fragment, type JSX } from "./jsx/index.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { jsx, jsx as jsxs, Fragment, type JSX } from "./jsx/index.js";
|
|
@@ -0,0 +1,335 @@
|
|
|
1
|
+
export type Params = Record<string, string>;
|
|
2
|
+
|
|
3
|
+
export class Route<Store> {
|
|
4
|
+
/** Route pattern */
|
|
5
|
+
pattern: string;
|
|
6
|
+
|
|
7
|
+
/** Store returned when route is found */
|
|
8
|
+
store: Store;
|
|
9
|
+
|
|
10
|
+
constructor(pattern: string, store: Store) {
|
|
11
|
+
if (pattern[0] !== "/") {
|
|
12
|
+
throw new Error(
|
|
13
|
+
`Invalid route: ${pattern} - route pattern must begin with "/"`,
|
|
14
|
+
);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
this.pattern = pattern;
|
|
18
|
+
this.store = store;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
class ParamNode<Store> {
|
|
23
|
+
/** Name of the parameter (without the colon ":") */
|
|
24
|
+
name: string;
|
|
25
|
+
|
|
26
|
+
/** Matched route */
|
|
27
|
+
route: Route<Store> | null = null;
|
|
28
|
+
|
|
29
|
+
/** Static child node */
|
|
30
|
+
staticChild: Trie<Store> | null = null;
|
|
31
|
+
|
|
32
|
+
constructor(name: string) {
|
|
33
|
+
this.name = name;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export class Trie<Store> {
|
|
38
|
+
/** Unique segment of the pattern trie */
|
|
39
|
+
segment: string;
|
|
40
|
+
|
|
41
|
+
/** Static child node map, key is the first character in the segment */
|
|
42
|
+
staticMap: Map<number, Trie<Store>> | null = null;
|
|
43
|
+
|
|
44
|
+
/** Parametric child node */
|
|
45
|
+
paramChild: ParamNode<Store> | null = null;
|
|
46
|
+
|
|
47
|
+
/** Matched route */
|
|
48
|
+
route: Route<Store> | null = null;
|
|
49
|
+
|
|
50
|
+
/** Matched wildcard route */
|
|
51
|
+
wildcardRoute: Route<Store> | null = null;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* @param segment pattern segment
|
|
55
|
+
* @param staticChildren static children nodes to add to staticMap
|
|
56
|
+
*/
|
|
57
|
+
constructor(segment = "/", staticChildren?: Trie<Store>[]) {
|
|
58
|
+
this.segment = segment;
|
|
59
|
+
|
|
60
|
+
if (staticChildren?.length) {
|
|
61
|
+
this.staticMap ??= new Map();
|
|
62
|
+
|
|
63
|
+
for (const child of staticChildren) {
|
|
64
|
+
this.staticMap.set(child.segment.charCodeAt(0), child);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* @param segment new segment
|
|
71
|
+
* @returns a clone of the Node with a new segment
|
|
72
|
+
*/
|
|
73
|
+
clone(segment: string) {
|
|
74
|
+
const clone = new Trie(segment);
|
|
75
|
+
|
|
76
|
+
clone.staticMap = this.staticMap;
|
|
77
|
+
clone.paramChild = this.paramChild;
|
|
78
|
+
clone.route = this.route;
|
|
79
|
+
clone.wildcardRoute = this.wildcardRoute;
|
|
80
|
+
|
|
81
|
+
return clone;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* If the current segment is "api/posts/"
|
|
86
|
+
* and "api/movies/" is added,
|
|
87
|
+
* the node will need to be reassigned to "api/" and create two static children.
|
|
88
|
+
*
|
|
89
|
+
* @param charIndex where to split the node
|
|
90
|
+
* @param segment new segment to use
|
|
91
|
+
* @returns the new child produced from the new segment
|
|
92
|
+
*/
|
|
93
|
+
fork(charIndex: number, segment: string) {
|
|
94
|
+
const existingChild = this.clone(this.segment.slice(charIndex)); // "posts/"
|
|
95
|
+
const newChild = new Trie<Store>(segment.slice(charIndex)); // "movies/"
|
|
96
|
+
|
|
97
|
+
Object.assign(
|
|
98
|
+
this,
|
|
99
|
+
// "api/" with the above as children
|
|
100
|
+
new Trie(this.segment.slice(0, charIndex), [existingChild, newChild]),
|
|
101
|
+
);
|
|
102
|
+
|
|
103
|
+
return newChild;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* node.segment = "static/static",
|
|
108
|
+
* staticSegment = "static/",
|
|
109
|
+
* then the node needs to be split to accommodate the shorter segment
|
|
110
|
+
*
|
|
111
|
+
* @param segment
|
|
112
|
+
*/
|
|
113
|
+
split(segment: string) {
|
|
114
|
+
const secondHalf = this.clone(this.segment.slice(segment.length));
|
|
115
|
+
|
|
116
|
+
Object.assign(this, new Trie(segment, [secondHalf]));
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* @param name name of the param
|
|
121
|
+
* @returns the existing child with the same name, or creates a new
|
|
122
|
+
*/
|
|
123
|
+
setParamChild(name: string) {
|
|
124
|
+
if (this.paramChild && this.paramChild.name !== name) {
|
|
125
|
+
throw new Error(
|
|
126
|
+
`Cannot create parameter "${name}" because a different parameter ` +
|
|
127
|
+
`("${this.paramChild.name}") already exists in this location.\n\n${this}`,
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return (this.paramChild ??= new ParamNode<Store>(name));
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* @param route route return when pattern is matched
|
|
136
|
+
* @returns this - the Node
|
|
137
|
+
*/
|
|
138
|
+
add(route: Route<Store>) {
|
|
139
|
+
let current: Trie<Store> = this;
|
|
140
|
+
let pattern = route.pattern; // created to not modify the original
|
|
141
|
+
|
|
142
|
+
const endsWithWildcard = pattern.endsWith("*");
|
|
143
|
+
if (endsWithWildcard) pattern = pattern.slice(0, -1);
|
|
144
|
+
|
|
145
|
+
const paramSegments = pattern.match(/:.+?(?=\/|$)/g) ?? []; // match the params
|
|
146
|
+
const staticSegments = pattern.split(/:.+?(?=\/|$)/); // split on the params
|
|
147
|
+
|
|
148
|
+
// if the last segment is a param without a trailing slash
|
|
149
|
+
// then there will be an empty string, remove
|
|
150
|
+
if (staticSegments.at(-1) === "") staticSegments.pop();
|
|
151
|
+
|
|
152
|
+
let paramIndex = 0;
|
|
153
|
+
// for each static segment, if there are no static segments, this is skipped
|
|
154
|
+
for (
|
|
155
|
+
let staticIndex = 0;
|
|
156
|
+
staticIndex < staticSegments.length;
|
|
157
|
+
staticIndex++
|
|
158
|
+
) {
|
|
159
|
+
let staticSegment = staticSegments[staticIndex]!;
|
|
160
|
+
|
|
161
|
+
if (staticIndex > 0) {
|
|
162
|
+
// there is only a second static segment (could just be "/")
|
|
163
|
+
// if there is a param to split them, so there must be a param here
|
|
164
|
+
|
|
165
|
+
const paramChild = current.setParamChild(
|
|
166
|
+
// param without the ":" (only increment when this is reached)
|
|
167
|
+
paramSegments[paramIndex++]!.slice(1),
|
|
168
|
+
);
|
|
169
|
+
|
|
170
|
+
if (!paramChild.staticChild) {
|
|
171
|
+
// new - create node with the next static segment
|
|
172
|
+
current = paramChild.staticChild = new Trie<Store>(staticSegment);
|
|
173
|
+
continue; // next segment - no need to check since it's new
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// there's already a static child - need to check if it's a match
|
|
177
|
+
current = paramChild.staticChild;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// check if the staticSegment matches the current node
|
|
181
|
+
for (let charIndex = 0; ; ) {
|
|
182
|
+
if (charIndex === staticSegment.length) {
|
|
183
|
+
// finished iterating through the staticSegment
|
|
184
|
+
if (charIndex < current.segment.length) {
|
|
185
|
+
// too short
|
|
186
|
+
current.split(staticSegment);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
break; // next segment
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
if (charIndex === current.segment.length) {
|
|
193
|
+
// passed the end of the current node
|
|
194
|
+
if (!current.staticMap) {
|
|
195
|
+
// new pattern, create new leaf
|
|
196
|
+
current.staticMap = new Map();
|
|
197
|
+
} else {
|
|
198
|
+
// there's already static children,
|
|
199
|
+
// check to see if there's a leaf that starts with the char
|
|
200
|
+
const staticChild = current.staticMap.get(
|
|
201
|
+
staticSegment.charCodeAt(charIndex),
|
|
202
|
+
);
|
|
203
|
+
|
|
204
|
+
if (staticChild) {
|
|
205
|
+
// re-run loop with existing staticChild
|
|
206
|
+
current = staticChild;
|
|
207
|
+
staticSegment = staticSegment.slice(charIndex);
|
|
208
|
+
charIndex = 0;
|
|
209
|
+
continue;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// otherwise, add new staticChild
|
|
214
|
+
const staticChild = new Trie<Store>(staticSegment.slice(charIndex));
|
|
215
|
+
current.staticMap.set(
|
|
216
|
+
staticSegment.charCodeAt(charIndex),
|
|
217
|
+
staticChild,
|
|
218
|
+
);
|
|
219
|
+
current = staticChild;
|
|
220
|
+
|
|
221
|
+
break; // next segment
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
if (staticSegment[charIndex] !== current.segment[charIndex]) {
|
|
225
|
+
// different than the node - fork
|
|
226
|
+
current = current.fork(charIndex, staticSegment);
|
|
227
|
+
|
|
228
|
+
break; // next segment
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// character is the same - rerun to check next char
|
|
232
|
+
charIndex++;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
if (paramIndex < paramSegments.length) {
|
|
237
|
+
// final segment is a param
|
|
238
|
+
current.setParamChild(paramSegments[paramIndex]!.slice(1)).route ??=
|
|
239
|
+
route;
|
|
240
|
+
} else if (endsWithWildcard) {
|
|
241
|
+
// final segment is a wildcard
|
|
242
|
+
current.wildcardRoute ??= route;
|
|
243
|
+
} else {
|
|
244
|
+
// final segment is static
|
|
245
|
+
current.route ??= route;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
return this;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* @param pathname Path to find
|
|
253
|
+
* @returns `Route` and the matched `params` if found, otherwise `null`
|
|
254
|
+
*/
|
|
255
|
+
find(pathname: string): { route: Route<Store>; params: Params } | null {
|
|
256
|
+
if (
|
|
257
|
+
// too short
|
|
258
|
+
pathname.length < this.segment.length ||
|
|
259
|
+
// segment does not match current node segment
|
|
260
|
+
!pathname.startsWith(this.segment)
|
|
261
|
+
) {
|
|
262
|
+
return null;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
if (pathname === this.segment) {
|
|
266
|
+
// reached the end of the path
|
|
267
|
+
if (this.route) return { route: this.route, params: {} };
|
|
268
|
+
|
|
269
|
+
if (this.wildcardRoute)
|
|
270
|
+
return { route: this.wildcardRoute, params: { "*": "" } };
|
|
271
|
+
|
|
272
|
+
return null;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
if (this.staticMap) {
|
|
276
|
+
// check for a static leaf that starts with the first character
|
|
277
|
+
const staticChild = this.staticMap.get(
|
|
278
|
+
pathname.charCodeAt(this.segment.length),
|
|
279
|
+
);
|
|
280
|
+
|
|
281
|
+
if (staticChild) {
|
|
282
|
+
const result = staticChild.find(pathname.slice(this.segment.length));
|
|
283
|
+
if (result) return result;
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
// check for param leaf
|
|
288
|
+
if (this.paramChild) {
|
|
289
|
+
const slashIndex = pathname.indexOf("/", this.segment.length);
|
|
290
|
+
|
|
291
|
+
// if there is not a slash immediately following this.segment
|
|
292
|
+
if (slashIndex !== this.segment.length) {
|
|
293
|
+
// there is a valid parameter
|
|
294
|
+
if (
|
|
295
|
+
// param is the end of the pathname
|
|
296
|
+
slashIndex === -1 &&
|
|
297
|
+
this.paramChild.route
|
|
298
|
+
) {
|
|
299
|
+
return {
|
|
300
|
+
route: this.paramChild.route,
|
|
301
|
+
params: {
|
|
302
|
+
[this.paramChild.name]: pathname.slice(this.segment.length),
|
|
303
|
+
},
|
|
304
|
+
};
|
|
305
|
+
} else if (this.paramChild.staticChild) {
|
|
306
|
+
// there's a static node after the param
|
|
307
|
+
// this is how there can be multiple params, "/" in between
|
|
308
|
+
const result = this.paramChild.staticChild.find(
|
|
309
|
+
pathname.slice(slashIndex),
|
|
310
|
+
);
|
|
311
|
+
|
|
312
|
+
if (result) {
|
|
313
|
+
// add original params to the result
|
|
314
|
+
result.params[this.paramChild.name] = pathname.slice(
|
|
315
|
+
this.segment.length,
|
|
316
|
+
slashIndex,
|
|
317
|
+
);
|
|
318
|
+
|
|
319
|
+
return result;
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
// check for wildcard leaf
|
|
326
|
+
if (this.wildcardRoute) {
|
|
327
|
+
return {
|
|
328
|
+
route: this.wildcardRoute,
|
|
329
|
+
params: { "*": pathname.slice(this.segment.length) },
|
|
330
|
+
};
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
return null;
|
|
334
|
+
}
|
|
335
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { Params } from "./index.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @param parts Pattern parts
|
|
5
|
+
* @param params Parameters to insert
|
|
6
|
+
* @returns Resolved pathname with params
|
|
7
|
+
*/
|
|
8
|
+
export const insertParams = (parts: string[], params: Params) => {
|
|
9
|
+
const wild = "*";
|
|
10
|
+
|
|
11
|
+
return parts
|
|
12
|
+
.map((part) => {
|
|
13
|
+
if (part.startsWith(":")) {
|
|
14
|
+
const param = part.slice(1);
|
|
15
|
+
|
|
16
|
+
if (!(param in params))
|
|
17
|
+
throw new Error(`Parameter "${param}" did not match pattern.`);
|
|
18
|
+
|
|
19
|
+
return params[param as keyof typeof params];
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
if (part === wild) {
|
|
23
|
+
if (!(wild in params)) throw new Error("No wildcard parameter found.");
|
|
24
|
+
|
|
25
|
+
return params[wild];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return part;
|
|
29
|
+
})
|
|
30
|
+
.join("/");
|
|
31
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export type MaybePromise<T> = T | Promise<T>;
|
|
2
|
+
|
|
3
|
+
export type MaybeFunction<T> = T | (() => T);
|
|
4
|
+
|
|
5
|
+
export type DeepArray<T> = T | DeepArray<T>[];
|
|
6
|
+
|
|
7
|
+
export type ExtractParams<Pattern extends string = string> =
|
|
8
|
+
Pattern extends `${infer _Start}:${infer Param}/${infer Rest}`
|
|
9
|
+
? { [k in Param | keyof ExtractParams<Rest>]: string }
|
|
10
|
+
: Pattern extends `${infer _Start}:${infer Param}`
|
|
11
|
+
? { [k in Param]: string }
|
|
12
|
+
: Pattern extends `${infer _Rest}*`
|
|
13
|
+
? { "*": string }
|
|
14
|
+
: {};
|
|
15
|
+
|
|
16
|
+
export type ExtractMultiParams<Patterns extends string[]> = Patterns extends [
|
|
17
|
+
infer First extends string,
|
|
18
|
+
...infer Rest extends string[],
|
|
19
|
+
]
|
|
20
|
+
? Rest["length"] extends 0
|
|
21
|
+
? ExtractParams<First>
|
|
22
|
+
: ExtractParams<First> | ExtractMultiParams<Rest>
|
|
23
|
+
: never;
|
package/src/util/hash.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Fast hashing algorithm - [djb2](http://www.cse.yorku.ca/~oz/hash.html)
|
|
3
|
+
*
|
|
4
|
+
* Adapted from
|
|
5
|
+
* [SvelteKit](https://github.com/sveltejs/kit/blob/25d459104814b0c2dc6b4cf73b680378a29d8200/packages/kit/src/runtime/hash.js)
|
|
6
|
+
*
|
|
7
|
+
* @param s String to hash
|
|
8
|
+
* @returns Hashed string
|
|
9
|
+
*/
|
|
10
|
+
export const hash = (s: string) => {
|
|
11
|
+
let hash = 5381;
|
|
12
|
+
|
|
13
|
+
let i = s.length;
|
|
14
|
+
while (i) hash = (hash * 33) ^ s.charCodeAt(--i);
|
|
15
|
+
|
|
16
|
+
return (hash >>> 0).toString(36);
|
|
17
|
+
};
|
|
File without changes
|