js-snippets
2的n次方
1 | 1 << 2 // 4 |
判断n是否是2的整数幂
1 | // n & (n - 1) === 0,则n是2的整数幂 |
开关
1 | let toggle = 0; |
取整
1 | let num = 3.14 |
符号判断
1 | (3 ^ -5) >= 0 // false |
评分
1 | const rate = r => '★★★★★☆☆☆☆☆'.slice(5 - r, 10 - r) |
随机整数 [min, max)
1 | function getRandomInt(min, max) { |
Hex随机色
1 | const randomHexColor = () => `#${Math.random().toString(16).slice(2, 8).padEnd(6, "0")}`; |
RGB随机色
1 | const randomRGBColor = () => `rgb(${getRandomInt(0, 256)}, ${getRandomInt(0, 256)}, ${getRandomInt(0, 256)})`; |
文件大小单位转换
1 | function autoSizeUnit(byte) { |
小驼峰字符串
1 | function camelCase(str) { |
滑动Chunk
1 | function slidingChunk<T>(array: T[], size: number) { |
排列数 P(n,m)
1 | const getPermutations = <T>(array: T[], m: number): T[][] => { |
All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.



