Skip to content

Commit

Permalink
Remove unnecessary helper.
Browse files Browse the repository at this point in the history
  • Loading branch information
wjhsf committed Oct 26, 2023
1 parent 421b195 commit f2df0bb
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 7 deletions.
6 changes: 1 addition & 5 deletions lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,14 @@ export type Callback<T> = (error: Error | null, result: T | undefined) => void
/** Signature for a callback function that expects an error to be passed. */
export type ErrorCallback = (error: Error, result?: never) => void

/** Wrapped `Object.prototype.toString`, so that you don't need to remember to use `.call()`. */
export const objectToString = (obj: unknown) =>
Object.prototype.toString.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)
return Object.prototype.toString.call(val)
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ SOFTWARE.
************************************************************************************ */

import { ErrorCallback, objectToString, safeToString } from './utils'
import { ErrorCallback, safeToString } from './utils'

/* Validation functions copied from check-types package - https://www.npmjs.com/package/check-types */

Expand All @@ -52,7 +52,7 @@ export function isString(data: unknown): boolean {

/** Determines whether the string representation of the argument is "[object Object]". */
export function isObject(data: unknown): boolean {
return objectToString(data) === '[object Object]'
return safeToString(data) === '[object Object]'
}

/** Determines whether the argument is an integer. */
Expand Down

0 comments on commit f2df0bb

Please sign in to comment.