Skip to content

Commit 197031b

Browse files
authored
Merge pull request #83 from keenwon/elint3
feat: 升级 elint
2 parents 4c2fb7c + 7cf5325 commit 197031b

File tree

8 files changed

+2846
-5940
lines changed

8 files changed

+2846
-5940
lines changed

.github/dependabot.yml

-19
This file was deleted.

.github/workflows/unittest.yml

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
name: unittest
22

3-
on:
4-
push:
5-
schedule:
6-
- cron: "0 0 * * *"
3+
on: push
74

85
jobs:
96
test:
107
name: Test on node ${{ matrix.node_version }} and ${{ matrix.os }}
118
runs-on: ${{ matrix.os }}
129
strategy:
1310
matrix:
14-
node_version: [8, 16]
11+
node_version: [14, 18]
1512
os: [ubuntu-latest, windows-latest, macOS-latest]
1613
steps:
1714
- uses: actions/checkout@v2
@@ -32,11 +29,11 @@ jobs:
3229
run: npm run test
3330

3431
- name: npm run test:coverage
35-
if: matrix.node_version == 16 && matrix.os == 'ubuntu-latest'
32+
if: matrix.node_version == 14 && matrix.os == 'ubuntu-latest'
3633
run: npm run test:coverage
3734

3835
- name: Upload coverage to Codecov
39-
if: matrix.node_version == 16 && matrix.os == 'ubuntu-latest'
36+
if: matrix.node_version == 14 && matrix.os == 'ubuntu-latest'
4037
uses: codecov/codecov-action@v2
4138
with:
4239
token: ${{secrets.CODECOV_TOKEN}}

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ coverage/
77

88
# ELint (https://github.com/keenwon/elint)
99
.eslintrc.js
10+
.eslintrc.cjs
1011
.stylelintrc.js
12+
.stylelintrc.cjs
1113
.commitlintrc.js
14+
.commitlintrc.cjs
1215
.huskyrc.js
16+
.husky
17+
.prettierrc.cjs

example/index.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
const stringBreak = require('../index')
44
const clear = require('cli-clear')
55

6-
const str = '远处海港传来阵阵船笛 我一直飘零到被你拣起 如今望著反映窗户玻璃 有个我陌生又熟悉 I can Smile a little more Sing a little more Feel a little more 全因为你 说好了要为幸福 一天天地练习 练习 Laugh a little more Love myself a little more 要学会更加善待我自己 为你我变成了 Better me 甚麼距离都不算是真的分离 想念和默契能代替一切言语 有一天生命会老去 还好谢谢有你 在你眼中 I see the better in me Coz I can Smile a little more Sing a little more Feel a little more 全因为你 说好了要为幸福 一天天地练习 练习 Laugh a little more Love myself a little more 要学会更加善待我自己 为你我变成了 Better me 就是那麼神奇 从前的错都有意义 教我抛开所有猜疑 也许 我也美丽 值得一个奇迹 我的眼泪会坠落 绝不是因为懦弱 而是感谢天让我遇见你 不然今天就不能 如此地有勇气 Now I promise to you And I can swear to you 为你我 一定加倍 爱护我自己 做一个值得你 骄傲的 Better me 一个值得你爱的Better me'
6+
const str =
7+
'远处海港传来阵阵船笛 我一直飘零到被你拣起 如今望著反映窗户玻璃 有个我陌生又熟悉 I can Smile a little more Sing a little more Feel a little more 全因为你 说好了要为幸福 一天天地练习 练习 Laugh a little more Love myself a little more 要学会更加善待我自己 为你我变成了 Better me 甚麼距离都不算是真的分离 想念和默契能代替一切言语 有一天生命会老去 还好谢谢有你 在你眼中 I see the better in me Coz I can Smile a little more Sing a little more Feel a little more 全因为你 说好了要为幸福 一天天地练习 练习 Laugh a little more Love myself a little more 要学会更加善待我自己 为你我变成了 Better me 就是那麼神奇 从前的错都有意义 教我抛开所有猜疑 也许 我也美丽 值得一个奇迹 我的眼泪会坠落 绝不是因为懦弱 而是感谢天让我遇见你 不然今天就不能 如此地有勇气 Now I promise to you And I can swear to you 为你我 一定加倍 爱护我自己 做一个值得你 骄傲的 Better me 一个值得你爱的Better me'
78

89
let width = 50
910

