All files / src/functions guards.ts

100% Statements 9/9
100% Branches 0/0
100% Functions 9/9
100% Lines 9/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216                                                                                          1690x                                             1692x                                         321x                                       363x                                         287x                                       1245x                                         715x                                         443x                                         324x    
/**
 * Type guards that recognise library types at run time, across copies of the package.
 *
 * @packageDocumentation
 */
 
import type { ReadonlyCollection } from '../interfaces/collection'
import type { ReadonlyList, ReadonlySequencedCollection } from '../interfaces/list'
import type { ReadonlyMapCollection } from '../interfaces/map'
import type { ReadonlyDeque, ReadonlyQueue } from '../interfaces/queue'
import type { ReadonlySetCollection } from '../interfaces/set'
import type { ReadonlySortedMap } from '../interfaces/sorted-map'
import type { ReadonlySortedSet } from '../interfaces/sorted-set'
import {
  DEQUE_BRAND,
  isBranded,
  LIST_BRAND,
  MAP_COLLECTION_BRAND,
  QUEUE_BRAND,
  SEQUENCED_COLLECTION_BRAND,
  SET_COLLECTION_BRAND,
  SORTED_MAP_BRAND,
  SORTED_SET_BRAND,
} from '../internal/brand'
import { COLLECTION_BRAND } from '../internal/output'
 
/**
 * Returns whether `value` is a collection from this library — any collection, view or read-only
 * wrapper of the same major version, from any loaded copy of the package. Maps, arrays and native
 * `Set`/`Map` are not.
 *
 * Time: O(1), plus any getter or proxy trap that reading a property of `value` runs.
 *
 * @param value - Any value.
 * @returns `true` when `value` is a library collection.
 * @throws whatever a getter or proxy trap on `value` throws while the property is read.
 *
 * @example
 * ```ts
 * function count(value: unknown): number {
 *   return isCollection(value) ? value.size : 0
 * }
 * ```
 */
export function isCollection(value: unknown): value is ReadonlyCollection<unknown> {
  return isBranded(value, COLLECTION_BRAND)
}
 
/**
 * Returns whether `value` is a sequenced collection from this library — a collection with a first
 * and last element, such as a list or one of its views — of the same major version, from any loaded
 * copy of the package.
 *
 * Time: O(1), plus any getter or proxy trap that reading a property of `value` runs.
 *
 * @param value - Any value.
 * @returns `true` when `value` is a library sequenced collection.
 * @throws whatever a getter or proxy trap on `value` throws while the property is read.
 *
 * @example
 * ```ts
 * isSequencedCollection(new ArrayList([1])) // true
 * isSequencedCollection([1])                // false
 * ```
 */
export function isSequencedCollection(
  value: unknown,
): value is ReadonlySequencedCollection<unknown> {
  return isBranded(value, SEQUENCED_COLLECTION_BRAND)
}
 
/**
 * Returns whether `value` is a list from this library — an `ArrayList`, a sub-list or a reversed
 * view — of the same major version, from any loaded copy of the package. Arrays are not.
 *
 * Time: O(1), plus any getter or proxy trap that reading a property of `value` runs.
 *
 * @param value - Any value.
 * @returns `true` when `value` is a library list.
 * @throws whatever a getter or proxy trap on `value` throws while the property is read.
 *
 * @example
 * ```ts
 * function first(value: unknown): unknown {
 *   return isList(value) && !value.isEmpty() ? value.get(0) : undefined
 * }
 * ```
 */
export function isList(value: unknown): value is ReadonlyList<unknown> {
  return isBranded(value, LIST_BRAND)
}
 
/**
 * Returns whether `value` is a queue from this library — an `ArrayDeque` or its reversed view —
 * of the same major version, from any loaded copy of the package.
 *
 * Time: O(1), plus any getter or proxy trap that reading a property of `value` runs.
 *
 * @param value - Any value.
 * @returns `true` when `value` is a library queue.
 * @throws whatever a getter or proxy trap on `value` throws while the property is read.
 *
 * @example
 * ```ts
 * isQueue(new ArrayDeque()) // true
 * isQueue([])               // false
 * ```
 */
