Skip to content

Instantly share code, notes, and snippets.

@kessler
Created February 21, 2017 18:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kessler/06f0db899d7845a6a01f8f77d7fc6757 to your computer and use it in GitHub Desktop.
Save kessler/06f0db899d7845a6a01f8f77d7fc6757 to your computer and use it in GitHub Desktop.
string length is broken
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