mirror of
https://github.com/typicode/json-server.git
synced 2025-07-28 04:32:24 +08:00
Fix #25
This commit is contained in:
@ -8,13 +8,14 @@ _.mixin(require('underscore.inflections'))
|
|||||||
// 'true' -> true
|
// 'true' -> true
|
||||||
// '1' -> 1
|
// '1' -> 1
|
||||||
function toNative(value) {
|
function toNative(value) {
|
||||||
|
if (typeof value === 'string') {
|
||||||
if (value === 'true' || value === 'false') {
|
if (value === 'true' || value === 'false') {
|
||||||
return value === 'true'
|
return value === 'true'
|
||||||
} else if (!isNaN(+value)) {
|
} else if (!isNaN(+value)) {
|
||||||
return +value
|
return +value
|
||||||
} else {
|
|
||||||
return value
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
return value
|
||||||
}
|
}
|
||||||
|
|
||||||
// Creates incremental id.
|
// Creates incremental id.
|
||||||
|
@ -28,4 +28,19 @@ describe('utils', function() {
|
|||||||
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('toNative', function() {
|
||||||
|
|
||||||
|
it('should convert string to native type', function() {
|
||||||
|
|
||||||
|
assert.strictEqual(utils.toNative('1'), 1)
|
||||||
|
assert.strictEqual(utils.toNative('true'), true)
|
||||||
|
|
||||||
|
assert.strictEqual(utils.toNative('string'), 'string')
|
||||||
|
assert.strictEqual(utils.toNative(1), 1)
|
||||||
|
assert.strictEqual(utils.toNative(true), true)
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
|
})
|
||||||
})
|
})
|
Reference in New Issue
Block a user