@shakerquiz/utilities 0.5.178 → 0.5.180

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "@shakerquiz/utilities",
4
- "version": "0.5.178",
4
+ "version": "0.5.180",
5
5
  "author": "yurkimus <yurkimus@gmail.com>",
6
6
  "license": "ISC",
7
7
  "repository": {
@@ -13,3 +13,8 @@ export const Method = Object.freeze({
13
13
  })
14
14
 
15
15
  export const Methods = Object.freeze(Object.values(Method))
16
+
17
+ export const inferMethod = Object.freeze(
18
+ /** @returns {keyof typeof Method | 'Unknown'} */
19
+ x => Method[x] ?? 'Unknown',
20
+ )
@@ -1,24 +1,32 @@
1
- import { Route, RouteParams, RoutePathname } from '../enumerations/route.js'
1
+ import { inferRoute, inferRouteParams, inferRoutePathname } from '../enumerations/route.js'
2
2
 
3
- import { inferKey } from './infer-key.js'
4
- import { isTag } from './tag.js'
3
+ export const hydrateRoutePathname = Object.freeze(
4
+ /**
5
+ * @param {*} maybeRoute
6
+ * @param {any[]} maybeRouteParams
7
+ */
8
+ (maybeRoute, maybeRouteParams) => {
9
+ if (!Array.isArray(maybeRouteParams))
10
+ throw TypeError(`Parameter 'maybeRouteParams' must be 'Array'.`)
5
11
 
6
- /**
7
- * @param {*} maybeRoute
8
- * @param {any[]} maybeRouteParams
9
- */
10
- export var hydrateRoutePathname = (maybeRoute, maybeRouteParams) => {
11
- if (!isTag('Array', maybeRouteParams))
12
- throw TypeError(`Parameter 'maybeRouteParams' must be 'Array'.`)
12
+ var route = inferRoute(maybeRoute)
13
13
 
14
- var route = inferKey(Route, maybeRoute)
14
+ if (route === 'Unknown')
15
+ throw TypeError(`Could not infer route of: '${maybeRoute}'.`)
15
16
 
16
- var pathname = inferKey(RoutePathname, route)
17
+ var routePathname = inferRoutePathname(route)
17
18
 
18
- var params = inferKey(RouteParams, route)
19
+ if (routePathname === 'Unknown')
20
+ throw TypeError(`Could not infer route pathname of: '${route}'.`)
19
21
 
20
- return params.reduce(
21
- (pathname, param, index) => pathname.replace(param, maybeRouteParams[index]),
22
- pathname,
23
- )
24
- }
22
+ var routeParams = inferRouteParams(route)
23
+
24
+ if (routeParams === 'Unknown')
25
+ throw TypeError(`Could not infer route params of: '${route}'.`)
26
+
27
+ return routeParams.reduce(
28
+ (pathname, param, index) => pathname.replace(param, maybeRouteParams[index]),
29
+ routePathname,
30
+ )
31
+ },
32
+ )
@@ -1,15 +1,19 @@
1
- import { Method } from '../enumerations/method.js'
1
+ import { inferMethod } from '../enumerations/method.js'
2
2
 
3
3
  import { hydrateRoutePathname } from './hydrate-route-pathname.js'
4
- import { inferKey } from './infer-key.js'
5
-
6
- /**
7
- * @param {any} maybeMethod
8
- * @param {any} maybeRoute
9
- * @param {any[]} maybeParameters
10
- */
11
- export var routeKey = (maybeMethod, maybeRoute, maybeParameters) =>
12
- ''
13
- + inferKey(Method, maybeMethod)
14
- + '/'
15
- + hydrateRoutePathname(maybeRoute, maybeParameters)
4
+
5
+ export const routeKey = Object.freeze(
6
+ /**
7
+ * @param {any} maybeMethod
8
+ * @param {any} maybeRoute
9
+ * @param {any[]} maybeParameters
10
+ */
11
+ (maybeMethod, maybeRoute, maybeParameters) => {
12
+ var method = inferMethod(maybeMethod)
13
+
14
+ if (method === 'Unknown')
15
+ throw TypeError(`Could not infer method of: '${maybeMethod}'.`)
16
+
17
+ return method + '/' + hydrateRoutePathname(maybeRoute, maybeParameters)
18
+ },
19
+ )
package/source/index.js CHANGED
@@ -8,9 +8,9 @@ export * from './enumerations/method.js'
8
8
  export * from './enumerations/mode.js'
9
9
  export * from './enumerations/network.js'
10
10
  export * from './enumerations/numerosity.js'
11
+ export * from './enumerations/pattern.js'
11
12
  export * from './enumerations/phase.js'
12
13
  export * from './enumerations/quantifier.js'
13
- export * from './enumerations/pattern.js'
14
14
  export * from './enumerations/route.js'
15
15
  export * from './enumerations/runtime.js'
16
16
  export * from './enumerations/service.js'
@@ -30,6 +30,5 @@ export * from './enumerations/venue/audience.js'
30
30
  export * from './enumerations/venue/status.js'
31
31
 
32
32
  export * from './functions/hydrate-route-pathname.js'
33
- export * from './functions/infer-key.js'
34
33
  export * from './functions/route-key.js'
35
34
  export * from './functions/tag.js'
@@ -1,13 +0,0 @@
1
- /**
2
- * @template {any} E
3
- *
4
- * @param {E} Enumeration
5
- *
6
- * @returns {E[keyof E]}
7
- */
8
- export var inferKey = (Enumeration, value) => {
9
- if (value in Enumeration)
10
- return Enumeration[value]
11
- else
12
- throw TypeError(`Cannot infer key of value: '${value}' for enum: '${Object.values(Enumeration).join(', ')}'.`)
13
- }