lodash里to系列之將數(shù)據(jù)轉(zhuǎn)換成數(shù)字類型實現(xiàn)示例
正文
在lodash里的to系列里,將目標(biāo)數(shù)據(jù)轉(zhuǎn)換為數(shù)字類型的數(shù)據(jù)的方法,包括了toNumber方法、toFinit方法、toInteger方法,toSafeInteger方法,下面來看看各個方法的使用和實現(xiàn)。
toNumber
toNumber方法主要是將參數(shù)value
轉(zhuǎn)換為一個數(shù)字類型。
使用如下:
toNumber(3.2) // => 3.2 toNumber(Number.MIN_VALUE) // => 5e-324 toNumber(Infinity) // => Infinity toNumber('3.2') // => 3.2
toNumber方法在實現(xiàn)上借助了內(nèi)部封裝的is系列方法,主要是isObject方法和isSymbol方法。
實現(xiàn)上借助typeof,具體處理如下:
- 對于數(shù)據(jù)類型直接返回參數(shù)。
- 對于symbol類型直接返回NaN。
- 對于對象類型,分兩種情況處理。
- 如果參數(shù)原型鏈上存在valueOf方法,直接調(diào)用其返回結(jié)果供后續(xù)處理。
- 如果參數(shù)原型鏈上不存在valueOf方法,直接轉(zhuǎn)換成字符串類型供后續(xù)處理。
- 對于非字符串類型的,調(diào)用隱式轉(zhuǎn)換。
- 對于其他類型的,會進(jìn)行正則匹配處理數(shù)據(jù)格式,reTrim是去除空格,reIsBadHex是去除十六進(jìn)制,reIsBinary是去除二進(jìn)制,reIsOctal是去除八進(jìn)制。
- 對于二進(jìn)制和八進(jìn)制的字符串會調(diào)用原生的parseInt方法將參數(shù)轉(zhuǎn)換為相應(yīng)的進(jìn)制數(shù)。而對于十六進(jìn)制則返回NaN,否則調(diào)用隱式轉(zhuǎn)換。
源碼如下:
import isObject from './isObject.js' import isSymbol from './isSymbol.js' const NAN = 0 / 0 const reTrim = /^\s+|\s+$/g const reIsBadHex = /^[-+]0x[0-9a-f]+$/i const reIsBinary = /^0b[01]+$/i const reIsOctal = /^0o[0-7]+$/i const freeParseInt = parseInt function toNumber(value) { if (typeof value === 'number') { return value } if (isSymbol(value)) { return NAN } if (isObject(value)) { const other = typeof value.valueOf === 'function' ? value.valueOf() : value value = isObject(other) ? `${other}` : other } if (typeof value !== 'string') { return value === 0 ? value : +value } value = value.replace(reTrim, '') const isBinary = reIsBinary.test(value) return (isBinary || reIsOctal.test(value)) ? freeParseInt(value.slice(2), isBinary ? 2 : 8) : (reIsBadHex.test(value) ? NAN : +value) }
toFinit
toFinit方法主要是將參數(shù)value
轉(zhuǎn)換為一個有限的數(shù)字。
使用如下:
toFinite(3.2) // => 3.2 toFinite(Number.MIN_VALUE) // => 5e-324 toFinite(Infinity) // => 1.7976931348623157e+308 toFinite('3.2') // => 3.2
toFinit方法實現(xiàn)上借助toNumber方法,具體處理如下:
- 對于不存在的參數(shù)會直接返回參數(shù),0的話返回0。
- 其次調(diào)用toNumber方法轉(zhuǎn)換參數(shù),再通過if判斷匹配INFINITY值,將其返回對應(yīng)的MAX_INTEGER。
源碼如下:
import toNumber from './toNumber.js' const INFINITY = 1 / 0 const MAX_INTEGER = 1.7976931348623157e+308 function toFinite(value) { if (!value) { return value === 0 ? value : 0 } value = toNumber(value) if (value === INFINITY || value === -INFINITY) { const sign = (value < 0 ? -1 : 1) return sign * MAX_INTEGER } return value === value ? value : 0 }
toInteger
toInteger方法主要是將參數(shù)value
轉(zhuǎn)換為一個整數(shù)。
使用如下:
toInteger(3.2) // => 3 toInteger(Number.MIN_VALUE) // => 0 toInteger(Infinity) // => 1.7976931348623157e+308 toInteger('3.2') // => 3
實現(xiàn)上借助toFinite方法,首先調(diào)用toInteger方法將參數(shù)轉(zhuǎn)換為有限的值,然后通過取模操作獲取小數(shù)部分,然后取差值。
源碼如下:
import toFinite from './toFinite.js' function toInteger(value) { const result = toFinite(value) const remainder = result % 1 return remainder ? result - remainder : result } export default toInteger
toSafeInteger
toSafeInteger方法主要是將參數(shù)value
轉(zhuǎn)換為安全整數(shù),安全整數(shù)可以用于比較和準(zhǔn)確的表示。
使用如下:
toSafeInteger(3.2) // => 3 toSafeInteger(Number.MIN_VALUE) // => 0 toSafeInteger(Infinity) // => 9007199254740991 toSafeInteger('3.2') // => 3
toSafeInteger方法在實現(xiàn)上主要通過toInteger方法,同樣的,對于不存在的參數(shù)直接返回參數(shù),而0則返回0。對于小于最小安全數(shù)MAX_SAFE_INTEGER則返回MAX_SAFE_INTEGER,值都將限制在[-MAX_SAFE_INTEGER,MAX_SAFE_INTEGER]的區(qū)間。
源碼如下:
import toInteger from './toInteger.js' const MAX_SAFE_INTEGER = 9007199254740991 function toSafeInteger(value) { if (!value) { return value === 0 ? value : 0 } value = toInteger(value) if (value < -MAX_SAFE_INTEGER) { return -MAX_SAFE_INTEGER } if (value > MAX_SAFE_INTEGER) { return MAX_SAFE_INTEGER } return value } export default toSafeInteger
小結(jié)
本篇章我們了解了toNumber、toFinit、toInteger以及toSafeInteger四個方法,在實現(xiàn)上依次借助調(diào)用實現(xiàn)。
toNumber方法是其他方法的核心,其他方法是toNumber方法的拓展。toNumber方法的核心是isObject和isSymbol兩個判斷方法。
更多關(guān)于lodash to數(shù)據(jù)轉(zhuǎn)換數(shù)字類型的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
微信小程序 input輸入及動態(tài)設(shè)置按鈕的實現(xiàn)
這篇文章主要介紹了微信小程序 input輸入及動態(tài)設(shè)置按鈕的實現(xiàn)的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-10-10詳解Anyscript開發(fā)指南繞過typescript類型檢查
這篇文章主要為大家介紹了詳解Anyscript開發(fā)指南繞過typescript類型檢查,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09three.js-結(jié)合dat.gui實現(xiàn)界面可視化修改及調(diào)試詳解
這篇文章主要為大家介紹了three.js-結(jié)合dat.gui實現(xiàn)界面可視化修改及調(diào)試詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02解析Javascript設(shè)計模式Revealing?Module?揭示模式單例模式
這篇文章主要為大家解析了Javascript設(shè)計模式Revealing?Module?揭示模式及Singleton單例模式示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08