export function isQueue(value: unknown): value is ReadonlyQueue<unknown> {
  return isBranded(value, QUEUE_BRAND)
}
 
/**
 * Returns whether `value` is a deque from this library — an `ArrayDeque` or its reversed view —
 * of the same major version, from any loaded copy of the package.
 *
 * Time: O(1), plus any getter or proxy trap that reading a property of `value` runs.
 *
 * @param value - Any value.
 * @returns `true` when `value` is a library deque.
 * @throws whatever a getter or proxy trap on `value` throws while the property is read.
 *
 * @example
 * ```ts
 * function last(value: unknown): unknown {
 *   return isDeque(value) ? value.peekLast() : undefined
 * }
 * ```
 */
export function isDeque(value: unknown): value is ReadonlyDeque<unknown> {
  return isBranded(value, DEQUE_BRAND)
}
 
/**
 * Returns whether `value` is a set from this library — a `HashSet` or any other set — of the same
 * major version, from any loaded copy of the package. Native `Set` is not.
 *
 * Time: O(1), plus any getter or proxy trap that reading a property of `value` runs.
 *
 * @param value - Any value.
 * @returns `true` when `value` is a library set.
 * @throws whatever a getter or proxy trap on `value` throws while the property is read.
 *
 * @example
 * ```ts
 * isSetCollection(new HashSet([1])) // true
 * isSetCollection(new Set([1]))     // false
 * ```
 */
export function isSetCollection(value: unknown): value is ReadonlySetCollection<unknown> {
  return isBranded(value, SET_COLLECTION_BRAND)
}
 
/**
 * Returns whether `value` is a map from this library — a `HashMap` or any other map — of the same
 * major version, from any loaded copy of the package. Native `Map` is not, and neither are the
 * views a map returns (they are collections).
 *
 * Time: O(1), plus any getter or proxy trap that reading a property of `value` runs.
 *
 * @param value - Any value.
 * @returns `true` when `value` is a library map.
 * @throws whatever a getter or proxy trap on `value` throws while the property is read.
 *
 * @example
 * ```ts
 * isMapCollection(new HashMap([['a', 1]])) // true
 * isMapCollection(new Map([['a', 1]]))     // false
 * ```
 */
export function isMapCollection(value: unknown): value is ReadonlyMapCollection<unknown, unknown> {
  return isBranded(value, MAP_COLLECTION_BRAND)
}
 
/**
 * Returns whether `value` is a sorted set from this library — a `TreeSet`, one of its range views
 * or a reversed view — of the same major version, from any loaded copy of the package. A `HashSet`
 * and native `Set` are not.
 *
 * Time: O(1), plus any getter or proxy trap that reading a property of `value` runs.
 *
 * @param value - Any value.
 * @returns `true` when `value` is a library sorted set.
 * @throws whatever a getter or proxy trap on `value` throws while the property is read.
 *
 * @example
 * ```ts
 * isSortedSet(new TreeSet([1]))  // true
 * isSortedSet(new HashSet([1]))  // false
 * ```
 */
export function isSortedSet(value: unknown): value is ReadonlySortedSet<unknown> {
  return isBranded(value, SORTED_SET_BRAND)
}
 
/**
 * Returns whether `value` is a sorted map from this library — a `TreeMap`, one of its range views
 * or a reversed view — of the same major version, from any loaded copy of the package. A `HashMap`,
 * native `Map` and the key set of a sorted map (a sorted set) are not.
 *
 * Time: O(1), plus any getter or proxy trap that reading a property of `value` runs.
 *
 * @param value - Any value.
 * @returns `true` when `value` is a library sorted map.
 * @throws whatever a getter or proxy trap on `value` throws while the property is read.
 *
 * @example
 * ```ts
 * isSortedMap(new TreeMap([[1, 'a']])) // true
 * isSortedMap(new HashMap([[1, 'a']])) // false
 * ```
 */
export function isSortedMap(value: unknown): value is ReadonlySortedMap<unknown, unknown> {
  return isBranded(value, SORTED_MAP_BRAND)
}