|
| 1 | + |
| 2 | +function createCsr (type) { |
| 3 | + if (!type) return require('CSRBitMap').toCompressedString() |
| 4 | + const arr = [] |
| 5 | + |
| 6 | + const allNumbers = Array.from({ length: 43094 - 1 + 1 }, (_, i) => i + 1) |
| 7 | + for (let i = 0; i < 100; i++) { |
| 8 | + const randomIndex = Math.floor(Math.random() * allNumbers.length) |
| 9 | + const randomNumber = allNumbers[randomIndex] |
| 10 | + arr.push(randomNumber) |
| 11 | + allNumbers.splice(randomIndex, 1) |
| 12 | + } |
| 13 | + |
| 14 | + // 二进制转换文本 |
| 15 | + function convertToBinaryString (num) { |
| 16 | + const binaryString = num.toString(2) |
| 17 | + const padding = '0'.repeat(binaryString.length - 1) |
| 18 | + return padding + binaryString |
| 19 | + } |
| 20 | + // 二进制转换base64 |
| 21 | + function convertToBase64String (binaryString) { |
| 22 | + const list = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_' |
| 23 | + const sixBitChunks = (binaryString + '00000').match(/[01]{6}/g) |
| 24 | + let base64String = '' |
| 25 | + for (let i = 0; i < sixBitChunks.length; i++) { |
| 26 | + base64String += list[parseInt(sixBitChunks[i], 2)] |
| 27 | + } |
| 28 | + return base64String |
| 29 | + } |
| 30 | + // 压缩字符串 |
| 31 | + function toCompressedString () { |
| 32 | + const bitMap = [] |
| 33 | + for (const item in arr) { |
| 34 | + bitMap[arr[item]] = 1 |
| 35 | + } |
| 36 | + if (bitMap.length === 0) return '' |
| 37 | + const compressedBits = [] |
| 38 | + let count = 1 |
| 39 | + let currentBit = bitMap[0] || 0 |
| 40 | + const currentBitString = currentBit.toString(2) |
| 41 | + for (let i = 1; i < bitMap.length; i++) { |
| 42 | + const nextBit = bitMap[i] || 0 |
| 43 | + if (nextBit === currentBit) { |
| 44 | + count++ |
| 45 | + } else { |
| 46 | + compressedBits.push(convertToBinaryString(count)) |
| 47 | + currentBit = nextBit |
| 48 | + count = 1 |
| 49 | + } |
| 50 | + } |
| 51 | + if (count) compressedBits.push(convertToBinaryString(count)) |
| 52 | + return convertToBase64String(currentBitString + compressedBits.join('')) |
| 53 | + } |
| 54 | + return toCompressedString() |
| 55 | +} |
| 56 | +createCsr() |
0 commit comments