Skip to content

Commit c6b9b0c

Browse files
committed
add string/padStart & padEnd
1 parent f6026e1 commit c6b9b0c

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

docs/intro.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ $ node --v8-options | grep harmony
9393

9494
上面命令的输出结果,会因为版本的不同而有所不同。
9595

96-
我写了一个[ES-Checker](https://github.com/ruanyf/es-checker)模块,用来检查各种运行环境对ES6的支持情况。访问[ruanyf.github.io/es-checker](http://ruanyf.github.io/es-checker)可以看到您的默认浏览器支持ES6的程度。运行下面的命令,可以查看本机支持ES6的程度。
96+
我写了一个[ES-Checker](https://github.com/ruanyf/es-checker)模块,用来检查各种运行环境对ES6的支持情况。访问[ruanyf.github.io/es-checker](http://ruanyf.github.io/es-checker)可以看到您的浏览器支持ES6的程度。运行下面的命令,可以查看本机支持ES6的程度。
9797

9898
```bash
9999
$ npm install -g es-checker

docs/reference.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
- Addy Osmani, [Getting Literal With ES6 Template Strings](http://updates.html5rocks.com/2015/01/ES6-Template-Strings): 模板字符串的介绍
4949
- Blake Winton, [ES6 Templates](https://weblog.latte.ca/blake/tech/firefox/templates.html): 模板字符串的介绍
5050
- Peter Jaszkowiak, [How to write a template compiler in JavaScript](https://medium.com/@PitaJ/how-to-write-a-template-compiler-in-javascript-f174df6f32f): 使用模板字符串,编写一个模板编译函数
51+
- Axel Rauschmayer, [ES.stage3: string padding](http://www.2ality.com/2015/11/string-padding.html)
5152

5253
## 正则
5354

docs/string.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,34 @@ s.includes('Hello', 6) // false
297297
'na'.repeat('3') // "nanana"
298298
```
299299

300+
## padStart(),padEnd()
301+
302+
ES7推出了字符串补全长度的功能。如果某个字符串不够指定长度,会在头部或尾部补全。`padStart`用于头部补全,`padEnd`用于尾部补全。
303+
304+
```javascript
305+
'x'.padStart(5, 'ab') // 'ababx'
306+
'x'.padStart(4, 'ab') // 'abax'
307+
308+
'x'.padEnd(5, 'ab') // 'xabab'
309+
'x'.padEnd(4, 'ab') // 'xaba'
310+
```
311+
312+
上面代码中,`padStart``padEnd`一共接受两个参数,第一个参数用来指定字符串的最小长度,第二个参数是用来补全的字符串。
313+
314+
如果原字符串的长度,等于或大于指定的最小长度,则返回原字符串。
315+
316+
```javascript
317+
'xxx'.padStart(2, 'ab') // 'xxx'
318+
'xxx'.padEnd(2, 'ab') // 'xxx'
319+
```
320+
321+
如果省略第二个参数,则会用空格补全长度。
322+
323+
```javascript
324+
'x'.padStart(4) // ' x'
325+
'x'.padEnd(4) // 'x '
326+
```
327+
300328
## 模板字符串
301329

302330
传统的JavaScript语言,输出模板通常是这样写的。

0 commit comments

Comments
 (0)