Skip to content

Instantly share code, notes, and snippets.

@AshKyd
Created November 23, 2023 00:38
Show Gist options
  • Save AshKyd/44c443c26639d84ddf5e5776b28f25c7 to your computer and use it in GitHub Desktop.
Save AshKyd/44c443c26639d84ddf5e5776b28f25c7 to your computer and use it in GitHub Desktop.
search through a gzip file line by line on the client
function search(keyword) {
let chunks = 0;
isSearching = true;
return fetch(`${__webpack_public_path__}/placeNames.tsv.gz`).then(async res => {
const blob = await res.blob();
const ds = new DecompressionStream('gzip');
const reader = blob.stream().pipeThrough(ds).pipeThrough(new TextDecoderStream()).getReader();
let partialLine = '';
const found = [];
while (true) {
const { done, value } = await reader.read();
if (searchTerm !== keyword) {
console.log('aborting search', keyword);
}
chunks++;
if (done) break;
const theseLines = partialLine + value;
const split = theseLines.split('\n');
partialLine = split.pop();
found.push(
...split
.filter(line => line.split('\t')[0].toLowerCase().includes(keyword.toLowerCase()))
.map(line => {
const [name, latitude, longitude, countrycode, population] = line.split('\t');
return {
id: name + population + latitude + longitude,
name,
latitude,
longitude,
countrycode,
population: Number(population)
};
})
);
}
isSearching = false;
return found;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment