|
| 1 | +import { SerovalUnknownTypedArrayError } from './errors'; |
1 | 2 | import { |
2 | 3 | SYM_ASYNC_ITERATOR, |
3 | 4 | SYM_HAS_INSTANCE, |
@@ -200,3 +201,120 @@ export const ERROR_CONSTRUCTOR: Record<ErrorConstructorTag, ErrorConstructors> = |
200 | 201 | [ErrorConstructorTag.TypeError]: TypeError, |
201 | 202 | [ErrorConstructorTag.URIError]: URIError, |
202 | 203 | }; |
| 204 | + |
| 205 | +export function isWellKnownSymbol(value: symbol): value is WellKnownSymbols { |
| 206 | + return value in INV_SYMBOL_REF; |
| 207 | +} |
| 208 | + |
| 209 | +export const DEFAULT_DEPTH_LIMIT = 64; |
| 210 | + |
| 211 | +export const enum TypedArrayTag { |
| 212 | + Int8Array = 1, |
| 213 | + Int16Array = 2, |
| 214 | + Int32Array = 3, |
| 215 | + Uint8Array = 4, |
| 216 | + Uint16Array = 5, |
| 217 | + Uint32Array = 6, |
| 218 | + Uint8ClampedArray = 7, |
| 219 | + Float32Array = 8, |
| 220 | + Float64Array = 9, |
| 221 | +} |
| 222 | + |
| 223 | +export const enum BigIntTypedArrayTag { |
| 224 | + BigInt64Array = 1, |
| 225 | + BigUint64Array = 2, |
| 226 | +} |
| 227 | + |
| 228 | +type TypedArrayConstructor = |
| 229 | + | Int8ArrayConstructor |
| 230 | + | Int16ArrayConstructor |
| 231 | + | Int32ArrayConstructor |
| 232 | + | Uint8ArrayConstructor |
| 233 | + | Uint16ArrayConstructor |
| 234 | + | Uint32ArrayConstructor |
| 235 | + | Uint8ClampedArrayConstructor |
| 236 | + | Float32ArrayConstructor |
| 237 | + | Float64ArrayConstructor; |
| 238 | + |
| 239 | +type BigIntTypedArrayConstructor = |
| 240 | + | BigInt64ArrayConstructor |
| 241 | + | BigUint64ArrayConstructor; |
| 242 | + |
| 243 | +export const TYPED_ARRAY_CONSTRUCTOR: Record< |
| 244 | + TypedArrayTag, |
| 245 | + TypedArrayConstructor |
| 246 | +> = { |
| 247 | + [TypedArrayTag.Float32Array]: Float32Array, |
| 248 | + [TypedArrayTag.Float64Array]: Float64Array, |
| 249 | + [TypedArrayTag.Int16Array]: Int16Array, |
| 250 | + [TypedArrayTag.Int32Array]: Int32Array, |
| 251 | + [TypedArrayTag.Int8Array]: Int8Array, |
| 252 | + [TypedArrayTag.Uint16Array]: Uint16Array, |
| 253 | + [TypedArrayTag.Uint32Array]: Uint32Array, |
| 254 | + [TypedArrayTag.Uint8Array]: Uint8Array, |
| 255 | + [TypedArrayTag.Uint8ClampedArray]: Uint8ClampedArray, |
| 256 | +}; |
| 257 | + |
| 258 | +export const BIG_INT_TYPED_ARRAY_CONSTRUCTOR: Record< |
| 259 | + BigIntTypedArrayTag, |
| 260 | + BigIntTypedArrayConstructor |
| 261 | +> = { |
| 262 | + [BigIntTypedArrayTag.BigInt64Array]: BigInt64Array, |
| 263 | + [BigIntTypedArrayTag.BigUint64Array]: BigUint64Array, |
| 264 | +}; |
| 265 | + |
| 266 | +export type TypedArrayValue = |
| 267 | + | Int8Array |
| 268 | + | Int16Array |
| 269 | + | Int32Array |
| 270 | + | Uint8Array |
| 271 | + | Uint16Array |
| 272 | + | Uint32Array |
| 273 | + | Uint8ClampedArray |
| 274 | + | Float32Array |
| 275 | + | Float64Array; |
| 276 | + |
| 277 | +export type BigIntTypedArrayValue = BigInt64Array | BigUint64Array; |
| 278 | + |
| 279 | +export function getTypedArrayTag(value: TypedArrayValue): TypedArrayTag { |
| 280 | + if (value instanceof Int8Array) { |
| 281 | + return TypedArrayTag.Int8Array; |
| 282 | + } |
| 283 | + if (value instanceof Int16Array) { |
| 284 | + return TypedArrayTag.Int16Array; |
| 285 | + } |
| 286 | + if (value instanceof Int32Array) { |
| 287 | + return TypedArrayTag.Int32Array; |
| 288 | + } |
| 289 | + if (value instanceof Uint8Array) { |
| 290 | + return TypedArrayTag.Uint8Array; |
| 291 | + } |
| 292 | + if (value instanceof Uint16Array) { |
| 293 | + return TypedArrayTag.Uint16Array; |
| 294 | + } |
| 295 | + if (value instanceof Uint32Array) { |
| 296 | + return TypedArrayTag.Uint32Array; |
| 297 | + } |
| 298 | + if (value instanceof Uint8ClampedArray) { |
| 299 | + return TypedArrayTag.Uint8ClampedArray; |
| 300 | + } |
| 301 | + if (value instanceof Float32Array) { |
| 302 | + return TypedArrayTag.Float32Array; |
| 303 | + } |
| 304 | + if (value instanceof Float64Array) { |
| 305 | + return TypedArrayTag.Float64Array; |
| 306 | + } |
| 307 | + throw new SerovalUnknownTypedArrayError(); |
| 308 | +} |
| 309 | + |
| 310 | +export function getBigIntTypedArrayTag( |
| 311 | + value: BigIntTypedArrayValue, |
| 312 | +): BigIntTypedArrayTag { |
| 313 | + if (value instanceof BigInt64Array) { |
| 314 | + return BigIntTypedArrayTag.BigInt64Array; |
| 315 | + } |
| 316 | + if (value instanceof BigUint64Array) { |
| 317 | + return BigIntTypedArrayTag.BigUint64Array; |
| 318 | + } |
| 319 | + throw new SerovalUnknownTypedArrayError(); |
| 320 | +} |
0 commit comments