1,使用js过程中,有一个需求,是超长的数据,10进制到16进制互转,,
2,由于本人有压缩代码的强烈欲望,故将自己写的一个函数极限压缩到了一行,
3,本人觉得,压缩代码能够锻炼写代码的精简程度,
4,大家要是有更多的压缩方向,可以提交代码,大家一起瞧瞧
这是原始代码,下面是复制的字符串代码function $10To16($num) {
// let $list = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'];
// return $num > 1 ? $10To16(BigInt($num / 16n)) + st : st;
// let st = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' ][$num % 16n];
// return ($num > 1 ? $10To16($num / 16n) : '') + ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g'][$num % 16n];
return (($n2 = $num /16n) &&$n2 >0 ?$10To16($n2) :'') + ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'][$num %16n];
}
$ = $num => (($n2 = $num /16n) &&$n2 >0 ?$($n2) :'') + ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'][$num %16n];
console.log($10To16(BigInt('100000000000000000000000000000000000000000000000000000000000000')));
console.log($(BigInt('100000000000000000000000000000000000000000000000000000000000000')));
又删除了一段 ,这个更短,
$ = $num => (($n2 = $num /16n) ?$($n2) :'') + ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'][$num %16n];
这个最短,欢迎测试,
$ = $0 => (($1 = $0 /16n) ?$($1) :'') + String.fromCharCode(($2 = Number($0 %16n)) >9 ?$2 +87 :$2 +48);
网友评论