Created
February 21, 2017 18:35
-
-
Save kessler/06f0db899d7845a6a01f8f77d7fc6757 to your computer and use it in GitHub Desktop.
string length is broken
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const loEndsWith = require('lodash.endsWith') | |
console.log('naive:', endsWith('asdsds😀', '?')) | |
console.log('naive:', endsWith('asdsds😀?', '?')) | |
console.log('naive:', endsWith('asdsds😀', '😀')) | |
console.log('mozilla polyfill:', mozEndsWith('asdsds😀', '?')) | |
console.log('mozilla polyfill:', mozEndsWith('asdsds😀?', '?')) | |
console.log('mozilla polyfill:', mozEndsWith('asdsds😀', '😀')) | |
console.log('lodash polyfill:', loEndsWith('asdsds😀', '?')) | |
console.log('lodash polyfill:', loEndsWith('asdsds😀?', '?')) | |
console.log('lodash polyfill:', loEndsWith('asdsds😀', '😀')) | |
function endsWith(a, b) { | |
return a[a.length - 1] === b | |
} | |
function mozEndsWith(subjectString, searchString, position) { | |
if (typeof position !== 'number' || !isFinite(position) || Math.floor(position) !== position || position > subjectString.length) { | |
position = subjectString.length; | |
} | |
position -= searchString.length; | |
var lastIndex = subjectString.lastIndexOf(searchString, position); | |
return lastIndex !== -1 && lastIndex === position; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment