inferred-types 0.54.7 → 0.54.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2197,6 +2197,18 @@ function narrowObjectToType() {
2197
2197
  );
2198
2198
  }
2199
2199
 
2200
+ // src/dictionary/objectValues.ts
2201
+ function objectValues(obj) {
2202
+ const tuple3 = Object.keys(obj).reduce(
2203
+ (acc, key) => [
2204
+ ...acc,
2205
+ obj[key]
2206
+ ],
2207
+ []
2208
+ );
2209
+ return tuple3;
2210
+ }
2211
+
2200
2212
  // src/dictionary/omit.ts
2201
2213
  function omit(obj, ...removeKeys) {
2202
2214
  const keys = Object.keys(obj);
@@ -2263,13 +2275,115 @@ function withoutKeys(dict, ...exclude) {
2263
2275
  return omit(dict, ...exclude);
2264
2276
  }
2265
2277
 
2278
+ // src/runtime-types/doesExtend.ts
2279
+ function doesExtend(type) {
2280
+ return (val) => {
2281
+ let response = false;
2282
+ if (isString(val)) {
2283
+ if (type === "string") {
2284
+ response = true;
2285
+ }
2286
+ if (type.startsWith("string(")) {
2287
+ const literals = stripAfter(
2288
+ stripBefore(type, "string("),
2289
+ ")"
2290
+ ).split(/,\s*/);
2291
+ if (literals.includes(val)) {
2292
+ response = true;
2293
+ }
2294
+ }
2295
+ }
2296
+ if (isNumber(val)) {
2297
+ if (type === "number") {
2298
+ response = true;
2299
+ }
2300
+ if (type.startsWith("number(")) {
2301
+ const literals = stripAfter(
2302
+ stripBefore(type, "number("),
2303
+ ")"
2304
+ ).split(/,\s*/).map(Number);
2305
+ if (literals.includes(val)) {
2306
+ response = true;
2307
+ }
2308
+ }
2309
+ }
2310
+ if (isNull(val) && (type === "null" || type === "Opt<null>")) {
2311
+ response = true;
2312
+ }
2313
+ if (isUndefined(val) && (type === "undefined" || type.startsWith("Opt<"))) {
2314
+ response = true;
2315
+ }
2316
+ if (isBoolean(val)) {
2317
+ if (type === "boolean") {
2318
+ response = true;
2319
+ }
2320
+ if (type === "true" && val === true || type === "false" && val === false) {
2321
+ response = true;
2322
+ }
2323
+ }
2324
+ if (isNarrowableObject(val)) {
2325
+ if (type === "Dict" || type === "Dict<string, unknown>") {
2326
+ response = true;
2327
+ }
2328
+ if (startsWith("Dict<")(type)) {
2329
+ const match = infer("Dict<{{infer key}}, {{infer value}}>")(type);
2330
+ if (match) {
2331
+ const { value } = match;
2332
+ const isOpt = value.includes(`Opt<`);
2333
+ const values = objectValues(val);
2334
+ if (values.every((i) => isOpt ? isString(i) || isUndefined(i) : isString(i)) && type === "Dict<string, string>" || values.every((i) => isOpt ? isUndefined(i) || isNumber(i) : isNumber(i)) && type === "Dict<string, number>" || values.every((i) => isOpt ? isUndefined(i) || isBoolean(i) : isBoolean(i)) && type === "Dict<string, boolean>") {
2335
+ response = true;
2336
+ }
2337
+ }
2338
+ }
2339
+ }
2340
+ if (isArray(val)) {
2341
+ if (type === "Array" || type === "Array<unknown>") {
2342
+ return true;
2343
+ }
2344
+ if (type === "Array<Dict>" && val.every(isObject) || type === "Array<boolean>" && val.every(isBoolean) || type === "Array<string>" && val.every(isString) || type === "Array<number>" && val.every(isNumber) || type === "Array<Map>" && val.every(isMap) || type === "Array<Set>" && val.every(isSetContainer)) {
2345
+ response = true;
2346
+ }
2347
+ }
2348
+ if (isMap(val)) {
2349
+ if (type === "Map" || type === "Map<Dict, Array>" && Array.from(val.keys()).every(isObject) && Array.from(val.values()).every(isArray) || type === "Map<Dict, Dict>" && Array.from(val.keys()).every(isObject) && Array.from(val.values()).every(isObject) || type === "Map<Dict, boolean>" && Array.from(val.keys()).every(isObject) && Array.from(val.values()).every(isBoolean) || type === "Map<Dict, number>" && Array.from(val.keys()).every(isObject) && Array.from(val.values()).every(isNumber) || type === "Map<Dict, string>" && Array.from(val.keys()).every(isObject) && Array.from(val.values()).every(isString) || type === "Map<Dict, unknown>" && Array.from(val.keys()).every(isObject) || type === "Map<string, string>" && Array.from(val.keys()).every(isString) && Array.from(val.values()).every(isString) || type === "Map<string, number>" && Array.from(val.keys()).every(isString) && Array.from(val.values()).every(isNumber) || type === "Map<string, boolean>" && Array.from(val.keys()).every(isString) && Array.from(val.values()).every(isBoolean) || type === "Map<string, unknown>" && Array.from(val.keys()).every(isString) || type === "Map<number, string>" && Array.from(val.keys()).every(isNumber) && Array.from(val.values()).every(isString) || type === "Map<number, number>" && Array.from(val.keys()).every(isNumber) && Array.from(val.values()).every(isNumber) || type === "Map<number, boolean>" && Array.from(val.keys()).every(isNumber) && Array.from(val.values()).every(isBoolean) || type === "Map<number, unknown>" && Array.from(val.keys()).every(isNumber)) {
2350
+ response = true;
2351
+ }
2352
+ }
2353
+ if (isSetContainer(val)) {
2354
+ if (type === "Set" || type === "Set<unknown>" || type === "Set<boolean>" && Array.from(val).every(isBoolean) || type === "Set<string>" && Array.from(val).every(isString) || type === "Set<number>" && Array.from(val).every(isNumber) || type === "Set<Opt<boolean>>" && Array.from(val).every((i) => isBoolean(i) || isUndefined(i)) || type === "Set<Opt<string>>" && Array.from(val).every((i) => isString(i) || isUndefined(i)) || type === "Set<Opt<number>>" && Array.from(val).every((i) => isNumber(i) || isUndefined(i))) {
2355
+ response = true;
2356
+ }
2357
+ }
2358
+ return response;
2359
+ };
2360
+ }
2361
+
2266
2362
  // src/dictionary/withoutValue.ts
2267
- function withoutValue(val) {
2363
+ function withoutValue(wo) {
2268
2364
  return (obj) => {
2269
- return Object.keys(obj).reduce(
2270
- (acc, key) => val === obj[key] ? acc : { ...acc, [key]: obj[key] },
2271
- {}
2272
- );
2365
+ const output = {};
2366
+ for (const key of keysOf(obj)) {
2367
+ const val = obj[key];
2368
+ if (!doesExtend(wo)(val)) {
2369
+ output[key] = val;
2370
+ }
2371
+ }
2372
+ return output;
2373
+ };
2374
+ }
2375
+
2376
+ // src/dictionary/withValue.ts
2377
+ function withValue(wo) {
2378
+ return (obj) => {
2379
+ const output = {};
2380
+ for (const key of keysOf(obj)) {
2381
+ const val = obj[key];
2382
+ if (doesExtend(wo)(val)) {
2383
+ output[key] = val;
2384
+ }
2385
+ }
2386
+ return output;
2273
2387
  };
2274
2388
  }
2275
2389
 
@@ -2544,6 +2658,9 @@ function isFunction(value) {
2544
2658
  function isObject(value) {
2545
2659
  return typeof value === "object" && value !== null && Array.isArray(value) === false;
2546
2660
  }
2661
+ function isNarrowableObject(value) {
2662
+ return isObject(value) && Object.keys(value).every((key) => ["string", "number", "boolean", "symbol", "object", "undefined", "void", "null"].includes(typeof value[key]));
2663
+ }
2547
2664
 
2548
2665
  // src/type-guards/api-tg.ts
2549
2666
  function isEscapeFunction(val) {
@@ -3241,6 +3358,11 @@ function isInlineSvg(v) {
3241
3358
  return isString(v) && v.trim().startsWith(`<svg`) && v.trim().endsWith(`</svg>`);
3242
3359
  }
3243
3360
 
3361
+ // src/type-guards/isMap.ts
3362
+ function isMap(val) {
3363
+ return val instanceof Map;
3364
+ }
3365
+
3244
3366
  // src/type-guards/isNever.ts
3245
3367
  function isNever(val) {
3246
3368
  return isConstant(val) && val.kind === "never";
@@ -3303,6 +3425,16 @@ function isLikeRegExp(val) {
3303
3425
  return false;
3304
3426
  }
3305
3427
 
3428
+ // src/type-guards/isSet.ts
3429
+ function isSet(val) {
3430
+ return isObject(val) ? val.kind !== "Unset" : true;
3431
+ }
3432
+
3433
+ // src/type-guards/isSetContainer.ts
3434
+ function isSetContainer(val) {
3435
+ return val instanceof Set;
3436
+ }
3437
+
3306
3438
  // src/type-guards/isStringArray.ts
3307
3439
  function isStringArray(val) {
3308
3440
  return Array.isArray(val) && val.every((i) => isString(i));
@@ -3342,9 +3474,6 @@ function isTypeTuple(value) {
3342
3474
  function isUnset(val) {
3343
3475
  return isObject(val) && val.kind === "Unset";
3344
3476
  }
3345
- function isSet(val) {
3346
- return isObject(val) ? val.kind !== "Unset" : true;
3347
- }
3348
3477
 
3349
3478
  // src/type-guards/isUrl.ts
3350
3479
  function isUri(val, ...protocols) {
@@ -4022,26 +4151,23 @@ function ifUppercaseChar(ch, callbackForMatch, callbackForNoMatch) {
4022
4151
 
4023
4152
  // src/literals/infer.ts
4024
4153
  function parseTemplate(template) {
4025
- const pattern = /\{\{\s*infer\s+([A-Za-z_]\w*)\s*(?:as\s+(string|number|boolean)\s*)?\}\}/g;
4154
+ const pattern = /\{\{\s*infer\s+([A-Za-z_]\w*)\s*(?:(?:extends|as)\s+(string|number|boolean)\s*)?\}\}/g;
4026
4155
  let lastIndex = 0;
4027
4156
  let match;
4028
4157
  const segments = [];
4029
- do {
4030
- match = pattern.exec(template);
4031
- if (match) {
4032
- const [fullMatch, varName, asType2] = match;
4033
- const staticPart = template.slice(lastIndex, match.index);
4034
- if (staticPart) {
4035
- segments.push({ dynamic: false, text: staticPart });
4036
- }
4037
- segments.push({
4038
- dynamic: true,
4039
- varName,
4040
- type: asType2 ? asType2 : "string"
4041
- });
4042
- lastIndex = match.index + fullMatch.length;
4158
+ while (match = pattern.exec(template)) {
4159
+ const [fullMatch, varName, asType2] = match;
4160
+ const staticPart = template.slice(lastIndex, match.index);
4161
+ if (staticPart) {
4162
+ segments.push({ dynamic: false, text: staticPart });
4043
4163
  }
4044
- } while (match);
4164
+ segments.push({
4165
+ dynamic: true,
4166
+ varName,
4167
+ type: asType2 ? asType2 : "string"
4168
+ });
4169
+ lastIndex = match.index + fullMatch.length;
4170
+ }
4045
4171
  const remainder = template.slice(lastIndex);
4046
4172
  if (remainder) {
4047
4173
  segments.push({ dynamic: false, text: remainder });
@@ -4059,19 +4185,19 @@ function buildRegexPattern(segments) {
4059
4185
  } else {
4060
4186
  switch (seg.type) {
4061
4187
  case "string":
4062
- regexStr += `(?<${seg.varName}>.*?)`;
4188
+ regexStr += "(.*?)";
4063
4189
  break;
4064
4190
  case "number":
4065
- regexStr += `(?<${seg.varName}>\\d+)`;
4191
+ regexStr += "(\\d+)";
4066
4192
  break;
4067
4193
  case "boolean":
4068
- regexStr += `(?<${seg.varName}>(?:true|false))`;
4194
+ regexStr += "(true|false)";
4069
4195
  break;
4070
4196
  }
4071
4197
  }
4072
4198
  }
4073
4199
  regexStr += "$";
4074
- return new RegExp(regexStr, "s");
4200
+ return new RegExp(regexStr);
4075
4201
  }
4076
4202
  function convertValue(type, value) {
4077
4203
  switch (type) {
@@ -4083,19 +4209,20 @@ function convertValue(type, value) {
4083
4209
  return value === "true";
4084
4210
  }
4085
4211
  }
4086
- function matchTemplate(inference, test) {
4087
- const segments = parseTemplate(inference);
4212
+ function matchTemplate(template, test) {
4213
+ const segments = parseTemplate(template);
4088
4214
  const regex = buildRegexPattern(segments);
4089
- const match = test.match(regex);
4215
+ const match = regex.exec(test);
4090
4216
  if (!match)
4091
4217
  return false;
4218
+ let captureIndex = 1;
4092
4219
  const result2 = {};
4093
4220
  for (const seg of segments) {
4094
4221
  if (seg.dynamic) {
4095
- const rawVal = match.groups?.[seg.varName];
4222
+ const rawVal = match[captureIndex++];
4096
4223
  if (rawVal === void 0)
4097
4224
  return false;
4098
- result2[seg.varName.toLowerCase()] = convertValue(seg.type, rawVal);
4225
+ result2[seg.varName] = convertValue(seg.type, rawVal);
4099
4226
  }
4100
4227
  }
4101
4228
  return result2;
@@ -5160,12 +5287,14 @@ export {
5160
5287
  isLuminosityUom,
5161
5288
  isLuxonDateTime,
5162
5289
  isMacysUrl,
5290
+ isMap,
5163
5291
  isMapToken,
5164
5292
  isMassMetric,
5165
5293
  isMassUom,
5166
5294
  isMetric,
5167
5295
  isMexicanNewsUrl,
5168
5296
  isMoment,
5297
+ isNarrowableObject,
5169
5298
  isNever,
5170
5299
  isNewsUrl,
5171
5300
  isNikeUrl,
@@ -5200,6 +5329,7 @@ export {
5200
5329
  isSet,
5201
5330
  isSetBasedKind,
5202
5331
  isSetBasedToken,
5332
+ isSetContainer,
5203
5333
  isSetToken,
5204
5334
  isShape,
5205
5335
  isShapeCallback,
@@ -5309,6 +5439,7 @@ export {
5309
5439
  narrowObjectToType,
5310
5440
  never,
5311
5441
  objectToApi,
5442
+ objectValues,
5312
5443
  omit,
5313
5444
  optional,
5314
5445
  optionalOrNull,
@@ -5385,6 +5516,7 @@ export {
5385
5516
  widen,
5386
5517
  withDefaults,
5387
5518
  withKeys,
5519
+ withValue,
5388
5520
  withoutKeys,
5389
5521
  withoutValue,
5390
5522
  wrapFn,