Skip to content

Instantly share code, notes, and snippets.

@Woodsphreaker
Created October 21, 2017 05:04
Show Gist options
  • Save Woodsphreaker/489a53f00f613880325621adcdf123b6 to your computer and use it in GitHub Desktop.
Save Woodsphreaker/489a53f00f613880325621adcdf123b6 to your computer and use it in GitHub Desktop.
find occurrences in a string
// Verifica se o elemento passado é um array, caso positivo, converte para uma única string
const toString = el => Array.isArray(el) ? el.join('') : el
// Cria um array com o elemento passado
const toArray = el => Array.from( toString ( el ) )
// Busca e soma quantas ocorrências foram encontradas com o termo passado
const sumOcurrences = (str, find) => (acc, el, i) => {
str.indexOf(find, i) === i
? acc += 1
: acc
return acc
}
// Função inical que dispara o processo
const findOcurrences = ( str, find ) => toArray( str ).reduce( sumOcurrences( toString ( str ) , find ) ,0)
//Busca quantas ocorrências acontecem pelo termo passado
console.log(findOcurrences([255,255,255,102,255,255,255,102,255,255,255,102] , "255255255102"))
console.log(findOcurrences("lorem ipsum dolor site amet lorem" , "lorem"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment