Skip to content

Commit 60fb2ef

Browse files
theriaultkvz
authored andcommitted
Fixed example locutusjs#2 and start/length (locutusjs#322)
* Fixed example locutusjs#2 and start/length * Fixed lint issues
1 parent 92a4073 commit 60fb2ef

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

src/php/strings/strcspn.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
11
module.exports = function strcspn (str, mask, start, length) {
22
// discuss at: http://locutus.io/php/strcspn/
33
// original by: Brett Zamir (http://brett-zamir.me)
4+
// revised by: Theriault
45
// example 1: strcspn('abcdefg123', '1234567890')
56
// returns 1: 7
67
// example 2: strcspn('123abc', '1234567890')
7-
// returns 2: 3
8+
// returns 2: 0
9+
// example 3: strcspn('abcdefg123', '1234567890', 1)
10+
// returns 3: 6
11+
// example 4: strcspn('abcdefg123', '1234567890', -6, -5)
12+
// returns 4: 1
813

914
start = start || 0
10-
var count = (length && ((start + length) < str.length)) ? start + length : str.length
11-
strct: for (var i = start, lgth = 0; i < count; i++) { // eslint-disable-line no-labels
12-
for (var j = 0; j < mask.length; j++) {
13-
if (str.charAt(i).indexOf(mask[j]) !== -1) {
14-
continue strct // eslint-disable-line no-labels
15-
}
15+
length = typeof length === 'undefined' ? str.length : (length || 0)
16+
if (start < 0) start = str.length + start
17+
if (length < 0) length = str.length - start + length
18+
if (start < 0 || start >= str.length || length <= 0 || e >= str.length) return 0
19+
var e = Math.min(str.length, start + length)
20+
for (var i = start, lgth = 0; i < e; i++) {
21+
if (mask.indexOf(str.charAt(i)) !== -1) {
22+
break
1623
}
1724
++lgth
1825
}
19-
2026
return lgth
2127
}

0 commit comments

Comments
 (0)