mirror of
https://github.com/typicode/json-server.git
synced 2025-07-28 20:52:08 +08:00
25 lines
480 B
JavaScript
25 lines
480 B
JavaScript
module.exports = {
|
|
toNative: toNative
|
|
}
|
|
|
|
// Turns string to native.
|
|
// Example:
|
|
// 'true' -> true
|
|
// '1' -> 1
|
|
function toNative (value) {
|
|
if (typeof value === 'string') {
|
|
if (
|
|
value === '' ||
|
|
value.trim() !== value ||
|
|
(value.length > 1 && value[0] === '0')
|
|
) {
|
|
return value
|
|
} else if (value === 'true' || value === 'false') {
|
|
return value === 'true'
|
|
} else if (!isNaN(+value)) {
|
|
return +value
|
|
}
|
|
}
|
|
return value
|
|
}
|