MCPcopy
hub / github.com/josdejong/jsoneditor / parseString

Function parseString

src/js/util.js:1122–1155  ·  view source on GitHub ↗
(str)

Source from the content-addressed store, hash-verified

1120 * @private
1121 */
1122export function parseString (str) {
1123 if (str === '') {
1124 return ''
1125 }
1126
1127 const lower = str.toLowerCase()
1128 if (lower === 'null') {
1129 return null
1130 }
1131 if (lower === 'true') {
1132 return true
1133 }
1134 if (lower === 'false') {
1135 return false
1136 }
1137
1138 const containsLeadingZero = /^0\d+$/
1139 const startsWithZeroPrefix = /^0[xbo]/i // hex, binary, octal numbers
1140 if (containsLeadingZero.test(str) || startsWithZeroPrefix.test(str)) {
1141 // treat '001', '0x1A', '0b1101', and '0o3700' as a string
1142 return str
1143 }
1144
1145 const num = Number(str) // will nicely fail with '123ab'
1146 const numFloat = parseFloat(str) // will nicely fail with ' '
1147 const isFiniteNumber = !isNaN(num) && !isNaN(numFloat) && isFinite(num)
1148 const isInSafeRange = num <= Number.MAX_SAFE_INTEGER && num >= Number.MIN_SAFE_INTEGER
1149 const isInteger = /^\d+$/.test(str)
1150 if (isFiniteNumber && (isInSafeRange || !isInteger)) {
1151 return num
1152 }
1153
1154 return str
1155}
1156
1157/**
1158 * Test whether some field contains a timestamp in milliseconds after the year 2000.

Callers 5

util.test.jsFile · 0.90
createQueryFunction · 0.90
changeTypeMethod · 0.90
_getDomValueMethod · 0.90
_getTypeMethod · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…