Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace deprecated String.prototype.substr() #163

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions minimatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ class Minimatch {
negateOffset++
}

if (negateOffset) this.pattern = pattern.substr(negateOffset)
if (negateOffset) this.pattern = pattern.slice(negateOffset)
this.negate = negate
}

Expand Down Expand Up @@ -614,7 +614,7 @@ class Minimatch {
} catch (er) {
// not a valid class!
sp = this.parse(cs, SUBPARSE)
re = re.substr(0, reClassStart) + '\\[' + sp[0] + '\\]'
re = re.substring(0, reClassStart) + '\\[' + sp[0] + '\\]'
hasMagic = hasMagic || sp[1]
inClass = false
continue
Expand Down Expand Up @@ -647,9 +647,9 @@ class Minimatch {
// this is a huge pita. We now have to re-walk
// the contents of the would-be class to re-translate
// any characters that were passed through as-is
cs = pattern.substr(classStart + 1)
cs = pattern.slice(classStart + 1)
sp = this.parse(cs, SUBPARSE)
re = re.substr(0, reClassStart) + '\\[' + sp[0]
re = re.substring(0, reClassStart) + '\\[' + sp[0]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why substring here, and not slice?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reClassStart could be -1, in that case substring() will return an empty string like substr(), slice() however would return everything from the start until the second last char. This would be a different behaviour than we currently have.

'example'.slice(0, -1) == 'exampl'
'example'.substring(0, -1) == ''
'example'.substr(0, -1) == ''

I generally like slice() more as it's stricter and doesn't switch args around but in this case it's not the right tool to use to have the same result as substr().

hasMagic = hasMagic || sp[1]
}

Expand Down
2 changes: 1 addition & 1 deletion test/escaping.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var pre = 'x' // prepended to the testable character
var post = 'y' // appended to the testable character

function escapeChar (cc) {
return '"\\u' + ('000' + cc.toString(16).toUpperCase()).substr(-4) + '"'
return '"\\u' + ('000' + cc.toString(16).toUpperCase()).slice(-4) + '"'
}

tap.test('escaping tests', function (t) {
Expand Down