params_utils.js (2233B)
1 /** 2 * AUTO-GENERATED - DO NOT EDIT. Source: https://github.com/gpuweb/cts 3 **/import { assert } from '../util/util.js'; 4 5 import { comparePublicParamsPaths, Ordering } from './query/compare.js'; 6 import { kWildcard, kParamSeparator, kParamKVSeparator } from './query/separators.js'; 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 export function paramKeyIsPublic(key) { 23 return !key.startsWith('_'); 24 } 25 26 export function extractPublicParams(params) { 27 const publicParams = {}; 28 for (const k of Object.keys(params)) { 29 if (paramKeyIsPublic(k)) { 30 publicParams[k] = params[k]; 31 } 32 } 33 return publicParams; 34 } 35 36 /** Used to escape reserved characters in URIs */ 37 const kPercent = '%'; 38 39 export const badParamValueChars = new RegExp( 40 '[' + kParamKVSeparator + kParamSeparator + kWildcard + kPercent + ']' 41 ); 42 43 export function publicParamsEquals(x, y) { 44 return comparePublicParamsPaths(x, y) === Ordering.Equal; 45 } 46 47 48 49 50 51 /** 52 * Flatten a union of interfaces into a single interface encoding the same type. 53 * 54 * Flattens a union in such a way that: 55 * `{ a: number, b?: undefined } | { b: string, a?: undefined }` 56 * (which is the value type of `[{ a: 1 }, { b: 1 }]`) 57 * becomes `{ a: number | undefined, b: string | undefined }`. 58 * 59 * And also works for `{ a: number } | { b: string }` which maps to the same. 60 */ 61 62 63 64 65 66 67 68 69 70 71 72 function typeAssert() {} 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 typeAssert(); 98 typeAssert(); 99 typeAssert(); 100 typeAssert(); 101 typeAssert(); 102 103 typeAssert(); 104 105 typeAssert(); 106 typeAssert(); 107 typeAssert(); 108 typeAssert(); 109 typeAssert(); 110 111 // Unexpected test results - hopefully okay to ignore these 112 typeAssert(); 113 typeAssert(); 114 } 115 } 116 117 118 119 120 121 122 /** Merges two objects into one `{ ...a, ...b }` and return it with a flattened type. */ 123 export function mergeParams(a, b) { 124 return { ...a, ...b }; 125 } 126 127 /** 128 * Merges two objects into one `{ ...a, ...b }` and asserts they had no overlapping keys. 129 * This is slower than {@link mergeParams}. 130 */ 131 export function mergeParamsChecked(a, b) { 132 const merged = mergeParams(a, b); 133 assert( 134 Object.keys(merged).length === Object.keys(a).length + Object.keys(b).length, 135 () => `Duplicate key between ${JSON.stringify(a)} and ${JSON.stringify(b)}` 136 ); 137 return merged; 138 }