Skip to content

Commit

Permalink
throw Error objects
Browse files Browse the repository at this point in the history
  • Loading branch information
kamalyusuf authored and vanska committed Sep 16, 2024
1 parent 61eeab2 commit f54f580
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/forty-files-do.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"typed-string-interpolation": patch
---

Throw Error objects
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export function stringInterpolation<
raw: rawOutput = false,
}: StringInterpolationOptions<OptionRaw> = {}
): StringInterpolationReturn<VariableValue, OptionRaw> {
if (!string && sanity) throw "Empty string"
if (!string && sanity) throw new Error("Empty string")

// Find all variables within string
const stringVariables = matchAllPolyfill(string, pattern)
Expand All @@ -68,11 +68,11 @@ export function stringInterpolation<
const variableKeys = Object.keys(variables)
// Checks whether variables parsed from string exist in passed argument
if (stringVariables.length !== variableKeys.length)
throw "Variable count mismatch"
throw new Error("Variable count mismatch")
for (const regExpMatchArray of stringVariables) {
const variableKeyInString = regExpMatchArray[1]
if (variableKeyInString && !variableKeys.includes(variableKeyInString))
throw `Variable '${variableKeyInString}' not found`
throw new Error(`Variable '${variableKeyInString}' not found`)
}
}

Expand Down

0 comments on commit f54f580

Please sign in to comment.