10-
function output () {
11+
function output() {
1112
clear()
1213
console.log(`width: ${width}`)
1314
console.log('')

index.js

+56-17
Original file line numberDiff line numberDiff line change
@@ -7,53 +7,89 @@ const stringWidth = require('string-width')
77
// "开"标点
88
const openPunctuations = [
99
// 中文
10-
'“', '‘', '(', '《', '〈', '〔', '【',
10+
'“',
11+
'‘',
12+
'(',
13+
'《',
14+
'〈',
15+
'〔',
16+
'【',
1117

1218
// 英文
13-
'(', '[', '{'
19+
'(',
20+
'[',
21+
'{',
1422
]
1523

16-
function isOpenPunctuation (char) {
24+
function isOpenPunctuation(char) {
1725
return openPunctuations.includes(char)
1826
}
1927

2028
// "闭"标点
2129
const closePunctuations = [
2230
// 中文
23-
'。', '?', '!', ',', '、', ';', '”',
24-
'’', ')', '》', '〉', '〕', '】',
31+
'。',
32+
'?',
33+
'!',
34+
',',
35+
'、',
36+
';',
37+
'”',
38+
'’',
39+
')',
40+
'》',
41+
'〉',
42+
'〕',
43+
'】',
2544

2645
// 英文
27-
'.', '?', '!', ',', ';', ')', ']', '}'
46+
'.',
47+
'?',
48+
'!',
49+
',',
50+
';',
51+
')',
52+
']',
53+
'}',
2854
]
2955

30-
function isClosePunctuation (char) {
56+
function isClosePunctuation(char) {
3157
return closePunctuations.includes(char)
3258
}
3359

3460
// "其他"标点
3561
const otherPunctuations = [
3662
// 中文
37-
':', '─', '…', '·',
63+
':',
64+
'─',
65+
'…',
66+
'·',
3867

3968
// 英文
4069
// eslint-disable-next-line quotes
41-
':', '-', '–', '—', '"', "'"
70+
':',
71+
'-',
72+
'–',
73+
'—',
74+
'"',
75+
"'",
4276
]
4377

44-
function isPunctuation (char) {
45-
return openPunctuations.includes(char) ||
78+
function isPunctuation(char) {
79+
return (
80+
openPunctuations.includes(char) ||
4681
closePunctuations.includes(char) ||
4782
otherPunctuations.includes(char)
83+
)
4884
}
4985

5086
// 中文
51-
function isFullWidthChar (char) {
87+
function isFullWidthChar(char) {
5288
return /[\u4e00-\u9fa5]/.test(char)
5389
}
5490

5591
// 获取下一个分割点
56-
function getNextBreakPoint (str, width, from, lastIndex) {
92+
function getNextBreakPoint(str, width, from, lastIndex) {
5793
let idealIndex = from
5894
const length = str.length
5995
let subWidth = 0
@@ -62,7 +98,7 @@ function getNextBreakPoint (str, width, from, lastIndex) {
6298
do {
6399
idealIndex++
64100
subWidth = stringWidth(str.slice(from, idealIndex))
65-
} while ((subWidth <= width) && (idealIndex <= length))
101+
} while (subWidth <= width && idealIndex <= length)
66102

67103
// 获取下一个实际分割点(只退不进)
68104
let index = idealIndex - 1
@@ -74,7 +110,9 @@ function getNextBreakPoint (str, width, from, lastIndex) {
74110
while (index > lastIndex) {
75111
const preValue = str.charAt(index - 1)
76112
const value = str.charAt(index)
77-
const canBreak = preValue === ' ' || value === ' ' ||
113+
const canBreak =
114+
preValue === ' ' ||
115+
value === ' ' ||
78116
(isFullWidthChar(value) && isFullWidthChar(preValue)) ||
79117
(isClosePunctuation(preValue) && !isPunctuation(value)) ||
80118
(!isPunctuation(preValue) && isOpenPunctuation(value)) ||
@@ -95,15 +133,16 @@ function getNextBreakPoint (str, width, from, lastIndex) {
95133
return index
96134
}
97135

98-
function breaker (str, width) {
136+
function breaker(str, width) {
99137
if (width < 2) {
100138
throw new Error('Width must be greater than 2')
101139
}
102140

103141
const length = str.length
104142
let index = 0
105143
let breakPoint = 0
106-
let line; const lines = []
144+
let line
145+
const lines = []
107146

108147
while (index < length) {
109148
breakPoint = getNextBreakPoint(str, width, index, breakPoint)

0 commit comments

Comments
 (0)