forked from jquense/yup
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request jquense#108 from jquense/errors
Clearer errors for common pitfalls
- Loading branch information
Showing
9 changed files
with
155 additions
and
37 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
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
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
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,67 @@ | ||
import isFunction from 'lodash/isFunction'; | ||
import isMap from 'lodash/isMap'; | ||
import isSet from 'lodash/isSet'; | ||
import isWeakMap from 'lodash/isWeakMap'; | ||
import isWeakSet from 'lodash/isWeakSet'; | ||
import isSymbol from 'lodash/isSymbol'; | ||
|
||
const toString = Object.prototype.toString; | ||
const toISOString = Date.prototype.toISOString; | ||
const errorToString = Error.prototype.toString; | ||
const regExpToString = RegExp.prototype.toString; | ||
const symbolToString = Symbol.prototype.toString; | ||
|
||
const SYMBOL_REGEXP = /^Symbol\((.*)\)(.*)$/; | ||
const NEWLINE_REGEXP = /\n/gi; | ||
|
||
const getSymbols = Object.getOwnPropertySymbols || (obj => []); | ||
|
||
|
||
function printNumber(val) { | ||
if (val != +val) return 'NaN'; | ||
const isNegativeZero = val === 0 && 1 / val < 0; | ||
return isNegativeZero ? '-0' : '' + val; | ||
} | ||
|
||
function printFunction(val) { | ||
return '[Function ' + (val.name || 'anonymous') + ']'; | ||
} | ||
|
||
function printSymbol(val: Symbol): string { | ||
return symbolToString.call(val).replace(SYMBOL_REGEXP, 'Symbol($1)'); | ||
} | ||
|
||
function printError(val: Error): string { | ||
return '[' + errorToString.call(val) + ']'; | ||
} | ||
|
||
function printSimpleValue(val, quoteStrings = false) { | ||
if (val === true || val === false) return '' + val; | ||
if (val === undefined) return 'undefined'; | ||
if (val === null) return 'null'; | ||
|
||
const typeOf = typeof val; | ||
|
||
if (typeOf === 'number') return printNumber(val); | ||
if (typeOf === 'string') return quoteStrings ? `"${val}"` : val; | ||
if (isFunction(val)) return printFunction(val); | ||
if (isSymbol(val)) return printSymbol(val); | ||
|
||
const tag = toString.call(val); | ||
if (tag === '[object Date]') return isNaN(val.getTime()) ? String(val) : toISOString.call(val); | ||
if (tag === '[object Error]' || val instanceof Error) return printError(val); | ||
if (tag === '[object RegExp]') return regExpToString.call(val); | ||
|
||
return null; | ||
} | ||
|
||
export default function printValue(value, quoteStrings) { | ||
let result = printSimpleValue(value, quoteStrings); | ||
if (result !== null) return result; | ||
|
||
return JSON.stringify(value, function (key, value) { | ||
let result = printSimpleValue(this[key], quoteStrings); | ||
if (result !== null) return result | ||
return value | ||
}, 2) | ||
} |
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