格式化 JavaScript

重新缩进和排版源代码以便阅读;不能代替运行验证或业务规则检查。

输入: 文本结果: 文本

文本输入

工具选项

案例

Read an integer-cents checkout total

输入

function totalCents(items){return items.reduce(function(sum,item){return sum+item.cents*item.quantity;},0);}

Expected answer

function totalCents(items) {
  return items.reduce(function (sum, item) {
    return sum + item.cents * item.quantity;
  }, 0);
}
缩进字符串
引号
Auto
在右花括号前保留分号
On
包含注释
On
Inspect Fahrenheit conversion and its zero-degree boundary

输入

function celsius(f){return(f-32)*5/9;}

Expected answer

function celsius(f) {
    return (f - 32) * 5 / 9;
}
缩进字符串
引号
Auto
在右花括号前保留分号
On
包含注释
On
Switch an API message to double-quoted strings

输入

function message(name){return 'Hello, '+name+'!';}

Expected answer

function message(name) {
  return "Hello, " + name + "!";
}
缩进字符串
引号
Double
在右花括号前保留分号
On
包含注释
On
Keep a note explaining a longitude-latitude pair

输入

// GeoJSON positions use longitude before latitude
var point={type:"Point",coordinates:[100,0]};

Expected answer

// GeoJSON positions use longitude before latitude
var point = {
  type: 'Point',
  coordinates: [
    100,
    0
  ]
};
缩进字符串
引号
Single
在右花括号前保留分号
On
包含注释
On
Remove a stale comment while keeping a leap-year check

输入

/* old debug note */function isLeap(y){return y%4===0&&(y%100!==0||y%400===0);}

Expected answer

function isLeap(y) {
  return y % 4 === 0 && (y % 100 !== 0 || y % 400 === 0)
}
缩进字符串
引号
Auto
在右花括号前保留分号
Off
包含注释
Off

参数

参数默认值输入与选项
缩进字符串"\\t"支持 \n、\r 和 \t 等转义序列
引号自动自动, Single, Double
在右花括号前保留分号true开启或关闭
包含注释true开启或关闭

相关工具

代码整理

操作参考

查看原始技术文档 (English)