• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ecamp / hal-json-vuex / 14422895817

12 Apr 2025 07:59PM UTC coverage: 93.432%. Remained the same
14422895817

push

github

usu
3.0.0-alpha.10

160 of 183 branches covered (87.43%)

Branch coverage included in aggregate %.

281 of 289 relevant lines covered (97.23%)

930.78 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/normalizeEntityUri.ts
1
import type ResourceInterface from './interfaces/ResourceInterface'
2

3
/**
4
 * Sorts the query parameters in a URI, keeping the values of duplicate keys in order.
5
 * Example:
6
 * sortQueryParams('localhost/api/books?q=something&dup=true&alpha=0&dup=false')
7
 * // 'localhost/api/books?alpha=0&dup=true&dup=false&q=something'
8
 * @param uri      to be processed
9
 * @returns string URI with sorted query parameters
10
 */
11
function sortQueryParams (uri: string): string {
12
  const queryStart = uri.indexOf('?')
5,031✔
13
  if (queryStart === -1) return uri
5,031✔
14

15
  const prefix = uri.substring(0, queryStart)
135✔
16
  const query = new URLSearchParams(uri.substring(queryStart + 1))
135✔
17
  const modifiedQuery = new URLSearchParams();
135✔
18

19
  [...new Set(query.keys())].sort().forEach((key) => {
135✔
20
    query.getAll(key).forEach((value) => {
246✔
21
      modifiedQuery.append(key, value)
255✔
22
    })
23
  })
24

25
  if ([...modifiedQuery.keys()].length) {
135✔
26
    return `${prefix}?${modifiedQuery.toString()}`
123✔
27
  }
28
  return prefix
12✔
29
}
30

31
/**
32
 * Extracts the URI from an entity (or uses the passed URI if it is a string) and normalizes it for use in
33
 * the Vuex store.
34
 * @param uriOrEntity     entity or literal URI string
35
 * @param baseUrl         common URI prefix to remove during normalization
36
 * @returns {null|string} normalized URI, or null if the uriOrEntity argument was not understood
37
 */
38
function normalizeEntityUri (uriOrEntity: string | ResourceInterface | null = '', baseUrl = ''): string | null {
165✔
39
  const uri = typeof uriOrEntity === 'string' ? uriOrEntity : uriOrEntity?._meta?.self
5,064✔
40

41
  return normalizeUri(uri, baseUrl)
5,064✔
42
}
43

44
/**
45
 * Normalize a URI by sorting the query parameters and removing a given prefix.
46
 * @param uri             to be normalized
47
 * @param baseUrl         prefix to remove from the beginning of the URI if present
48
 * @returns {null|string} normalized URI, or null if uri is not a string
49
 */
50
function normalizeUri (uri: unknown, baseUrl: string): string | null {
51
  if (typeof uri !== 'string') return null
5,064✔
52
  const sorted = sortQueryParams(uri)
5,031✔
53
  const simpleReplace = sorted.replace(new RegExp(`^${baseUrl}`), '')
5,031✔
54

55
  try {
5,031✔
56
    const parsedBaseUrl = new URL(baseUrl)
5,031✔
57
    const uriHasHost = getHostOfUri(uri) !== undefined
4,629✔
58
    if (parsedBaseUrl.host && uriHasHost) {
4,629✔
59
      return simpleReplace
159✔
60
    }
61

62
    const pathname = parsedBaseUrl.pathname.replace(/\/$/, '')
4,470✔
63
    return sorted.replace(new RegExp(`^${pathname}`), '')
4,470✔
64
  } catch (_) {
65
    return simpleReplace
402✔
66
  }
67
}
68

69
/**
70
 * returns the host of uri if present, or undefined if new URL throws exception.
71
 * @param uri
72
 */
73
function getHostOfUri (uri: string): string | undefined {
74
  try {
4,629✔
75
    return new URL(uri).host
4,629✔
76
  } catch (_) {
77
    return undefined
4,470✔
78
  }
79
}
80

81
export default normalizeEntityUri
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc