typicalcrimes
Version:
3 lines (2 loc) • 7.38 kB
JavaScript
import e from"axios";class t{constructor(e,t){if(!t)throw new Error("Please provide an API key");this._baseURL=e,this._headers={},this._apiKey=t}async fetch(t="",r){let n=""+this._baseURL;return-1===t.indexOf("?")?n+=`${t}?api_key=${this._apiKey}`:n+=`${t}&api_key=${this._apiKey}`,(await e.get(n,{...r,headers:{...this._headers,...r?r.headers:void 0}})).data}getAgencies(e,t,r){const n="/agencies";switch(e){case"default":return this.fetch(n);case"ori":return this.fetch(`${n}/${t}?page=${r}`);case"state":return this.fetch(`${n}/byStateAbbr/${t}?page=${r}`)}}getAgencyParticipation(e,t){return this.fetch(`/participation/${e}/${t}`)}getArsonStats(e,t){return this.fetch(`/arson/${e}/${t}`)}getCrimeCount(e,t,r){return this.getParticipants("offense",e,t,"count",r)}getCrimeSummary(e,t){return this.fetch(`/summarized/agencies/${e}/${t}`)}getEstimates(e,t){return this.fetch(`/estimates/${e}/${t}`)}getParticipants(e,t,r,n,a){return this.fetch("national"==t?`/nibrs/${r}/${e}/${t}/${n}`:`/nibrs/${r}/${e}/${t}/${a}/${n}`)}getPoliceEmployment(e="national",t=""){return this.fetch(`/police-employment/${e}/${t}`)}getRegions(e){return this.fetch("/regions/"+e)}getStates(e,t){return this.fetch(`/states/${e}?page=${t}`)}}class r{constructor(e){this._request=new t("https://api.usa.gov/crime/fbi/sapi/api",e)}getAgencies({type:e="default",relevantInfo:t="",page:r=0}={}){return this._request.getAgencies(e,t,r)}async getAgencyParticipation({scope:e,relevantInfo:t=""}){return await this._request.getAgencyParticipation(e,t)}async getArsonStats({scope:e,relevantInfo:t=""}){return await this._request.getArsonStats(e,t)}async getCrimeCount({scope:e,offense:t,relevantInfo:r=""}){return await this._request.getCrimeCount(e,t,r)}async getCrimeSummary({ori:e,offense:t}){return await this._request.getCrimeSummary(e,t)}async getEstimates({scope:e,relevantInfo:t=""}){return await this._request.getEstimates(e,t)}async getParticipants({type:e,scope:t,offense:r,classification:n,relevantInfo:a}){return await this._request.getParticipants(e,t,r,n,a)}async getPoliceEmployment({scope:e="national",relevantInfo:t=""}){return await this._request.getPoliceEmployment(e,t)}async getRegions(e=""){return await this._request.getRegions(e)}async getStates({state:e="",page:t=0}){return await this._request.getStates(e,t)}_validateCoords(e,t){if("number"!=typeof e)throw new Error("Expected latitude to be number, received: "+typeof e);if("number"!=typeof t)throw new Error("Expected longitude to be number, received: "+typeof t);if(Math.abs(e)>85.05115)throw new Error("Latitude is beyond the valid range");if(Math.abs(t)>180)throw new Error("Longitude is beyond the valid range")}_validateRange(e){if("number"!=typeof e)throw new TypeError("Expected range to be number, received: "+typeof e);if(e<0)throw new Error("Range cannot be negative")}_validateNumParams(e,t){const r=t.length,n=t.name;if(e<r)throw new Error(`Insufficent arguments were passed to method ${n}. ${r} were expected, but only ${e} were passed.`);if(e>r)throw new Error(`Too many arguments were passed to method ${n}. ${r} were expected, but ${e} were passed.`)}_validateTypeParam(e,t,r){if(("array"!=t||!Array.isArray(e))&&typeof e!=t)throw new Error(`Failed expectation: Argument '${r}' must be a '${t}'.`)}}class n{constructor(e){this._baseURL=e||"https://typicalcrimes.com/.netlify/functions/missing"}async getItem(t){return(await e.get(`${this._baseURL}?uid=${t}`,{headers:{"Content-Type":"application/json"}})).data}async getItems({page:e=1,...t}){let r=`${this._baseURL}?page=${e}`;const n=Object.keys(t);if(n.length)for(let e=0;e<n.length;e++){const a=n[e];r+=`&${a}=${t[a]}`}return(await fetch(r)).json()}}function a(e){if(!e)return{latitude:0,longitude:0};let t,r;if(!e)return{latitude:0,longitude:0};if(Array.isArray(e)){const[n,a]=e;if(!n||!a)return{latitude:0,longitude:0};t=n,r=a}else"latitude"in e?t=e.latitude:"lat"in e&&(t=e.lat),"longitude"in e?r=e.longitude:"lon"in e?r=e.lon:"lng"in e&&(r=e.lng);return{latitude:Number(t),longitude:Number(r)}}function i(e){return e*(Math.PI/180)}function s(e){switch(e.toLowerCase()){case"u.s. territories":return"U.S. Territories";case"northwest":return"Northeast";case"midwest":return"Midwest";case"south":return"South";case"west":return"West";case"other":return"Other";default:throw new Error("The provided region name is invalid. Received: "+e)}}function o(e){switch(e){case 0:return"U.S. Territories";case 1:return"Northeast";case 2:return"Midwest";case 3:return"South";case 4:return"West";case 99:return"Other";default:throw new Error("The provided region number is invalid. Expected 0-4 or 99 but received: "+e)}}var u={coords:{__proto__:null,format:a,isValid:function(e){return e.length>=2},toLatLon:function(e){const t=a(e);return t?{lat:t.latitude||0,lon:t.longitude||0}:null},toLatLng:function(e){const t=a(e);return t?{lat:t.latitude||0,lng:t.longitude||0}:null},toNative:function(e){return a(e)},toPigeon:function(e){const t=a(e);return t?[t.latitude,t.longitude]:[0,0]}},crimes:{__proto__:null,getHumanReadableRace:function(e){switch(e){case"A":return"Other Asian";case"B":return"Black";case"C":return"Chinese";case"D":return"Cambodian";case"F":return"Filipino";case"G":return"Guamanian";case"H":return"Hispanic";case"I":return"American Indian";case"J":return"Japanese";case"K":return"Korean";case"L":return"Laotian";case"O":return"Other";case"P":return"Pacific Islander";case"S":return"Samoan";case"U":return"Hawaiian";case"V":return"Vietnamese";case"W":return"White";case"X":return"Unknown";case"Z":return"Asian Indian";default:return"Unknown"}}},formulas:{__proto__:null,degreesToRadians:i,getArrestRate:function(e,t){return e/t*1e5},getClearanceRate:function(e,t){return e/t*100},getCrimeRate:function(e,t){return e/t*1e5},getCrimeTrendRate:function(e,t){return(t-e)/e*100},getPolicePersonnelRate:function(e,t){return e/t*1e3},getKineticEnergy:function(e,t){return.5*t*Math.pow(e,2)},poundsToGrains:function(e){return 7e3*e},grainsToPounds:function(e){return e/7e3},ouncesToGrains:function(e){return 437.5*e},grainsToOunces:function(e){return.00229*e},gramsToGrains:function(e){return 15.4324*e},grainsToGrams:function(e){return.648*e},inchesToMillimeters:function(e){return 25.4*e},millimetersToInches:function(e){return.03937*e},getBacLevel:function(e,t,r,n){return 5.14*e/t*r-.015*n}},geo:{__proto__:null,getDistance:function(e,t,r,n){const a=i(r-e),s=i(n-t),o=Math.pow(Math.sin(a/2),2)+Math.cos(i(e))*Math.cos(i(r))*Math.pow(Math.sin(s/2),2);return 12742*Math.asin(Math.sqrt(o))},getUserCoords:function(){return new Promise((e,t)=>{"undefined"!=typeof window&&window.navigator.geolocation.getCurrentPosition(t=>{const{coords:r}=t;e({accuracy:r.accuracy,latitude:t.coords.latitude,longitude:t.coords.longitude,timestamp:t.timestamp})},e=>{console.error(e),t(e)})})}},region:{__proto__:null,isValid:function(e){if("number"==typeof e)return o(e);if("string"==typeof e)return s(e);throw new Error("Region must be a string or a number")},standardizeRegionName:s,toNum:function(e){switch(e){case"U.S. Territories":return 0;case"Northeast":return 1;case"Midwest":return 2;case"South":return 3;case"West":return 4;case"Other":return 99;default:throw new Error("The provided region number is invalid. Expected 0-4 or 99 but received: "+e)}},toName:o}};export{n as MissingPeople,r as Typicalcrimes,u as utils};
//# sourceMappingURL=typicalcrimes.modern.js.map