/**
* 检查字符串string是否以给定的target字符串结尾
* Checks if `string` ends with the given target string
* @param {string} [string=''] The string to inspect
* @param {string} [target] The string to search for
* @param {number} [position=string.length] The position to search up to
* @returns {boolean} Returns `true` if `string` ends with `target`, else `false`
* @example
* endsWith('abc', 'c')
* // => true
* endsWith('abc', 'b')
* // => false
* endsWith('abc', 'b', 2)
* // => true
*/
import toString from "./toString"
import baseToString from "./baseToString"
import baseClamp from "./baseClamp"
import toInteger from "./toInteger"
function endsWith(string, target, position) {
string = toString(string) //如果string为null或undefined则会返回空字符串
target = baseToString(target) //如果string为null或undefined则原样返回
var length = string.length
position =
position === undefined ? length : baseClamp(toInteger(position), 0, length)
var end = position
position = position - target.length
return position >= 0 && string.slice(position, end) == target
}
export default endsWith
/**
* Converts `value` to a string. An empty string is returned for `null` and `undefined` values.
* The sign of `-0` is preserved.
* 转换`value`成字符串,`null`和`undefined`返回空字符串,`-0`转成'-0'
* @param {*} value The value to convert
* @retuens {string} Returns the converted string
* @example
* toString(null)
* // => ''
* toString(-0)
* // => '-0'
* toString([1, 2, 3])
* // => '1, 2, 3'
* toString({a: 1})
* // => [object Object]
*/
const symbolTag = "[object Symbol]"
function isObjectLike(value) {
return typeof value == "object" && value !== null;
}
function isSymbol(value) {
return (
typeof value === "symbol" ||
(isObjectLike(value) && Object.prototype.toString.call(value) === symbolTag)
)
}
function arrayMap(array, iteratee) {
var index = -1,
length = array == null ? 0 : array.length,
result = Array(length)
while (++index < length) {
result[index] = iteratee(array[index], index, array)
}
return result
}
function baseToString(value) {
if (typeof value === "string") {
return value
}
if (Array.isArray(value)) {
return arrayMap(value, baseToString) + ""
}
if (isSymbol(value)) {
return Symbol.prototype.toString
? Symbol.prototype.toString.call(value)
: ""
}
var result = value + ""
return result == "0" && 1 / value == -Infinity ? "-0" : result
}
function toString(value) {
return value == null ? "" : baseToString(value)
}
export default toString
/**
* 返回限制在`lower`和`upper`之间的值
* The base implementation of 'clamp' which doesn't coerce arguments
*
* @param {number} number The number to clamp.
* @param {number} [lower] The lower bound.
* @param {number} upper The upper bound.
* @returns {number} Returns the clamped number.
*/
function baseClamp(number, lower, upper) {
if (number === number) {
if (upper !== undefined) {
number = number < upper ? number : upper
}
if (lower !== undefined) {
number = number > lower ? number : lower
}
}
return number
}
export default baseClamp
/**
* 转换value为整数
* Converts `value` to an integer.
*
* @param {*} value The value to convert.
* @returns {number} Returns the converted integer.
* @example
*
* toInteger(3.3)
* // => 3
*
* toInteger('3.3')
* // => 3
*
* toInteger(Number.MIN_VALUE)
* // => 0
*
* toInteger(Infinity)
* // => 1.7976931348623157e+308
*
* toInteger(-3.3)
* // => -3
*
* toInteger(new Date())
* // => 1561453581720
*
* toInteger(() => {})
* // => 0
*/
const NAN = 0 / 0
const INFINITY = 1 / 0
const symbolTag = "[object Symbol]"
const MAX_INTEGER = Number.MAX_VALUE || 1.7976931348623157e308
function isObjectLike(value) {
return typeof value == "object" && value !== null
}
function isSymbol(value) {
return (
typeof value === "symbol" ||
(isObjectLike(value) && Object.prototype.toString.call(value) === symbolTag)
)
}
function toNumber(value) {
if (typeof value === "number") {
return value
}
if (isSymbol(value)) {
return NAN
}
return Number(value)
}
function toFinite(value) {
if (!value) {
return value === 0 ? value : 0
}
value = toNumber(value)
if (value === INFINITY || value === -INFINITY) {
var sign = value < 0 ? -1 : 1 //可用Math.sign代替
return sign * MAX_INTEGER
}
return value === value ? value : 0 //NaN 不等于 NaN
}
function toInteger(value) {
var result = toFinite(value)
var remainder = result % 1
return result === result ? (remainder ? result - remainder : result) : 0 //可用Math.trunc代替
}
export default toInteger