-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathmock-utils.js
76 lines (67 loc) · 2.19 KB
/
mock-utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { isPlainObject, validKeys, buildRecursive } from '../utils/index'
export function toSortedQueryString(entry) {
if (!isPlainObject(entry)) {
return entry
}
return validKeys(entry)
.sort()
.map((key) => {
const value = entry[key]
if (isPlainObject(value)) {
return toSortedQueryString(value)
}
return buildRecursive(key, value)
})
.join('&')
.replace(/%20/g, '+')
}
/**
* Filters an `object` by keeping only the keys fulfilling the `predicate`
*
* @param {Object} object - An object
* @param {Function} predicate - A function of type (key: string) => boolean
* @returns {Object} The filtered object
*/
function filterByPredicate(object, predicate) {
return Object.entries(object)
.filter(([key]) => predicate(key))
.reduce((acc, [key, value]) => ({ ...acc, [key]: value }), {})
}
/**
* Verify if the object `A` is contained within object `B` - shallowly.
* In other words, is A a subset of B?
*
* @see https://en.wikipedia.org/wiki/Subset
*
* @param {Object} A - The object to test
* @param {Object} B - The superset object to verify against
* @returns A boolean representing if A is a shallow subset of B
*/
export function isSubset(A, B) {
// Make B only contain the non-nullish keys it has in in common with A
const keysFromA = validKeys(A)
const filteredB = filterByPredicate(B, (keyFromB) => keysFromA.includes(keyFromB))
return toSortedQueryString(A) === toSortedQueryString(filteredB)
}
export function filterKeys(A, B) {
// Make B only contain the non-nullish keys it has in in common with A
const keysFromA = validKeys(A)
const filteredB = filterByPredicate(B, (keyFromB) => keysFromA.includes(keyFromB))
return filteredB
}
/**
* Sort the query params on a URL based on the 'key=value' string value.
* E.g. /example?b=2&a=1 will become /example?a=1&b=2
*
* @param {String} url - a URL that should be sorted (with or without query params)
*/
export function sortedUrl(url) {
const urlParts = url.split('?')
if (urlParts.length > 1) {
const query = urlParts[1]
const sortedQuery = query.split('&').sort().join('&')
return `${urlParts[0]}?${sortedQuery}`
} else {
return urlParts[0]
}
}