UNPKG

user-agents

Version:

A JavaScript library for generating random user agents.

4 lines (3 loc) 2.91 kB
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }var _useragentsjson = require('./user-agents.json'); var _useragentsjson2 = _interopRequireDefault(_useragentsjson);var o=_useragentsjson2.default,p=r=>{let t=r.reduce((e,[s])=>e+s,0),a=0;return r.map(([e,s])=>(a+=e/t,[a,s]))},y=o.map(({weight:r},t)=>[r,t]),b=p(y),c=(r,t=a=>a)=>{let a;return typeof r=="function"?a=[r]:r instanceof RegExp?a=[e=>typeof e=="object"&&e&&"userAgent"in e&&e.userAgent?r.test(e.userAgent):r.test(e)]:r instanceof Array?a=r.map(e=>c(e)):typeof r=="object"?a=Object.entries(r).map(([e,s])=>c(s,n=>n[e])):a=[e=>typeof e=="object"&&e&&"userAgent"in e&&e.userAgent?r===e.userAgent:r===e],e=>{try{let s=t(e);return a.every(n=>n(s))}catch (e2){return!1}}},A=r=>{if(!r)return b;let t=c(r),a=[];return o.forEach((e,s)=>{t(e)&&a.push([e.weight,s])}),p(a)},g=(r,t)=>{Object.defineProperty(r,"cumulativeWeightIndexPairs",{configurable:!0,enumerable:!1,writable:!1,value:t})},d=new WeakSet,m,h,u=class r{constructor(t){this[m]=()=>this.data.userAgent;this.toString=()=>this.data.userAgent;this.random=()=>{let t=new r;return g(t,this.cumulativeWeightIndexPairs),t.randomize(),t};this.top=t=>{let a=this.cumulativeWeightIndexPairs,e=a.map(([n,i],l)=>({weight:l>0?n-a[l-1][0]:n,index:i}));e.sort((n,i)=>i.weight-n.weight);let s=t!=null?Math.min(t,e.length):e.length;return e.slice(0,s).map(({index:n})=>structuredClone(o[n]))};this.randomize=()=>{let t=Math.random(),[,a]=_nullishCoalesce(this.cumulativeWeightIndexPairs.find(([e])=>e>t), () => ([]));if(a==null)throw new Error("Error finding a random user agent.");this.data=structuredClone(o[a])};if(g(this,A(t)),this.cumulativeWeightIndexPairs.length===0)throw new Error("No user agents matched your filters.");this.randomize();let a=()=>{},e=new Proxy(a,{apply:()=>this.random(),get:(s,n)=>{if(this.data&&typeof n=="string"&&Object.prototype.hasOwnProperty.call(this.data,n)&&Object.prototype.propertyIsEnumerable.call(this.data,n)){let i=this.data[n];if(i!==void 0)return i}return Reflect.get(this,n)},set:(s,n,i)=>Reflect.set(this,n,i),defineProperty:(s,n,i)=>Reflect.defineProperty(this,n,i),getOwnPropertyDescriptor:(s,n)=>Reflect.getOwnPropertyDescriptor(this,n),has:(s,n)=>Reflect.has(this,n),deleteProperty:(s,n)=>Reflect.deleteProperty(this,n),ownKeys:()=>Reflect.ownKeys(this),getPrototypeOf:()=>r.prototype});return d.add(e),e}static[(h=Symbol.hasInstance,m=Symbol.toPrimitive,h)](t){return t instanceof Object&&d.has(t)}static{this.random=t=>{try{return new r(t)}catch (e3){return null}}}static{this.top=(t,a)=>{try{return new r(a).top(t)}catch (e4){return[]}}}};var U=u;exports.default = U; module.exports = exports.default; //# sourceMappingURL=index.cjs.map