Skip to content

一行代码

能一行解决, 绝不用两行

日期处理

检查日期是否有效

该方法用于检测给出的日期是否有效:

js
const isDateValid = (...args) => !Number.isNaN(new Date(...args).valueOf())

isDateValid('December 17, 1995 03:24:00') // true
const isDateValid = (...args) => !Number.isNaN(new Date(...args).valueOf())

isDateValid('December 17, 1995 03:24:00') // true

计算两个日期之间的间隔

该方法用于计算两个日期之间的间隔时间:

js
const dayDif = (date1, date2) => Math.ceil(Math.abs(date1.getTime() - date2.getTime()) / 86400000)

dayDif(new Date('2021-11-3'), new Date('2022-2-1')) // 90
const dayDif = (date1, date2) => Math.ceil(Math.abs(date1.getTime() - date2.getTime()) / 86400000)

dayDif(new Date('2021-11-3'), new Date('2022-2-1')) // 90

查找日期位于一年中的第几天

该方法用于检测给出的日期位于今年的第几天:

js
const dayOfYear = date => Math.floor((date - new Date(date.getFullYear(), 0, 0)) / 1000 / 60 / 60 / 24)

dayOfYear(new Date()) // 307
const dayOfYear = date => Math.floor((date - new Date(date.getFullYear(), 0, 0)) / 1000 / 60 / 60 / 24)

dayOfYear(new Date()) // 307

字符串处理

字符串首字母大写

该方法用于将英文字符串的首字母大写处理:

js
const capitalize = str => str.charAt(0).toUpperCase() + str.slice(1)

capitalize('hello world') // Hello world
const capitalize = str => str.charAt(0).toUpperCase() + str.slice(1)

capitalize('hello world') // Hello world

翻转字符串

该方法用于将一个字符串进行翻转操作,返回翻转后的字符串:

js
const reverse = str => str.split('').reverse().join('')

reverse('hello world') // 'dlrow olleh'
const reverse = str => str.split('').reverse().join('')

reverse('hello world') // 'dlrow olleh'

随机字符串

该方法可以从指定长度处截断字符串:

js
const truncateString = (string, length) => string.length < length ? string : `${string.slice(0, length - 3)}...`

truncateString('Hi, I should be truncated because I am too long!', 36) // 'Hi, I should be truncated because...'
const truncateString = (string, length) => string.length < length ? string : `${string.slice(0, length - 3)}...`

truncateString('Hi, I should be truncated because I am too long!', 36) // 'Hi, I should be truncated because...'

去除字符串中的HTML

该方法用于去除字符串中的HTML元素:

js
const stripHtml = html => (new DOMParser().parseFromString(html, 'text/html')).body.textContent || ''
const stripHtml = html => (new DOMParser().parseFromString(html, 'text/html')).body.textContent || ''

数字操作

获得一组数的平均值

js
const average = (...args) => args.reduce((a, b) => a + b) / args.length

average(1, 2, 3, 4, 5) // 3
const average = (...args) => args.reduce((a, b) => a + b) / args.length

average(1, 2, 3, 4, 5) // 3

获取两个整数之间的随机整数

该方法用于获取两个整数之间的随机整数

js
const random = (min, max) => Math.floor(Math.random() * (max - min + 1) + min)

random(1, 50)
const random = (min, max) => Math.floor(Math.random() * (max - min + 1) + min)

random(1, 50)

指定位数四舍五

该方法用于将一个数字按照指定位进行四舍五入:

js
const round = (n, d) => Number(`${Math.round(`${n}e${d}`)}e-${d}`)

round(1.005, 2) // 1.01
round(1.555, 2) // 1.56
const round = (n, d) => Number(`${Math.round(`${n}e${d}`)}e-${d}`)

round(1.005, 2) // 1.01
round(1.555, 2) // 1.56

颜色操作

将RGB转化为十六机制

该方法可以将一个RGB的颜色值转化为16进制值:

js
const rgbToHex = (r, g, b) => `#${((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1)}`

rgbToHex(255, 255, 255) // '#ffffff'
const rgbToHex = (r, g, b) => `#${((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1)}`

rgbToHex(255, 255, 255) // '#ffffff'

获取随机十六进制颜色

该方法用于获取一个随机的十六进制颜色值:

js
const randomHex = () => `#${Math.floor(Math.random() * 0xFFFFFF).toString(16).padEnd(6, '0')}`

randomHex()
const randomHex = () => `#${Math.floor(Math.random() * 0xFFFFFF).toString(16).padEnd(6, '0')}`

randomHex()

判断当前是否是苹果设备

该方法用于检测当前的设备是否是苹果的设备

js
const isAppleDevice = () => /Mac|iPod|iPhone|iPad/.test(navigator.platform)

isAppleDevice()
const isAppleDevice = () => /Mac|iPod|iPhone|iPad/.test(navigator.platform)

isAppleDevice()

随机布尔值

该方法可以返回一个随机的布尔值,使用Math.random()可以获得0-1的随机数,与0.5进行比较,就有一半的概率获得真值或者假值。

js
const randomBoolean = () => Math.random() >= 0.5

randomBoolean()
const randomBoolean = () => Math.random() >= 0.5

randomBoolean()

获取变量的类型

该方法用于获取一个变量的类型

js
const trueTypeOf = obj => Object.prototype.toString.call(obj).slice(8, -1).toLowerCase()

trueTypeOf('') // string
trueTypeOf(0) // number
trueTypeOf() // undefined
trueTypeOf(null) // null
trueTypeOf({}) // object
trueTypeOf([]) // array
trueTypeOf(0) // number
trueTypeOf(() => {}) // function
const trueTypeOf = obj => Object.prototype.toString.call(obj).slice(8, -1).toLowerCase()

trueTypeOf('') // string
trueTypeOf(0) // number
trueTypeOf() // undefined
trueTypeOf(null) // null
trueTypeOf({}) // object
trueTypeOf([]) // array
trueTypeOf(0) // number
trueTypeOf(() => {}) // function

华氏度和摄氏度之间的转化

该方法用于摄氏度和华氏度之间的转化:

js
const celsiusToFahrenheit = celsius => celsius * 9 / 5 + 32
const fahrenheitToCelsius = fahrenheit => (fahrenheit - 32) * 5 / 9

celsiusToFahrenheit(15) // 59
celsiusToFahrenheit(0) // 32
celsiusToFahrenheit(-20) // -4
fahrenheitToCelsius(59) // 15
fahrenheitToCelsius(32) // 0
const celsiusToFahrenheit = celsius => celsius * 9 / 5 + 32
const fahrenheitToCelsius = fahrenheit => (fahrenheit - 32) * 5 / 9

celsiusToFahrenheit(15) // 59
celsiusToFahrenheit(0) // 32
celsiusToFahrenheit(-20) // -4
fahrenheitToCelsius(59) // 15
fahrenheitToCelsius(32) // 0

检测对象是否为空

该方法用于检测一个JavaScript对象是否为空:

js
const isEmpty = obj => Reflect.ownKeys(obj).length === 0 && obj.constructor === Object
const isEmpty = obj => Reflect.ownKeys(obj).length === 0 && obj.constructor === Object