-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Clean up validate function and add descriptions to validators. * Fix type error. * Add @types/node * Fix validator implementation and add safe toString function.
- Loading branch information
Showing
5 changed files
with
90 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** Signature for a callback function that expects an error to be passed. */ | ||
export type ErrorCallback = (error: Error, result?: never) => void | ||
|
||
/** Unbound `Object.prototype.toString`. */ | ||
const unboundToString = Object.prototype.toString | ||
|
||
/** Wrapped `Object.prototype.toString`, so that you don't need to remember to use `.call()`. */ | ||
export const objectToString = (obj: unknown) => unboundToString.call(obj) | ||
|
||
/** Safely converts any value to string, using the value's own `toString` when available. */ | ||
export const safeToString = (val: unknown) => { | ||
// Ideally, we'd just use String() for everything, but it breaks if `toString` is missing (mostly | ||
// values with no prototype), so we have to use Object#toString as a fallback. | ||
if (val === undefined || val === null || typeof val.toString === 'function') { | ||
return String(val) | ||
} else { | ||
return objectToString(val) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters