PageRenderTime 56ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/src/php/strings/stristr.js

https://github.com/andriuspetrauskis/phpjs
JavaScript | 25 lines | 16 code | 2 blank | 7 comment | 4 complexity | dea32d08a8cbb534ef19520f7780f8c0 MD5 | raw file
  1. module.exports = function stristr (haystack, needle, bool) {
  2. // discuss at: http://locutus.io/php/stristr/
  3. // original by: Kevin van Zonneveld (http://kvz.io)
  4. // bugfixed by: Onno Marsman (https://twitter.com/onnomarsman)
  5. // example 1: stristr('Kevin van Zonneveld', 'Van')
  6. // returns 1: 'van Zonneveld'
  7. // example 2: stristr('Kevin van Zonneveld', 'VAN', true)
  8. // returns 2: 'Kevin '
  9. var pos = 0
  10. haystack += ''
  11. pos = haystack.toLowerCase()
  12. .indexOf((needle + '')
  13. .toLowerCase())
  14. if (pos === -1) {
  15. return false
  16. } else {
  17. if (bool) {
  18. return haystack.substr(0, pos)
  19. } else {
  20. return haystack.slice(pos)
  21. }
  22. }
  23. }