Skip to content

Instantly share code, notes, and snippets.

@samjmck
Last active October 4, 2018 20:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save samjmck/5a734d907bcf7c4a8223724a0a30260f to your computer and use it in GitHub Desktop.
Save samjmck/5a734d907bcf7c4a8223724a0a30260f to your computer and use it in GitHub Desktop.
// identifier(s) should be in the following format:
// {
// resultName: 'name of key that will be used in the returned object',
// identifier: 'identifier that will be searched for in the format'
// }
function breakDown(string, format, identifiers){
for(let i = 0; i < identifiers.length; i++){
const identifier = identifiers[i];
identifier.index = format.indexOf(identifier.identifier);
}
identifiers = identifiers.sort((a, b)=>{return a.index - b.index}) // sort the indexes in ascending order
.filter((object)=>{return object.index !== -1}); // remove the indexes that weren't found (not found means index value of -1)
if(identifiers.length > 0){
identifiers[0].leftSideCharacters = format.slice(0, identifiers[0].index);
for(let i = 0; i < identifiers.length; i++){ // this will handle all the characters between the identifiers
const {identifier, index} = identifiers[i];
if(i + 1 !== identifiers.length){ // this is not the last array item
const nextIndex = identifiers[i + 1].index; // we can use i + 1 because it will never return undefined because we skip the last item
const characters = format.slice(index + identifier.length, nextIndex);
identifiers[i].rightSideCharacters = characters;
identifiers[i + 1].leftSideCharacters = characters;
}
}
const previousItem = identifiers[identifiers.length - 1];
previousItem.rightSideCharacters = format.slice(previousItem.index + previousItem.identifier.length);
}
let finished = false;
const brokenDown = {};
for(let i = 0; i < identifiers.length; i++){
const item = identifiers[i];
const {resultName, leftSideCharacters, rightSideCharacters} = identifiers[i];
const previousItem = identifiers[i - 1];
const leftSideCharactersIndex = previousItem === undefined ? leftSideCharacters.length : previousItem.rightSideCharactersIndex + previousItem.rightSideCharacters.length;
let rightSideCharactersIndex = rightSideCharacters === '' ? string.length : string.indexOf(rightSideCharacters, leftSideCharactersIndex);
if(rightSideCharactersIndex === -1){
rightSideCharactersIndex = string.length;
}
item.rightSideCharactersIndex = rightSideCharactersIndex;
if(finished){
brokenDown[resultName] = null;
continue;
}
brokenDown[resultName] = string.slice(leftSideCharactersIndex, rightSideCharactersIndex);
if(rightSideCharactersIndex === string.length){
finished = true;
}
}
return brokenDown;
}
function getProxyDetails(proxy, format){
return breakDown(proxy, format, [
{
resultName: 'address',
identifier: '#address#'
},
{
resultName: 'port',
identifier: '#port#'
},
{
resultName: 'username',
identifier: '#username#'
},
{
resultName: 'password',
identifier: '#password#'
}
]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment