Skip to content

Commit

Permalink
chore: TypeScript cleanup (ca-plants#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnkenny54 authored Feb 15, 2025
1 parent d78dd5a commit aa4bda6
Show file tree
Hide file tree
Showing 17 changed files with 208 additions and 116 deletions.
12 changes: 6 additions & 6 deletions data/taxa.csv
Original file line number Diff line number Diff line change
Expand Up @@ -720,11 +720,11 @@ Eriophyllum confertiflorum var. confertiflorum,golden-yarrow,N,69866,3423,53397,
Eriophyllum jepsonii,Jepson's woolly-sunflower,N,2816,3426,77048,203742,true,Jepson's Woolly Sunflower,,,,,776,4.3,,,S3,G3
Eriophyllum lanatum var. achilleoides,,N,7369,10587,80880,260847,true,Common Woollysunflower
Eriophyllum staechadifolium,seaside woolly-sunflower,N,2825,3444,60963,203755,true,Seaside Woolly Sunflower
Erodium botrys,long-beaked filaree,X,25037,3446,57090,17986,,,,pink,3,7
Erodium brachycarpum,,X,25038,3447,58171,17987
Erodium cicutarium,red-stemmed filaree,X,25041,3448,47687,17989
Erodium botrys,long-beaked filaree,X,25037,3446,57090,17986,,,annual,pink,3,7
Erodium brachycarpum,,X,25038,3447,58171,17987,,,annual,pink,3,7
Erodium cicutarium,red-stemmed filaree,X,25041,3448,47687,17989,,,annual,pink,3,9
Erodium malacoides,,X,76818,3451,77056,17994
Erodium moschatum,white-stem filaree,X,25048,3452,57092,17995
Erodium moschatum,white-stem filaree,X,25048,3452,57092,17995,,,"annual,biennial",pink,2,9
Eryngium aristulatum var. aristulatum,coyote-thistle,N,58845,3458,64189,211707,,California Eryngo
Eryngium aristulatum var. hooveri,,N,58846,3459,57904,203761,,Hoover's Button-celery,,,,,783,1B.1,,,S1,G5T1
Eryngium aristulatum,Jepson's button celery,N,25070,3457,57901,203759,,Jepson's Button Celery
Expand Down Expand Up @@ -1599,7 +1599,7 @@ Salix exigua var. hindsiana,Hind's willow,N,81242,11063,64195,51553,true,Sandbar
Salix gooddingii,Goodding's black willow,N,42825,7272,60240,31274,true,Goodding's Black Willow
Salix laevigata,red willow,N,42850,7276,58319,31302,true,Red Willow
Salix lasiandra var. lasiandra,Pacific willow,N,76043,11065,81358,51640,true,Yellow Willow
Salix lasiolepis,arroyo willow,N,42855,7277,53452,31307,true,Arroyo Willow
Salix lasiolepis,arroyo willow,N,42855,7277,53452,31307,true,Arroyo Willow,perennial,,1,6
Salix melanopsis,dusky willow,N,42881,7284,78947,31332,true,Dusky Willow
Salix scouleriana,Scouler's willow,N,42973,7289,71076,31425,true,Scouler's Willow
Salix sitchensis,Sitka willow,N,42994,7291,61100,31435,true,Sitka Willow
Expand Down Expand Up @@ -1676,7 +1676,7 @@ Silene lemmonii,Lemmon's catchfly,N,44533,7603,60523,119295,true,Lemmon's Catchf
Silene sargentii,Sargent's catchfly,N,44566,7614,62697,119340,true,Sargent's Catchfly,perennial,white,7,8
Silene verecunda,,N,44589,7617,60581,119364,true,San Francisco Campion
Silybum marianum,milk-thistle,X,4918,7622,52586,205950,true
Sinapis arvensis,charlock,X,44609,7625,1562069,32232,true
Sinapis arvensis,charlock,X,44609,7625,1562069,32232,true,,annual,yellow,3,10
Sisymbrium irio,London rocket,X,44651,7628,58085,32256,true
Sisymbrium officinale,hedge mustard,X,44653,7630,53266,32267,true
Sisymbrium orientale,,X,44654,7631,58086,32268,true
Expand Down
3 changes: 3 additions & 0 deletions data/text/Erodium-botrys.footer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## Resources

- [How to identify Erodium botrys](https://www.inaturalist.org/projects/erodium-botrys-mediterranean-stork-s-bill/journal/61038-how-to-identify-erodium-botrys-mediterranean-stork-s-bill) iNaturalist journal post
1 change: 1 addition & 0 deletions data/text/Erodium-botrys.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Petals with purple streaks.
3 changes: 3 additions & 0 deletions data/text/Erodium-brachycarpum.footer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## Resources

- [How to identify Erodium brachycarpum](https://www.inaturalist.org/projects/erodium-brachycarpum-hairy-pitted-stork-s-bill/journal/61042-how-to-identify-erodium-brachycarpum-hairy-pitted-stork-s-bill) iNaturalist journal post
1 change: 1 addition & 0 deletions data/text/Erodium-brachycarpum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Petals with purple streaks.
1 change: 1 addition & 0 deletions data/text/Erodium-cicutarium.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Petals not streaked. Leaf compound, fern-like. Bristles on sepals.
3 changes: 3 additions & 0 deletions data/text/Erodium-moschatum.footer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## Resources

- [How to identify Erodium moschatum](https://www.inaturalist.org/projects/erodium-moschatum-musk-stork-s-bill/journal/61039-how-to-identify-erodium-moschatum-musk-stork-s-bill) iNaturalist journal post
1 change: 1 addition & 0 deletions data/text/Erodium-moschatum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Petals not streaked, except near base. Leaves compound but not fern-like. No bristles on sepals.
196 changes: 118 additions & 78 deletions jekyll/assets/js/name_search.js
Original file line number Diff line number Diff line change
@@ -1,141 +1,173 @@
import { Utils } from "./utils.js";

/**
* @typedef {[string]|[string,string]|[string,string|undefined,string[]]} RawSearchData
* @typedef {{raw:RawSearchData,searchSci:string,searchCommon?:string,synonyms?:string[]}} SearchData
*/

const MIN_LEN = 2;
const MAX_RESULTS = 50;

class Search {

/** @type {number|undefined} */
static #debounceTimer;
/** @type {SearchData[]} */
static #searchData;

static #debounce( timeout = 500 ) {
clearTimeout( this.#debounceTimer );
this.#debounceTimer = setTimeout( Search.#doSearch, timeout );
/**
* @param {number} [timeout]
*/
static #debounce(timeout = 500) {
clearTimeout(this.#debounceTimer);
this.#debounceTimer = window.setTimeout(Search.#doSearch, timeout);
}

static #doSearch() {

function matchTaxon( taxon, value ) {

function matchSynonyms( syns, value ) {
/**
* @param {SearchData} taxon
* @param {string} value
*/
function matchTaxon(taxon, value) {
/**
* @param {string[]|undefined} syns
* @param {string} value
* @returns {number[]}
*/
function matchSynonyms(syns, value) {
const matchedIndexes = [];
if ( syns ) {
for ( let index = 0; index < syns.length; index++ ) {
if ( syns[ index ].includes( value ) ) {
matchedIndexes.push( index );
if (syns) {
for (let index = 0; index < syns.length; index++) {
if (syns[index].includes(value)) {
matchedIndexes.push(index);
}
}
}
return matchedIndexes;
}

const rawData = taxon[ 0 ];
const name = taxon[ 1 ];
const cn = taxon[ 2 ];
const syns = matchSynonyms( taxon[ 3 ], value );
if ( syns.length > 0 ) {
const name = taxon.searchSci;
const cn = taxon.searchCommon;
const syns = matchSynonyms(taxon.synonyms, value);
if (syns.length > 0) {
// Include any matching synonyms.
for ( const index of syns ) {
matches.push( [ rawData[ 0 ], rawData[ 1 ], rawData[ 2 ][ index ] ] );
for (const index of syns) {
matches.push([
taxon.raw[0],
taxon.raw[1],
taxon.raw[2] ? taxon.raw[2][index] : undefined,
]);
}
} else {
// No synonyms match; see if the scientific or common names match.
const namesMatch = name.includes( value ) || ( cn && cn.includes( value ) );
if ( namesMatch ) {
matches.push( [ rawData[ 0 ], rawData[ 1 ] ] );
const namesMatch =
name.includes(value) || (cn && cn.includes(value));
if (namesMatch) {
matches.push([taxon.raw[0], taxon.raw[1]]);
}
}

}

Search.#debounceTimer = undefined;

const value = Search.#normalizeName( document.getElementById( "name" ).value );
const input = Utils.getElement("name");
if (!(input instanceof HTMLInputElement)) {
throw new Error();
}
const value = Search.#normalizeName(input.value);

/**
* @type {([string,string|undefined]|[string,string|undefined,string|undefined])[]}
*/
const matches = [];
const shouldSearch = ( value.length >= MIN_LEN );

if ( shouldSearch ) {
const shouldSearch = value.length >= MIN_LEN;

if (shouldSearch) {
// If the search data is not done generating, try again later.
if ( !Search.#searchData ) {
this.#debounce( Search.#doSearch );
if (!Search.#searchData) {
this.#debounce();
}

for ( const taxon of Search.#searchData ) {
matchTaxon( taxon, value );
for (const taxon of Search.#searchData) {
matchTaxon(taxon, value);
}
}

const eBody = document.createElement( "tbody" );
if ( matches.length <= MAX_RESULTS ) {
for ( const match of matches ) {

const tr = document.createElement( "tr" );
const eBody = document.createElement("tbody");
if (matches.length <= MAX_RESULTS) {
for (const match of matches) {
const tr = document.createElement("tr");

// Scientific name.
const name = match[ 0 ];
const syn = match[ 2 ];
const td1 = document.createElement( "td" );
const link = Utils.domTaxonLink( name );
td1.appendChild( link );
if ( syn ) {
td1.appendChild( document.createTextNode( " (" + syn + ")" ) );
const name = match[0];
const syn = match[2];
const td1 = document.createElement("td");
const link = Utils.domTaxonLink(name);
td1.appendChild(link);
if (syn) {
td1.appendChild(document.createTextNode(" (" + syn + ")"));
}
tr.appendChild( td1 );
tr.appendChild(td1);

const cn = match[ 1 ];
const td2 = document.createElement( "td" );
if ( cn ) {
const cn = match[1];
const td2 = document.createElement("td");
if (cn) {
td2.textContent = cn;
}
tr.appendChild( td2 );

eBody.appendChild( tr );
tr.appendChild(td2);

eBody.appendChild(tr);
}
}

// Delete current message
const eMessage = document.getElementById( "message" );
if ( eMessage.firstChild ) {
eMessage.removeChild( eMessage.firstChild );
const eMessage = Utils.getElement("message");
if (eMessage.firstChild) {
eMessage.removeChild(eMessage.firstChild);
}
if ( shouldSearch ) {
if ( matches.length === 0 ) {
if (shouldSearch) {
if (matches.length === 0) {
eMessage.textContent = "Nothing found.";
}
if ( matches.length > MAX_RESULTS ) {
if (matches.length > MAX_RESULTS) {
eMessage.textContent = "Too many results.";
}
}

// Delete current results
const eTable = document.getElementById( "results" );
if ( eTable.firstChild ) {
eTable.removeChild( eTable.firstChild );
const eTable = Utils.getElement("results");
if (eTable.firstChild) {
eTable.removeChild(eTable.firstChild);
}

eTable.appendChild( eBody );
eTable.appendChild(eBody);
}

static async generateSearchData() {
/** @type {SearchData[]} */
const searchData = [];

/** @type {RawSearchData[]} */
// @ts-ignore
// eslint-disable-next-line no-undef
for ( const taxon of NAMES ) {
const taxonData = [ taxon ];
taxonData.push( this.#normalizeName( taxon[ 0 ] ) );
if ( taxon[ 1 ] ) {
taxonData.push( taxon[ 1 ].toLowerCase() );
const names = NAMES;

for (const taxon of names) {
/** @type {SearchData} */
const taxonData = {
raw: taxon,
searchSci: this.#normalizeName(taxon[0]),
};
if (taxon[1]) {
taxonData.searchCommon = taxon[1].toLowerCase();
}
if ( taxon[ 2 ] ) {
if (taxon[2]) {
const syns = [];
for ( const syn of taxon[ 2 ] ) {
syns.push( this.#normalizeName( syn ) );
for (const syn of taxon[2]) {
syns.push(this.#normalizeName(syn));
}
taxonData[ 3 ] = syns;
taxonData.synonyms = syns;
}
searchData.push( taxonData );
searchData.push(taxonData);
}
this.#searchData = searchData;
}
Expand All @@ -145,21 +177,29 @@ class Search {
}

static #handleSubmit() {
this.#debounce( 0 );
this.#debounce(0);
}

static init() {
this.generateSearchData();
const eName = document.getElementById( "name" );
const eName = Utils.getElement("name");
eName.focus();
eName.oninput = ( ev ) => { return this.#handleChange( ev ); };
document.getElementById( "search_form" ).onsubmit = () => { this.#handleSubmit(); return false; };
eName.oninput = () => {
return this.#handleChange();
};
Utils.getElement("search_form").onsubmit = () => {
this.#handleSubmit();
return false;
};
}

static #normalizeName( name ) {
return name.toLowerCase().replace( / (subsp|var)\./, "" );
/**
* @param {string} name
* @returns {string}
*/
static #normalizeName(name) {
return name.toLowerCase().replace(/ (subsp|var)\./, "");
}

}

Search.init();
Search.init();
9 changes: 5 additions & 4 deletions jekyll/assets/js/ui.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
class UI {

static init() {
const tooltips = document.querySelectorAll( "span[title]" );
[ ...tooltips ].map( tooltipTriggerEl => new bootstrap.Tooltip( tooltipTriggerEl ) );
const tooltips = document.querySelectorAll("span[title]");
[...tooltips].map(
// @ts-ignore
(tooltipTriggerEl) => new bootstrap.Tooltip(tooltipTriggerEl),
);
}

}

UI.init();
Loading

0 comments on commit aa4bda6

Please sign in to comment.